PageRenderTime 72ms CodeModel.GetById 43ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Doctrine/Tests/ORM/EntityManagerTest.php

https://github.com/fabpot/doctrine2
PHP | 308 lines | 212 code | 58 blank | 38 comment | 0 complexity | 5fee348a9400acdef8e4e18f4c453428 MD5 | raw file
  1. <?php
  2. namespace Doctrine\Tests\ORM;
  3. use Doctrine\Common\EventManager;
  4. use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
  5. use Doctrine\Common\Persistence\Mapping\MappingException;
  6. use Doctrine\DBAL\Connection;
  7. use Doctrine\ORM\Configuration;
  8. use Doctrine\ORM\EntityManager;
  9. use Doctrine\ORM\Mapping\ClassMetadataFactory;
  10. use Doctrine\ORM\NativeQuery;
  11. use Doctrine\ORM\ORMException;
  12. use Doctrine\ORM\ORMInvalidArgumentException;
  13. use Doctrine\ORM\Proxy\ProxyFactory;
  14. use Doctrine\ORM\Query;
  15. use Doctrine\ORM\Query\ResultSetMapping;
  16. use Doctrine\ORM\QueryBuilder;
  17. use Doctrine\ORM\UnitOfWork;
  18. use Doctrine\Tests\Models\CMS\CmsUser;
  19. use Doctrine\Tests\Models\GeoNames\Country;
  20. use Doctrine\Tests\OrmTestCase;
  21. class EntityManagerTest extends OrmTestCase
  22. {
  23. /**
  24. * @var EntityManager
  25. */
  26. private $_em;
  27. function setUp()
  28. {
  29. parent::setUp();
  30. $this->_em = $this->_getTestEntityManager();
  31. }
  32. /**
  33. * @group DDC-899
  34. */
  35. public function testIsOpen()
  36. {
  37. $this->assertTrue($this->_em->isOpen());
  38. $this->_em->close();
  39. $this->assertFalse($this->_em->isOpen());
  40. }
  41. public function testGetConnection()
  42. {
  43. $this->assertInstanceOf(Connection::class, $this->_em->getConnection());
  44. }
  45. public function testGetMetadataFactory()
  46. {
  47. $this->assertInstanceOf(ClassMetadataFactory::class, $this->_em->getMetadataFactory());
  48. }
  49. public function testGetConfiguration()
  50. {
  51. $this->assertInstanceOf(Configuration::class, $this->_em->getConfiguration());
  52. }
  53. public function testGetUnitOfWork()
  54. {
  55. $this->assertInstanceOf(UnitOfWork::class, $this->_em->getUnitOfWork());
  56. }
  57. public function testGetProxyFactory()
  58. {
  59. $this->assertInstanceOf(ProxyFactory::class, $this->_em->getProxyFactory());
  60. }
  61. public function testGetEventManager()
  62. {
  63. $this->assertInstanceOf(EventManager::class, $this->_em->getEventManager());
  64. }
  65. public function testCreateNativeQuery()
  66. {
  67. $rsm = new ResultSetMapping();
  68. $query = $this->_em->createNativeQuery('SELECT foo', $rsm);
  69. $this->assertSame('SELECT foo', $query->getSql());
  70. }
  71. /**
  72. * @covers \Doctrine\ORM\EntityManager::createNamedNativeQuery
  73. */
  74. public function testCreateNamedNativeQuery()
  75. {
  76. $rsm = new ResultSetMapping();
  77. $this->_em->getConfiguration()->addNamedNativeQuery('foo', 'SELECT foo', $rsm);
  78. $query = $this->_em->createNamedNativeQuery('foo');
  79. $this->assertInstanceOf(NativeQuery::class, $query);
  80. }
  81. public function testCreateQueryBuilder()
  82. {
  83. $this->assertInstanceOf(QueryBuilder::class, $this->_em->createQueryBuilder());
  84. }
  85. public function testCreateQueryBuilderAliasValid()
  86. {
  87. $q = $this->_em->createQueryBuilder()
  88. ->select('u')->from(CmsUser::class, 'u');
  89. $q2 = clone $q;
  90. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q->getQuery()->getDql());
  91. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q2->getQuery()->getDql());
  92. $q3 = clone $q;
  93. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $q3->getQuery()->getDql());
  94. }
  95. public function testCreateQuery_DqlIsOptional()
  96. {
  97. $this->assertInstanceOf(Query::class, $this->_em->createQuery());
  98. }
  99. public function testGetPartialReference()
  100. {
  101. $user = $this->_em->getPartialReference(CmsUser::class, 42);
  102. $this->assertTrue($this->_em->contains($user));
  103. $this->assertEquals(42, $user->id);
  104. $this->assertNull($user->getName());
  105. }
  106. public function testCreateQuery()
  107. {
  108. $q = $this->_em->createQuery('SELECT 1');
  109. $this->assertInstanceOf(Query::class, $q);
  110. $this->assertEquals('SELECT 1', $q->getDql());
  111. }
  112. /**
  113. * @covers Doctrine\ORM\EntityManager::createNamedQuery
  114. */
  115. public function testCreateNamedQuery()
  116. {
  117. $this->_em->getConfiguration()->addNamedQuery('foo', 'SELECT 1');
  118. $query = $this->_em->createNamedQuery('foo');
  119. $this->assertInstanceOf(Query::class, $query);
  120. $this->assertEquals('SELECT 1', $query->getDql());
  121. }
  122. static public function dataMethodsAffectedByNoObjectArguments()
  123. {
  124. return [
  125. ['persist'],
  126. ['remove'],
  127. ['merge'],
  128. ['refresh'],
  129. ['detach']
  130. ];
  131. }
  132. /**
  133. * @dataProvider dataMethodsAffectedByNoObjectArguments
  134. */
  135. public function testThrowsExceptionOnNonObjectValues($methodName) {
  136. $this->expectException(ORMInvalidArgumentException::class);
  137. $this->expectExceptionMessage('EntityManager#' . $methodName . '() expects parameter 1 to be an entity object, NULL given.');
  138. $this->_em->$methodName(null);
  139. }
  140. static public function dataAffectedByErrorIfClosedException()
  141. {
  142. return [
  143. ['flush'],
  144. ['persist'],
  145. ['remove'],
  146. ['merge'],
  147. ['refresh'],
  148. ];
  149. }
  150. /**
  151. * @dataProvider dataAffectedByErrorIfClosedException
  152. * @param string $methodName
  153. */
  154. public function testAffectedByErrorIfClosedException($methodName)
  155. {
  156. $this->expectException(ORMException::class);
  157. $this->expectExceptionMessage('closed');
  158. $this->_em->close();
  159. $this->_em->$methodName(new \stdClass());
  160. }
  161. /**
  162. * @group DDC-1125
  163. */
  164. public function testTransactionalAcceptsReturn()
  165. {
  166. $return = $this->_em->transactional(function ($em) {
  167. return 'foo';
  168. });
  169. $this->assertEquals('foo', $return);
  170. }
  171. public function testTransactionalAcceptsVariousCallables()
  172. {
  173. $this->assertSame('callback', $this->_em->transactional([$this, 'transactionalCallback']));
  174. }
  175. public function testTransactionalThrowsInvalidArgumentExceptionIfNonCallablePassed()
  176. {
  177. $this->expectException(\InvalidArgumentException::class);
  178. $this->expectExceptionMessage('Expected argument of type "callable", got "object"');
  179. $this->_em->transactional($this);
  180. }
  181. public function transactionalCallback($em)
  182. {
  183. $this->assertSame($this->_em, $em);
  184. return 'callback';
  185. }
  186. public function testCreateInvalidConnection()
  187. {
  188. $this->expectException(\InvalidArgumentException::class);
  189. $this->expectExceptionMessage('Invalid $connection argument of type integer given: "1".');
  190. $config = new Configuration();
  191. $config->setMetadataDriverImpl($this->createMock(MappingDriver::class));
  192. EntityManager::create(1, $config);
  193. }
  194. /**
  195. * @group #5796
  196. */
  197. public function testTransactionalReThrowsThrowables()
  198. {
  199. try {
  200. $this->_em->transactional(function () {
  201. (function (array $value) {
  202. // this only serves as an IIFE that throws a `TypeError`
  203. })(null);
  204. });
  205. self::fail('TypeError expected to be thrown');
  206. } catch (\TypeError $ignored) {
  207. self::assertFalse($this->_em->isOpen());
  208. }
  209. }
  210. /**
  211. * @group 6017
  212. */
  213. public function testClearManagerWithObject()
  214. {
  215. $entity = new Country(456, 'United Kingdom');
  216. $this->expectException(ORMInvalidArgumentException::class);
  217. $this->_em->clear($entity);
  218. }
  219. /**
  220. * @group 6017
  221. */
  222. public function testClearManagerWithUnknownEntityName()
  223. {
  224. $this->expectException(MappingException::class);
  225. $this->_em->clear(uniqid('nonExisting', true));
  226. }
  227. /**
  228. * @group 6017
  229. */
  230. public function testClearManagerWithProxyClassName()
  231. {
  232. $proxy = $this->_em->getReference(Country::class, ['id' => random_int(457, 100000)]);
  233. $entity = new Country(456, 'United Kingdom');
  234. $this->_em->persist($entity);
  235. $this->assertTrue($this->_em->contains($entity));
  236. $this->_em->clear(get_class($proxy));
  237. $this->assertFalse($this->_em->contains($entity));
  238. }
  239. /**
  240. * @group 6017
  241. */
  242. public function testClearManagerWithNullValue()
  243. {
  244. $entity = new Country(456, 'United Kingdom');
  245. $this->_em->persist($entity);
  246. $this->assertTrue($this->_em->contains($entity));
  247. $this->_em->clear(null);
  248. $this->assertFalse($this->_em->contains($entity));
  249. }
  250. }