1
0
mirror of synced 2025-01-09 18:47:10 +03:00
doctrine2/tests/Doctrine/Tests/ORM/Utility/IdentifierFlattenerTest.php
Rob Caiger dd398ce577 - Fixed the basic entity persister so that versioned OneToOne entities can be created
- Created an IdentifierFlattener utility class
- Moved the logic for the flatten identifier method into the new utility class
- Replaced calls for private flattenIdentifier to use new utility
- Added appropriate unit tests
2014-10-01 14:01:44 +01:00

132 lines
4.2 KiB
PHP

<?php
namespace Doctrine\Tests\ORM\Utility;
use Doctrine\Tests\OrmFunctionalTestCase;
use Doctrine\Tests\Models\VersionedOneToOne\FirstRelatedEntity;
use Doctrine\Tests\Models\VersionedOneToOne\SecondRelatedEntity;
use Doctrine\Tests\Models\Cache\City;
use Doctrine\Tests\Models\Cache\Flight;
use Doctrine\ORM\ORMException;
use Doctrine\ORM\Utility\IdentifierFlattener;
/**
* Test the IdentifierFlattener utility class
*
* @author Rob Caiger <rob@clocal.co.uk>
* @covers \Doctrine\ORM\Utility\IdentifierFlattener
*/
class IdentifierFlattenerTest extends OrmFunctionalTestCase
{
/**
* Identifier flattener
*
* @var \Doctrine\ORM\Utility\IdentifierFlattener
*/
private $identifierFlattener;
protected function setUp()
{
parent::setUp();
$this->identifierFlattener = new IdentifierFlattener(
$this->_em->getUnitOfWork(),
$this->_em->getMetadataFactory()
);
try {
$this->_schemaTool->createSchema(
array(
$this->_em->getClassMetadata('Doctrine\Tests\Models\VersionedOneToOne\FirstRelatedEntity'),
$this->_em->getClassMetadata('Doctrine\Tests\Models\VersionedOneToOne\SecondRelatedEntity'),
$this->_em->getClassMetadata('Doctrine\Tests\Models\Cache\Flight'),
$this->_em->getClassMetadata('Doctrine\Tests\Models\Cache\City')
)
);
} catch (ORMException $e) {
}
}
/**
* @group utilities
*/
public function testFlattenIdentifierWithOneToOneId()
{
$secondRelatedEntity = new SecondRelatedEntity();
$secondRelatedEntity->name = 'Bob';
$this->_em->persist($secondRelatedEntity);
$this->_em->flush();
$firstRelatedEntity = new FirstRelatedEntity();
$firstRelatedEntity->name = 'Fred';
$firstRelatedEntity->secondEntity = $secondRelatedEntity;
$this->_em->persist($firstRelatedEntity);
$this->_em->flush();
$firstEntity = $this->_em->getRepository('Doctrine\Tests\Models\VersionedOneToOne\FirstRelatedEntity')
->findOneBy(array('name' => 'Fred'));
$class = $this->_em->getClassMetadata('Doctrine\Tests\Models\VersionedOneToOne\FirstRelatedEntity');
$id = $class->getIdentifierValues($firstEntity);
$this->assertCount(1, $id, 'We should have 1 identifier');
$this->assertArrayHasKey('secondEntity', $id, 'It should be called secondEntity');
$this->assertInstanceOf(
'\Doctrine\Tests\Models\VersionedOneToOne\SecondRelatedEntity',
$id['secondEntity'],
'The entity should be an instance of SecondRelatedEntity'
);
$flatIds = $this->identifierFlattener->flattenIdentifier($class, $id);
$this->assertCount(1, $flatIds, 'We should have 1 flattened id');
$this->assertArrayHasKey('secondEntity', $flatIds, 'It should be called secondEntity');
$this->assertSame($id['secondEntity']->id, $flatIds['secondEntity']);
}
/**
* @group utilities
*/
public function testFlattenIdentifierWithMutlipleIds()
{
$leeds = new City('Leeds');
$london = new City('London');
$this->_em->persist($leeds);
$this->_em->persist($london);
$this->_em->flush();
$flight = new Flight($leeds, $london);
$this->_em->persist($flight);
$this->_em->flush();
$class = $this->_em->getClassMetadata('Doctrine\Tests\Models\Cache\Flight');
$id = $class->getIdentifierValues($flight);
$this->assertCount(2, $id);
$this->assertArrayHasKey('leavingFrom', $id);
$this->assertArrayHasKey('goingTo', $id);
$this->assertSame($leeds, $id['leavingFrom']);
$this->assertSame($london, $id['goingTo']);
$flatIds = $this->identifierFlattener->flattenIdentifier($class, $id);
$this->assertCount(2, $flatIds);
$this->assertArrayHasKey('leavingFrom', $flatIds);
$this->assertArrayHasKey('goingTo', $flatIds);
$this->assertSame($id['leavingFrom']->getId(), $flatIds['leavingFrom']);
$this->assertSame($id['goingTo']->getId(), $flatIds['goingTo']);
}
}