|
10 | 10 | use Interop\Container\ContainerInterface;
|
11 | 11 | use PHPUnit\Framework\TestCase;
|
12 | 12 | use Zend\Form\ElementInterface;
|
| 13 | +use Zend\Form\Element\Number; |
13 | 14 | use Zend\Form\FormElementManager;
|
14 | 15 | use Zend\Form\FormElementManagerFactory;
|
15 | 16 | use Zend\ServiceManager\ServiceLocatorInterface;
|
@@ -70,4 +71,100 @@ public function testFactoryConfiguresPluginManagerUnderServiceManagerV2()
|
70 | 71 | $elements = $factory->createService($container->reveal());
|
71 | 72 | $this->assertSame($element, $elements->get('test'));
|
72 | 73 | }
|
| 74 | + |
| 75 | + public function testConfiguresFormElementsServicesWhenFound() |
| 76 | + { |
| 77 | + $element = $this->prophesize(ElementInterface::class)->reveal(); |
| 78 | + $config = [ |
| 79 | + 'form_elements' => [ |
| 80 | + 'aliases' => [ |
| 81 | + 'test' => Number::class, |
| 82 | + ], |
| 83 | + 'factories' => [ |
| 84 | + 'test-too' => function ($container) use ($element) { |
| 85 | + return $element; |
| 86 | + }, |
| 87 | + ], |
| 88 | + ], |
| 89 | + ]; |
| 90 | + |
| 91 | + $container = $this->prophesize(ServiceLocatorInterface::class); |
| 92 | + $container->willImplement(ContainerInterface::class); |
| 93 | + |
| 94 | + $container->has('ServiceListener')->willReturn(false); |
| 95 | + $container->has('config')->willReturn(true); |
| 96 | + $container->get('config')->willReturn($config); |
| 97 | + |
| 98 | + $factory = new FormElementManagerFactory(); |
| 99 | + $elements = $factory($container->reveal(), 'FormElementManager'); |
| 100 | + |
| 101 | + $this->assertInstanceOf(FormElementManager::class, $elements); |
| 102 | + $this->assertTrue($elements->has('test')); |
| 103 | + $this->assertInstanceOf(Number::class, $elements->get('test')); |
| 104 | + $this->assertTrue($elements->has('test-too')); |
| 105 | + $this->assertSame($element, $elements->get('test-too')); |
| 106 | + } |
| 107 | + |
| 108 | + public function testDoesNotConfigureFormElementsServicesWhenServiceListenerPresent() |
| 109 | + { |
| 110 | + $element = $this->prophesize(ElementInterface::class)->reveal(); |
| 111 | + $config = [ |
| 112 | + 'form_elements' => [ |
| 113 | + 'aliases' => [ |
| 114 | + 'test' => Number::class, |
| 115 | + ], |
| 116 | + 'factories' => [ |
| 117 | + 'test-too' => function ($container) use ($element) { |
| 118 | + return $element; |
| 119 | + }, |
| 120 | + ], |
| 121 | + ], |
| 122 | + ]; |
| 123 | + |
| 124 | + $container = $this->prophesize(ServiceLocatorInterface::class); |
| 125 | + $container->willImplement(ContainerInterface::class); |
| 126 | + |
| 127 | + $container->has('ServiceListener')->willReturn(true); |
| 128 | + $container->has('config')->shouldNotBeCalled(); |
| 129 | + $container->get('config')->shouldNotBeCalled(); |
| 130 | + |
| 131 | + $factory = new FormElementManagerFactory(); |
| 132 | + $elements = $factory($container->reveal(), 'FormElementManager'); |
| 133 | + |
| 134 | + $this->assertInstanceOf(FormElementManager::class, $elements); |
| 135 | + $this->assertFalse($elements->has('test')); |
| 136 | + $this->assertFalse($elements->has('test-too')); |
| 137 | + } |
| 138 | + |
| 139 | + public function testDoesNotConfigureFormElementsServicesWhenConfigServiceNotPresent() |
| 140 | + { |
| 141 | + $container = $this->prophesize(ServiceLocatorInterface::class); |
| 142 | + $container->willImplement(ContainerInterface::class); |
| 143 | + |
| 144 | + $container->has('ServiceListener')->willReturn(false); |
| 145 | + $container->has('config')->willReturn(false); |
| 146 | + $container->get('config')->shouldNotBeCalled(); |
| 147 | + |
| 148 | + $factory = new FormElementManagerFactory(); |
| 149 | + $elements = $factory($container->reveal(), 'FormElementManager'); |
| 150 | + |
| 151 | + $this->assertInstanceOf(FormElementManager::class, $elements); |
| 152 | + } |
| 153 | + |
| 154 | + public function testDoesNotConfigureFormElementServicesWhenConfigServiceDoesNotContainFormElementsConfig() |
| 155 | + { |
| 156 | + $container = $this->prophesize(ServiceLocatorInterface::class); |
| 157 | + $container->willImplement(ContainerInterface::class); |
| 158 | + |
| 159 | + $container->has('ServiceListener')->willReturn(false); |
| 160 | + $container->has('config')->willReturn(true); |
| 161 | + $container->get('config')->willReturn(['foo' => 'bar']); |
| 162 | + $container->has('MvcTranslator')->willReturn(false); // necessary due to default initializers |
| 163 | + |
| 164 | + $factory = new FormElementManagerFactory(); |
| 165 | + $elements = $factory($container->reveal(), 'FormElementManager'); |
| 166 | + |
| 167 | + $this->assertInstanceOf(FormElementManager::class, $elements); |
| 168 | + $this->assertFalse($elements->has('foo')); |
| 169 | + } |
73 | 170 | }
|
0 commit comments