FormRegistryTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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\Component\Form\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Form\FormRegistry;
  13. use Symfony\Component\Form\FormTypeGuesserChain;
  14. use Symfony\Component\Form\ResolvedFormType;
  15. use Symfony\Component\Form\ResolvedFormTypeFactoryInterface;
  16. use Symfony\Component\Form\Tests\Fixtures\FooSubType;
  17. use Symfony\Component\Form\Tests\Fixtures\FooType;
  18. use Symfony\Component\Form\Tests\Fixtures\FooTypeBarExtension;
  19. use Symfony\Component\Form\Tests\Fixtures\FooTypeBazExtension;
  20. use Symfony\Component\Form\Tests\Fixtures\LegacyFooSubType;
  21. use Symfony\Component\Form\Tests\Fixtures\LegacyFooSubTypeWithParentInstance;
  22. use Symfony\Component\Form\Tests\Fixtures\LegacyFooType;
  23. use Symfony\Component\Form\Tests\Fixtures\LegacyFooTypeBarExtension;
  24. use Symfony\Component\Form\Tests\Fixtures\LegacyFooTypeBazExtension;
  25. use Symfony\Component\Form\Tests\Fixtures\TestExtension;
  26. /**
  27. * @author Bernhard Schussek <bschussek@gmail.com>
  28. */
  29. class FormRegistryTest extends TestCase
  30. {
  31. /**
  32. * @var FormRegistry
  33. */
  34. private $registry;
  35. /**
  36. * @var \PHPUnit_Framework_MockObject_MockObject|ResolvedFormTypeFactoryInterface
  37. */
  38. private $resolvedTypeFactory;
  39. /**
  40. * @var \PHPUnit_Framework_MockObject_MockObject
  41. */
  42. private $guesser1;
  43. /**
  44. * @var \PHPUnit_Framework_MockObject_MockObject
  45. */
  46. private $guesser2;
  47. /**
  48. * @var TestExtension
  49. */
  50. private $extension1;
  51. /**
  52. * @var TestExtension
  53. */
  54. private $extension2;
  55. protected function setUp()
  56. {
  57. $this->resolvedTypeFactory = $this->getMockBuilder('Symfony\Component\Form\ResolvedFormTypeFactory')->getMock();
  58. $this->guesser1 = $this->getMockBuilder('Symfony\Component\Form\FormTypeGuesserInterface')->getMock();
  59. $this->guesser2 = $this->getMockBuilder('Symfony\Component\Form\FormTypeGuesserInterface')->getMock();
  60. $this->extension1 = new TestExtension($this->guesser1);
  61. $this->extension2 = new TestExtension($this->guesser2);
  62. $this->registry = new FormRegistry(array(
  63. $this->extension1,
  64. $this->extension2,
  65. ), $this->resolvedTypeFactory);
  66. }
  67. public function testGetTypeFromExtension()
  68. {
  69. $type = new FooType();
  70. $resolvedType = new ResolvedFormType($type);
  71. $this->extension2->addType($type);
  72. $this->resolvedTypeFactory->expects($this->once())
  73. ->method('createResolvedType')
  74. ->with($type)
  75. ->willReturn($resolvedType);
  76. $this->assertSame($resolvedType, $this->registry->getType(\get_class($type)));
  77. }
  78. public function testLoadUnregisteredType()
  79. {
  80. $type = new FooType();
  81. $resolvedType = new ResolvedFormType($type);
  82. $this->resolvedTypeFactory->expects($this->once())
  83. ->method('createResolvedType')
  84. ->with($type)
  85. ->willReturn($resolvedType);
  86. $this->assertSame($resolvedType, $this->registry->getType('Symfony\Component\Form\Tests\Fixtures\FooType'));
  87. }
  88. /**
  89. * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
  90. */
  91. public function testFailIfUnregisteredTypeNoClass()
  92. {
  93. $this->registry->getType('Symfony\Blubb');
  94. }
  95. /**
  96. * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
  97. */
  98. public function testFailIfUnregisteredTypeNoFormType()
  99. {
  100. $this->registry->getType('stdClass');
  101. }
  102. /**
  103. * @group legacy
  104. */
  105. public function testLegacyGetTypeFromExtension()
  106. {
  107. $type = new LegacyFooType();
  108. $resolvedType = new ResolvedFormType($type);
  109. $this->extension2->addType($type);
  110. $this->resolvedTypeFactory->expects($this->once())
  111. ->method('createResolvedType')
  112. ->with($type)
  113. ->willReturn($resolvedType);
  114. $this->assertSame($resolvedType, $this->registry->getType('foo'));
  115. // Even types with explicit getName() methods must support access by
  116. // FQCN to support a smooth transition from 2.8 => 3.0
  117. $this->assertSame($resolvedType, $this->registry->getType(\get_class($type)));
  118. }
  119. public function testGetTypeWithTypeExtensions()
  120. {
  121. $type = new FooType();
  122. $ext1 = new FooTypeBarExtension();
  123. $ext2 = new FooTypeBazExtension();
  124. $resolvedType = new ResolvedFormType($type, array($ext1, $ext2));
  125. $this->extension2->addType($type);
  126. $this->extension1->addTypeExtension($ext1);
  127. $this->extension2->addTypeExtension($ext2);
  128. $this->resolvedTypeFactory->expects($this->once())
  129. ->method('createResolvedType')
  130. ->with($type, array($ext1, $ext2))
  131. ->willReturn($resolvedType);
  132. $this->assertSame($resolvedType, $this->registry->getType(\get_class($type)));
  133. }
  134. /**
  135. * @group legacy
  136. */
  137. public function testLegacyGetTypeWithTypeExtensions()
  138. {
  139. $type = new LegacyFooType();
  140. $ext1 = new LegacyFooTypeBarExtension();
  141. $ext2 = new LegacyFooTypeBazExtension();
  142. $resolvedType = new ResolvedFormType($type, array($ext1, $ext2));
  143. $this->extension2->addType($type);
  144. $this->extension1->addTypeExtension($ext1);
  145. $this->extension2->addTypeExtension($ext2);
  146. $this->resolvedTypeFactory->expects($this->once())
  147. ->method('createResolvedType')
  148. ->with($type, array($ext1, $ext2))
  149. ->willReturn($resolvedType);
  150. $this->assertSame($resolvedType, $this->registry->getType('foo'));
  151. }
  152. public function testGetTypeConnectsParent()
  153. {
  154. $parentType = new FooType();
  155. $type = new FooSubType();
  156. $parentResolvedType = new ResolvedFormType($parentType);
  157. $resolvedType = new ResolvedFormType($type);
  158. $this->extension1->addType($parentType);
  159. $this->extension2->addType($type);
  160. $this->resolvedTypeFactory->expects($this->at(0))
  161. ->method('createResolvedType')
  162. ->with($parentType)
  163. ->willReturn($parentResolvedType);
  164. $this->resolvedTypeFactory->expects($this->at(1))
  165. ->method('createResolvedType')
  166. ->with($type, array(), $parentResolvedType)
  167. ->willReturn($resolvedType);
  168. $this->assertSame($resolvedType, $this->registry->getType(\get_class($type)));
  169. }
  170. /**
  171. * @group legacy
  172. */
  173. public function testLegacyGetTypeConnectsParent()
  174. {
  175. $parentType = new LegacyFooType();
  176. $type = new LegacyFooSubType();
  177. $parentResolvedType = new ResolvedFormType($parentType);
  178. $resolvedType = new ResolvedFormType($type);
  179. $this->extension1->addType($parentType);
  180. $this->extension2->addType($type);
  181. $this->resolvedTypeFactory->expects($this->at(0))
  182. ->method('createResolvedType')
  183. ->with($parentType)
  184. ->willReturn($parentResolvedType);
  185. $this->resolvedTypeFactory->expects($this->at(1))
  186. ->method('createResolvedType')
  187. ->with($type, array(), $parentResolvedType)
  188. ->willReturn($resolvedType);
  189. $this->assertSame($resolvedType, $this->registry->getType('foo_sub_type'));
  190. }
  191. /**
  192. * @group legacy
  193. */
  194. public function testGetTypeConnectsParentIfGetParentReturnsInstance()
  195. {
  196. $type = new LegacyFooSubTypeWithParentInstance();
  197. $parentResolvedType = new ResolvedFormType($type->getParent());
  198. $resolvedType = new ResolvedFormType($type);
  199. $this->extension1->addType($type);
  200. $this->resolvedTypeFactory->expects($this->at(0))
  201. ->method('createResolvedType')
  202. ->with($type->getParent())
  203. ->willReturn($parentResolvedType);
  204. $this->resolvedTypeFactory->expects($this->at(1))
  205. ->method('createResolvedType')
  206. ->with($type, array(), $parentResolvedType)
  207. ->willReturn($resolvedType);
  208. $this->assertSame($resolvedType, $this->registry->getType('foo_sub_type_parent_instance'));
  209. }
  210. /**
  211. * @expectedException \Symfony\Component\Form\Exception\InvalidArgumentException
  212. */
  213. public function testGetTypeThrowsExceptionIfTypeNotFound()
  214. {
  215. $this->registry->getType('bar');
  216. }
  217. public function testHasTypeAfterLoadingFromExtension()
  218. {
  219. $type = new FooType();
  220. $resolvedType = new ResolvedFormType($type);
  221. $this->resolvedTypeFactory->expects($this->once())
  222. ->method('createResolvedType')
  223. ->with($type)
  224. ->willReturn($resolvedType);
  225. $this->extension2->addType($type);
  226. $this->assertTrue($this->registry->hasType(\get_class($type)));
  227. }
  228. public function testHasTypeIfFQCN()
  229. {
  230. $this->assertTrue($this->registry->hasType('Symfony\Component\Form\Tests\Fixtures\FooType'));
  231. }
  232. public function testDoesNotHaveTypeIfNonExistingClass()
  233. {
  234. $this->assertFalse($this->registry->hasType('Symfony\Blubb'));
  235. }
  236. public function testDoesNotHaveTypeIfNoFormType()
  237. {
  238. $this->assertFalse($this->registry->hasType('stdClass'));
  239. }
  240. /**
  241. * @group legacy
  242. */
  243. public function testLegacyHasTypeAfterLoadingFromExtension()
  244. {
  245. $type = new LegacyFooType();
  246. $resolvedType = new ResolvedFormType($type);
  247. $this->resolvedTypeFactory->expects($this->once())
  248. ->method('createResolvedType')
  249. ->with($type)
  250. ->willReturn($resolvedType);
  251. $this->extension2->addType($type);
  252. $this->assertTrue($this->registry->hasType('foo'));
  253. }
  254. public function testGetTypeGuesser()
  255. {
  256. $expectedGuesser = new FormTypeGuesserChain(array($this->guesser1, $this->guesser2));
  257. $this->assertEquals($expectedGuesser, $this->registry->getTypeGuesser());
  258. $registry = new FormRegistry(
  259. array($this->getMockBuilder('Symfony\Component\Form\FormExtensionInterface')->getMock()),
  260. $this->resolvedTypeFactory
  261. );
  262. $this->assertNull($registry->getTypeGuesser());
  263. }
  264. public function testGetExtensions()
  265. {
  266. $expectedExtensions = array($this->extension1, $this->extension2);
  267. $this->assertEquals($expectedExtensions, $this->registry->getExtensions());
  268. }
  269. }