2009-11-06 10:28:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Doctrine\Tests\ORM\Id;
|
|
|
|
|
2010-03-30 21:14:17 +00:00
|
|
|
use Doctrine\ORM\Id\AssignedGenerator;
|
2016-05-11 01:55:12 +07:00
|
|
|
use Doctrine\ORM\ORMException;
|
|
|
|
use Doctrine\Tests\OrmTestCase;
|
2009-11-06 10:28:37 +00:00
|
|
|
|
|
|
|
/**
|
2010-03-30 21:14:17 +00:00
|
|
|
* AssignedGeneratorTest
|
2009-11-06 10:28:37 +00:00
|
|
|
*
|
|
|
|
* @author robo
|
|
|
|
*/
|
2016-05-11 01:55:12 +07:00
|
|
|
class AssignedGeneratorTest extends OrmTestCase
|
2009-11-06 10:28:37 +00:00
|
|
|
{
|
|
|
|
private $_em;
|
|
|
|
private $_assignedGen;
|
|
|
|
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
$this->_em = $this->_getTestEntityManager();
|
2010-03-30 21:14:17 +00:00
|
|
|
$this->_assignedGen = new AssignedGenerator;
|
2009-11-06 10:28:37 +00:00
|
|
|
}
|
|
|
|
|
2017-05-31 08:14:53 +02:00
|
|
|
/**
|
|
|
|
* @dataProvider entitiesWithoutId
|
|
|
|
*/
|
|
|
|
public function testThrowsExceptionIfIdNotAssigned($entity)
|
2009-11-06 10:28:37 +00:00
|
|
|
{
|
2017-05-31 08:14:53 +02:00
|
|
|
$this->expectException(ORMException::class);
|
2009-11-06 10:28:37 +00:00
|
|
|
|
2017-05-31 08:14:53 +02:00
|
|
|
$this->_assignedGen->generate($this->_em, $entity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function entitiesWithoutId(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'single' => [new AssignedSingleIdEntity()],
|
|
|
|
'composite' => [new AssignedCompositeIdEntity()],
|
|
|
|
];
|
2009-11-06 10:28:37 +00:00
|
|
|
}
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-06 10:28:37 +00:00
|
|
|
public function testCorrectIdGeneration()
|
|
|
|
{
|
|
|
|
$entity = new AssignedSingleIdEntity;
|
|
|
|
$entity->myId = 1;
|
|
|
|
$id = $this->_assignedGen->generate($this->_em, $entity);
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals(['myId' => 1], $id);
|
2011-12-19 22:56:19 +01:00
|
|
|
|
2009-11-06 10:28:37 +00:00
|
|
|
$entity = new AssignedCompositeIdEntity;
|
|
|
|
$entity->myId2 = 2;
|
|
|
|
$entity->myId1 = 4;
|
|
|
|
$id = $this->_assignedGen->generate($this->_em, $entity);
|
2016-12-07 23:33:41 +01:00
|
|
|
$this->assertEquals(['myId1' => 4, 'myId2' => 2], $id);
|
2009-11-06 10:28:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @Entity */
|
|
|
|
class AssignedSingleIdEntity {
|
|
|
|
/** @Id @Column(type="integer") */
|
|
|
|
public $myId;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @Entity */
|
|
|
|
class AssignedCompositeIdEntity {
|
|
|
|
/** @Id @Column(type="integer") */
|
|
|
|
public $myId1;
|
|
|
|
/** @Id @Column(type="integer") */
|
|
|
|
public $myId2;
|
|
|
|
}
|