EntityChoiceFieldTest.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 testSetDataMultipleExpanded_null()
  125. {
  126. $field = $this->factory->getEntityChoiceField('name', array(
  127. 'multiple' => true,
  128. 'expanded' => true,
  129. 'em' => $this->em,
  130. 'class' => self::SINGLE_IDENT_CLASS,
  131. ));
  132. $field->setData(null);
  133. $this->assertEquals(new ArrayCollection(), $field->getData());
  134. $this->assertEquals(array(), $field->getDisplayedData());
  135. }
  136. public function testSetDataMultipleNonExpanded_null()
  137. {
  138. $field = $this->factory->getEntityChoiceField('name', array(
  139. 'multiple' => true,
  140. 'expanded' => false,
  141. 'em' => $this->em,
  142. 'class' => self::SINGLE_IDENT_CLASS,
  143. ));
  144. $field->setData(null);
  145. $this->assertEquals(new ArrayCollection(), $field->getData());
  146. $this->assertEquals(array(), $field->getDisplayedData());
  147. }
  148. public function testSubmitSingleExpanded_null()
  149. {
  150. $field = $this->factory->getEntityChoiceField('name', array(
  151. 'multiple' => false,
  152. 'expanded' => true,
  153. 'em' => $this->em,
  154. 'class' => self::SINGLE_IDENT_CLASS,
  155. ));
  156. $field->submit(null);
  157. $this->assertEquals(null, $field->getData());
  158. $this->assertEquals(array(), $field->getDisplayedData());
  159. }
  160. public function testSubmitSingleNonExpanded_null()
  161. {
  162. $field = $this->factory->getEntityChoiceField('name', array(
  163. 'multiple' => false,
  164. 'expanded' => false,
  165. 'em' => $this->em,
  166. 'class' => self::SINGLE_IDENT_CLASS,
  167. ));
  168. $field->submit(null);
  169. $this->assertEquals(null, $field->getData());
  170. $this->assertEquals('', $field->getDisplayedData());
  171. }
  172. public function testSubmitMultiple_null()
  173. {
  174. $field = $this->factory->getEntityChoiceField('name', array(
  175. 'multiple' => true,
  176. 'em' => $this->em,
  177. 'class' => self::SINGLE_IDENT_CLASS,
  178. ));
  179. $field->submit(null);
  180. $this->assertEquals(new ArrayCollection(), $field->getData());
  181. $this->assertEquals(array(), $field->getDisplayedData());
  182. }
  183. public function testSubmitSingleNonExpanded_singleIdentifier()
  184. {
  185. $entity1 = new SingleIdentEntity(1, 'Foo');
  186. $entity2 = new SingleIdentEntity(2, 'Bar');
  187. $this->persist(array($entity1, $entity2));
  188. $field = $this->factory->getEntityChoiceField('name', array(
  189. 'multiple' => false,
  190. 'expanded' => false,
  191. 'em' => $this->em,
  192. 'class' => self::SINGLE_IDENT_CLASS,
  193. 'property' => 'name',
  194. ));
  195. $field->submit('2');
  196. $this->assertTrue($field->isTransformationSuccessful());
  197. $this->assertEquals($entity2, $field->getData());
  198. $this->assertEquals(2, $field->getDisplayedData());
  199. }
  200. public function testSubmitSingleNonExpanded_compositeIdentifier()
  201. {
  202. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  203. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  204. $this->persist(array($entity1, $entity2));
  205. $field = $this->factory->getEntityChoiceField('name', array(
  206. 'multiple' => false,
  207. 'expanded' => false,
  208. 'em' => $this->em,
  209. 'class' => self::COMPOSITE_IDENT_CLASS,
  210. 'property' => 'name',
  211. ));
  212. // the collection key is used here
  213. $field->submit('1');
  214. $this->assertTrue($field->isTransformationSuccessful());
  215. $this->assertEquals($entity2, $field->getData());
  216. $this->assertEquals(1, $field->getDisplayedData());
  217. }
  218. public function testSubmitMultipleNonExpanded_singleIdentifier()
  219. {
  220. $entity1 = new SingleIdentEntity(1, 'Foo');
  221. $entity2 = new SingleIdentEntity(2, 'Bar');
  222. $entity3 = new SingleIdentEntity(3, 'Baz');
  223. $this->persist(array($entity1, $entity2, $entity3));
  224. $field = $this->factory->getEntityChoiceField('name', array(
  225. 'multiple' => true,
  226. 'expanded' => false,
  227. 'em' => $this->em,
  228. 'class' => self::SINGLE_IDENT_CLASS,
  229. 'property' => 'name',
  230. ));
  231. $field->submit(array('1', '3'));
  232. $expected = new ArrayCollection(array($entity1, $entity3));
  233. $this->assertTrue($field->isTransformationSuccessful());
  234. $this->assertEquals($expected, $field->getData());
  235. $this->assertEquals(array(1, 3), $field->getDisplayedData());
  236. }
  237. public function testSubmitMultipleNonExpanded_singleIdentifier_existingData()
  238. {
  239. $entity1 = new SingleIdentEntity(1, 'Foo');
  240. $entity2 = new SingleIdentEntity(2, 'Bar');
  241. $entity3 = new SingleIdentEntity(3, 'Baz');
  242. $this->persist(array($entity1, $entity2, $entity3));
  243. $field = $this->factory->getEntityChoiceField('name', array(
  244. 'multiple' => true,
  245. 'expanded' => false,
  246. 'em' => $this->em,
  247. 'class' => self::SINGLE_IDENT_CLASS,
  248. 'property' => 'name',
  249. ));
  250. $existing = new ArrayCollection(array($entity2));
  251. $field->setData($existing);
  252. $field->submit(array('1', '3'));
  253. // entry with index 0 was removed
  254. $expected = new ArrayCollection(array(1 => $entity1, 2 => $entity3));
  255. $this->assertTrue($field->isTransformationSuccessful());
  256. $this->assertEquals($expected, $field->getData());
  257. // same object still, useful if it is a PersistentCollection
  258. $this->assertSame($existing, $field->getData());
  259. $this->assertEquals(array(1, 3), $field->getDisplayedData());
  260. }
  261. public function testSubmitMultipleNonExpanded_compositeIdentifier()
  262. {
  263. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  264. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  265. $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
  266. $this->persist(array($entity1, $entity2, $entity3));
  267. $field = $this->factory->getEntityChoiceField('name', array(
  268. 'multiple' => true,
  269. 'expanded' => false,
  270. 'em' => $this->em,
  271. 'class' => self::COMPOSITE_IDENT_CLASS,
  272. 'property' => 'name',
  273. ));
  274. // because of the composite key collection keys are used
  275. $field->submit(array('0', '2'));
  276. $expected = new ArrayCollection(array($entity1, $entity3));
  277. $this->assertTrue($field->isTransformationSuccessful());
  278. $this->assertEquals($expected, $field->getData());
  279. $this->assertEquals(array(0, 2), $field->getDisplayedData());
  280. }
  281. public function testSubmitMultipleNonExpanded_compositeIdentifier_existingData()
  282. {
  283. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  284. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  285. $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
  286. $this->persist(array($entity1, $entity2, $entity3));
  287. $field = $this->factory->getEntityChoiceField('name', array(
  288. 'multiple' => true,
  289. 'expanded' => false,
  290. 'em' => $this->em,
  291. 'class' => self::COMPOSITE_IDENT_CLASS,
  292. 'property' => 'name',
  293. ));
  294. $existing = new ArrayCollection(array(0 => $entity2));
  295. $field->setData($existing);
  296. $field->submit(array('0', '2'));
  297. // entry with index 0 was removed
  298. $expected = new ArrayCollection(array(1 => $entity1, 2 => $entity3));
  299. $this->assertTrue($field->isTransformationSuccessful());
  300. $this->assertEquals($expected, $field->getData());
  301. // same object still, useful if it is a PersistentCollection
  302. $this->assertSame($existing, $field->getData());
  303. $this->assertEquals(array(0, 2), $field->getDisplayedData());
  304. }
  305. public function testSubmitSingleExpanded()
  306. {
  307. $entity1 = new SingleIdentEntity(1, 'Foo');
  308. $entity2 = new SingleIdentEntity(2, 'Bar');
  309. $this->persist(array($entity1, $entity2));
  310. $field = $this->factory->getEntityChoiceField('name', array(
  311. 'multiple' => false,
  312. 'expanded' => true,
  313. 'em' => $this->em,
  314. 'class' => self::SINGLE_IDENT_CLASS,
  315. 'property' => 'name',
  316. ));
  317. $field->submit('2');
  318. $this->assertTrue($field->isTransformationSuccessful());
  319. $this->assertEquals($entity2, $field->getData());
  320. $this->assertSame(false, $field['1']->getData());
  321. $this->assertSame(true, $field['2']->getData());
  322. $this->assertSame('', $field['1']->getDisplayedData());
  323. $this->assertSame('1', $field['2']->getDisplayedData());
  324. $this->assertSame(array('1' => '', '2' => '1'), $field->getDisplayedData());
  325. }
  326. public function testSubmitMultipleExpanded()
  327. {
  328. $entity1 = new SingleIdentEntity(1, 'Foo');
  329. $entity2 = new SingleIdentEntity(2, 'Bar');
  330. $entity3 = new SingleIdentEntity(3, 'Bar');
  331. $this->persist(array($entity1, $entity2, $entity3));
  332. $field = $this->factory->getEntityChoiceField('name', array(
  333. 'multiple' => true,
  334. 'expanded' => true,
  335. 'em' => $this->em,
  336. 'class' => self::SINGLE_IDENT_CLASS,
  337. 'property' => 'name',
  338. ));
  339. $field->submit(array('1' => '1', '3' => '3'));
  340. $expected = new ArrayCollection(array($entity1, $entity3));
  341. $this->assertTrue($field->isTransformationSuccessful());
  342. $this->assertEquals($expected, $field->getData());
  343. $this->assertSame(true, $field['1']->getData());
  344. $this->assertSame(false, $field['2']->getData());
  345. $this->assertSame(true, $field['3']->getData());
  346. $this->assertSame('1', $field['1']->getDisplayedData());
  347. $this->assertSame('', $field['2']->getDisplayedData());
  348. $this->assertSame('1', $field['3']->getDisplayedData());
  349. $this->assertSame(array('1' => '1', '2' => '', '3' => '1'), $field->getDisplayedData());
  350. }
  351. public function testOverrideChoices()
  352. {
  353. $entity1 = new SingleIdentEntity(1, 'Foo');
  354. $entity2 = new SingleIdentEntity(2, 'Bar');
  355. $entity3 = new SingleIdentEntity(3, 'Baz');
  356. $this->persist(array($entity1, $entity2, $entity3));
  357. $field = $this->factory->getEntityChoiceField('name', array(
  358. 'em' => $this->em,
  359. 'class' => self::SINGLE_IDENT_CLASS,
  360. // not all persisted entities should be displayed
  361. 'choices' => array($entity1, $entity2),
  362. 'property' => 'name',
  363. ));
  364. $field->submit('2');
  365. $this->assertEquals(array(1 => 'Foo', 2 => 'Bar'), $field->getRenderer()->getVar('choices'));
  366. $this->assertTrue($field->isTransformationSuccessful());
  367. $this->assertEquals($entity2, $field->getData());
  368. $this->assertEquals(2, $field->getDisplayedData());
  369. }
  370. public function testDisallowChoicesThatAreNotIncluded_choices_singleIdentifier()
  371. {
  372. $entity1 = new SingleIdentEntity(1, 'Foo');
  373. $entity2 = new SingleIdentEntity(2, 'Bar');
  374. $entity3 = new SingleIdentEntity(3, 'Baz');
  375. $this->persist(array($entity1, $entity2, $entity3));
  376. $field = $this->factory->getEntityChoiceField('name', array(
  377. 'em' => $this->em,
  378. 'class' => self::SINGLE_IDENT_CLASS,
  379. 'choices' => array($entity1, $entity2),
  380. 'property' => 'name',
  381. ));
  382. $field->submit('3');
  383. $this->assertFalse($field->isTransformationSuccessful());
  384. $this->assertNull($field->getData());
  385. }
  386. public function testDisallowChoicesThatAreNotIncluded_choices_compositeIdentifier()
  387. {
  388. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  389. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  390. $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
  391. $this->persist(array($entity1, $entity2, $entity3));
  392. $field = $this->factory->getEntityChoiceField('name', array(
  393. 'em' => $this->em,
  394. 'class' => self::COMPOSITE_IDENT_CLASS,
  395. 'choices' => array($entity1, $entity2),
  396. 'property' => 'name',
  397. ));
  398. $field->submit('2');
  399. $this->assertFalse($field->isTransformationSuccessful());
  400. $this->assertNull($field->getData());
  401. }
  402. public function testDisallowChoicesThatAreNotIncluded_queryBuilder_singleIdentifier()
  403. {
  404. $entity1 = new SingleIdentEntity(1, 'Foo');
  405. $entity2 = new SingleIdentEntity(2, 'Bar');
  406. $entity3 = new SingleIdentEntity(3, 'Baz');
  407. $this->persist(array($entity1, $entity2, $entity3));
  408. $repository = $this->em->getRepository(self::SINGLE_IDENT_CLASS);
  409. $field = $this->factory->getEntityChoiceField('name', array(
  410. 'em' => $this->em,
  411. 'class' => self::SINGLE_IDENT_CLASS,
  412. 'query_builder' => $repository->createQueryBuilder('e')
  413. ->where('e.id IN (1, 2)'),
  414. 'property' => 'name',
  415. ));
  416. $field->submit('3');
  417. $this->assertFalse($field->isTransformationSuccessful());
  418. $this->assertNull($field->getData());
  419. }
  420. public function testDisallowChoicesThatAreNotIncluded_queryBuilderAsClosure_singleIdentifier()
  421. {
  422. $entity1 = new SingleIdentEntity(1, 'Foo');
  423. $entity2 = new SingleIdentEntity(2, 'Bar');
  424. $entity3 = new SingleIdentEntity(3, 'Baz');
  425. $this->persist(array($entity1, $entity2, $entity3));
  426. $field = $this->factory->getEntityChoiceField('name', array(
  427. 'em' => $this->em,
  428. 'class' => self::SINGLE_IDENT_CLASS,
  429. 'query_builder' => function ($repository) {
  430. return $repository->createQueryBuilder('e')
  431. ->where('e.id IN (1, 2)');
  432. },
  433. 'property' => 'name',
  434. ));
  435. $field->submit('3');
  436. $this->assertFalse($field->isTransformationSuccessful());
  437. $this->assertNull($field->getData());
  438. }
  439. public function testDisallowChoicesThatAreNotIncluded_queryBuilderAsClosure_compositeIdentifier()
  440. {
  441. $entity1 = new CompositeIdentEntity(10, 20, 'Foo');
  442. $entity2 = new CompositeIdentEntity(30, 40, 'Bar');
  443. $entity3 = new CompositeIdentEntity(50, 60, 'Baz');
  444. $this->persist(array($entity1, $entity2, $entity3));
  445. $field = $this->factory->getEntityChoiceField('name', array(
  446. 'em' => $this->em,
  447. 'class' => self::COMPOSITE_IDENT_CLASS,
  448. 'query_builder' => function ($repository) {
  449. return $repository->createQueryBuilder('e')
  450. ->where('e.id1 IN (10, 50)');
  451. },
  452. 'property' => 'name',
  453. ));
  454. $field->submit('2');
  455. $this->assertFalse($field->isTransformationSuccessful());
  456. $this->assertNull($field->getData());
  457. }
  458. }