Skip to content

Commit 9607e5f

Browse files
Fix Oracle tests
1 parent 2d3e9c6 commit 9607e5f

4 files changed

Lines changed: 92 additions & 50 deletions

File tree

src/TestUtility/TestCommandTrait.php

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,8 @@
44

55
namespace Yiisoft\Db\TestUtility;
66

7-
use function call_user_func_array;
8-
use function date;
9-
use function is_array;
107
use PDO;
11-
use function range;
12-
use function rtrim;
13-
use function setlocale;
148
use Throwable;
15-
use function time;
16-
179
use Yiisoft\Db\Connection\ConnectionInterface;
1810
use Yiisoft\Db\Data\DataReader;
1911
use Yiisoft\Db\Exception\Exception;
@@ -22,6 +14,14 @@
2214
use Yiisoft\Db\Query\Query;
2315
use Yiisoft\Db\Schema\Schema;
2416

17+
use function call_user_func_array;
18+
use function date;
19+
use function is_array;
20+
use function range;
21+
use function rtrim;
22+
use function setlocale;
23+
use function time;
24+
2525
trait TestCommandTrait
2626
{
2727
public function testConstruct(): void
@@ -283,7 +283,7 @@ public function testBatchInsertDataTypesLocale(): void
283283
$this->markTestSkipped('Your platform does not support locales.');
284284
}
285285

286-
$db = $this->getConnection();
286+
$db = $this->getConnection(true);
287287

288288
try {
289289
/* This one sets decimal mark to comma sign */
@@ -300,12 +300,17 @@ public function testBatchInsertDataTypesLocale(): void
300300
/* clear data in "type" table */
301301
$db->createCommand()->delete('type')->execute();
302302

303+
/* change, for point oracle. */
304+
if ($db->getDriverName() === 'oci') {
305+
$db->createCommand("ALTER SESSION SET NLS_NUMERIC_CHARACTERS='.,'")->execute();
306+
}
307+
303308
/* batch insert on "type" table */
304309
$db->createCommand()->batchInsert('type', $cols, $data)->execute();
305310

306311
$data = $db->createCommand(
307-
'SELECT int_col, char_col, float_col, bool_col FROM {{type}} WHERE [[int_col]] IN (1,2,3)
308-
ORDER BY [[int_col]];'
312+
'SELECT [[int_col]], [[char_col]], [[float_col]], [[bool_col]] ' .
313+
'FROM {{type}} WHERE [[int_col]] IN (1,2,3) ORDER BY [[int_col]]'
309314
)->queryAll();
310315

311316
$this->assertCount(3, $data);
@@ -1403,4 +1408,37 @@ public function upsertProviderTrait(): array
14031408
],
14041409
];
14051410
}
1411+
1412+
public function testAlterTable(): void
1413+
{
1414+
$db = $this->getConnection();
1415+
1416+
if ($db->getDriverName() === 'sqlite') {
1417+
$this->markTestSkipped('Sqlite does not support alterTable');
1418+
}
1419+
1420+
if ($db->getSchema()->getTableSchema('testAlterTable') !== null) {
1421+
$db->createCommand()->dropTable('testAlterTable')->execute();
1422+
}
1423+
1424+
$db->createCommand()->createTable(
1425+
'testAlterTable',
1426+
[
1427+
'id' => Schema::TYPE_PK,
1428+
'bar' => Schema::TYPE_INTEGER,
1429+
]
1430+
)->execute();
1431+
1432+
$db->createCommand()->insert('testAlterTable', ['bar' => 1])->execute();
1433+
1434+
$db->createCommand()->alterColumn('testAlterTable', 'bar', Schema::TYPE_STRING)->execute();
1435+
1436+
$db->createCommand()->insert('testAlterTable', ['bar' => 'hello'])->execute();
1437+
1438+
$records = $db->createCommand('SELECT [[id]], [[bar]] FROM {{testAlterTable}}')->queryAll();
1439+
$this->assertEquals([
1440+
['id' => 1, 'bar' => 1],
1441+
['id' => 2, 'bar' => 'hello'],
1442+
], $records);
1443+
}
14061444
}

src/TestUtility/TestConnectionTrait.php

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
namespace Yiisoft\Db\TestUtility;
66

7-
use function serialize;
8-
use function unserialize;
97
use Yiisoft\Db\Connection\ConnectionInterface;
108
use Yiisoft\Db\Exception\Exception;
11-
129
use Yiisoft\Db\Exception\NotSupportedException;
1310
use Yiisoft\Db\Transaction\Transaction;
1411

12+
use function serialize;
13+
use function unserialize;
14+
1515
trait TestConnectionTrait
1616
{
1717
public function testSerialize(): void
@@ -49,7 +49,7 @@ public function testTransaction(): void
4949
$this->assertFalse($transaction->isActive());
5050
$this->assertNull($db->getTransaction());
5151
$this->assertEquals(0, $db->createCommand(
52-
"SELECT COUNT(*) FROM profile WHERE description = 'test transaction';"
52+
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction'"
5353
)->queryScalar());
5454

5555
$transaction = $db->beginTransaction();
@@ -61,7 +61,7 @@ public function testTransaction(): void
6161
$this->assertFalse($transaction->isActive());
6262
$this->assertNull($db->getTransaction());
6363
$this->assertEquals(1, $db->createCommand(
64-
"SELECT COUNT(*) FROM profile WHERE description = 'test transaction';"
64+
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction'"
6565
)->queryScalar());
6666
}
6767

@@ -91,20 +91,18 @@ public function testTransactionIsolation(): void
9191

9292
public function testTransactionShortcutException(): void
9393
{
94-
$db = $this->getConnection();
95-
96-
$result = $db->transaction(static function (ConnectionInterface $db) {
97-
$db->createCommand()->insert('profile', ['description' => 'test transaction shortcut'])->execute();
98-
return true;
99-
}, Transaction::READ_UNCOMMITTED);
94+
$db = $this->getConnection(true);
10095

101-
$this->assertTrue($result, 'transaction shortcut valid value should be returned from callback');
96+
$this->expectException(Exception::class);
10297

98+
$db->transaction(function () use ($db) {
99+
$db->createCommand()->insert('profile', ['description' => 'test transaction shortcut'])->execute();
100+
throw new Exception('Exception in transaction shortcut');
101+
});
103102
$profilesCount = $db->createCommand(
104-
"SELECT COUNT(*) FROM profile WHERE description = 'test transaction shortcut';"
103+
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction shortcut'"
105104
)->queryScalar();
106-
107-
$this->assertEquals(1, $profilesCount, 'profile should be inserted in transaction shortcut');
105+
$this->assertEquals(0, $profilesCount, 'profile should not be inserted in transaction shortcut');
108106
}
109107

110108
public function testTransactionShortcutCorrect(): void
@@ -119,7 +117,7 @@ public function testTransactionShortcutCorrect(): void
119117
$this->assertTrue($result, 'transaction shortcut valid value should be returned from callback');
120118

121119
$profilesCount = $db->createCommand(
122-
"SELECT COUNT(*) FROM profile WHERE description = 'test transaction shortcut';"
120+
"SELECT COUNT(*) FROM {{profile}} WHERE [[description]] = 'test transaction shortcut'"
123121
)->queryScalar();
124122

125123
$this->assertEquals(1, $profilesCount, 'profile should be inserted in transaction shortcut');
@@ -203,7 +201,7 @@ public function testEnableQueryLog(): void
203201
$this->logger->flush();
204202
$this->profiler->flush();
205203

206-
$db->createCommand('SELECT * FROM qlog1')->queryAll();
204+
$db->createCommand('SELECT * FROM {{qlog1}}')->queryAll();
207205

208206
$this->assertCount(1, $this->getInaccessibleProperty($this->logger, 'messages'));
209207
$this->assertCount(1, $this->getInaccessibleProperty($this->profiler, 'messages'));
@@ -224,7 +222,7 @@ public function testEnableQueryLog(): void
224222
$this->logger->flush();
225223
$this->profiler->flush();
226224

227-
$db->createCommand('SELECT * FROM qlog2')->queryAll();
225+
$db->createCommand('SELECT * FROM {{qlog2}}')->queryAll();
228226

229227
$this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages'));
230228
$this->assertCount(1, $this->getInaccessibleProperty($this->profiler, 'messages'));
@@ -245,7 +243,7 @@ public function testEnableQueryLog(): void
245243
$this->logger->flush();
246244
$this->profiler->flush();
247245

248-
$db->createCommand('SELECT * FROM qlog3')->queryAll();
246+
$db->createCommand('SELECT * FROM {{qlog3}}')->queryAll();
249247

250248
$this->assertCount(1, $this->getInaccessibleProperty($this->logger, 'messages'));
251249
$this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages'));
@@ -263,7 +261,7 @@ public function testEnableQueryLog(): void
263261
$this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages'));
264262
$this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages'));
265263

266-
$db->createCommand('SELECT * FROM qlog4')->queryAll();
264+
$db->createCommand('SELECT * FROM {{qlog4}}')->queryAll();
267265

268266
$this->assertCount(0, $this->getInaccessibleProperty($this->logger, 'messages'));
269267
$this->assertCount(0, $this->getInaccessibleProperty($this->profiler, 'messages'));

src/TestUtility/TestQueryBuilderTrait.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44

55
namespace Yiisoft\Db\TestUtility;
66

7-
use function array_key_exists;
8-
use function array_merge;
9-
use function array_values;
10-
use function is_array;
11-
use function preg_match_all;
12-
use function str_replace;
13-
use function strncmp;
14-
use function substr;
157
use Yiisoft\Db\Connection\Connection;
16-
178
use Yiisoft\Db\Expression\Expression;
18-
use Yiisoft\Db\Query\Conditions\BetweenColumnsCondition;
199
use Yiisoft\Db\Query\Conditions\InCondition;
2010
use Yiisoft\Db\Query\Conditions\LikeCondition;
11+
use Yiisoft\Db\Query\Conditions\BetweenColumnsCondition;
2112
use Yiisoft\Db\Query\Query;
2213
use Yiisoft\Db\Query\QueryBuilder;
2314
use Yiisoft\Db\Schema\Schema;
2415
use Yiisoft\Db\Schema\SchemaBuilderTrait;
2516

17+
use function array_key_exists;
18+
use function array_merge;
19+
use function array_values;
20+
use function is_array;
21+
use function preg_match_all;
22+
use function str_replace;
23+
use function strncmp;
24+
use function substr;
25+
2626
trait TestQueryBuilderTrait
2727
{
2828
use SchemaBuilderTrait;
@@ -937,10 +937,10 @@ public function columnTypes(): array
937937
$this->primaryKey()->first(),
938938
[
939939
'mysql' => 'int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST',
940-
'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY',
941940
'sqlsrv' => 'int IDENTITY PRIMARY KEY',
942941
],
943942
[
943+
'oci' => 'NUMBER(10) NOT NULL PRIMARY KEY FIRST',
944944
'sqlsrv' => 'pk',
945945
],
946946
],
@@ -949,10 +949,10 @@ public function columnTypes(): array
949949
$this->integer()->first(),
950950
[
951951
'mysql' => 'int(11) FIRST',
952-
'oci' => 'NUMBER(10)',
953952
'sqlsrv' => 'int',
954953
],
955954
[
955+
'oci' => 'NUMBER(10) FIRST',
956956
'pgsql' => 'integer',
957957
'sqlsrv' => 'integer',
958958
],
@@ -962,10 +962,10 @@ public function columnTypes(): array
962962
$this->string()->first(),
963963
[
964964
'mysql' => 'varchar(255) FIRST',
965-
'oci' => 'VARCHAR2(255)',
966965
'sqlsrv' => 'nvarchar(255)',
967966
],
968967
[
968+
'oci' => 'VARCHAR2(255) FIRST',
969969
'sqlsrv' => 'string',
970970
],
971971
],
@@ -974,10 +974,10 @@ public function columnTypes(): array
974974
$this->integer()->append('NOT NULL')->first(),
975975
[
976976
'mysql' => 'int(11) NOT NULL FIRST',
977-
'oci' => 'NUMBER(10) NOT NULL',
978977
'sqlsrv' => 'int NOT NULL',
979978
],
980979
[
980+
'oci' => 'NUMBER(10) NOT NULL FIRST',
981981
'sqlsrv' => 'integer NOT NULL',
982982
],
983983
],
@@ -986,10 +986,10 @@ public function columnTypes(): array
986986
$this->string()->append('NOT NULL')->first(),
987987
[
988988
'mysql' => 'varchar(255) NOT NULL FIRST',
989-
'oci' => 'VARCHAR2(255) NOT NULL',
990989
'sqlsrv' => 'nvarchar(255) NOT NULL',
991990
],
992991
[
992+
'oci' => 'VARCHAR2(255) NOT NULL FIRST',
993993
'sqlsrv' => 'string NOT NULL',
994994
],
995995
],

src/TestUtility/TestQueryTrait.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,19 +483,21 @@ public function testLimitOffsetWithExpression(): void
483483
$result = $query->column();
484484

485485
$this->assertCount(2, $result);
486-
if ($db->getDriverName() !== 'sqlsrv') {
486+
487+
if ($db->getDriverName() !== 'sqlsrv' && $db->getDriverName() !== 'oci') {
487488
$this->assertContains(2, $result);
488489
$this->assertContains(3, $result);
489490
} else {
490491
$this->assertContains('2', $result);
491492
$this->assertContains('3', $result);
492493
}
494+
493495
$this->assertNotContains(1, $result);
494496
}
495497

496498
public function testOne(): void
497499
{
498-
$db = $this->getConnection();
500+
$db = $this->getConnection(true);
499501

500502
$result = (new Query($db))->from('customer')->where(['status' => 2])->one();
501503

@@ -773,10 +775,14 @@ public function testMultipleLikeConditions(): void
773775
*/
774776
public function testExpressionInFrom(): void
775777
{
776-
$db = $this->getConnection();
778+
$db = $this->getConnection(true);
777779

778780
$query = (new Query($db))
779-
->from(new Expression('(SELECT id, name, email, address, status FROM customer) c'))
781+
->from(
782+
new Expression(
783+
'(SELECT [[id]], [[name]], [[email]], [[address]], [[status]] FROM {{customer}}) c'
784+
)
785+
)
780786
->where(['status' => 2]);
781787

782788
$result = $query->one();

0 commit comments

Comments
 (0)