Rajout de doctrine/orm
[zf2.biz/galerie.git] / vendor / doctrine / orm / tests / Doctrine / Tests / ORM / EntityManagerTest.php
1 <?php
2
3 namespace Doctrine\Tests\ORM;
4
5 require_once __DIR__ . '/../TestInit.php';
6
7 class EntityManagerTest extends \Doctrine\Tests\OrmTestCase
8 {
9     private $_em;
10
11     function setUp()
12     {
13         parent::setUp();
14         $this->_em = $this->_getTestEntityManager();
15     }
16
17     /**
18      * @group DDC-899
19      */
20     public function testIsOpen()
21     {
22         $this->assertTrue($this->_em->isOpen());
23         $this->_em->close();
24         $this->assertFalse($this->_em->isOpen());
25     }
26
27     public function testGetConnection()
28     {
29         $this->assertInstanceOf('Doctrine\DBAL\Connection', $this->_em->getConnection());
30     }
31
32     public function testGetMetadataFactory()
33     {
34         $this->assertInstanceOf('Doctrine\ORM\Mapping\ClassMetadataFactory', $this->_em->getMetadataFactory());
35     }
36
37     public function testGetConfiguration()
38     {
39         $this->assertInstanceOf('Doctrine\ORM\Configuration', $this->_em->getConfiguration());
40     }
41
42     public function testGetUnitOfWork()
43     {
44         $this->assertInstanceOf('Doctrine\ORM\UnitOfWork', $this->_em->getUnitOfWork());
45     }
46
47     public function testGetProxyFactory()
48     {
49         $this->assertInstanceOf('Doctrine\ORM\Proxy\ProxyFactory', $this->_em->getProxyFactory());
50     }
51
52     public function testGetEventManager()
53     {
54         $this->assertInstanceOf('Doctrine\Common\EventManager', $this->_em->getEventManager());
55     }
56
57     public function testCreateNativeQuery()
58     {
59         $rsm = new \Doctrine\ORM\Query\ResultSetMapping();
60         $query = $this->_em->createNativeQuery('SELECT foo', $rsm);
61
62         $this->assertSame('SELECT foo', $query->getSql());
63     }
64
65     public function testCreateQueryBuilder()
66     {
67         $this->assertInstanceOf('Doctrine\ORM\QueryBuilder', $this->_em->createQueryBuilder());
68     }
69
70     public function testCreateQueryBuilderAliasValid()
71     {
72         $q = $this->_em->createQueryBuilder()
73              ->select('u')->from('Doctrine\Tests\Models\CMS\CmsUser', 'u');
74         $q2 = clone $q;
75
76         $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q->getQuery()->getDql());
77         $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q2->getQuery()->getDql());
78
79         $q3 = clone $q;
80
81         $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q3->getQuery()->getDql());
82     }
83
84     public function testCreateQuery_DqlIsOptional()
85     {
86         $this->assertInstanceOf('Doctrine\ORM\Query', $this->_em->createQuery());
87     }
88
89     public function testGetPartialReference()
90     {
91         $user = $this->_em->getPartialReference('Doctrine\Tests\Models\CMS\CmsUser', 42);
92         $this->assertTrue($this->_em->contains($user));
93         $this->assertEquals(42, $user->id);
94         $this->assertNull($user->getName());
95     }
96
97     public function testCreateQuery()
98     {
99         $q = $this->_em->createQuery('SELECT 1');
100         $this->assertInstanceOf('Doctrine\ORM\Query', $q);
101         $this->assertEquals('SELECT 1', $q->getDql());
102     }
103
104     static public function dataMethodsAffectedByNoObjectArguments()
105     {
106         return array(
107             array('persist'),
108             array('remove'),
109             array('merge'),
110             array('refresh'),
111             array('detach')
112         );
113     }
114
115     /**
116      * @dataProvider dataMethodsAffectedByNoObjectArguments
117      */
118     public function testThrowsExceptionOnNonObjectValues($methodName) {
119         $this->setExpectedException('Doctrine\ORM\ORMInvalidArgumentException',
120             'EntityManager#'.$methodName.'() expects parameter 1 to be an entity object, NULL given.');
121         $this->_em->$methodName(null);
122     }
123
124     static public function dataAffectedByErrorIfClosedException()
125     {
126         return array(
127             array('flush'),
128             array('persist'),
129             array('remove'),
130             array('merge'),
131             array('refresh'),
132         );
133     }
134
135     /**
136      * @dataProvider dataAffectedByErrorIfClosedException
137      * @param string $methodName
138      */
139     public function testAffectedByErrorIfClosedException($methodName)
140     {
141         $this->setExpectedException('Doctrine\ORM\ORMException', 'closed');
142
143         $this->_em->close();
144         $this->_em->$methodName(new \stdClass());
145     }
146
147     /**
148      * @group DDC-1125
149      */
150     public function testTransactionalAcceptsReturn()
151     {
152         $return = $this->_em->transactional(function ($em) {
153             return 'foo';
154         });
155
156         $this->assertEquals('foo', $return);
157     }
158
159     public function testTransactionalAcceptsVariousCallables()
160     {
161         $this->assertSame('callback', $this->_em->transactional(array($this, 'transactionalCallback')));
162     }
163
164     public function testTransactionalThrowsInvalidArgumentExceptionIfNonCallablePassed()
165     {
166         $this->setExpectedException('InvalidArgumentException', 'Expected argument of type "callable", got "object"');
167         $this->_em->transactional($this);
168     }
169
170     public function transactionalCallback($em)
171     {
172         $this->assertSame($this->_em, $em);
173         return 'callback';
174     }
175 }