|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * @link http://github.com/zendframework/zend-mvc for the canonical source repository |
| 4 | + * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com) |
| 5 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 6 | + */ |
| 7 | + |
| 8 | +namespace ZendTest\Mvc\Controller; |
| 9 | + |
| 10 | +use Interop\Container\ContainerInterface; |
| 11 | +use PHPUnit_Framework_TestCase as TestCase; |
| 12 | +use Zend\Mvc\Controller\LazyControllerFactory; |
| 13 | +use Zend\ServiceManager\Exception\ServiceNotFoundException; |
| 14 | +use Zend\Validator\ValidatorPluginManager; |
| 15 | + |
| 16 | +class LazyControllerFactoryTest extends TestCase |
| 17 | +{ |
| 18 | + public function setUp() |
| 19 | + { |
| 20 | + $this->container = $this->prophesize(ContainerInterface::class); |
| 21 | + } |
| 22 | + |
| 23 | + public function nonClassRequestedNames() |
| 24 | + { |
| 25 | + return [ |
| 26 | + 'null' => [null], |
| 27 | + 'true' => [true], |
| 28 | + 'false' => [false], |
| 29 | + 'zero' => [0], |
| 30 | + 'int' => [1], |
| 31 | + 'zero-float' => [0.0], |
| 32 | + 'float' => [1.1], |
| 33 | + 'non-class-string' => ['non-class-string'], |
| 34 | + 'array' => [['non-class-string']], |
| 35 | + 'object' => [(object) ['class' => 'non-class-string']], |
| 36 | + ]; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider nonClassRequestedNames |
| 41 | + */ |
| 42 | + public function testCanCreateReturnsFalseForNonClassRequestedNames($requestedName) |
| 43 | + { |
| 44 | + $factory = new LazyControllerFactory(); |
| 45 | + $this->assertFalse($factory->canCreate($this->container->reveal(), $requestedName)); |
| 46 | + } |
| 47 | + |
| 48 | + public function testCanCreateReturnsFalseForClassesThatDoNotImplementDispatchableInterface() |
| 49 | + { |
| 50 | + $factory = new LazyControllerFactory(); |
| 51 | + $this->assertFalse($factory->canCreate($this->container->reveal(), __CLASS__)); |
| 52 | + } |
| 53 | + |
| 54 | + public function testFactoryInstantiatesClassDirectlyIfItHasNoConstructor() |
| 55 | + { |
| 56 | + $factory = new LazyControllerFactory(); |
| 57 | + $controller = $factory($this->container->reveal(), TestAsset\SampleController::class); |
| 58 | + $this->assertInstanceOf(TestAsset\SampleController::class, $controller); |
| 59 | + } |
| 60 | + |
| 61 | + public function testFactoryInstantiatesClassDirectlyIfConstructorHasNoArguments() |
| 62 | + { |
| 63 | + $factory = new LazyControllerFactory(); |
| 64 | + $controller = $factory($this->container->reveal(), TestAsset\ControllerWithEmptyConstructor::class); |
| 65 | + $this->assertInstanceOf(TestAsset\ControllerWithEmptyConstructor::class, $controller); |
| 66 | + } |
| 67 | + |
| 68 | + public function testFactoryRaisesExceptionWhenUnableToResolveATypeHintedService() |
| 69 | + { |
| 70 | + $this->container->has(TestAsset\SampleInterface::class)->willReturn(false); |
| 71 | + $factory = new LazyControllerFactory(); |
| 72 | + $this->setExpectedException( |
| 73 | + ServiceNotFoundException::class, |
| 74 | + sprintf( |
| 75 | + 'Unable to create controller "%s"; unable to resolve parameter "sample" using type hint "%s"', |
| 76 | + TestAsset\ControllerWithTypeHintedConstructorParameter::class, |
| 77 | + TestAsset\SampleInterface::class |
| 78 | + ) |
| 79 | + ); |
| 80 | + $factory($this->container->reveal(), TestAsset\ControllerWithTypeHintedConstructorParameter::class); |
| 81 | + } |
| 82 | + |
| 83 | + public function testFactoryPassesNullForScalarParameters() |
| 84 | + { |
| 85 | + $factory = new LazyControllerFactory(); |
| 86 | + $controller = $factory($this->container->reveal(), TestAsset\ControllerWithScalarParameters::class); |
| 87 | + $this->assertInstanceOf(TestAsset\ControllerWithScalarParameters::class, $controller); |
| 88 | + $this->assertNull($controller->foo); |
| 89 | + $this->assertNull($controller->bar); |
| 90 | + } |
| 91 | + |
| 92 | + public function testFactoryInjectsConfigServiceForConfigArgumentsTypeHintedAsArray() |
| 93 | + { |
| 94 | + $config = ['foo' => 'bar']; |
| 95 | + $this->container->has('config')->willReturn(true); |
| 96 | + $this->container->get('config')->willReturn($config); |
| 97 | + |
| 98 | + $factory = new LazyControllerFactory(); |
| 99 | + $controller = $factory($this->container->reveal(), TestAsset\ControllerAcceptingConfigToConstructor::class); |
| 100 | + $this->assertInstanceOf(TestAsset\ControllerAcceptingConfigToConstructor::class, $controller); |
| 101 | + $this->assertEquals($config, $controller->config); |
| 102 | + } |
| 103 | + |
| 104 | + public function testFactoryCanInjectKnownTypeHintedServices() |
| 105 | + { |
| 106 | + $sample = $this->prophesize(TestAsset\SampleInterface::class)->reveal(); |
| 107 | + $this->container->has(TestAsset\SampleInterface::class)->willReturn(true); |
| 108 | + $this->container->get(TestAsset\SampleInterface::class)->willReturn($sample); |
| 109 | + |
| 110 | + $factory = new LazyControllerFactory(); |
| 111 | + $controller = $factory($this->container->reveal(), TestAsset\ControllerWithTypeHintedConstructorParameter::class); |
| 112 | + $this->assertInstanceOf(TestAsset\ControllerWithTypeHintedConstructorParameter::class, $controller); |
| 113 | + $this->assertSame($sample, $controller->sample); |
| 114 | + } |
| 115 | + |
| 116 | + public function testFactoryResolvesTypeHintsForServicesToWellKnownServiceNames() |
| 117 | + { |
| 118 | + $validators = $this->prophesize(ValidatorPluginManager::class)->reveal(); |
| 119 | + $this->container->has('ValidatorManager')->willReturn(true); |
| 120 | + $this->container->get('ValidatorManager')->willReturn($validators); |
| 121 | + |
| 122 | + $factory = new LazyControllerFactory(); |
| 123 | + $controller = $factory( |
| 124 | + $this->container->reveal(), |
| 125 | + TestAsset\ControllerAcceptingWellKnownServicesAsConstructorParameters::class |
| 126 | + ); |
| 127 | + $this->assertInstanceOf( |
| 128 | + TestAsset\ControllerAcceptingWellKnownServicesAsConstructorParameters::class, |
| 129 | + $controller |
| 130 | + ); |
| 131 | + $this->assertSame($validators, $controller->validators); |
| 132 | + } |
| 133 | + |
| 134 | + public function testFactoryCanSupplyAMixOfParameterTypes() |
| 135 | + { |
| 136 | + $validators = $this->prophesize(ValidatorPluginManager::class)->reveal(); |
| 137 | + $this->container->has('ValidatorManager')->willReturn(true); |
| 138 | + $this->container->get('ValidatorManager')->willReturn($validators); |
| 139 | + |
| 140 | + $sample = $this->prophesize(TestAsset\SampleInterface::class)->reveal(); |
| 141 | + $this->container->has(TestAsset\SampleInterface::class)->willReturn(true); |
| 142 | + $this->container->get(TestAsset\SampleInterface::class)->willReturn($sample); |
| 143 | + |
| 144 | + $config = ['foo' => 'bar']; |
| 145 | + $this->container->has('config')->willReturn(true); |
| 146 | + $this->container->get('config')->willReturn($config); |
| 147 | + |
| 148 | + $factory = new LazyControllerFactory(); |
| 149 | + $controller = $factory($this->container->reveal(), TestAsset\ControllerWithMixedConstructorParameters::class); |
| 150 | + $this->assertInstanceOf(TestAsset\ControllerWithMixedConstructorParameters::class, $controller); |
| 151 | + |
| 152 | + $this->assertEquals($config, $controller->config); |
| 153 | + $this->assertNull($controller->foo); |
| 154 | + $this->assertEquals([], $controller->options); |
| 155 | + $this->assertSame($sample, $controller->sample); |
| 156 | + $this->assertSame($validators, $controller->validators); |
| 157 | + } |
| 158 | +} |
0 commit comments