2009-03-30 19:43:05 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Id;
|
|
|
|
|
2017-06-21 06:04:06 +02:00
|
|
|
use Doctrine\ORM\EntityManager;
|
2009-03-30 19:43:05 +00:00
|
|
|
use Doctrine\ORM\Id\SequenceGenerator;
|
2017-06-21 06:04:06 +02:00
|
|
|
use Doctrine\Tests\Mocks\ConnectionMock;
|
2016-12-12 07:45:25 +01:00
|
|
|
use Doctrine\Tests\Mocks\StatementArrayMock;
|
2016-05-11 01:55:12 +07:00
|
|
|
use Doctrine\Tests\OrmTestCase;
|
2009-03-30 19:43:05 +00:00
|
|
|
|
2016-05-11 01:55:12 +07:00
|
|
|
class SequenceGeneratorTest extends OrmTestCase
|
2009-03-30 19:43:05 +00:00
|
|
|
{
|
2017-06-21 06:04:06 +02:00
|
|
|
/**
|
|
|
|
* @var EntityManager
|
|
|
|
*/
|
|
|
|
private $entityManager;
|
2009-03-30 19:43:05 +00:00
|
|
|
|
2017-06-21 06:04:06 +02:00
|
|
|
/**
|
|
|
|
* @var SequenceGenerator
|
|
|
|
*/
|
|
|
|
private $sequenceGenerator;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var ConnectionMock
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
protected function setUp() : void
|
2009-03-30 19:43:05 +00:00
|
|
|
{
|
2017-06-21 06:04:06 +02:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->entityManager = $this->_getTestEntityManager();
|
|
|
|
$this->sequenceGenerator = new SequenceGenerator('seq', 10);
|
|
|
|
$this->connection = $this->entityManager->getConnection();
|
|
|
|
|
|
|
|
self::assertInstanceOf(ConnectionMock::class, $this->connection);
|
2009-03-30 19:43:05 +00:00
|
|
|
}
|
|
|
|
|
2017-06-21 06:04:06 +02:00
|
|
|
public function testGeneration() : void
|
2009-03-30 19:43:05 +00:00
|
|
|
{
|
2017-06-21 06:04:06 +02:00
|
|
|
$this->connection->setFetchOneException(new \BadMethodCallException(
|
|
|
|
'Fetch* method used. Query method should be used instead, '
|
|
|
|
. 'as NEXTVAL should be run on a master server in master-slave setup.'
|
|
|
|
));
|
2016-12-12 12:29:29 +01:00
|
|
|
|
2017-06-21 06:04:06 +02:00
|
|
|
for ($i = 0; $i < 42; ++$i) {
|
2009-03-30 19:43:05 +00:00
|
|
|
if ($i % 10 == 0) {
|
2017-06-21 06:04:06 +02:00
|
|
|
$this->connection->setQueryResult(new StatementArrayMock([[(int)($i / 10) * 10]]));
|
2009-03-30 19:43:05 +00:00
|
|
|
}
|
2017-06-21 06:04:06 +02:00
|
|
|
|
|
|
|
$id = $this->sequenceGenerator->generate($this->entityManager, null);
|
|
|
|
|
|
|
|
self::assertSame($i, $id);
|
|
|
|
self::assertSame((int)($i / 10) * 10 + 10, $this->sequenceGenerator->getCurrentMaxValue());
|
|
|
|
self::assertSame($i + 1, $this->sequenceGenerator->getNextValue());
|
2009-03-30 19:43:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|