Skip to content

Commit

Permalink
#6167 - fixed tests and added info why query is used in SequenceGener…
Browse files Browse the repository at this point in the history
…ator
  • Loading branch information
mkurzeja authored and Ocramius committed Jun 21, 2017
1 parent 60b6073 commit edffb4d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
3 changes: 2 additions & 1 deletion lib/Doctrine/ORM/Id/SequenceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ public function generate(EntityManager $em, $entity)
$conn = $em->getConnection();
$sql = $conn->getDatabasePlatform()->getSequenceNextValSQL($this->_sequenceName);

$this->_nextValue = (int) $conn->query($sql)->fetchColumn(0);
// Use query to force master in MasterSlaveConnection
$this->_nextValue = (int) $conn->query($sql)->fetchColumn();
$this->_maxValue = $this->_nextValue + $this->_allocationSize;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Doctrine/Tests/Mocks/ConnectionMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\Tests\Mocks;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\Statement;

/**
* Mock class for Connection.
Expand All @@ -13,6 +14,11 @@ class ConnectionMock extends Connection
*/
private $_fetchOneResult;

/**
* @var Statement
*/
private $_queryResult;

/**
* @var DatabasePlatformMock
*/
Expand Down Expand Up @@ -89,6 +95,14 @@ public function fetchColumn($statement, array $params = [], $colnum = 0, array $
return $this->_fetchOneResult;
}

/**
* {@inheritdoc}
*/
public function query()
{
return $this->_queryResult;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -132,6 +146,14 @@ public function setLastInsertId($id)
$this->_lastInsertId = $id;
}

/**
* @param Statement $result
*/
public function setQueryResult($result)
{
$this->_queryResult = $result;
}

/**
* @return array
*/
Expand Down
6 changes: 3 additions & 3 deletions tests/Doctrine/Tests/ORM/Id/SequenceGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Tests\ORM\Id;

use Doctrine\ORM\Id\SequenceGenerator;
use Doctrine\Tests\Mocks\StatementArrayMock;
use Doctrine\Tests\OrmTestCase;

/**
Expand All @@ -25,15 +26,14 @@ public function testGeneration()
{
for ($i=0; $i < 42; ++$i) {
if ($i % 10 == 0) {
$this->_em->getConnection()->setFetchOneResult((int)($i / 10) * 10);
$nextId = array(array((int)($i / 10) * 10));
$this->_em->getConnection()->setQueryResult(new StatementArrayMock($nextId));
}
$id = $this->_seqGen->generate($this->_em, null);
$this->assertEquals($i, $id);
$this->assertEquals((int)($i / 10) * 10 + 10, $this->_seqGen->getCurrentMaxValue());
$this->assertEquals($i + 1, $this->_seqGen->getNextValue());
}


}
}

0 comments on commit edffb4d

Please sign in to comment.