BasicFunctionalTest.php 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984
  1. <?php
  2. namespace Doctrine\Tests\ORM\Functional;
  3. use Doctrine\ORM\Tools\SchemaTool;
  4. use Doctrine\ORM\Query;
  5. use Doctrine\Tests\Models\CMS\CmsUser;
  6. use Doctrine\Tests\Models\CMS\CmsPhonenumber;
  7. use Doctrine\Tests\Models\CMS\CmsAddress;
  8. use Doctrine\Tests\Models\CMS\CmsGroup;
  9. use Doctrine\Tests\Models\CMS\CmsArticle;
  10. use Doctrine\Tests\Models\CMS\CmsComment;
  11. require_once __DIR__ . '/../../TestInit.php';
  12. class BasicFunctionalTest extends \Doctrine\Tests\OrmFunctionalTestCase
  13. {
  14. protected function setUp()
  15. {
  16. $this->useModelSet('cms');
  17. parent::setUp();
  18. }
  19. public function testBasicUnitsOfWorkWithOneToManyAssociation()
  20. {
  21. // Create
  22. $user = new CmsUser;
  23. $user->name = 'Roman';
  24. $user->username = 'romanb';
  25. $user->status = 'developer';
  26. $this->_em->persist($user);
  27. $this->_em->flush();
  28. $this->assertTrue(is_numeric($user->id));
  29. $this->assertTrue($this->_em->contains($user));
  30. // Read
  31. $user2 = $this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
  32. $this->assertTrue($user === $user2);
  33. // Add a phonenumber
  34. $ph = new CmsPhonenumber;
  35. $ph->phonenumber = "12345";
  36. $user->addPhonenumber($ph);
  37. $this->_em->flush();
  38. $this->assertTrue($this->_em->contains($ph));
  39. $this->assertTrue($this->_em->contains($user));
  40. //$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
  41. // Update name
  42. $user->name = 'guilherme';
  43. $this->_em->flush();
  44. $this->assertEquals('guilherme', $user->name);
  45. // Add another phonenumber
  46. $ph2 = new CmsPhonenumber;
  47. $ph2->phonenumber = "6789";
  48. $user->addPhonenumber($ph2);
  49. $this->_em->flush();
  50. $this->assertTrue($this->_em->contains($ph2));
  51. // Delete
  52. $this->_em->remove($user);
  53. $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($user));
  54. $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph));
  55. $this->assertTrue($this->_em->getUnitOfWork()->isScheduledForDelete($ph2));
  56. $this->_em->flush();
  57. $this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($user));
  58. $this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph));
  59. $this->assertFalse($this->_em->getUnitOfWork()->isScheduledForDelete($ph2));
  60. $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user));
  61. $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($ph));
  62. $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($ph2));
  63. }
  64. public function testOneToManyAssociationModification()
  65. {
  66. $user = new CmsUser;
  67. $user->name = 'Roman';
  68. $user->username = 'romanb';
  69. $user->status = 'developer';
  70. $ph1 = new CmsPhonenumber;
  71. $ph1->phonenumber = "0301234";
  72. $ph2 = new CmsPhonenumber;
  73. $ph2->phonenumber = "987654321";
  74. $user->addPhonenumber($ph1);
  75. $user->addPhonenumber($ph2);
  76. $this->_em->persist($user);
  77. $this->_em->flush();
  78. //$this->assertTrue($user->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
  79. // Remove the first element from the collection
  80. unset($user->phonenumbers[0]);
  81. $ph1->user = null; // owning side!
  82. $this->_em->flush();
  83. $this->assertEquals(1, count($user->phonenumbers));
  84. $this->assertNull($ph1->user);
  85. }
  86. public function testBasicOneToOne()
  87. {
  88. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  89. $user = new CmsUser;
  90. $user->name = 'Roman';
  91. $user->username = 'romanb';
  92. $user->status = 'developer';
  93. $address = new CmsAddress;
  94. $address->country = 'Germany';
  95. $address->city = 'Berlin';
  96. $address->zip = '12345';
  97. $user->address = $address; // inverse side
  98. $address->user = $user; // owning side!
  99. $this->_em->persist($user);
  100. $this->_em->flush();
  101. // Check that the foreign key has been set
  102. $userId = $this->_em->getConnection()->executeQuery(
  103. "SELECT user_id FROM cms_addresses WHERE id=?", array($address->id)
  104. )->fetchColumn();
  105. $this->assertTrue(is_numeric($userId));
  106. $this->_em->clear();
  107. $user2 = $this->_em->createQuery('select u from \Doctrine\Tests\Models\CMS\CmsUser u where u.id=?1')
  108. ->setParameter(1, $userId)
  109. ->getSingleResult();
  110. // Address has been eager-loaded because it cant be lazy
  111. $this->assertTrue($user2->address instanceof CmsAddress);
  112. $this->assertFalse($user2->address instanceof \Doctrine\ORM\Proxy\Proxy);
  113. }
  114. /**
  115. * @group DDC-1230
  116. */
  117. public function testRemove()
  118. {
  119. $user = new CmsUser;
  120. $user->name = 'Guilherme';
  121. $user->username = 'gblanco';
  122. $user->status = 'developer';
  123. $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
  124. $this->_em->persist($user);
  125. $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_MANAGED, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_MANAGED");
  126. $this->_em->remove($user);
  127. $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
  128. $this->_em->persist($user);
  129. $this->_em->flush();
  130. $id = $user->getId();
  131. $this->_em->remove($user);
  132. $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_REMOVED, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_REMOVED");
  133. $this->_em->flush();
  134. $this->assertEquals(\Doctrine\ORM\UnitOfWork::STATE_NEW, $this->_em->getUnitOfWork()->getEntityState($user), "State should be UnitOfWork::STATE_NEW");
  135. $this->assertNull($this->_em->find('Doctrine\Tests\Models\CMS\CmsUser', $id));
  136. }
  137. public function testOneToManyOrphanRemoval()
  138. {
  139. $user = new CmsUser;
  140. $user->name = 'Guilherme';
  141. $user->username = 'gblanco';
  142. $user->status = 'developer';
  143. for ($i=0; $i<3; ++$i) {
  144. $phone = new CmsPhonenumber;
  145. $phone->phonenumber = 100 + $i;
  146. $user->addPhonenumber($phone);
  147. }
  148. $this->_em->persist($user);
  149. $this->_em->flush();
  150. $user->getPhonenumbers()->remove(0);
  151. $this->assertEquals(2, count($user->getPhonenumbers()));
  152. $this->_em->flush();
  153. // Check that there are just 2 phonenumbers left
  154. $count = $this->_em->getConnection()->fetchColumn("SELECT COUNT(*) FROM cms_phonenumbers");
  155. $this->assertEquals(2, $count); // only 2 remaining
  156. // check that clear() removes the others via orphan removal
  157. $user->getPhonenumbers()->clear();
  158. $this->_em->flush();
  159. $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_phonenumbers"));
  160. }
  161. public function testBasicQuery()
  162. {
  163. $user = new CmsUser;
  164. $user->name = 'Guilherme';
  165. $user->username = 'gblanco';
  166. $user->status = 'developer';
  167. $this->_em->persist($user);
  168. $this->_em->flush();
  169. $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u");
  170. $users = $query->getResult();
  171. $this->assertEquals(1, count($users));
  172. $this->assertEquals('Guilherme', $users[0]->name);
  173. $this->assertEquals('gblanco', $users[0]->username);
  174. $this->assertEquals('developer', $users[0]->status);
  175. //$this->assertNull($users[0]->phonenumbers);
  176. //$this->assertNull($users[0]->articles);
  177. $usersArray = $query->getArrayResult();
  178. $this->assertTrue(is_array($usersArray));
  179. $this->assertEquals(1, count($usersArray));
  180. $this->assertEquals('Guilherme', $usersArray[0]['name']);
  181. $this->assertEquals('gblanco', $usersArray[0]['username']);
  182. $this->assertEquals('developer', $usersArray[0]['status']);
  183. $usersScalar = $query->getScalarResult();
  184. $this->assertTrue(is_array($usersScalar));
  185. $this->assertEquals(1, count($usersScalar));
  186. $this->assertEquals('Guilherme', $usersScalar[0]['u_name']);
  187. $this->assertEquals('gblanco', $usersScalar[0]['u_username']);
  188. $this->assertEquals('developer', $usersScalar[0]['u_status']);
  189. }
  190. public function testBasicOneToManyInnerJoin()
  191. {
  192. $user = new CmsUser;
  193. $user->name = 'Guilherme';
  194. $user->username = 'gblanco';
  195. $user->status = 'developer';
  196. $this->_em->persist($user);
  197. $this->_em->flush();
  198. $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p");
  199. $users = $query->getResult();
  200. $this->assertEquals(0, count($users));
  201. }
  202. public function testBasicOneToManyLeftJoin()
  203. {
  204. $user = new CmsUser;
  205. $user->name = 'Guilherme';
  206. $user->username = 'gblanco';
  207. $user->status = 'developer';
  208. $this->_em->persist($user);
  209. $this->_em->flush();
  210. $query = $this->_em->createQuery("select u,p from Doctrine\Tests\Models\CMS\CmsUser u left join u.phonenumbers p");
  211. $users = $query->getResult();
  212. $this->assertEquals(1, count($users));
  213. $this->assertEquals('Guilherme', $users[0]->name);
  214. $this->assertEquals('gblanco', $users[0]->username);
  215. $this->assertEquals('developer', $users[0]->status);
  216. $this->assertTrue($users[0]->phonenumbers instanceof \Doctrine\ORM\PersistentCollection);
  217. $this->assertTrue($users[0]->phonenumbers->isInitialized());
  218. $this->assertEquals(0, $users[0]->phonenumbers->count());
  219. //$this->assertNull($users[0]->articles);
  220. }
  221. public function testBasicRefresh()
  222. {
  223. $user = new CmsUser;
  224. $user->name = 'Guilherme';
  225. $user->username = 'gblanco';
  226. $user->status = 'developer';
  227. $this->_em->persist($user);
  228. $this->_em->flush();
  229. $user->status = 'mascot';
  230. $this->assertEquals('mascot', $user->status);
  231. $this->_em->refresh($user);
  232. $this->assertEquals('developer', $user->status);
  233. }
  234. /**
  235. * @group DDC-833
  236. */
  237. public function testRefreshResetsCollection()
  238. {
  239. $user = new CmsUser;
  240. $user->name = 'Guilherme';
  241. $user->username = 'gblanco';
  242. $user->status = 'developer';
  243. // Add a phonenumber
  244. $ph1 = new CmsPhonenumber;
  245. $ph1->phonenumber = "12345";
  246. $user->addPhonenumber($ph1);
  247. // Add a phonenumber
  248. $ph2 = new CmsPhonenumber;
  249. $ph2->phonenumber = "54321";
  250. $this->_em->persist($user);
  251. $this->_em->persist($ph1);
  252. $this->_em->persist($ph2);
  253. $this->_em->flush();
  254. $user->addPhonenumber($ph2);
  255. $this->assertEquals(2, count($user->phonenumbers));
  256. $this->_em->refresh($user);
  257. $this->assertEquals(1, count($user->phonenumbers));
  258. }
  259. /**
  260. * @group DDC-833
  261. */
  262. public function testDqlRefreshResetsCollection()
  263. {
  264. $user = new CmsUser;
  265. $user->name = 'Guilherme';
  266. $user->username = 'gblanco';
  267. $user->status = 'developer';
  268. // Add a phonenumber
  269. $ph1 = new CmsPhonenumber;
  270. $ph1->phonenumber = "12345";
  271. $user->addPhonenumber($ph1);
  272. // Add a phonenumber
  273. $ph2 = new CmsPhonenumber;
  274. $ph2->phonenumber = "54321";
  275. $this->_em->persist($user);
  276. $this->_em->persist($ph1);
  277. $this->_em->persist($ph2);
  278. $this->_em->flush();
  279. $user->addPhonenumber($ph2);
  280. $this->assertEquals(2, count($user->phonenumbers));
  281. $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
  282. $user = $this->_em->createQuery($dql)
  283. ->setParameter(1, $user->id)
  284. ->setHint(Query::HINT_REFRESH, true)
  285. ->getSingleResult();
  286. $this->assertEquals(1, count($user->phonenumbers));
  287. }
  288. /**
  289. * @group DDC-833
  290. */
  291. public function testCreateEntityOfProxy()
  292. {
  293. $user = new CmsUser;
  294. $user->name = 'Guilherme';
  295. $user->username = 'gblanco';
  296. $user->status = 'developer';
  297. // Add a phonenumber
  298. $ph1 = new CmsPhonenumber;
  299. $ph1->phonenumber = "12345";
  300. $user->addPhonenumber($ph1);
  301. // Add a phonenumber
  302. $ph2 = new CmsPhonenumber;
  303. $ph2->phonenumber = "54321";
  304. $this->_em->persist($user);
  305. $this->_em->persist($ph1);
  306. $this->_em->persist($ph2);
  307. $this->_em->flush();
  308. $this->_em->clear();
  309. $userId = $user->id;
  310. $user = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->id);
  311. $dql = "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1";
  312. $user = $this->_em->createQuery($dql)
  313. ->setParameter(1, $userId)
  314. ->getSingleResult();
  315. $this->assertEquals(1, count($user->phonenumbers));
  316. }
  317. public function testAddToCollectionDoesNotInitialize()
  318. {
  319. $user = new CmsUser;
  320. $user->name = 'Guilherme';
  321. $user->username = 'gblanco';
  322. $user->status = 'developer';
  323. for ($i=0; $i<3; ++$i) {
  324. $phone = new CmsPhonenumber;
  325. $phone->phonenumber = 100 + $i;
  326. $user->addPhonenumber($phone);
  327. }
  328. $this->_em->persist($user);
  329. $this->_em->flush();
  330. $this->_em->clear();
  331. $this->assertEquals(3, $user->getPhonenumbers()->count());
  332. $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
  333. $gblanco = $query->getSingleResult();
  334. $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
  335. $newPhone = new CmsPhonenumber;
  336. $newPhone->phonenumber = 555;
  337. $gblanco->addPhonenumber($newPhone);
  338. $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
  339. $this->_em->persist($gblanco);
  340. $this->_em->flush();
  341. $this->_em->clear();
  342. $query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
  343. $gblanco2 = $query->getSingleResult();
  344. $this->assertEquals(4, $gblanco2->getPhonenumbers()->count());
  345. }
  346. public function testInitializeCollectionWithNewObjectsRetainsNewObjects()
  347. {
  348. $user = new CmsUser;
  349. $user->name = 'Guilherme';
  350. $user->username = 'gblanco';
  351. $user->status = 'developer';
  352. for ($i=0; $i<3; ++$i) {
  353. $phone = new CmsPhonenumber;
  354. $phone->phonenumber = 100 + $i;
  355. $user->addPhonenumber($phone);
  356. }
  357. $this->_em->persist($user);
  358. $this->_em->flush();
  359. $this->_em->clear();
  360. $this->assertEquals(3, $user->getPhonenumbers()->count());
  361. $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
  362. $gblanco = $query->getSingleResult();
  363. $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
  364. $newPhone = new CmsPhonenumber;
  365. $newPhone->phonenumber = 555;
  366. $gblanco->addPhonenumber($newPhone);
  367. $this->assertFalse($gblanco->getPhonenumbers()->isInitialized());
  368. $this->assertEquals(4, $gblanco->getPhonenumbers()->count());
  369. $this->assertTrue($gblanco->getPhonenumbers()->isInitialized());
  370. $this->_em->flush();
  371. $this->_em->clear();
  372. $query = $this->_em->createQuery("select u, p from Doctrine\Tests\Models\CMS\CmsUser u join u.phonenumbers p where u.username='gblanco'");
  373. $gblanco2 = $query->getSingleResult();
  374. $this->assertEquals(4, $gblanco2->getPhonenumbers()->count());
  375. }
  376. public function testSetSetAssociationWithGetReference()
  377. {
  378. $user = new CmsUser;
  379. $user->name = 'Guilherme';
  380. $user->username = 'gblanco';
  381. $user->status = 'developer';
  382. $this->_em->persist($user);
  383. $address = new CmsAddress;
  384. $address->country = 'Germany';
  385. $address->city = 'Berlin';
  386. $address->zip = '12345';
  387. $this->_em->persist($address);
  388. $this->_em->flush();
  389. $this->_em->detach($address);
  390. $this->assertFalse($this->_em->contains($address));
  391. $this->assertTrue($this->_em->contains($user));
  392. // Assume we only got the identifier of the address and now want to attach
  393. // that address to the user without actually loading it, using getReference().
  394. $addressRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsAddress', $address->getId());
  395. //$addressRef->getId();
  396. //\Doctrine\Common\Util\Debug::dump($addressRef);
  397. $user->setAddress($addressRef); // Ugh! Initializes address 'cause of $address->setUser($user)!
  398. $this->_em->flush();
  399. $this->_em->clear();
  400. // Check with a fresh load that the association is indeed there
  401. $query = $this->_em->createQuery("select u, a from Doctrine\Tests\Models\CMS\CmsUser u join u.address a where u.username='gblanco'");
  402. $gblanco = $query->getSingleResult();
  403. $this->assertTrue($gblanco instanceof CmsUser);
  404. $this->assertTrue($gblanco->getAddress() instanceof CmsAddress);
  405. $this->assertEquals('Berlin', $gblanco->getAddress()->getCity());
  406. }
  407. public function testOneToManyCascadeRemove()
  408. {
  409. $user = new CmsUser;
  410. $user->name = 'Guilherme';
  411. $user->username = 'gblanco';
  412. $user->status = 'developer';
  413. for ($i=0; $i<3; ++$i) {
  414. $phone = new CmsPhonenumber;
  415. $phone->phonenumber = 100 + $i;
  416. $user->addPhonenumber($phone);
  417. }
  418. $this->_em->persist($user);
  419. $this->_em->flush();
  420. $this->_em->clear();
  421. $query = $this->_em->createQuery("select u from Doctrine\Tests\Models\CMS\CmsUser u where u.username='gblanco'");
  422. $gblanco = $query->getSingleResult();
  423. $this->_em->remove($gblanco);
  424. $this->_em->flush();
  425. $this->_em->clear();
  426. $this->assertEquals(0, $this->_em->createQuery(
  427. "select count(p.phonenumber) from Doctrine\Tests\Models\CMS\CmsPhonenumber p")
  428. ->getSingleScalarResult());
  429. $this->assertEquals(0, $this->_em->createQuery(
  430. "select count(u.id) from Doctrine\Tests\Models\CMS\CmsUser u")
  431. ->getSingleScalarResult());
  432. }
  433. public function testTextColumnSaveAndRetrieve()
  434. {
  435. $user = new CmsUser;
  436. $user->name = 'Guilherme';
  437. $user->username = 'gblanco';
  438. $user->status = 'developer';
  439. $this->_em->persist($user);
  440. $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
  441. $article->text = "Lorem ipsum dolor sunt.";
  442. $article->topic = "A Test Article!";
  443. $article->setAuthor($user);
  444. $this->_em->persist($article);
  445. $this->_em->flush();
  446. $articleId = $article->id;
  447. $this->_em->clear();
  448. // test find() with leading backslash at the same time
  449. $articleNew = $this->_em->find('\Doctrine\Tests\Models\CMS\CmsArticle', $articleId);
  450. $this->assertTrue($this->_em->contains($articleNew));
  451. $this->assertEquals("Lorem ipsum dolor sunt.", $articleNew->text);
  452. $this->assertNotSame($article, $articleNew);
  453. $articleNew->text = "Lorem ipsum dolor sunt. And stuff!";
  454. $this->_em->flush();
  455. $this->_em->clear();
  456. $articleNew = $this->_em->find('Doctrine\Tests\Models\CMS\CmsArticle', $articleId);
  457. $this->assertEquals("Lorem ipsum dolor sunt. And stuff!", $articleNew->text);
  458. $this->assertTrue($this->_em->contains($articleNew));
  459. }
  460. public function testFlushDoesNotIssueUnnecessaryUpdates()
  461. {
  462. $user = new CmsUser;
  463. $user->name = 'Guilherme';
  464. $user->username = 'gblanco';
  465. $user->status = 'developer';
  466. $address = new CmsAddress;
  467. $address->country = 'Germany';
  468. $address->city = 'Berlin';
  469. $address->zip = '12345';
  470. $address->user = $user;
  471. $user->address = $address;
  472. $article = new \Doctrine\Tests\Models\CMS\CmsArticle();
  473. $article->text = "Lorem ipsum dolor sunt.";
  474. $article->topic = "A Test Article!";
  475. $article->setAuthor($user);
  476. $this->_em->persist($article);
  477. $this->_em->persist($user);
  478. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  479. $this->_em->flush();
  480. $this->_em->clear();
  481. $query = $this->_em->createQuery('select u,a,ad from Doctrine\Tests\Models\CMS\CmsUser u join u.articles a join u.address ad');
  482. $user2 = $query->getSingleResult();
  483. $this->assertEquals(1, count($user2->articles));
  484. $this->assertTrue($user2->address instanceof CmsAddress);
  485. $oldLogger = $this->_em->getConnection()->getConfiguration()->getSQLLogger();
  486. $debugStack = new \Doctrine\DBAL\Logging\DebugStack;
  487. $this->_em->getConnection()->getConfiguration()->setSQLLogger($debugStack);
  488. $this->_em->flush();
  489. $this->assertEquals(0, count($debugStack->queries));
  490. $this->_em->getConnection()->getConfiguration()->setSQLLogger($oldLogger);
  491. }
  492. public function testRemoveEntityByReference()
  493. {
  494. $user = new CmsUser;
  495. $user->name = 'Guilherme';
  496. $user->username = 'gblanco';
  497. $user->status = 'developer';
  498. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  499. $this->_em->persist($user);
  500. $this->_em->flush();
  501. $this->_em->clear();
  502. $userRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->getId());
  503. $this->_em->remove($userRef);
  504. $this->_em->flush();
  505. $this->_em->clear();
  506. $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_users"));
  507. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(null);
  508. }
  509. public function testQueryEntityByReference()
  510. {
  511. $user = new CmsUser;
  512. $user->name = 'Guilherme';
  513. $user->username = 'gblanco';
  514. $user->status = 'developer';
  515. $address = new CmsAddress;
  516. $address->country = 'Germany';
  517. $address->city = 'Berlin';
  518. $address->zip = '12345';
  519. $user->setAddress($address);
  520. $this->_em->transactional(function($em) use($user) {
  521. $em->persist($user);
  522. });
  523. $this->_em->clear();
  524. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  525. $userRef = $this->_em->getReference('Doctrine\Tests\Models\CMS\CmsUser', $user->getId());
  526. $address2 = $this->_em->createQuery('select a from Doctrine\Tests\Models\CMS\CmsAddress a where a.user = :user')
  527. ->setParameter('user', $userRef)
  528. ->getSingleResult();
  529. $this->assertTrue($address2->getUser() instanceof \Doctrine\ORM\Proxy\Proxy);
  530. $this->assertTrue($userRef === $address2->getUser());
  531. $this->assertFalse($userRef->__isInitialized__);
  532. $this->assertEquals('Germany', $address2->country);
  533. $this->assertEquals('Berlin', $address2->city);
  534. $this->assertEquals('12345', $address2->zip);
  535. }
  536. public function testOneToOneNullUpdate()
  537. {
  538. $user = new CmsUser();
  539. $user->username = "beberlei";
  540. $user->name = "Benjamin E.";
  541. $user->status = 'active';
  542. $address = new CmsAddress();
  543. $address->city = "Bonn";
  544. $address->zip = "12354";
  545. $address->country = "Germany";
  546. $address->street = "somestreet";
  547. $address->user = $user;
  548. $this->_em->persist($address);
  549. $this->_em->persist($user);
  550. $this->_em->flush();
  551. $this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id));
  552. $address->user = null;
  553. $this->_em->flush();
  554. $this->assertNotEquals(1, $this->_em->getConnection()->fetchColumn("select 1 from cms_addresses where user_id = ".$user->id));
  555. }
  556. /**
  557. * @group DDC-600
  558. * @group DDC-455
  559. */
  560. public function testNewAssociatedEntityDuringFlushThrowsException()
  561. {
  562. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  563. $user = new CmsUser();
  564. $user->username = "beberlei";
  565. $user->name = "Benjamin E.";
  566. $user->status = 'active';
  567. $address = new CmsAddress();
  568. $address->city = "Bonn";
  569. $address->zip = "12354";
  570. $address->country = "Germany";
  571. $address->street = "somestreet";
  572. $address->user = $user;
  573. $this->_em->persist($address);
  574. // pretend we forgot to persist $user
  575. try {
  576. $this->_em->flush(); // should raise an exception
  577. $this->fail();
  578. } catch (\InvalidArgumentException $expected) {}
  579. }
  580. /**
  581. * @group DDC-600
  582. * @group DDC-455
  583. */
  584. public function testNewAssociatedEntityDuringFlushThrowsException2()
  585. {
  586. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  587. $user = new CmsUser();
  588. $user->username = "beberlei";
  589. $user->name = "Benjamin E.";
  590. $user->status = 'active';
  591. $address = new CmsAddress();
  592. $address->city = "Bonn";
  593. $address->zip = "12354";
  594. $address->country = "Germany";
  595. $address->street = "somestreet";
  596. $address->user = $user;
  597. $this->_em->persist($address);
  598. $this->_em->persist($user);
  599. $this->_em->flush();
  600. $u2 = new CmsUser;
  601. $u2->username = "beberlei";
  602. $u2->name = "Benjamin E.";
  603. $u2->status = 'inactive';
  604. $address->user = $u2;
  605. // pretend we forgot to persist $u2
  606. try {
  607. $this->_em->flush(); // should raise an exception
  608. $this->fail();
  609. } catch (\InvalidArgumentException $expected) {}
  610. }
  611. /**
  612. * @group DDC-600
  613. * @group DDC-455
  614. */
  615. public function testNewAssociatedEntityDuringFlushThrowsException3()
  616. {
  617. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  618. $art = new CmsArticle();
  619. $art->topic = 'topic';
  620. $art->text = 'the text';
  621. $com = new CmsComment();
  622. $com->topic = 'Good';
  623. $com->text = 'Really good!';
  624. $art->addComment($com);
  625. $this->_em->persist($art);
  626. // pretend we forgot to persist $com
  627. try {
  628. $this->_em->flush(); // should raise an exception
  629. $this->fail();
  630. } catch (\InvalidArgumentException $expected) {}
  631. }
  632. public function testOneToOneOrphanRemoval()
  633. {
  634. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  635. $user = new CmsUser();
  636. $user->username = "beberlei";
  637. $user->name = "Benjamin E.";
  638. $user->status = 'active';
  639. $address = new CmsAddress();
  640. $address->city = "Bonn";
  641. $address->zip = "12354";
  642. $address->country = "Germany";
  643. $address->street = "somestreet";
  644. $address->user = $user;
  645. $user->address = $address;
  646. $this->_em->persist($address);
  647. $this->_em->persist($user);
  648. $this->_em->flush();
  649. $addressId = $address->getId();
  650. $user->address = null;
  651. $this->_em->flush();
  652. $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
  653. // check orphan removal through replacement
  654. $user->address = $address;
  655. $address->user = $user;
  656. $this->_em->flush();
  657. $this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
  658. // remove $address to free up unique key id
  659. $this->_em->remove($address);
  660. $this->_em->flush();
  661. $newAddress = new CmsAddress();
  662. $newAddress->city = "NewBonn";
  663. $newAddress->zip = "12354";
  664. $newAddress->country = "NewGermany";
  665. $newAddress->street = "somenewstreet";
  666. $newAddress->user = $user;
  667. $user->address = $newAddress;
  668. $this->_em->flush();
  669. $this->assertEquals(1, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses"));
  670. $this->assertEquals(0, $this->_em->getConnection()->fetchColumn("select count(*) from cms_addresses where id=".$addressId.""));
  671. }
  672. public function testGetPartialReferenceToUpdateObjectWithoutLoadingIt()
  673. {
  674. $user = new CmsUser();
  675. $user->username = "beberlei";
  676. $user->name = "Benjamin E.";
  677. $user->status = 'active';
  678. $this->_em->persist($user);
  679. $this->_em->flush();
  680. $userId = $user->id;
  681. $this->_em->clear();
  682. $user = $this->_em->getPartialReference('Doctrine\Tests\Models\CMS\CmsUser', $userId);
  683. $this->assertTrue($this->_em->contains($user));
  684. $this->assertNull($user->getName());
  685. $this->assertEquals($userId, $user->id);
  686. $user->name = 'Stephan';
  687. $this->_em->flush();
  688. $this->_em->clear();
  689. $this->assertEquals('Benjamin E.', $this->_em->find(get_class($user), $userId)->name);
  690. }
  691. public function testMergePersistsNewEntities()
  692. {
  693. $user = new CmsUser();
  694. $user->username = "beberlei";
  695. $user->name = "Benjamin E.";
  696. $user->status = 'active';
  697. $managedUser = $this->_em->merge($user);
  698. $this->assertEquals('beberlei', $managedUser->username);
  699. $this->assertEquals('Benjamin E.', $managedUser->name);
  700. $this->assertEquals('active', $managedUser->status);
  701. $this->assertTrue($user !== $managedUser);
  702. $this->assertTrue($this->_em->contains($managedUser));
  703. $this->_em->flush();
  704. $userId = $managedUser->id;
  705. $this->_em->clear();
  706. $user2 = $this->_em->find(get_class($managedUser), $userId);
  707. $this->assertTrue($user2 instanceof CmsUser);
  708. }
  709. public function testMergeThrowsExceptionIfEntityWithGeneratedIdentifierDoesNotExist()
  710. {
  711. $user = new CmsUser();
  712. $user->username = "beberlei";
  713. $user->name = "Benjamin E.";
  714. $user->status = 'active';
  715. $user->id = 42;
  716. try {
  717. $this->_em->merge($user);
  718. $this->fail();
  719. } catch (\Doctrine\ORM\EntityNotFoundException $enfe) {}
  720. }
  721. /**
  722. * @group DDC-634
  723. */
  724. public function testOneToOneMergeSetNull()
  725. {
  726. //$this->_em->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger);
  727. $user = new CmsUser();
  728. $user->username = "beberlei";
  729. $user->name = "Benjamin E.";
  730. $user->status = 'active';
  731. $ph = new CmsPhonenumber();
  732. $ph->phonenumber = "12345";
  733. $user->addPhonenumber($ph);
  734. $this->_em->persist($user);
  735. $this->_em->persist($ph);
  736. $this->_em->flush();
  737. $this->_em->clear();
  738. $ph->user = null;
  739. $managedPh = $this->_em->merge($ph);
  740. $this->_em->flush();
  741. $this->_em->clear();
  742. $this->assertNull($this->_em->find(get_class($ph), $ph->phonenumber)->getUser());
  743. }
  744. /**
  745. * @group DDC-952
  746. */
  747. public function testManyToOneFetchModeQuery()
  748. {
  749. $user = new CmsUser();
  750. $user->username = "beberlei";
  751. $user->name = "Benjamin E.";
  752. $user->status = 'active';
  753. $article = new CmsArticle();
  754. $article->topic = "foo";
  755. $article->text = "bar";
  756. $article->user = $user;
  757. $this->_em->persist($article);
  758. $this->_em->persist($user);
  759. $this->_em->flush();
  760. $this->_em->clear();
  761. $qc = $this->getCurrentQueryCount();
  762. $dql = "SELECT a FROM Doctrine\Tests\Models\CMS\CmsArticle a WHERE a.id = ?1";
  763. $article = $this->_em->createQuery($dql)
  764. ->setParameter(1, $article->id)
  765. ->setFetchMode('Doctrine\Tests\Models\CMS\CmsArticle', 'user', \Doctrine\ORM\Mapping\ClassMetadata::FETCH_EAGER)
  766. ->getSingleResult();
  767. $this->assertInstanceOf('Doctrine\ORM\Proxy\Proxy', $article->user, "It IS a proxy, ...");
  768. $this->assertTrue($article->user->__isInitialized__, "...but its initialized!");
  769. $this->assertEquals($qc+2, $this->getCurrentQueryCount());
  770. }
  771. }