Permalink
Browse files

Removing unnecessary logic in ReferenceRepository#setReference()

  • Loading branch information...
1 parent dcd9f78 commit a84a3e1903f51d4d3c3ff8362ea02710bc59ceb2 @jwage jwage committed Feb 26, 2011
@@ -57,11 +57,7 @@ public function __construct($manager)
* Set the reference entry identified by $name
* and referenced to managed $object. If $name
* already is set, it overrides it
- *
- * Notice: in case if identifier is generated after
- * the record is inserted, be sure tu use this method
- * after $object is flushed
- *
+ *
* @param string $name
* @param object $object - managed object
* @throws LogicException - if object is not mapped or
@@ -70,27 +66,7 @@ public function __construct($manager)
*/
public function setReference($name, $object)
{
- $objectClass = get_class($object);
- if (!$this->manager->getMetadataFactory()->hasMetadataFor(get_class($object))) {
- throw new \LogicException("While setting a reference object {$objectClass} - must be mapped or persisted before");
- }
- $meta = $this->manager->getClassMetadata(get_class($object));
-
- $identifier = array();
- foreach ((array)$meta->identifier as $field) {
- $id = $meta->getReflectionProperty($field)->getValue($object);
- if (!$id) {
- throw new \LogicException("Object: {$objectClass} - must be flushed first in order to have generated id");
- }
- $identifier[$field] = $id;
- }
- if (!is_array($meta->identifier)) {
- $identifier = reset($identifier);
- }
- $this->references[$name] = array(
- 'class' => $meta->name,
- 'identifier' => $identifier
- );
+ $this->references[$name] = $object;
}
/**
@@ -125,16 +101,7 @@ public function addReference($name, $object)
*/
public function getReference($name)
{
- $object = null;
- if (isset($this->references[$name])) {
- $object = $this->manager->getReference(
- $this->references[$name]['class'],
- $this->references[$name]['identifier']
- );
- } else {
- throw new \InvalidArgumentException("There is no reference stored under the name: [{$name}], check if fixtures are loading in correct order");
- }
- return $object;
+ return $this->references[$name];
}
/**
View
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<phpunit backupGlobals="false"
+ backupStaticAttributes="false"
+ colors="false"
+ convertErrorsToExceptions="true"
+ convertNoticesToExceptions="true"
+ convertWarningsToExceptions="true"
+ stopOnFailure="false"
+ syntaxCheck="false"
+>
+ <testsuites>
+ <testsuite name="Doctrine Data Fixtures Test Suite">
+ <directory>./tests/Doctrine/</directory>
+ </testsuite>
+ </testsuites>
+</phpunit>
@@ -24,6 +24,8 @@
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\Proxy\Proxy;
+use Doctrine\Tests\Common\DataFixtures\TestEntity\Role;
+use Doctrine\Tests\Common\DataFixtures\TestEntity\User;
/**
* Test referenced fixture execution
@@ -34,23 +36,23 @@ class ORMExecutorSharedFixtureTest extends BaseTest
{
const TEST_ENTITY_ROLE = 'Doctrine\Tests\Common\DataFixtures\TestEntity\Role';
const TEST_ENTITY_USER = 'Doctrine\Tests\Common\DataFixtures\TestEntity\User';
-
+
public function testFixtureExecution()
{
$em = $this->getMockAnnotationReaderEntityManager();
$purger = new ORMPurger();
$executor = new ORMExecutor($em, $purger);
-
+
$referenceRepository = $executor->getReferenceRepository();
$fixture = $this->getMockFixture();
$fixture->expects($this->once())
->method('load')
->with($em);
-
+
$fixture->expects($this->once())
->method('setReferenceRepository')
->with($referenceRepository);
-
+
$executor->execute(array($fixture), true);
}
@@ -59,35 +61,35 @@ public function testSharedFixtures()
if (!extension_loaded('pdo_sqlite')) {
$this->markTestSkipped('Missing pdo_sqlite extension.');
}
-
+
$em = $this->getMockSqliteEntityManager();
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($em);
$schemaTool->dropSchema(array());
$schemaTool->createSchema(array(
$em->getClassMetadata(self::TEST_ENTITY_ROLE),
$em->getClassMetadata(self::TEST_ENTITY_USER)
));
-
+
$purger = new ORMPurger();
$executor = new ORMExecutor($em, $purger);
-
+
$userFixture = new TestFixtures\UserFixture;
$roleFixture = new TestFixtures\RoleFixture;
$executor->execute(array($roleFixture, $userFixture), true);
-
+
$referenceRepository = $executor->getReferenceRepository();
$references = $referenceRepository->getReferences();
-
+
$this->assertEquals(2, count($references));
$roleReference = $referenceRepository->getReference('admin-role');
- $this->assertTrue($roleReference instanceof Proxy);
+ $this->assertTrue($roleReference instanceof Role);
$this->assertEquals('admin', $roleReference->getName());
-
+
$userReference = $referenceRepository->getReference('admin');
- $this->assertTrue($userReference instanceof Proxy);
+ $this->assertTrue($userReference instanceof User);
$this->assertEquals('admin@example.com', $userReference->getEmail());
}
-
+
private function getMockFixture()
{
return $this->getMock('Doctrine\Common\DataFixtures\SharedFixtureInterface');
@@ -31,7 +31,7 @@
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
*/
class OrderedFixtureTest extends BaseTest
-{
+{
public function testFixtureOrder()
{
$loader = new Loader();
@@ -53,7 +53,7 @@ class OrderedFixture1 implements FixtureInterface, OrderedFixtureInterface
{
public function load($manager)
{}
-
+
public function getOrder()
{
return 5;
@@ -64,7 +64,7 @@ class OrderedFixture2 implements FixtureInterface, OrderedFixtureInterface
{
public function load($manager)
{}
-
+
public function getOrder()
{
return 2;
@@ -75,7 +75,7 @@ class OrderedFixture3 implements FixtureInterface, OrderedFixtureInterface
{
public function load($manager)
{}
-
+
public function getOrder()
{
return 8;
@@ -32,25 +32,6 @@ class ReferenceRepositoryTest extends BaseTest
{
const TEST_ENTITY_ROLE = 'Doctrine\Tests\Common\DataFixtures\TestEntity\Role';
- public function testInvalidReferenceObject()
- {
- $em = $this->getMockEntityManager();
- $referenceRepo = new ReferenceRepository($em);
- $this->setExpectedException('LogicException');
- $referenceRepo->addReference('test', new \stdClass());
- }
-
- public function testInvalidReferenceObjectNoPk()
- {
- $em = $this->getMockAnnotationReaderEntityManager();
- $role = new TestEntity\Role;
- $meta = $em->getClassMetadata(self::TEST_ENTITY_ROLE);
-
- $referenceRepo = new ReferenceRepository($em);
- $this->setExpectedException('LogicException');
- $referenceRepo->addReference('test', $role);
- }
-
public function testReferenceEntry()
{
$em = $this->getMockAnnotationReaderEntityManager();
@@ -65,10 +46,7 @@ public function testReferenceEntry()
$references = $referenceRepo->getReferences();
$this->assertEquals(1, count($references));
$this->assertArrayHasKey('test', $references);
- $this->assertEquals(self::TEST_ENTITY_ROLE, $references['test']['class']);
- $this->assertEquals(1, count($references['test']['identifier']));
- $this->assertArrayHasKey('id', $references['test']['identifier']);
- $this->assertEquals(1, $references['test']['identifier']['id']);
+ $this->assertInstanceOf(self::TEST_ENTITY_ROLE, $references['test']);
}
private function getMockReferenceRepository()
@@ -15,7 +15,7 @@ class Role
private $id;
/**
- * @Column(length=50, unique=true)
+ * @Column(length=50)
*/
private $name;
@@ -25,12 +25,12 @@ class User
private $password;
/**
- * @Column(length=255, unique=true)
+ * @Column(length=255)
*/
private $email;
/**
- * @ManyToOne(targetEntity="Role")
+ * @ManyToOne(targetEntity="Role", cascade={"persist"})
*/
private $role;
@@ -9,24 +9,24 @@
class RoleFixture implements SharedFixtureInterface
{
private $referenceRepository;
-
+
public function setReferenceRepository(ReferenceRepository $referenceRepository)
{
$this->referenceRepository = $referenceRepository;
}
-
+
public function load($manager)
{
$adminRole = new Role();
$adminRole->setName('admin');
-
+
$anonymousRole = new Role;
$anonymousRole->setName('anonymous');
$manager->persist($adminRole);
$manager->persist($anonymousRole);
$manager->flush();
-
+
$this->referenceRepository->addReference('admin-role', $adminRole);
}
}
@@ -15,10 +15,10 @@ public function load($manager)
$admin->setPassword('secret');
$role = $this->getReference('admin-role');
$admin->setRole($role);
-
+
$manager->persist($admin);
$manager->flush();
-
+
$this->addReference('admin', $admin);
}
}
View
@@ -1,8 +0,0 @@
-<?xml version="1.0" encoding="UTF-8"?>
-<phpunit>
- <testsuites>
- <testsuite name="Data Fixture tests">
- <directory suffix="Test.php">Doctrine/Tests/Common/DataFixtures/</directory>
- </testsuite>
- </testsuites>
-</phpunit>

0 comments on commit a84a3e1

Please sign in to comment.