vendor/doctrine-fixtures/tests/Doctrine/Tests/Common/DataFixtures/ReferenceRepositoryTest.php
author cavaliet
Mon, 07 Jul 2014 17:23:47 +0200
changeset 122 d672f7dd74dc
parent 58 624e5900f5a4
permissions -rw-r--r--
Added tag V00.17 for changeset ada5f3d8b5b4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/*
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
 *
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
 * This software consists of voluntary contributions made by many individuals
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
 * and is licensed under the LGPL. For more information, see
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
 * <http://www.doctrine-project.org>.
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
 */
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
namespace Doctrine\Tests\Common\DataFixtures;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
use Doctrine\Common\DataFixtures\ReferenceRepository;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
use Doctrine\Common\DataFixtures\Event\Listener\ORMReferenceListener;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
use Doctrine\ORM\Tools\SchemaTool;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
use Doctrine\ORM\Proxy\Proxy;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
require_once __DIR__.'/TestInit.php';
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
/**
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
 * Test ReferenceRepository.
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
 *
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
 * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
 */
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
class ReferenceRepositoryTest extends BaseTest
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
{
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    const TEST_ENTITY_ROLE = 'Doctrine\Tests\Common\DataFixtures\TestEntity\Role';
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    public function testReferenceEntry()
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    39
    {
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        $em = $this->getMockAnnotationReaderEntityManager();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        $role = new TestEntity\Role;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
        $role->setName('admin');
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        $meta = $em->getClassMetadata(self::TEST_ENTITY_ROLE);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        $meta->getReflectionProperty('id')->setValue($role, 1);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
        $referenceRepo = new ReferenceRepository($em);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
        $referenceRepo->addReference('test', $role);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
        $references = $referenceRepo->getReferences();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
        $this->assertEquals(1, count($references));
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
        $this->assertArrayHasKey('test', $references);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
        $this->assertInstanceOf(self::TEST_ENTITY_ROLE, $references['test']);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
    }
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
    public function testReferenceIdentityPopulation()
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
    {
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
        $em = $this->getMockSqliteEntityManager();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
        $referenceRepository = $this->getMockBuilder('Doctrine\Common\DataFixtures\ReferenceRepository')
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
            ->setConstructorArgs(array($em))
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
            ->getMock();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        $em->getEventManager()->addEventSubscriber(
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
            new ORMReferenceListener($referenceRepository)
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        );
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        $schemaTool = new SchemaTool($em);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        $schemaTool->dropSchema(array());
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        $schemaTool->createSchema(array(
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
            $em->getClassMetadata(self::TEST_ENTITY_ROLE)
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        ));
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        $referenceRepository->expects($this->once())
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
            ->method('addReference')
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
            ->with('admin-role');
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        $referenceRepository->expects($this->once())
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
            ->method('getReferenceName')
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
            ->will($this->returnValue('admin-role'));
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        $referenceRepository->expects($this->once())
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            ->method('setReferenceIdentity')
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
            ->with('admin-role', array('id' => 1));
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
        $roleFixture = new TestFixtures\RoleFixture;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
        $roleFixture->setReferenceRepository($referenceRepository);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
        $roleFixture->load($em);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
    }
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
    public function testReferenceReconstruction()
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
    {
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        $em = $this->getMockSqliteEntityManager();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        $referenceRepository = new ReferenceRepository($em);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        $em->getEventManager()->addEventSubscriber(
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
            new ORMReferenceListener($referenceRepository)
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        );
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        $schemaTool = new SchemaTool($em);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
        $schemaTool->dropSchema(array());
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        $schemaTool->createSchema(array(
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
            $em->getClassMetadata(self::TEST_ENTITY_ROLE)
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        ));
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        $roleFixture = new TestFixtures\RoleFixture;
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        $roleFixture->setReferenceRepository($referenceRepository);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        $roleFixture->load($em);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        // first test against managed state
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        $ref = $referenceRepository->getReference('admin-role');
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
        $this->assertFalse($ref instanceof Proxy);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        // now test reference reconstruction from identity
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        $em->clear();
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
        $ref = $referenceRepository->getReference('admin-role');
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
        $this->assertTrue($ref instanceof Proxy);
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
    }
624e5900f5a4 add tests and fixtures
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
}