EntityChoiceFieldTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Component\Form;
  11. require_once __DIR__.'/DoctrineOrmTestCase.php';
  12. require_once __DIR__.'/Fixtures/SingleIdentEntity.php';
  13. require_once __DIR__.'/Fixtures/CompositeIdentEntity.php';
  14. use Symfony\Component\Form\EntityChoiceField;
  15. use Symfony\Component\Form\Exception\UnexpectedTypeException;
  16. use Symfony\Tests\Component\Form\Fixtures\SingleIdentEntity;
  17. use Symfony\Tests\Component\Form\Fixtures\CompositeIdentEntity;
  18. use Doctrine\ORM\Tools\SchemaTool;
  19. use Doctrine\Common\Collections\ArrayCollection;
  20. class EntityChoiceFieldTest extends DoctrineOrmTestCase
  21. {
  22. const SINGLE_IDENT_CLASS = 'Symfony\Tests\Component\Form\Fixtures\SingleIdentEntity';
  23. const COMPOSITE_IDENT_CLASS = 'Symfony\Tests\Component\Form\Fixtures\CompositeIdentEntity';
  24. /**
  25. * @var EntityManager
  26. */
  27. private $em;
  28. protected function setUp()
  29. {
  30. parent::setUp();
  31. $this->em = $this->createTestEntityManager();
  32. $schemaTool = new SchemaTool($this->em);
  33. $classes = array(
  34. $this->em->getClassMetadata(self::SINGLE_IDENT_CLASS),
  35. $this->em->getClassMetadata(self::COMPOSITE_IDENT_CLASS),
  36. );
  37. try {
  38. $schemaTool->dropSchema($classes);
  39. } catch(\Exception $e) {
  40. }
  41. try {
  42. $schemaTool->createSchema($classes);
  43. } catch(\Exception $e) {
  44. }
  45. }
  46. protected function persist(array $entities)
  47. {
  48. foreach ($entities as $entity) {
  49. $this->em->persist($entity);
  50. }
  51. $this->em->flush();
  52. // no clear, because entities managed by the choice field must
  53. // be managed!
  54. }
  55. // public function testSetDataToUninitializedEntityWithNonRequired()
  56. // {
  57. // $entity1 = new SingleIdentEntity(1, 'Foo');
  58. // $entity2 = new SingleIdentEntity(2, 'Bar');
  59. //
  60. // $this->persist(array($entity1, $entity2));
  61. //
  62. // $field = $this->factory->getEntityChoiceField('name', array(
  63. // 'em' => $this->em,
  64. // 'class' => self::SINGLE_IDENT_CLASS,
  65. // 'required' => false,
  66. // 'property' => 'name'
  67. // ));
  68. //
  69. // $this->assertEquals(array('' => '', 1 => 'Foo', 2 => 'Bar'), $field->getRenderer()->getVar('choices'));
  70. //
  71. // }
  72. /**
  73. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  74. */
  75. public function testConfigureQueryBuilderWithNonQueryBuilderAndNonClosure()
  76. {
  77. $field = $this->factory->getEntityChoiceField('name', array(
  78. 'em' => $this->em,
  79. 'class' => self::SINGLE_IDENT_CLASS,
  80. 'query_builder' => new \stdClass(),
  81. ));
  82. }
  83. /**
  84. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  85. */
  86. public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
  87. {
  88. $field = $this->factory->getEntityChoiceField('name', array(
  89. 'em' => $this->em,
  90. 'class' => self::SINGLE_IDENT_CLASS,
  91. 'query_builder' => function () {
  92. return new \stdClass();
  93. },
  94. ));
  95. $field->submit('2');
  96. }
  97. /**
  98. * @expectedException Symfony\Component\Form\Exception\FormException
  99. */
  100. public function testChoicesMustBeManaged()
  101. {
  102. $entity1 = new SingleIdentEntity(1, 'Foo');
  103. $entity2 = new SingleIdentEntity(2, 'Bar');
  104. // no persist here!
  105. $field = $this->factory->getEntityChoiceField('name', array(
  106. 'multiple' => false,
  107. 'em' => $this->em,
  108. 'class' => self::SINGLE_IDENT_CLASS,
  109. 'choices' => array($entity1, $entity2),
  110. 'property' => 'name',
  111. ));
  112. }
  113. public function testSetDataSingle_null()
  114. {
  115. $field = $this->factory->getEntityChoiceField('name', array(
  116. 'multiple' => false,
  117. 'em' => $this->em,
  118. 'class' => self::SINGLE_IDENT_CLASS,
  119. ));
  120. $field->setData(null);
  121. $this->assertEquals(null, $field->getData());
  122. $this->assertEquals('', $field->getDisplayedData());
  123. }
  124. public function testSetDataMultiple_null()
  125. {
  126. $field = $this->factory->getEntityChoiceField('name', array(
  127. 'multiple' => true,
  128. 'em' => $this->em,
  129. 'class' => self::SINGLE_IDENT_CLASS,
  130. ));
  131. $field->setData(null);
  132. $this->assertEquals(null, $field->getData());
  133. $this->assertEquals(array(), $field->getDisplayedData());
  134. }
  135. public function testSubmitSingle_null()
  136. {
  137. $field = $this->factory->getEntityChoiceField('name', array(
  138. 'multiple' => false,
  139. 'em' => $this->em,
  140. 'class' => self::SINGLE_IDENT_CLASS,
  141. ));
  142. $field->submit(null);
  143. $this->assertEquals(null, $field->getData());
  144. $this->assertEquals('', $field->getDisplayedData());
  145. }
  146. public function testSubmitMultiple_null()
  147. {
  148. $field = $this->factory->getEntityChoiceField('name', array(
  149. 'multiple' => true,
  150. 'em' => $this->em,
  151. 'class' => self::SINGLE_IDENT_CLASS,
  152. ));
  153. $field->submit(null);
  154. $this->assertEquals(new ArrayCollection(), $field->getData());
  155. $this->assertEquals(array(), $field->getDisplayedData());
  156. }
  157. public function testSubmitSingleNonExpanded_singleIdentifier()
  158. {
  159. $entity1 = new SingleIdentEntity(1, 'Foo');
  160. $entity2 = new SingleIdentEntity(2, 'Bar');
  161. $this->persist(array($entity1, $entity2));
  162. $field = $this->factory->getEntityChoiceField('name', array(
  163. 'multiple' => false,
  164. 'expanded' => false,
  165. 'em' => $this->em,
  166. 'class' => self::SINGLE_IDENT_CLASS,
  167. 'property' => 'name',
  168. ));
  169. $field->submit('2');
  170. $this->assertTrue($field->isTransformationSuccessful());
  171. $this->assertEquals($entity2, $field->getData());
  172. $this->assertEquals(2, $field->getDisplayedData());
  173. }
  174. public function testSubmitSingleNonExpanded_compositeIdentifier()
  175. {
  176. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  177. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  178. $this->persist(array($entity1, $entity2));
  179. $field = $this->factory->getEntityChoiceField('name', array(
  180. 'multiple' => false,
  181. 'expanded' => false,
  182. 'em' => $this->em,
  183. 'class' => self::COMPOSITE_IDENT_CLASS,
  184. 'property' => 'name',
  185. ));
  186. // the collection key is used here
  187. $field->submit('1');
  188. $this->assertTrue($field->isTransformationSuccessful());
  189. $this->assertEquals($entity2, $field->getData());
  190. $this->assertEquals(1, $field->getDisplayedData());
  191. }
  192. public function testSubmitMultipleNonExpanded_singleIdentifier()
  193. {
  194. $entity1 = new SingleIdentEntity(1, 'Foo');
  195. $entity2 = new SingleIdentEntity(2, 'Bar');
  196. $entity3 = new SingleIdentEntity(3, 'Baz');
  197. $this->persist(array($entity1, $entity2, $entity3));
  198. $field = $this->factory->getEntityChoiceField('name', array(
  199. 'multiple' => true,
  200. 'expanded' => false,
  201. 'em' => $this->em,
  202. 'class' => self::SINGLE_IDENT_CLASS,
  203. 'property' => 'name',
  204. ));
  205. $field->submit(array('1', '3'));
  206. $expected = new ArrayCollection(array($entity1, $entity3));
  207. $this->assertTrue($field->isTransformationSuccessful());
  208. $this->assertEquals($expected, $field->getData());
  209. $this->assertEquals(array(1, 3), $field->getDisplayedData());
  210. }
  211. public function testSubmitMultipleNonExpanded_singleIdentifier_existingData()
  212. {
  213. $entity1 = new SingleIdentEntity(1, 'Foo');
  214. $entity2 = new SingleIdentEntity(2, 'Bar');
  215. $entity3 = new SingleIdentEntity(3, 'Baz');
  216. $this->persist(array($entity1, $entity2, $entity3));
  217. $field = $this->factory->getEntityChoiceField('name', array(
  218. 'multiple' => true,
  219. 'expanded' => false,
  220. 'em' => $this->em,
  221. 'class' => self::SINGLE_IDENT_CLASS,
  222. 'property' => 'name',
  223. ));
  224. $existing = new ArrayCollection(array($entity2));
  225. $field->setData($existing);
  226. $field->submit(array('1', '3'));
  227. // entry with index 0 was removed
  228. $expected = new ArrayCollection(array(1 => $entity1, 2 => $entity3));
  229. $this->assertTrue($field->isTransformationSuccessful());
  230. $this->assertEquals($expected, $field->getData());
  231. // same object still, useful if it is a PersistentCollection
  232. $this->assertSame($existing, $field->getData());
  233. $this->assertEquals(array(1, 3), $field->getDisplayedData());
  234. }
  235. public function testSubmitMultipleNonExpanded_compositeIdentifier()
  236. {
  237. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  238. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  239. $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
  240. $this->persist(array($entity1, $entity2, $entity3));
  241. $field = $this->factory->getEntityChoiceField('name', array(
  242. 'multiple' => true,
  243. 'expanded' => false,
  244. 'em' => $this->em,
  245. 'class' => self::COMPOSITE_IDENT_CLASS,
  246. 'property' => 'name',
  247. ));
  248. // because of the composite key collection keys are used
  249. $field->submit(array('0', '2'));
  250. $expected = new ArrayCollection(array($entity1, $entity3));
  251. $this->assertTrue($field->isTransformationSuccessful());
  252. $this->assertEquals($expected, $field->getData());
  253. $this->assertEquals(array(0, 2), $field->getDisplayedData());
  254. }
  255. public function testSubmitMultipleNonExpanded_compositeIdentifier_existingData()
  256. {
  257. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  258. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  259. $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
  260. $this->persist(array($entity1, $entity2, $entity3));
  261. $field = $this->factory->getEntityChoiceField('name', array(
  262. 'multiple' => true,
  263. 'expanded' => false,
  264. 'em' => $this->em,
  265. 'class' => self::COMPOSITE_IDENT_CLASS,
  266. 'property' => 'name',
  267. ));
  268. $existing = new ArrayCollection(array($entity2));
  269. $field->setData($existing);
  270. $field->submit(array('0', '2'));
  271. // entry with index 0 was removed
  272. $expected = new ArrayCollection(array(1 => $entity1, 2 => $entity3));
  273. $this->assertTrue($field->isTransformationSuccessful());
  274. $this->assertEquals($expected, $field->getData());
  275. // same object still, useful if it is a PersistentCollection
  276. $this->assertSame($existing, $field->getData());
  277. $this->assertEquals(array(0, 2), $field->getDisplayedData());
  278. }
  279. public function testSubmitSingleExpanded()
  280. {
  281. $entity1 = new SingleIdentEntity(1, 'Foo');
  282. $entity2 = new SingleIdentEntity(2, 'Bar');
  283. $this->persist(array($entity1, $entity2));
  284. $field = $this->factory->getEntityChoiceField('name', array(
  285. 'multiple' => false,
  286. 'expanded' => true,
  287. 'em' => $this->em,
  288. 'class' => self::SINGLE_IDENT_CLASS,
  289. 'property' => 'name',
  290. ));
  291. $field->submit('2');
  292. $this->assertTrue($field->isTransformationSuccessful());
  293. $this->assertEquals($entity2, $field->getData());
  294. $this->assertSame(false, $field['1']->getData());
  295. $this->assertSame(true, $field['2']->getData());
  296. $this->assertSame('', $field['1']->getDisplayedData());
  297. $this->assertSame('1', $field['2']->getDisplayedData());
  298. $this->assertSame(array('1' => '', '2' => '1'), $field->getDisplayedData());
  299. }
  300. public function testSubmitMultipleExpanded()
  301. {
  302. $entity1 = new SingleIdentEntity(1, 'Foo');
  303. $entity2 = new SingleIdentEntity(2, 'Bar');
  304. $entity3 = new SingleIdentEntity(3, 'Bar');
  305. $this->persist(array($entity1, $entity2, $entity3));
  306. $field = $this->factory->getEntityChoiceField('name', array(
  307. 'multiple' => true,
  308. 'expanded' => true,
  309. 'em' => $this->em,
  310. 'class' => self::SINGLE_IDENT_CLASS,
  311. 'property' => 'name',
  312. ));
  313. $field->submit(array('1' => '1', '3' => '3'));
  314. $expected = new ArrayCollection(array($entity1, $entity3));
  315. $this->assertTrue($field->isTransformationSuccessful());
  316. $this->assertEquals($expected, $field->getData());
  317. $this->assertSame(true, $field['1']->getData());
  318. $this->assertSame(false, $field['2']->getData());
  319. $this->assertSame(true, $field['3']->getData());
  320. $this->assertSame('1', $field['1']->getDisplayedData());
  321. $this->assertSame('', $field['2']->getDisplayedData());
  322. $this->assertSame('1', $field['3']->getDisplayedData());
  323. $this->assertSame(array('1' => '1', '2' => '', '3' => '1'), $field->getDisplayedData());
  324. }
  325. public function testOverrideChoices()
  326. {
  327. $entity1 = new SingleIdentEntity(1, 'Foo');
  328. $entity2 = new SingleIdentEntity(2, 'Bar');
  329. $entity3 = new SingleIdentEntity(3, 'Baz');
  330. $this->persist(array($entity1, $entity2, $entity3));
  331. $field = $this->factory->getEntityChoiceField('name', array(
  332. 'em' => $this->em,
  333. 'class' => self::SINGLE_IDENT_CLASS,
  334. // not all persisted entities should be displayed
  335. 'choices' => array($entity1, $entity2),
  336. 'property' => 'name',
  337. ));
  338. $field->submit('2');
  339. $this->assertEquals(array(1 => 'Foo', 2 => 'Bar'), $field->getRenderer()->getVar('choices'));
  340. $this->assertTrue($field->isTransformationSuccessful());
  341. $this->assertEquals($entity2, $field->getData());
  342. $this->assertEquals(2, $field->getDisplayedData());
  343. }
  344. public function testDisallowChoicesThatAreNotIncluded_choices_singleIdentifier()
  345. {
  346. $entity1 = new SingleIdentEntity(1, 'Foo');
  347. $entity2 = new SingleIdentEntity(2, 'Bar');
  348. $entity3 = new SingleIdentEntity(3, 'Baz');
  349. $this->persist(array($entity1, $entity2, $entity3));
  350. $field = $this->factory->getEntityChoiceField('name', array(
  351. 'em' => $this->em,
  352. 'class' => self::SINGLE_IDENT_CLASS,
  353. 'choices' => array($entity1, $entity2),
  354. 'property' => 'name',
  355. ));
  356. $field->submit('3');
  357. $this->assertFalse($field->isTransformationSuccessful());
  358. $this->assertNull($field->getData());
  359. }
  360. public function testDisallowChoicesThatAreNotIncluded_choices_compositeIdentifier()
  361. {
  362. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  363. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  364. $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
  365. $this->persist(array($entity1, $entity2, $entity3));
  366. $field = $this->factory->getEntityChoiceField('name', array(
  367. 'em' => $this->em,
  368. 'class' => self::COMPOSITE_IDENT_CLASS,
  369. 'choices' => array($entity1, $entity2),
  370. 'property' => 'name',
  371. ));
  372. $field->submit('2');
  373. $this->assertFalse($field->isTransformationSuccessful());
  374. $this->assertNull($field->getData());
  375. }
  376. public function testDisallowChoicesThatAreNotIncluded_queryBuilder_singleIdentifier()
  377. {
  378. $entity1 = new SingleIdentEntity(1, 'Foo');
  379. $entity2 = new SingleIdentEntity(2, 'Bar');
  380. $entity3 = new SingleIdentEntity(3, 'Baz');
  381. $this->persist(array($entity1, $entity2, $entity3));
  382. $repository = $this->em->getRepository(self::SINGLE_IDENT_CLASS);
  383. $field = $this->factory->getEntityChoiceField('name', array(
  384. 'em' => $this->em,
  385. 'class' => self::SINGLE_IDENT_CLASS,
  386. 'query_builder' => $repository->createQueryBuilder('e')
  387. ->where('e.id IN (1, 2)'),
  388. 'property' => 'name',
  389. ));
  390. $field->submit('3');
  391. $this->assertFalse($field->isTransformationSuccessful());
  392. $this->assertNull($field->getData());
  393. }
  394. public function testDisallowChoicesThatAreNotIncluded_queryBuilderAsClosure_singleIdentifier()
  395. {
  396. $entity1 = new SingleIdentEntity(1, 'Foo');
  397. $entity2 = new SingleIdentEntity(2, 'Bar');
  398. $entity3 = new SingleIdentEntity(3, 'Baz');
  399. $this->persist(array($entity1, $entity2, $entity3));
  400. $field = $this->factory->getEntityChoiceField('name', array(
  401. 'em' => $this->em,
  402. 'class' => self::SINGLE_IDENT_CLASS,
  403. 'query_builder' => function ($repository) {
  404. return $repository->createQueryBuilder('e')
  405. ->where('e.id IN (1, 2)');
  406. },
  407. 'property' => 'name',
  408. ));
  409. $field->submit('3');
  410. $this->assertFalse($field->isTransformationSuccessful());
  411. $this->assertNull($field->getData());
  412. }
  413. public function testDisallowChoicesThatAreNotIncluded_queryBuilderAsClosure_compositeIdentifier()
  414. {
  415. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  416. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  417. $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
  418. $this->persist(array($entity1, $entity2, $entity3));
  419. $field = $this->factory->getEntityChoiceField('name', array(
  420. 'em' => $this->em,
  421. 'class' => self::COMPOSITE_IDENT_CLASS,
  422. 'query_builder' => function ($repository) {
  423. return $repository->createQueryBuilder('e')
  424. ->where('e.id1 IN (10, 50)');
  425. },
  426. 'property' => 'name',
  427. ));
  428. $field->submit('2');
  429. $this->assertFalse($field->isTransformationSuccessful());
  430. $this->assertNull($field->getData());
  431. }
  432. }