Skip to content

Commit 001bb58

Browse files
author
Walther Lalk
committed
Fix failing insert tests on sqlserver
1 parent b23f8c4 commit 001bb58

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

tests/TestCase/Database/QueryTest.php

Lines changed: 70 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2204,14 +2204,19 @@ public function testInsertSimple() {
22042204
]);
22052205
$result = $query->sql();
22062206
$this->assertQuotedQuery(
2207-
'INSERT INTO <articles> \(<title>, <body>\) ' .
2207+
'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )?' .
22082208
'VALUES \(:c0, :c1\)',
22092209
$result,
22102210
true
22112211
);
22122212

22132213
$result = $query->execute();
2214-
$this->assertCount(1, $result, '1 row should be inserted');
2214+
$result->closeCursor();
2215+
2216+
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2217+
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
2218+
$this->assertCount(1, $result, '1 row should be inserted');
2219+
}
22152220

22162221
$expected = [
22172222
[
@@ -2240,14 +2245,19 @@ public function testInsertSparseRow() {
22402245
]);
22412246
$result = $query->sql();
22422247
$this->assertQuotedQuery(
2243-
'INSERT INTO <articles> \(<title>, <body>\) ' .
2248+
'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )?' .
22442249
'VALUES \(:c0, :c1\)',
22452250
$result,
22462251
true
22472252
);
22482253

22492254
$result = $query->execute();
2250-
$this->assertCount(1, $result, '1 row should be inserted');
2255+
$result->closeCursor();
2256+
2257+
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2258+
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
2259+
$this->assertCount(1, $result, '1 row should be inserted');
2260+
}
22512261

22522262
$expected = [
22532263
[
@@ -2278,7 +2288,12 @@ public function testInsertMultipleRowsSparse() {
22782288
]);
22792289

22802290
$result = $query->execute();
2281-
$this->assertCount(2, $result, '2 rows should be inserted');
2291+
$result->closeCursor();
2292+
2293+
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2294+
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
2295+
$this->assertCount(2, $result, '2 rows should be inserted');
2296+
}
22822297

22832298
$expected = [
22842299
[
@@ -2319,15 +2334,20 @@ public function testInsertFromSelect() {
23192334

23202335
$result = $query->sql();
23212336
$this->assertQuotedQuery(
2322-
'INSERT INTO <articles> \(<title>, <body>, <author_id>\) SELECT',
2337+
'INSERT INTO <articles> \(<title>, <body>, <author_id>\) (OUTPUT INSERTED\.\* )?SELECT',
23232338
$result,
23242339
true
23252340
);
23262341
$this->assertQuotedQuery(
23272342
'SELECT <name>, \'some text\', 99 FROM <authors>', $result, true);
23282343
$result = $query->execute();
2344+
$result->closeCursor();
2345+
2346+
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2347+
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
2348+
$this->assertCount(1, $result);
2349+
}
23292350

2330-
$this->assertCount(1, $result);
23312351
$result = (new Query($this->connection))->select('*')
23322352
->from('articles')
23332353
->where(['author_id' => 99])
@@ -2376,24 +2396,61 @@ public function testInsertFailureMixingTypesQueryFirst() {
23762396
*/
23772397
public function testInsertExpressionValues() {
23782398
$query = new Query($this->connection);
2379-
$query->insert(['title'])
2399+
$query->insert(['title', 'author_id'])
23802400
->into('articles')
2381-
->values(['title' => $query->newExpr("SELECT 'jose'")]);
2401+
->values(['title' => $query->newExpr("SELECT 'jose'"), 'author_id' => 99]);
23822402

23832403
$result = $query->execute();
2404+
$result->closeCursor();
2405+
2406+
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2407+
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
2408+
$this->assertCount(1, $result);
2409+
}
2410+
2411+
$result = (new Query($this->connection))->select('*')
2412+
->from('articles')
2413+
->where(['author_id' => 99])
2414+
->execute();
23842415
$this->assertCount(1, $result);
2416+
$expected = [
2417+
'id' => 4,
2418+
'title' => 'jose',
2419+
'body' => null,
2420+
'author_id' => '99',
2421+
'published' => 'N',
2422+
];
2423+
$this->assertEquals($expected, $result->fetch('assoc'));
23852424

23862425
$subquery = new Query($this->connection);
23872426
$subquery->select(['name'])
23882427
->from('authors')
23892428
->where(['id' => 1]);
23902429

23912430
$query = new Query($this->connection);
2392-
$query->insert(['title'])
2431+
$query->insert(['title', 'author_id'])
23932432
->into('articles')
2394-
->values(['title' => $subquery]);
2433+
->values(['title' => $subquery, 'author_id' => 100]);
23952434
$result = $query->execute();
2435+
$result->closeCursor();
2436+
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
2437+
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
2438+
$this->assertCount(1, $result);
2439+
}
2440+
2441+
$result = (new Query($this->connection))->select('*')
2442+
->from('articles')
2443+
->where(['author_id' => 100])
2444+
->execute();
23962445
$this->assertCount(1, $result);
2446+
$expected = [
2447+
'id' => 5,
2448+
'title' => 'mariano',
2449+
'body' => null,
2450+
'author_id' => '100',
2451+
'published' => 'N',
2452+
];
2453+
$this->assertEquals($expected, $result->fetch('assoc'));
23972454
}
23982455

23992456
/**
@@ -2937,7 +2994,8 @@ public function testSqlCaseStatement() {
29372994
'comment' => 'In limbo',
29382995
'published' => 'L'
29392996
])
2940-
->execute();
2997+
->execute()
2998+
->closeCursor();
29412999

29423000
$query = new Query($this->connection);
29433001
$conditions = [

tests/TestCase/ORM/QueryTest.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1413,8 +1413,15 @@ public function testInsert() {
14131413
->values(['title' => 'Second'])
14141414
->execute();
14151415

1416+
$result->closeCursor();
1417+
14161418
$this->assertInstanceOf('Cake\Database\StatementInterface', $result);
1417-
$this->assertEquals(2, $result->rowCount());
1419+
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
1420+
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
1421+
$this->assertEquals(2, $result->rowCount());
1422+
} else {
1423+
$this->assertEquals(-1, $result->rowCount());
1424+
}
14181425
}
14191426

14201427
/**

0 commit comments

Comments
 (0)