Skip to content

Commit

Permalink
adds IdentifierInterface and improves unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkingmedia committed Feb 27, 2017
1 parent 5d68074 commit c0af4b3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/Database/Query.php
Expand Up @@ -238,15 +238,15 @@ 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
* records that were updated.
*
* @return int
*/
public function executeAndClose()
public function rowCountAndClose()
{
$statement = $this->execute();
try {
Expand Down
33 changes: 28 additions & 5 deletions tests/TestCase/Database/QueryTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit c0af4b3

Please sign in to comment.