EntityChoiceFieldTest.php 18 KB

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