EntityRepositoryTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\Tests\Models\CMS\CmsUser;
  4. use Doctrine\Tests\Models\CMS\CmsAddress;
  5. use Doctrine\Tests\Models\CMS\CmsPhonenumber;
  6. require_once __DIR__ . '/../../TestInit.php';
  7. /**
  8. * @author robo
  9. */
  10. class EntityRepositoryTest extends \Doctrine\Tests\OrmFunctionalTestCase
  11. {
  12. protected function setUp() {
  13. $this->useModelSet('cms');
  14. parent::setUp();
  15. }
  16. public function tearDown()
  17. {
  18. if ($this->_em) {
  19. $this->_em->getConfiguration()->setEntityNamespaces(array());
  20. }
  21. parent::tearDown();
  22. }
  23. public function loadFixture()
  24. {
  25. $user = new CmsUser;
  26. $user->name = 'Roman';
  27. $user->username = 'romanb';
  28. $user->status = 'freak';
  29. $this->_em->persist($user);
  30. $user2 = new CmsUser;
  31. $user2->name = 'Guilherme';
  32. $user2->username = 'gblanco';
  33. $user2->status = 'dev';
  34. $this->_em->persist($user2);
  35. $user3 = new CmsUser;
  36. $user3->name = 'Benjamin';
  37. $user3->username = 'beberlei';
  38. $user3->status = null;
  39. $this->_em->persist($user3);
  40. $this->_em->flush();
  41. $user1Id = $user->getId();
  42. unset($user);
  43. unset($user2);
  44. unset($user3);
  45. $this->_em->clear();
  46. return $user1Id;
  47. }
  48. public function loadAssociatedFixture()
  49. {
  50. $address = new CmsAddress();
  51. $address->city = "Berlin";
  52. $address->country = "Germany";
  53. $address->street = "Foostreet";
  54. $address->zip = "12345";
  55. $user = new CmsUser();
  56. $user->name = 'Roman';
  57. $user->username = 'romanb';
  58. $user->status = 'freak';
  59. $user->setAddress($address);
  60. $this->_em->persist($user);
  61. $this->_em->persist($address);
  62. $this->_em->flush();
  63. $this->_em->clear();
  64. return array($user->id, $address->id);
  65. }
  66. public function buildUser($name, $username, $status, $address)
  67. {
  68. $user = new CmsUser();
  69. $user->name = $name;
  70. $user->username = $username;
  71. $user->status = $status;
  72. $user->setAddress($address);
  73. $this->_em->persist($user);
  74. $this->_em->flush();
  75. return $user;
  76. }
  77. public function buildAddress($country, $city, $street, $zip)
  78. {
  79. $address = new CmsAddress();
  80. $address->country = $country;
  81. $address->city = $city;
  82. $address->street = $street;
  83. $address->zip = $zip;
  84. $this->_em->persist($address);
  85. $this->_em->flush();
  86. return $address;
  87. }
  88. public function testBasicFind()
  89. {
  90. $user1Id = $this->loadFixture();
  91. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  92. $user = $repos->find($user1Id);
  93. $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$user);
  94. $this->assertEquals('Roman', $user->name);
  95. $this->assertEquals('freak', $user->status);
  96. }
  97. public function testFindByField()
  98. {
  99. $user1Id = $this->loadFixture();
  100. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  101. $users = $repos->findBy(array('status' => 'dev'));
  102. $this->assertEquals(1, count($users));
  103. $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$users[0]);
  104. $this->assertEquals('Guilherme', $users[0]->name);
  105. $this->assertEquals('dev', $users[0]->status);
  106. }
  107. public function testFindByAssociationWithIntegerAsParameter()
  108. {
  109. $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
  110. $user1 = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
  111. $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
  112. $user2 = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
  113. $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
  114. $user3 = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
  115. unset($address1);
  116. unset($address2);
  117. unset($address3);
  118. $this->_em->clear();
  119. $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
  120. $addresses = $repository->findBy(array('user' => array($user1->getId(), $user2->getId())));
  121. $this->assertEquals(2, count($addresses));
  122. $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress',$addresses[0]);
  123. }
  124. public function testFindByAssociationWithObjectAsParameter()
  125. {
  126. $address1 = $this->buildAddress('Germany', 'Berlim', 'Foo st.', '123456');
  127. $user1 = $this->buildUser('Benjamin', 'beberlei', 'dev', $address1);
  128. $address2 = $this->buildAddress('Brazil', 'São Paulo', 'Bar st.', '654321');
  129. $user2 = $this->buildUser('Guilherme', 'guilhermeblanco', 'freak', $address2);
  130. $address3 = $this->buildAddress('USA', 'Nashville', 'Woo st.', '321654');
  131. $user3 = $this->buildUser('Jonathan', 'jwage', 'dev', $address3);
  132. unset($address1);
  133. unset($address2);
  134. unset($address3);
  135. $this->_em->clear();
  136. $repository = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
  137. $addresses = $repository->findBy(array('user' => array($user1, $user2)));
  138. $this->assertEquals(2, count($addresses));
  139. $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress',$addresses[0]);
  140. }
  141. public function testFindFieldByMagicCall()
  142. {
  143. $user1Id = $this->loadFixture();
  144. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  145. $users = $repos->findByStatus('dev');
  146. $this->assertEquals(1, count($users));
  147. $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsUser',$users[0]);
  148. $this->assertEquals('Guilherme', $users[0]->name);
  149. $this->assertEquals('dev', $users[0]->status);
  150. }
  151. public function testFindAll()
  152. {
  153. $user1Id = $this->loadFixture();
  154. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  155. $users = $repos->findAll();
  156. $this->assertEquals(3, count($users));
  157. }
  158. public function testFindByAlias()
  159. {
  160. $user1Id = $this->loadFixture();
  161. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  162. $this->_em->getConfiguration()->addEntityNamespace('CMS', 'Doctrine\Tests\Models\CMS');
  163. $repos = $this->_em->getRepository('CMS:CmsUser');
  164. $users = $repos->findAll();
  165. $this->assertEquals(3, count($users));
  166. }
  167. /**
  168. * @expectedException \Doctrine\ORM\ORMException
  169. */
  170. public function testExceptionIsThrownWhenCallingFindByWithoutParameter() {
  171. $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
  172. ->findByStatus();
  173. }
  174. /**
  175. * @expectedException \Doctrine\ORM\ORMException
  176. */
  177. public function testExceptionIsThrownWhenUsingInvalidFieldName() {
  178. $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
  179. ->findByThisFieldDoesNotExist('testvalue');
  180. }
  181. /**
  182. * @group locking
  183. * @group DDC-178
  184. */
  185. public function testPessimisticReadLockWithoutTransaction_ThrowsException()
  186. {
  187. $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
  188. $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
  189. ->find(1, \Doctrine\DBAL\LockMode::PESSIMISTIC_READ);
  190. }
  191. /**
  192. * @group locking
  193. * @group DDC-178
  194. */
  195. public function testPessimisticWriteLockWithoutTransaction_ThrowsException()
  196. {
  197. $this->setExpectedException('Doctrine\ORM\TransactionRequiredException');
  198. $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
  199. ->find(1, \Doctrine\DBAL\LockMode::PESSIMISTIC_WRITE);
  200. }
  201. /**
  202. * @group locking
  203. * @group DDC-178
  204. */
  205. public function testOptimisticLockUnversionedEntity_ThrowsException()
  206. {
  207. $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
  208. $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser')
  209. ->find(1, \Doctrine\DBAL\LockMode::OPTIMISTIC);
  210. }
  211. /**
  212. * @group locking
  213. * @group DDC-178
  214. */
  215. public function testIdentityMappedOptimisticLockUnversionedEntity_ThrowsException()
  216. {
  217. $user = new CmsUser;
  218. $user->name = 'Roman';
  219. $user->username = 'romanb';
  220. $user->status = 'freak';
  221. $this->_em->persist($user);
  222. $this->_em->flush();
  223. $userId = $user->id;
  224. $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId);
  225. $this->setExpectedException('Doctrine\ORM\OptimisticLockException');
  226. $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $userId, \Doctrine\DBAL\LockMode::OPTIMISTIC);
  227. }
  228. /**
  229. * @group DDC-819
  230. */
  231. public function testFindMagicCallByNullValue()
  232. {
  233. $this->loadFixture();
  234. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  235. $users = $repos->findByStatus(null);
  236. $this->assertEquals(1, count($users));
  237. }
  238. /**
  239. * @group DDC-819
  240. */
  241. public function testInvalidMagicCall()
  242. {
  243. $this->setExpectedException('BadMethodCallException');
  244. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  245. $repos->foo();
  246. }
  247. /**
  248. * @group DDC-817
  249. */
  250. public function testFindByAssociationKey_ExceptionOnInverseSide()
  251. {
  252. list($userId, $addressId) = $this->loadAssociatedFixture();
  253. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  254. $this->setExpectedException('Doctrine\ORM\ORMException', "You cannot search for the association field 'Doctrine\Tests\Models\CMS\CmsUser#address', because it is the inverse side of an association. Find methods only work on owning side associations.");
  255. $user = $repos->findBy(array('address' => $addressId));
  256. }
  257. /**
  258. * @group DDC-817
  259. */
  260. public function testFindOneByAssociationKey()
  261. {
  262. list($userId, $addressId) = $this->loadAssociatedFixture();
  263. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
  264. $address = $repos->findOneBy(array('user' => $userId));
  265. $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $address);
  266. $this->assertEquals($addressId, $address->id);
  267. }
  268. /**
  269. * @group DDC-817
  270. */
  271. public function testFindByAssociationKey()
  272. {
  273. list($userId, $addressId) = $this->loadAssociatedFixture();
  274. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
  275. $addresses = $repos->findBy(array('user' => $userId));
  276. $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsAddress', $addresses);
  277. $this->assertEquals(1, count($addresses));
  278. $this->assertEquals($addressId, $addresses[0]->id);
  279. }
  280. /**
  281. * @group DDC-817
  282. */
  283. public function testFindAssociationByMagicCall()
  284. {
  285. list($userId, $addressId) = $this->loadAssociatedFixture();
  286. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
  287. $addresses = $repos->findByUser($userId);
  288. $this->assertContainsOnly('Doctrine\Tests\Models\CMS\CmsAddress', $addresses);
  289. $this->assertEquals(1, count($addresses));
  290. $this->assertEquals($addressId, $addresses[0]->id);
  291. }
  292. /**
  293. * @group DDC-817
  294. */
  295. public function testFindOneAssociationByMagicCall()
  296. {
  297. list($userId, $addressId) = $this->loadAssociatedFixture();
  298. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsAddress');
  299. $address = $repos->findOneByUser($userId);
  300. $this->assertInstanceOf('Doctrine\Tests\Models\CMS\CmsAddress', $address);
  301. $this->assertEquals($addressId, $address->id);
  302. }
  303. public function testValidNamedQueryRetrieval()
  304. {
  305. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  306. $query = $repos->createNamedQuery('all');
  307. $this->assertInstanceOf('Doctrine\ORM\Query', $query);
  308. $this->assertEquals('SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u', $query->getDQL());
  309. }
  310. public function testInvalidNamedQueryRetrieval()
  311. {
  312. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  313. $this->setExpectedException('Doctrine\ORM\Mapping\MappingException');
  314. $repos->createNamedQuery('invalidNamedQuery');
  315. }
  316. /**
  317. * @group DDC-1087
  318. */
  319. public function testIsNullCriteriaDoesNotGenerateAParameter()
  320. {
  321. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  322. $users = $repos->findBy(array('status' => null, 'username' => 'romanb'));
  323. $params = $this->_sqlLoggerStack->queries[$this->_sqlLoggerStack->currentQuery]['params'];
  324. $this->assertEquals(1, count($params), "Should only execute with one parameter.");
  325. $this->assertEquals(array('romanb'), $params);
  326. }
  327. public function testIsNullCriteria()
  328. {
  329. $this->loadFixture();
  330. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  331. $users = $repos->findBy(array('status' => null));
  332. $this->assertEquals(1, count($users));
  333. }
  334. /**
  335. * @group DDC-1094
  336. */
  337. public function testFindByLimitOffset()
  338. {
  339. $this->loadFixture();
  340. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  341. $users1 = $repos->findBy(array(), null, 1, 0);
  342. $users2 = $repos->findBy(array(), null, 1, 1);
  343. $this->assertEquals(3, count($repos->findBy(array())));
  344. $this->assertEquals(1, count($users1));
  345. $this->assertEquals(1, count($users2));
  346. $this->assertNotSame($users1[0], $users2[0]);
  347. }
  348. /**
  349. * @group DDC-1094
  350. */
  351. public function testFindByOrderBy()
  352. {
  353. $this->loadFixture();
  354. $repos = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  355. $usersAsc = $repos->findBy(array(), array("username" => "ASC"));
  356. $usersDesc = $repos->findBy(array(), array("username" => "DESC"));
  357. $this->assertEquals(3, count($usersAsc), "Pre-condition: only three users in fixture");
  358. $this->assertEquals(3, count($usersDesc), "Pre-condition: only three users in fixture");
  359. $this->assertSame($usersAsc[0], $usersDesc[2]);
  360. $this->assertSame($usersAsc[2], $usersDesc[0]);
  361. }
  362. /**
  363. * @group DDC-753
  364. */
  365. public function testDefaultRepositoryClassName()
  366. {
  367. $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository");
  368. $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository");
  369. $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository");
  370. $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithDefaultCustomRepository');
  371. $this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753DefaultRepository", $repos);
  372. $this->assertTrue($repos->isDefaultRepository());
  373. $repos = $this->_em->getRepository('Doctrine\Tests\Models\DDC753\DDC753EntityWithCustomRepository');
  374. $this->assertInstanceOf("Doctrine\Tests\Models\DDC753\DDC753CustomRepository", $repos);
  375. $this->assertTrue($repos->isCustomRepository());
  376. $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\Tests\Models\DDC753\DDC753DefaultRepository");
  377. $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\ORM\EntityRepository");
  378. $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository");
  379. }
  380. /**
  381. * @group DDC-753
  382. * @expectedException Doctrine\ORM\ORMException
  383. * @expectedExceptionMessage Invalid repository class 'Doctrine\Tests\Models\DDC753\DDC753InvalidRepository'. it must be a Doctrine\ORM\EntityRepository.
  384. */
  385. public function testSetDefaultRepositoryInvalidClassError()
  386. {
  387. $this->assertEquals($this->_em->getConfiguration()->getDefaultRepositoryClassName(), "Doctrine\ORM\EntityRepository");
  388. $this->_em->getConfiguration()->setDefaultRepositoryClassName("Doctrine\Tests\Models\DDC753\DDC753InvalidRepository");
  389. }
  390. /**
  391. * @group DDC-1500
  392. */
  393. public function testInvalidOrientation()
  394. {
  395. $this->setExpectedException('Doctrine\ORM\ORMException', 'Invalid order by orientation specified for Doctrine\Tests\Models\CMS\CmsUser#username');
  396. $repo = $this->_em->getRepository('Doctrine\Tests\Models\CMS\CmsUser');
  397. $repo->findBy(array('status' => 'test'), array('username' => 'INVALID'));
  398. }
  399. }