From c0af4b3fdadfc2d4eb2caef7a1ab7d765797f987 Mon Sep 17 00:00:00 2001 From: thinkingmedia Date: Sun, 26 Feb 2017 19:53:22 -0500 Subject: [PATCH] adds IdentifierInterface and improves unit test --- src/Database/Query.php | 4 ++-- tests/TestCase/Database/QueryTest.php | 33 +++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/Database/Query.php b/src/Database/Query.php index a6df36b5b53..a1de793cfff 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -238,7 +238,7 @@ public function execute() * $rowCount = $query->update('articles') * ->set(['published'=>true]) * ->where(['published'=>false]) - * ->executeAndClose(); + * ->rowCountAndClose(); * ``` * * The above example will change the published column to true for all false records, and return the number of @@ -246,7 +246,7 @@ public function execute() * * @return int */ - public function executeAndClose() + public function rowCountAndClose() { $statement = $this->execute(); try { diff --git a/tests/TestCase/Database/QueryTest.php b/tests/TestCase/Database/QueryTest.php index 63f6fdc3e53..15e5e74f8c4 100644 --- a/tests/TestCase/Database/QueryTest.php +++ b/tests/TestCase/Database/QueryTest.php @@ -17,6 +17,7 @@ use Cake\Core\Configure; use Cake\Database\Expression\IdentifierExpression; use Cake\Database\Query; +use Cake\Database\StatementInterface; use Cake\Database\TypeMap; use Cake\Datasource\ConnectionManager; use Cake\TestSuite\TestCase; @@ -3886,15 +3887,37 @@ public function testDirectIsNotNull() * * @return void */ - public function testExecuteAndClose() + public function testRowCountAndClose() { $this->loadFixtures('Authors'); - $query = new Query($this->connection); - $rowCount = $query->update('authors') + + $statementMock = $this->getMockBuilder(StatementInterface::class) + ->setMethods(['rowCount','closeCursor']) + ->getMockForAbstractClass(); + + $statementMock->expects($this->once()) + ->method('rowCount') + ->willReturn(500); + + $statementMock->expects($this->once()) + ->method('closeCursor'); + + /** @var \Cake\ORM\Query|\PHPUnit_Framework_MockObject_MockObject $queryMock */ + $queryMock = $this->getMockBuilder(Query::class) + ->setMethods(['execute']) + ->setConstructorArgs((array)$this->connection) + ->getMock(); + + $queryMock->expects($this->once()) + ->method('execute') + ->willReturn($statementMock); + + $rowCount = $queryMock->update('authors') ->set('name', 'mark') ->where(['id' => 1]) - ->executeAndClose(); - $this->assertEquals(1, $rowCount); + ->rowCountAndClose(); + + $this->assertEquals(500, $rowCount); } /**