diff --git a/tests/TestCase/Database/QueryTest.php b/tests/TestCase/Database/QueryTest.php index 89cf842503f..200ae4fbda3 100644 --- a/tests/TestCase/Database/QueryTest.php +++ b/tests/TestCase/Database/QueryTest.php @@ -2204,14 +2204,19 @@ public function testInsertSimple() { ]); $result = $query->sql(); $this->assertQuotedQuery( - 'INSERT INTO \(, <body>\) ' . + 'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )?' . 'VALUES \(:c0, :c1\)', $result, true ); $result = $query->execute(); - $this->assertCount(1, $result, '1 row should be inserted'); + $result->closeCursor(); + + //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT + if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + $this->assertCount(1, $result, '1 row should be inserted'); + } $expected = [ [ @@ -2240,14 +2245,19 @@ public function testInsertSparseRow() { ]); $result = $query->sql(); $this->assertQuotedQuery( - 'INSERT INTO <articles> \(<title>, <body>\) ' . + 'INSERT INTO <articles> \(<title>, <body>\) (OUTPUT INSERTED\.\* )?' . 'VALUES \(:c0, :c1\)', $result, true ); $result = $query->execute(); - $this->assertCount(1, $result, '1 row should be inserted'); + $result->closeCursor(); + + //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT + if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + $this->assertCount(1, $result, '1 row should be inserted'); + } $expected = [ [ @@ -2278,7 +2288,12 @@ public function testInsertMultipleRowsSparse() { ]); $result = $query->execute(); - $this->assertCount(2, $result, '2 rows should be inserted'); + $result->closeCursor(); + + //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT + if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + $this->assertCount(2, $result, '2 rows should be inserted'); + } $expected = [ [ @@ -2319,15 +2334,20 @@ public function testInsertFromSelect() { $result = $query->sql(); $this->assertQuotedQuery( - 'INSERT INTO <articles> \(<title>, <body>, <author_id>\) SELECT', + 'INSERT INTO <articles> \(<title>, <body>, <author_id>\) (OUTPUT INSERTED\.\* )?SELECT', $result, true ); $this->assertQuotedQuery( 'SELECT <name>, \'some text\', 99 FROM <authors>', $result, true); $result = $query->execute(); + $result->closeCursor(); + + //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT + if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + $this->assertCount(1, $result); + } - $this->assertCount(1, $result); $result = (new Query($this->connection))->select('*') ->from('articles') ->where(['author_id' => 99]) @@ -2376,12 +2396,31 @@ public function testInsertFailureMixingTypesQueryFirst() { */ public function testInsertExpressionValues() { $query = new Query($this->connection); - $query->insert(['title']) + $query->insert(['title', 'author_id']) ->into('articles') - ->values(['title' => $query->newExpr("SELECT 'jose'")]); + ->values(['title' => $query->newExpr("SELECT 'jose'"), 'author_id' => 99]); $result = $query->execute(); + $result->closeCursor(); + + //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT + if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + $this->assertCount(1, $result); + } + + $result = (new Query($this->connection))->select('*') + ->from('articles') + ->where(['author_id' => 99]) + ->execute(); $this->assertCount(1, $result); + $expected = [ + 'id' => 4, + 'title' => 'jose', + 'body' => null, + 'author_id' => '99', + 'published' => 'N', + ]; + $this->assertEquals($expected, $result->fetch('assoc')); $subquery = new Query($this->connection); $subquery->select(['name']) @@ -2389,11 +2428,29 @@ public function testInsertExpressionValues() { ->where(['id' => 1]); $query = new Query($this->connection); - $query->insert(['title']) + $query->insert(['title', 'author_id']) ->into('articles') - ->values(['title' => $subquery]); + ->values(['title' => $subquery, 'author_id' => 100]); $result = $query->execute(); + $result->closeCursor(); + //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT + if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + $this->assertCount(1, $result); + } + + $result = (new Query($this->connection))->select('*') + ->from('articles') + ->where(['author_id' => 100]) + ->execute(); $this->assertCount(1, $result); + $expected = [ + 'id' => 5, + 'title' => 'mariano', + 'body' => null, + 'author_id' => '100', + 'published' => 'N', + ]; + $this->assertEquals($expected, $result->fetch('assoc')); } /** @@ -2937,7 +2994,8 @@ public function testSqlCaseStatement() { 'comment' => 'In limbo', 'published' => 'L' ]) - ->execute(); + ->execute() + ->closeCursor(); $query = new Query($this->connection); $conditions = [ diff --git a/tests/TestCase/ORM/QueryTest.php b/tests/TestCase/ORM/QueryTest.php index aa2b0397670..b22c39b5518 100644 --- a/tests/TestCase/ORM/QueryTest.php +++ b/tests/TestCase/ORM/QueryTest.php @@ -1413,8 +1413,15 @@ public function testInsert() { ->values(['title' => 'Second']) ->execute(); + $result->closeCursor(); + $this->assertInstanceOf('Cake\Database\StatementInterface', $result); - $this->assertEquals(2, $result->rowCount()); + //PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT + if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) { + $this->assertEquals(2, $result->rowCount()); + } else { + $this->assertEquals(-1, $result->rowCount()); + } } /**