|
30 | 30 | use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringCastableIdEntity;
|
31 | 31 | use Symfony\Bridge\Doctrine\Tests\Fixtures\SingleStringIdEntity;
|
32 | 32 | use Symfony\Component\Form\ChoiceList\LazyChoiceList;
|
| 33 | +use Symfony\Component\Form\ChoiceList\Loader\LazyChoiceLoader; |
33 | 34 | use Symfony\Component\Form\ChoiceList\View\ChoiceGroupView;
|
34 | 35 | use Symfony\Component\Form\ChoiceList\View\ChoiceView;
|
35 | 36 | use Symfony\Component\Form\Exception\RuntimeException;
|
@@ -1758,4 +1759,128 @@ public function testWithSameLoaderAndDifferentChoiceValueCallbacks()
|
1758 | 1759 | $this->assertSame('Foo', $view['entity_two']->vars['choices']['Foo']->value);
|
1759 | 1760 | $this->assertSame('Bar', $view['entity_two']->vars['choices']['Bar']->value);
|
1760 | 1761 | }
|
| 1762 | + |
| 1763 | + public function testEmptyChoicesWhenLazy() |
| 1764 | + { |
| 1765 | + if (!class_exists(LazyChoiceLoader::class)) { |
| 1766 | + $this->markTestSkipped('This test requires symfony/form 7.2 or superior.'); |
| 1767 | + } |
| 1768 | + |
| 1769 | + $entity1 = new SingleIntIdEntity(1, 'Foo'); |
| 1770 | + $entity2 = new SingleIntIdEntity(2, 'Bar'); |
| 1771 | + $this->persist([$entity1, $entity2]); |
| 1772 | + |
| 1773 | + $view = $this->factory->create(FormTypeTest::TESTED_TYPE) |
| 1774 | + ->add('entity_one', self::TESTED_TYPE, [ |
| 1775 | + 'em' => 'default', |
| 1776 | + 'class' => self::SINGLE_IDENT_CLASS, |
| 1777 | + 'choice_lazy' => true, |
| 1778 | + ]) |
| 1779 | + ->createView() |
| 1780 | + ; |
| 1781 | + |
| 1782 | + $this->assertCount(0, $view['entity_one']->vars['choices']); |
| 1783 | + } |
| 1784 | + |
| 1785 | + public function testLoadedChoicesWhenLazyAndBoundData() |
| 1786 | + { |
| 1787 | + if (!class_exists(LazyChoiceLoader::class)) { |
| 1788 | + $this->markTestSkipped('This test requires symfony/form 7.2 or superior.'); |
| 1789 | + } |
| 1790 | + |
| 1791 | + $entity1 = new SingleIntIdEntity(1, 'Foo'); |
| 1792 | + $entity2 = new SingleIntIdEntity(2, 'Bar'); |
| 1793 | + $this->persist([$entity1, $entity2]); |
| 1794 | + |
| 1795 | + $view = $this->factory->create(FormTypeTest::TESTED_TYPE, ['entity_one' => $entity1]) |
| 1796 | + ->add('entity_one', self::TESTED_TYPE, [ |
| 1797 | + 'em' => 'default', |
| 1798 | + 'class' => self::SINGLE_IDENT_CLASS, |
| 1799 | + 'choice_lazy' => true, |
| 1800 | + ]) |
| 1801 | + ->createView() |
| 1802 | + ; |
| 1803 | + |
| 1804 | + $this->assertCount(1, $view['entity_one']->vars['choices']); |
| 1805 | + $this->assertSame('1', $view['entity_one']->vars['choices'][1]->value); |
| 1806 | + } |
| 1807 | + |
| 1808 | + public function testLoadedChoicesWhenLazyAndSubmittedData() |
| 1809 | + { |
| 1810 | + if (!class_exists(LazyChoiceLoader::class)) { |
| 1811 | + $this->markTestSkipped('This test requires symfony/form 7.2 or superior.'); |
| 1812 | + } |
| 1813 | + |
| 1814 | + $entity1 = new SingleIntIdEntity(1, 'Foo'); |
| 1815 | + $entity2 = new SingleIntIdEntity(2, 'Bar'); |
| 1816 | + $this->persist([$entity1, $entity2]); |
| 1817 | + |
| 1818 | + $view = $this->factory->create(FormTypeTest::TESTED_TYPE) |
| 1819 | + ->add('entity_one', self::TESTED_TYPE, [ |
| 1820 | + 'em' => 'default', |
| 1821 | + 'class' => self::SINGLE_IDENT_CLASS, |
| 1822 | + 'choice_lazy' => true, |
| 1823 | + ]) |
| 1824 | + ->submit(['entity_one' => '2']) |
| 1825 | + ->createView() |
| 1826 | + ; |
| 1827 | + |
| 1828 | + $this->assertCount(1, $view['entity_one']->vars['choices']); |
| 1829 | + $this->assertSame('2', $view['entity_one']->vars['choices'][2]->value); |
| 1830 | + } |
| 1831 | + |
| 1832 | + public function testEmptyChoicesWhenLazyAndEmptyDataIsSubmitted() |
| 1833 | + { |
| 1834 | + if (!class_exists(LazyChoiceLoader::class)) { |
| 1835 | + $this->markTestSkipped('This test requires symfony/form 7.2 or superior.'); |
| 1836 | + } |
| 1837 | + |
| 1838 | + $entity1 = new SingleIntIdEntity(1, 'Foo'); |
| 1839 | + $entity2 = new SingleIntIdEntity(2, 'Bar'); |
| 1840 | + $this->persist([$entity1, $entity2]); |
| 1841 | + |
| 1842 | + $view = $this->factory->create(FormTypeTest::TESTED_TYPE, ['entity_one' => $entity1]) |
| 1843 | + ->add('entity_one', self::TESTED_TYPE, [ |
| 1844 | + 'em' => 'default', |
| 1845 | + 'class' => self::SINGLE_IDENT_CLASS, |
| 1846 | + 'choice_lazy' => true, |
| 1847 | + ]) |
| 1848 | + ->submit([]) |
| 1849 | + ->createView() |
| 1850 | + ; |
| 1851 | + |
| 1852 | + $this->assertCount(0, $view['entity_one']->vars['choices']); |
| 1853 | + } |
| 1854 | + |
| 1855 | + public function testErrorOnSubmitInvalidValuesWhenLazyAndCustomQueryBuilder() |
| 1856 | + { |
| 1857 | + if (!class_exists(LazyChoiceLoader::class)) { |
| 1858 | + $this->markTestSkipped('This test requires symfony/form 7.2 or superior.'); |
| 1859 | + } |
| 1860 | + |
| 1861 | + $entity1 = new SingleIntIdEntity(1, 'Foo'); |
| 1862 | + $entity2 = new SingleIntIdEntity(2, 'Bar'); |
| 1863 | + $this->persist([$entity1, $entity2]); |
| 1864 | + $qb = $this->em |
| 1865 | + ->createQueryBuilder() |
| 1866 | + ->select('e') |
| 1867 | + ->from(self::SINGLE_IDENT_CLASS, 'e') |
| 1868 | + ->where('e.id = 2') |
| 1869 | + ; |
| 1870 | + |
| 1871 | + $form = $this->factory->create(FormTypeTest::TESTED_TYPE, ['entity_one' => $entity2]) |
| 1872 | + ->add('entity_one', self::TESTED_TYPE, [ |
| 1873 | + 'em' => 'default', |
| 1874 | + 'class' => self::SINGLE_IDENT_CLASS, |
| 1875 | + 'query_builder' => $qb, |
| 1876 | + 'choice_lazy' => true, |
| 1877 | + ]) |
| 1878 | + ->submit(['entity_one' => '1']) |
| 1879 | + ; |
| 1880 | + $view = $form->createView(); |
| 1881 | + |
| 1882 | + $this->assertCount(0, $view['entity_one']->vars['choices']); |
| 1883 | + $this->assertCount(1, $errors = $form->getErrors(true)); |
| 1884 | + $this->assertSame('The selected choice is invalid.', $errors->current()->getMessage()); |
| 1885 | + } |
1761 | 1886 | }
|
0 commit comments