/integration-tests/performance-test-engine/vendor/symfony/symfony/src/Symfony/Component/Form/Tests/FormRegistryTest.php

https://github.com/societies/SOCIETIES-Platform · PHP · 263 lines · 166 code · 59 blank · 38 comment · 0 complexity · efd3ceb3967a8f2e6fa82b14bd2bc8a9 MD5 · raw file

  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;
  11. use Symfony\Component\Form\Tests\Fixtures\TestExtension;
  12. use Symfony\Component\Form\Tests\Fixtures\FooSubTypeWithParentInstance;
  13. use Symfony\Component\Form\Tests\Fixtures\FooSubType;
  14. use Symfony\Component\Form\Tests\Fixtures\FooTypeBazExtension;
  15. use Symfony\Component\Form\Tests\Fixtures\FooTypeBarExtension;
  16. use Symfony\Component\Form\Tests\Fixtures\FooType;
  17. /**
  18. * @author Bernhard Schussek <bschussek@gmail.com>
  19. */
  20. class FormRegistryTest extends \PHPUnit_Framework_TestCase
  21. {
  22. /**
  23. * @var FormRegistry
  24. */
  25. private $registry;
  26. /**
  27. * @var \PHPUnit_Framework_MockObject_MockObject
  28. */
  29. private $resolvedTypeFactory;
  30. /**
  31. * @var \PHPUnit_Framework_MockObject_MockObject
  32. */
  33. private $guesser1;
  34. /**
  35. * @var \PHPUnit_Framework_MockObject_MockObject
  36. */
  37. private $guesser2;
  38. /**
  39. * @var TestExtension
  40. */
  41. private $extension1;
  42. /**
  43. * @var TestExtension
  44. */
  45. private $extension2;
  46. protected function setUp()
  47. {
  48. $this->resolvedTypeFactory = $this->getMock('Symfony\Component\Form\ResolvedFormTypeFactory');
  49. $this->guesser1 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
  50. $this->guesser2 = $this->getMock('Symfony\Component\Form\FormTypeGuesserInterface');
  51. $this->extension1 = new TestExtension($this->guesser1);
  52. $this->extension2 = new TestExtension($this->guesser2);
  53. $this->registry = new FormRegistry(array(
  54. $this->extension1,
  55. $this->extension2,
  56. ), $this->resolvedTypeFactory);
  57. }
  58. public function testGetTypeReturnsAddedType()
  59. {
  60. $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  61. $resolvedType->expects($this->any())
  62. ->method('getName')
  63. ->will($this->returnValue('foo'));
  64. $this->registry->addType($resolvedType);
  65. $this->assertSame($resolvedType, $this->registry->getType('foo'));
  66. }
  67. public function testGetTypeFromExtension()
  68. {
  69. $type = new FooType();
  70. $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  71. $this->extension2->addType($type);
  72. $this->resolvedTypeFactory->expects($this->once())
  73. ->method('createResolvedType')
  74. ->with($type)
  75. ->will($this->returnValue($resolvedType));
  76. $resolvedType->expects($this->any())
  77. ->method('getName')
  78. ->will($this->returnValue('foo'));
  79. $resolvedType = $this->registry->getType('foo');
  80. $this->assertSame($resolvedType, $this->registry->getType('foo'));
  81. }
  82. public function testGetTypeWithTypeExtensions()
  83. {
  84. $type = new FooType();
  85. $ext1 = new FooTypeBarExtension();
  86. $ext2 = new FooTypeBazExtension();
  87. $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  88. $this->extension2->addType($type);
  89. $this->extension1->addTypeExtension($ext1);
  90. $this->extension2->addTypeExtension($ext2);
  91. $this->resolvedTypeFactory->expects($this->once())
  92. ->method('createResolvedType')
  93. ->with($type, array($ext1, $ext2))
  94. ->will($this->returnValue($resolvedType));
  95. $resolvedType->expects($this->any())
  96. ->method('getName')
  97. ->will($this->returnValue('foo'));
  98. $this->assertSame($resolvedType, $this->registry->getType('foo'));
  99. }
  100. public function testGetTypeConnectsParent()
  101. {
  102. $parentType = new FooType();
  103. $type = new FooSubType();
  104. $parentResolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  105. $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  106. $this->extension1->addType($parentType);
  107. $this->extension2->addType($type);
  108. $this->resolvedTypeFactory->expects($this->at(0))
  109. ->method('createResolvedType')
  110. ->with($parentType)
  111. ->will($this->returnValue($parentResolvedType));
  112. $this->resolvedTypeFactory->expects($this->at(1))
  113. ->method('createResolvedType')
  114. ->with($type, array(), $parentResolvedType)
  115. ->will($this->returnValue($resolvedType));
  116. $parentResolvedType->expects($this->any())
  117. ->method('getName')
  118. ->will($this->returnValue('foo'));
  119. $resolvedType->expects($this->any())
  120. ->method('getName')
  121. ->will($this->returnValue('foo_sub_type'));
  122. $this->assertSame($resolvedType, $this->registry->getType('foo_sub_type'));
  123. }
  124. public function testGetTypeConnectsParentIfGetParentReturnsInstance()
  125. {
  126. $type = new FooSubTypeWithParentInstance();
  127. $parentResolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  128. $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  129. $this->extension1->addType($type);
  130. $this->resolvedTypeFactory->expects($this->at(0))
  131. ->method('createResolvedType')
  132. ->with($this->isInstanceOf('Symfony\Component\Form\Tests\Fixtures\FooType'))
  133. ->will($this->returnValue($parentResolvedType));
  134. $this->resolvedTypeFactory->expects($this->at(1))
  135. ->method('createResolvedType')
  136. ->with($type, array(), $parentResolvedType)
  137. ->will($this->returnValue($resolvedType));
  138. $parentResolvedType->expects($this->any())
  139. ->method('getName')
  140. ->will($this->returnValue('foo'));
  141. $resolvedType->expects($this->any())
  142. ->method('getName')
  143. ->will($this->returnValue('foo_sub_type_parent_instance'));
  144. $this->assertSame($resolvedType, $this->registry->getType('foo_sub_type_parent_instance'));
  145. }
  146. /**
  147. * @expectedException Symfony\Component\Form\Exception\FormException
  148. */
  149. public function testGetTypeThrowsExceptionIfParentNotFound()
  150. {
  151. $type = new FooSubType();
  152. $this->extension1->addType($type);
  153. $this->registry->getType($type);
  154. }
  155. /**
  156. * @expectedException Symfony\Component\Form\Exception\FormException
  157. */
  158. public function testGetTypeThrowsExceptionIfTypeNotFound()
  159. {
  160. $this->registry->getType('bar');
  161. }
  162. /**
  163. * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  164. */
  165. public function testGetTypeThrowsExceptionIfNoString()
  166. {
  167. $this->registry->getType(array());
  168. }
  169. public function testHasTypeAfterAdding()
  170. {
  171. $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  172. $resolvedType->expects($this->any())
  173. ->method('getName')
  174. ->will($this->returnValue('foo'));
  175. $this->assertFalse($this->registry->hasType('foo'));
  176. $this->registry->addType($resolvedType);
  177. $this->assertTrue($this->registry->hasType('foo'));
  178. }
  179. public function testHasTypeAfterLoadingFromExtension()
  180. {
  181. $type = new FooType();
  182. $resolvedType = $this->getMock('Symfony\Component\Form\ResolvedFormTypeInterface');
  183. $this->resolvedTypeFactory->expects($this->once())
  184. ->method('createResolvedType')
  185. ->with($type)
  186. ->will($this->returnValue($resolvedType));
  187. $resolvedType->expects($this->any())
  188. ->method('getName')
  189. ->will($this->returnValue('foo'));
  190. $this->assertFalse($this->registry->hasType('foo'));
  191. $this->extension2->addType($type);
  192. $this->assertTrue($this->registry->hasType('foo'));
  193. }
  194. public function testGetTypeGuesser()
  195. {
  196. $expectedGuesser = new FormTypeGuesserChain(array($this->guesser1, $this->guesser2));
  197. $this->assertEquals($expectedGuesser, $this->registry->getTypeGuesser());
  198. }
  199. public function testGetExtensions()
  200. {
  201. $expectedExtensions = array($this->extension1, $this->extension2);
  202. $this->assertEquals($expectedExtensions, $this->registry->getExtensions());
  203. }
  204. }