@@ -15,78 +15,78 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | class ServiceLoaderTest extends TestCase |
| 17 | 17 | { |
| 18 | - /** @var ServiceLoader */ |
|
| 19 | - private $loaderYml; |
|
| 18 | + /** @var ServiceLoader */ |
|
| 19 | + private $loaderYml; |
|
| 20 | 20 | |
| 21 | - public function setUp() |
|
| 22 | - { |
|
| 23 | - $this->loaderYml = new ServiceLoader(new YamlParser()); |
|
| 24 | - } |
|
| 21 | + public function setUp() |
|
| 22 | + { |
|
| 23 | + $this->loaderYml = new ServiceLoader(new YamlParser()); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @return array |
|
| 28 | - */ |
|
| 29 | - public function ymlProvider() |
|
| 30 | - { |
|
| 31 | - return [ |
|
| 32 | - [__DIR__ . '/data/yml/test-empty.yml'], |
|
| 33 | - [__DIR__ . '/data/yml/test-full.yml'] |
|
| 34 | - ]; |
|
| 35 | - } |
|
| 26 | + /** |
|
| 27 | + * @return array |
|
| 28 | + */ |
|
| 29 | + public function ymlProvider() |
|
| 30 | + { |
|
| 31 | + return [ |
|
| 32 | + [__DIR__ . '/data/yml/test-empty.yml'], |
|
| 33 | + [__DIR__ . '/data/yml/test-full.yml'] |
|
| 34 | + ]; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - /** |
|
| 38 | - * @dataProvider ymlProvider |
|
| 39 | - * @param string $testFile |
|
| 40 | - * @throws \Wonderland\Container\Exception\InvalidConfigFormatException |
|
| 41 | - */ |
|
| 42 | - public function test_loadFile($testFile) |
|
| 43 | - { |
|
| 44 | - $services = $this->loaderYml->loadFile($testFile); |
|
| 45 | - $this->assertInternalType('array', $services); |
|
| 46 | - foreach ($services as $service) { |
|
| 47 | - $this->assertInstanceOf(ServiceDefinition::class, $service); |
|
| 48 | - } |
|
| 49 | - } |
|
| 37 | + /** |
|
| 38 | + * @dataProvider ymlProvider |
|
| 39 | + * @param string $testFile |
|
| 40 | + * @throws \Wonderland\Container\Exception\InvalidConfigFormatException |
|
| 41 | + */ |
|
| 42 | + public function test_loadFile($testFile) |
|
| 43 | + { |
|
| 44 | + $services = $this->loaderYml->loadFile($testFile); |
|
| 45 | + $this->assertInternalType('array', $services); |
|
| 46 | + foreach ($services as $service) { |
|
| 47 | + $this->assertInstanceOf(ServiceDefinition::class, $service); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @return array |
|
| 53 | - */ |
|
| 54 | - public function ymlErrorProvider() |
|
| 55 | - { |
|
| 56 | - return [ |
|
| 57 | - [__DIR__ . '/data/yml-error/empty.yml'], |
|
| 58 | - [__DIR__ . '/data/yml-error/err1.yml'], |
|
| 59 | - [__DIR__ . '/data/yml-error/err2.yml'], |
|
| 60 | - [__DIR__ . '/data/yml-error/err3.yml'], |
|
| 61 | - [__DIR__ . '/data/yml-error/err4.yml'], |
|
| 62 | - [__DIR__ . '/data/yml-error/err5.yml'], |
|
| 63 | - [__DIR__ . '/data/yml-error/err6.yml'], |
|
| 64 | - [__DIR__ . '/data/yml-error/err7.yml'], |
|
| 65 | - [__DIR__ . '/data/yml-error/err8.yml'] |
|
| 66 | - ]; |
|
| 67 | - } |
|
| 51 | + /** |
|
| 52 | + * @return array |
|
| 53 | + */ |
|
| 54 | + public function ymlErrorProvider() |
|
| 55 | + { |
|
| 56 | + return [ |
|
| 57 | + [__DIR__ . '/data/yml-error/empty.yml'], |
|
| 58 | + [__DIR__ . '/data/yml-error/err1.yml'], |
|
| 59 | + [__DIR__ . '/data/yml-error/err2.yml'], |
|
| 60 | + [__DIR__ . '/data/yml-error/err3.yml'], |
|
| 61 | + [__DIR__ . '/data/yml-error/err4.yml'], |
|
| 62 | + [__DIR__ . '/data/yml-error/err5.yml'], |
|
| 63 | + [__DIR__ . '/data/yml-error/err6.yml'], |
|
| 64 | + [__DIR__ . '/data/yml-error/err7.yml'], |
|
| 65 | + [__DIR__ . '/data/yml-error/err8.yml'] |
|
| 66 | + ]; |
|
| 67 | + } |
|
| 68 | 68 | |
| 69 | - /** |
|
| 70 | - * @dataProvider ymlErrorProvider |
|
| 71 | - * @param string $testFile |
|
| 72 | - * @throws \Wonderland\Container\Exception\InvalidConfigFormatException |
|
| 73 | - */ |
|
| 74 | - public function test_loadFileError($testFile) |
|
| 75 | - { |
|
| 76 | - $this->expectException(InvalidConfigFormatException::class); |
|
| 77 | - $this->loaderYml->loadFile($testFile); |
|
| 78 | - } |
|
| 69 | + /** |
|
| 70 | + * @dataProvider ymlErrorProvider |
|
| 71 | + * @param string $testFile |
|
| 72 | + * @throws \Wonderland\Container\Exception\InvalidConfigFormatException |
|
| 73 | + */ |
|
| 74 | + public function test_loadFileError($testFile) |
|
| 75 | + { |
|
| 76 | + $this->expectException(InvalidConfigFormatException::class); |
|
| 77 | + $this->loaderYml->loadFile($testFile); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @throws InvalidConfigFormatException |
|
| 82 | - */ |
|
| 83 | - public function test_loadDirectory() |
|
| 84 | - { |
|
| 85 | - $services = $this->loaderYml->loadDirectory(__DIR__ . '/data/yml'); |
|
| 86 | - $this->assertInternalType('array', $services); |
|
| 87 | - foreach ($services as $service) { |
|
| 88 | - $this->assertInstanceOf(ServiceDefinition::class, $service); |
|
| 89 | - } |
|
| 90 | - } |
|
| 80 | + /** |
|
| 81 | + * @throws InvalidConfigFormatException |
|
| 82 | + */ |
|
| 83 | + public function test_loadDirectory() |
|
| 84 | + { |
|
| 85 | + $services = $this->loaderYml->loadDirectory(__DIR__ . '/data/yml'); |
|
| 86 | + $this->assertInternalType('array', $services); |
|
| 87 | + foreach ($services as $service) { |
|
| 88 | + $this->assertInstanceOf(ServiceDefinition::class, $service); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | 91 | |
| 92 | 92 | } |
@@ -9,18 +9,18 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class SampleClass |
| 11 | 11 | { |
| 12 | - private $first; |
|
| 13 | - private $second; |
|
| 12 | + private $first; |
|
| 13 | + private $second; |
|
| 14 | 14 | |
| 15 | - public function __construct($param1, $param2) |
|
| 16 | - { |
|
| 17 | - $this->first = $param1; |
|
| 18 | - $this->second = $param2; |
|
| 19 | - } |
|
| 15 | + public function __construct($param1, $param2) |
|
| 16 | + { |
|
| 17 | + $this->first = $param1; |
|
| 18 | + $this->second = $param2; |
|
| 19 | + } |
|
| 20 | 20 | |
| 21 | - public function callMethod($param1, $param2) |
|
| 22 | - { |
|
| 23 | - $this->first = $param1; |
|
| 24 | - $this->second = $param2; |
|
| 25 | - } |
|
| 21 | + public function callMethod($param1, $param2) |
|
| 22 | + { |
|
| 23 | + $this->first = $param1; |
|
| 24 | + $this->second = $param2; |
|
| 25 | + } |
|
| 26 | 26 | } |
@@ -14,57 +14,57 @@ |
||
| 14 | 14 | class YamlParser implements ParserInterface |
| 15 | 15 | { |
| 16 | 16 | |
| 17 | - /** |
|
| 18 | - * @param string $filePath |
|
| 19 | - * @return array |
|
| 20 | - * @throws ResourceNotFoundException |
|
| 21 | - * @throws InvalidResourceException |
|
| 22 | - */ |
|
| 23 | - public function loadFile(string $filePath): array |
|
| 24 | - { |
|
| 25 | - if (false === file_exists($filePath)) { |
|
| 26 | - throw new ResourceNotFoundException('The file "' . $filePath . '" does not exists'); |
|
| 27 | - } |
|
| 17 | + /** |
|
| 18 | + * @param string $filePath |
|
| 19 | + * @return array |
|
| 20 | + * @throws ResourceNotFoundException |
|
| 21 | + * @throws InvalidResourceException |
|
| 22 | + */ |
|
| 23 | + public function loadFile(string $filePath): array |
|
| 24 | + { |
|
| 25 | + if (false === file_exists($filePath)) { |
|
| 26 | + throw new ResourceNotFoundException('The file "' . $filePath . '" does not exists'); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - if (false === is_file($filePath)) { |
|
| 30 | - throw new InvalidResourceException('The resource "' . $filePath . '" is not a file'); |
|
| 31 | - } |
|
| 29 | + if (false === is_file($filePath)) { |
|
| 30 | + throw new InvalidResourceException('The resource "' . $filePath . '" is not a file'); |
|
| 31 | + } |
|
| 32 | 32 | |
| 33 | - $parse = Yaml::parseFile($filePath); |
|
| 33 | + $parse = Yaml::parseFile($filePath); |
|
| 34 | 34 | |
| 35 | - if (false === is_array($parse)) { |
|
| 36 | - $parse = []; |
|
| 37 | - } |
|
| 35 | + if (false === is_array($parse)) { |
|
| 36 | + $parse = []; |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - return $parse; |
|
| 40 | - } |
|
| 39 | + return $parse; |
|
| 40 | + } |
|
| 41 | 41 | |
| 42 | - /** |
|
| 43 | - * @param string $directorPath |
|
| 44 | - * @return array |
|
| 45 | - * @throws InvalidResourceException |
|
| 46 | - * @throws ResourceNotFoundException |
|
| 47 | - */ |
|
| 48 | - public function loadDirectory(string $directorPath): array |
|
| 49 | - { |
|
| 50 | - if (false === file_exists($directorPath)) { |
|
| 51 | - throw new ResourceNotFoundException('The directory "' . $directorPath . '" does not exists'); |
|
| 52 | - } |
|
| 42 | + /** |
|
| 43 | + * @param string $directorPath |
|
| 44 | + * @return array |
|
| 45 | + * @throws InvalidResourceException |
|
| 46 | + * @throws ResourceNotFoundException |
|
| 47 | + */ |
|
| 48 | + public function loadDirectory(string $directorPath): array |
|
| 49 | + { |
|
| 50 | + if (false === file_exists($directorPath)) { |
|
| 51 | + throw new ResourceNotFoundException('The directory "' . $directorPath . '" does not exists'); |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - if (false === is_dir($directorPath)) { |
|
| 55 | - throw new InvalidResourceException('The resource "' . $directorPath . '" is not a directory'); |
|
| 56 | - } |
|
| 54 | + if (false === is_dir($directorPath)) { |
|
| 55 | + throw new InvalidResourceException('The resource "' . $directorPath . '" is not a directory'); |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $config = []; |
|
| 58 | + $config = []; |
|
| 59 | 59 | |
| 60 | - $files = array_diff(scandir($directorPath), array('.', '..')); |
|
| 61 | - foreach ($files as $file) { |
|
| 62 | - if (false !== preg_match('/^.+(yml|yaml)$/i', $file)) { |
|
| 63 | - $config[$file] = Yaml::parseFile(realpath($directorPath) . DIRECTORY_SEPARATOR . $file); |
|
| 64 | - } |
|
| 65 | - } |
|
| 60 | + $files = array_diff(scandir($directorPath), array('.', '..')); |
|
| 61 | + foreach ($files as $file) { |
|
| 62 | + if (false !== preg_match('/^.+(yml|yaml)$/i', $file)) { |
|
| 63 | + $config[$file] = Yaml::parseFile(realpath($directorPath) . DIRECTORY_SEPARATOR . $file); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - return $config; |
|
| 68 | - } |
|
| 67 | + return $config; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | 70 | } |
@@ -18,182 +18,182 @@ |
||
| 18 | 18 | */ |
| 19 | 19 | class ServiceContainerTest extends TestCase |
| 20 | 20 | { |
| 21 | - private const SERVICE_NAME = 'service.name'; |
|
| 22 | - private const DEFAULT_SERVICE_NANE = 'default.service.name'; |
|
| 23 | - |
|
| 24 | - /** @var ServiceContainer|MockObject */ |
|
| 25 | - private $container; |
|
| 26 | - |
|
| 27 | - /** @var ServiceContainer */ |
|
| 28 | - private $realContainer; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @throws DuplicatedServiceException |
|
| 32 | - */ |
|
| 33 | - protected function setUp() |
|
| 34 | - { |
|
| 35 | - $this->container = $this->getMockBuilder(ServiceContainer::class)->getMock(); |
|
| 36 | - $this->realContainer = new ServiceContainer(); |
|
| 37 | - $this->realContainer->addService(new ServiceDefinition(self::DEFAULT_SERVICE_NANE, \DateTime::class)); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - public function testConstructor() |
|
| 41 | - { |
|
| 42 | - $this->assertAttributeEquals([], 'serviceInstances', $this->container); |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * @covers \Wonderland\Container\ServiceContainer::addService |
|
| 47 | - * @throws DuplicatedServiceException |
|
| 48 | - */ |
|
| 49 | - public function test_AddService() |
|
| 50 | - { |
|
| 51 | - $definition = $this->getMockBuilder(ServiceDefinition::class) |
|
| 52 | - ->setConstructorArgs([self::SERVICE_NAME, \DateTime::class]) |
|
| 53 | - ->getMock(); |
|
| 54 | - ; |
|
| 55 | - |
|
| 56 | - $definition->expects($this->exactly(2)) |
|
| 57 | - ->method('getServiceName') |
|
| 58 | - ->willReturn(self::SERVICE_NAME); |
|
| 59 | - |
|
| 60 | - $this->assertSame($this->realContainer, $this->realContainer->addService($definition)); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @throws DuplicatedServiceException |
|
| 65 | - */ |
|
| 66 | - public function test_AddServiceException() |
|
| 67 | - { |
|
| 68 | - $this->expectException(DuplicatedServiceException::class); |
|
| 69 | - |
|
| 70 | - $definition = $this->getMockBuilder(ServiceDefinition::class) |
|
| 71 | - ->setConstructorArgs([self::SERVICE_NAME, \DateTime::class]) |
|
| 72 | - ->getMock(); |
|
| 73 | - ; |
|
| 74 | - |
|
| 75 | - $definition->expects($this->exactly(4)) |
|
| 76 | - ->method('getServiceName') |
|
| 77 | - ->willReturn(self::SERVICE_NAME); |
|
| 78 | - |
|
| 79 | - $this->realContainer->addService($definition); |
|
| 80 | - $this->realContainer->addService($definition); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - public function test_loadServices() |
|
| 84 | - { |
|
| 85 | - $definition = $this->getMockBuilder(ServiceDefinition::class) |
|
| 86 | - ->setConstructorArgs([self::SERVICE_NAME . '10', \DateTime::class]) |
|
| 87 | - ->getMock(); |
|
| 88 | - ; |
|
| 89 | - $definition2 = $this->getMockBuilder(ServiceDefinition::class) |
|
| 90 | - ->setConstructorArgs([self::SERVICE_NAME . '20', \DateTime::class]) |
|
| 91 | - ->getMock(); |
|
| 92 | - ; |
|
| 93 | - |
|
| 94 | - $list = [ |
|
| 95 | - $definition, |
|
| 96 | - $definition2 |
|
| 97 | - ]; |
|
| 98 | - |
|
| 99 | - $definition->expects($this->exactly(2)) |
|
| 100 | - ->method('getServiceName') |
|
| 101 | - ->willReturn(self::SERVICE_NAME . '10'); |
|
| 102 | - $definition2->expects($this->exactly(2)) |
|
| 103 | - ->method('getServiceName') |
|
| 104 | - ->willReturn(self::SERVICE_NAME . '20'); |
|
| 105 | - |
|
| 106 | - $this->realContainer->loadServices($list); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @throws DuplicatedServiceException |
|
| 111 | - */ |
|
| 112 | - public function test_AddServiceInstance() |
|
| 113 | - { |
|
| 114 | - $definition = $this->getMockBuilder(InstanceDefinition::class) |
|
| 115 | - ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
|
| 116 | - ->getMock(); |
|
| 117 | - |
|
| 118 | - $definition->expects($this->atLeast(2)) |
|
| 119 | - ->method('getServiceName') |
|
| 120 | - ->willReturn(self::SERVICE_NAME); |
|
| 121 | - |
|
| 122 | - $this->container->expects($this->once()) |
|
| 123 | - ->method('addServiceInstance') |
|
| 124 | - ->willReturn($this->container); |
|
| 125 | - |
|
| 126 | - $this->assertSame($this->container, $this->container->addServiceInstance($definition)); |
|
| 127 | - $this->assertSame($this->realContainer, $this->realContainer->addServiceInstance($definition)); |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * @throws DuplicatedServiceException |
|
| 132 | - */ |
|
| 133 | - public function test_AddServiceInstanceException() |
|
| 134 | - { |
|
| 135 | - $this->expectException(DuplicatedServiceException::class); |
|
| 136 | - |
|
| 137 | - $definition = $this->getMockBuilder(InstanceDefinition::class) |
|
| 138 | - ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
|
| 139 | - ->getMock(); |
|
| 140 | - |
|
| 141 | - $definition->expects($this->exactly(2)) |
|
| 142 | - ->method('getServiceName') |
|
| 143 | - ->willReturn(self::DEFAULT_SERVICE_NANE); |
|
| 144 | - |
|
| 145 | - $this->realContainer->addServiceInstance($definition); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - /** |
|
| 149 | - * @throws DuplicatedServiceException |
|
| 150 | - */ |
|
| 151 | - public function test_get() |
|
| 152 | - { |
|
| 153 | - $this->assertSame(null, $this->realContainer->get('test')); |
|
| 154 | - $this->assertSame(\DateTime::class, get_class($this->realContainer->get(self::DEFAULT_SERVICE_NANE))); |
|
| 155 | - $instance = $this->realContainer->get(self::DEFAULT_SERVICE_NANE); |
|
| 156 | - $this->assertSame(\DateTime::class, get_class($instance)); |
|
| 157 | - $this->assertSame($instance, $this->realContainer->get(self::DEFAULT_SERVICE_NANE)); |
|
| 158 | - |
|
| 159 | - $this->realContainer->addServiceInstance( |
|
| 160 | - new InstanceDefinition('call.format', 'd-m-Y') |
|
| 161 | - ); |
|
| 162 | - $this->realContainer->addServiceInstance( |
|
| 163 | - new InstanceDefinition('call.date', '1970-01-01') |
|
| 164 | - ); |
|
| 165 | - |
|
| 166 | - $this->realContainer->addService( |
|
| 167 | - new ServiceDefinition( |
|
| 168 | - 'service.call', |
|
| 169 | - \DateTime::class, |
|
| 170 | - ['@call.date'], |
|
| 171 | - ['format' => ['@call.format']] |
|
| 172 | - ) |
|
| 173 | - ); |
|
| 174 | - |
|
| 175 | - $this->realContainer->addService( |
|
| 176 | - new ServiceDefinition( |
|
| 177 | - 'service.call2', |
|
| 178 | - \DateTime::class, |
|
| 179 | - ['1970-01-01'], |
|
| 180 | - ['format' => ['Y-m-d']] |
|
| 181 | - ) |
|
| 182 | - ); |
|
| 183 | - |
|
| 184 | - $date = new \DateTime('1970-01-01'); |
|
| 185 | - $this->assertSame($date->format('m-d-Y'), $this->realContainer->get('service.call')->format('m-d-Y')); |
|
| 186 | - $this->assertSame($date->format('m-d-Y'), $this->realContainer->get('service.call2')->format('m-d-Y')); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - public function test_has() |
|
| 190 | - { |
|
| 191 | - $this->assertSame(false, $this->realContainer->has('fake')); |
|
| 192 | - $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
|
| 193 | - $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
|
| 194 | - |
|
| 195 | - $this->realContainer->addServiceInstance(new InstanceDefinition('instance.name', \DateTime::class)); |
|
| 196 | - $this->assertSame(true, $this->realContainer->has('instance.name')); |
|
| 197 | - } |
|
| 21 | + private const SERVICE_NAME = 'service.name'; |
|
| 22 | + private const DEFAULT_SERVICE_NANE = 'default.service.name'; |
|
| 23 | + |
|
| 24 | + /** @var ServiceContainer|MockObject */ |
|
| 25 | + private $container; |
|
| 26 | + |
|
| 27 | + /** @var ServiceContainer */ |
|
| 28 | + private $realContainer; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @throws DuplicatedServiceException |
|
| 32 | + */ |
|
| 33 | + protected function setUp() |
|
| 34 | + { |
|
| 35 | + $this->container = $this->getMockBuilder(ServiceContainer::class)->getMock(); |
|
| 36 | + $this->realContainer = new ServiceContainer(); |
|
| 37 | + $this->realContainer->addService(new ServiceDefinition(self::DEFAULT_SERVICE_NANE, \DateTime::class)); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + public function testConstructor() |
|
| 41 | + { |
|
| 42 | + $this->assertAttributeEquals([], 'serviceInstances', $this->container); |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * @covers \Wonderland\Container\ServiceContainer::addService |
|
| 47 | + * @throws DuplicatedServiceException |
|
| 48 | + */ |
|
| 49 | + public function test_AddService() |
|
| 50 | + { |
|
| 51 | + $definition = $this->getMockBuilder(ServiceDefinition::class) |
|
| 52 | + ->setConstructorArgs([self::SERVICE_NAME, \DateTime::class]) |
|
| 53 | + ->getMock(); |
|
| 54 | + ; |
|
| 55 | + |
|
| 56 | + $definition->expects($this->exactly(2)) |
|
| 57 | + ->method('getServiceName') |
|
| 58 | + ->willReturn(self::SERVICE_NAME); |
|
| 59 | + |
|
| 60 | + $this->assertSame($this->realContainer, $this->realContainer->addService($definition)); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @throws DuplicatedServiceException |
|
| 65 | + */ |
|
| 66 | + public function test_AddServiceException() |
|
| 67 | + { |
|
| 68 | + $this->expectException(DuplicatedServiceException::class); |
|
| 69 | + |
|
| 70 | + $definition = $this->getMockBuilder(ServiceDefinition::class) |
|
| 71 | + ->setConstructorArgs([self::SERVICE_NAME, \DateTime::class]) |
|
| 72 | + ->getMock(); |
|
| 73 | + ; |
|
| 74 | + |
|
| 75 | + $definition->expects($this->exactly(4)) |
|
| 76 | + ->method('getServiceName') |
|
| 77 | + ->willReturn(self::SERVICE_NAME); |
|
| 78 | + |
|
| 79 | + $this->realContainer->addService($definition); |
|
| 80 | + $this->realContainer->addService($definition); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + public function test_loadServices() |
|
| 84 | + { |
|
| 85 | + $definition = $this->getMockBuilder(ServiceDefinition::class) |
|
| 86 | + ->setConstructorArgs([self::SERVICE_NAME . '10', \DateTime::class]) |
|
| 87 | + ->getMock(); |
|
| 88 | + ; |
|
| 89 | + $definition2 = $this->getMockBuilder(ServiceDefinition::class) |
|
| 90 | + ->setConstructorArgs([self::SERVICE_NAME . '20', \DateTime::class]) |
|
| 91 | + ->getMock(); |
|
| 92 | + ; |
|
| 93 | + |
|
| 94 | + $list = [ |
|
| 95 | + $definition, |
|
| 96 | + $definition2 |
|
| 97 | + ]; |
|
| 98 | + |
|
| 99 | + $definition->expects($this->exactly(2)) |
|
| 100 | + ->method('getServiceName') |
|
| 101 | + ->willReturn(self::SERVICE_NAME . '10'); |
|
| 102 | + $definition2->expects($this->exactly(2)) |
|
| 103 | + ->method('getServiceName') |
|
| 104 | + ->willReturn(self::SERVICE_NAME . '20'); |
|
| 105 | + |
|
| 106 | + $this->realContainer->loadServices($list); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @throws DuplicatedServiceException |
|
| 111 | + */ |
|
| 112 | + public function test_AddServiceInstance() |
|
| 113 | + { |
|
| 114 | + $definition = $this->getMockBuilder(InstanceDefinition::class) |
|
| 115 | + ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
|
| 116 | + ->getMock(); |
|
| 117 | + |
|
| 118 | + $definition->expects($this->atLeast(2)) |
|
| 119 | + ->method('getServiceName') |
|
| 120 | + ->willReturn(self::SERVICE_NAME); |
|
| 121 | + |
|
| 122 | + $this->container->expects($this->once()) |
|
| 123 | + ->method('addServiceInstance') |
|
| 124 | + ->willReturn($this->container); |
|
| 125 | + |
|
| 126 | + $this->assertSame($this->container, $this->container->addServiceInstance($definition)); |
|
| 127 | + $this->assertSame($this->realContainer, $this->realContainer->addServiceInstance($definition)); |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * @throws DuplicatedServiceException |
|
| 132 | + */ |
|
| 133 | + public function test_AddServiceInstanceException() |
|
| 134 | + { |
|
| 135 | + $this->expectException(DuplicatedServiceException::class); |
|
| 136 | + |
|
| 137 | + $definition = $this->getMockBuilder(InstanceDefinition::class) |
|
| 138 | + ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
|
| 139 | + ->getMock(); |
|
| 140 | + |
|
| 141 | + $definition->expects($this->exactly(2)) |
|
| 142 | + ->method('getServiceName') |
|
| 143 | + ->willReturn(self::DEFAULT_SERVICE_NANE); |
|
| 144 | + |
|
| 145 | + $this->realContainer->addServiceInstance($definition); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + /** |
|
| 149 | + * @throws DuplicatedServiceException |
|
| 150 | + */ |
|
| 151 | + public function test_get() |
|
| 152 | + { |
|
| 153 | + $this->assertSame(null, $this->realContainer->get('test')); |
|
| 154 | + $this->assertSame(\DateTime::class, get_class($this->realContainer->get(self::DEFAULT_SERVICE_NANE))); |
|
| 155 | + $instance = $this->realContainer->get(self::DEFAULT_SERVICE_NANE); |
|
| 156 | + $this->assertSame(\DateTime::class, get_class($instance)); |
|
| 157 | + $this->assertSame($instance, $this->realContainer->get(self::DEFAULT_SERVICE_NANE)); |
|
| 158 | + |
|
| 159 | + $this->realContainer->addServiceInstance( |
|
| 160 | + new InstanceDefinition('call.format', 'd-m-Y') |
|
| 161 | + ); |
|
| 162 | + $this->realContainer->addServiceInstance( |
|
| 163 | + new InstanceDefinition('call.date', '1970-01-01') |
|
| 164 | + ); |
|
| 165 | + |
|
| 166 | + $this->realContainer->addService( |
|
| 167 | + new ServiceDefinition( |
|
| 168 | + 'service.call', |
|
| 169 | + \DateTime::class, |
|
| 170 | + ['@call.date'], |
|
| 171 | + ['format' => ['@call.format']] |
|
| 172 | + ) |
|
| 173 | + ); |
|
| 174 | + |
|
| 175 | + $this->realContainer->addService( |
|
| 176 | + new ServiceDefinition( |
|
| 177 | + 'service.call2', |
|
| 178 | + \DateTime::class, |
|
| 179 | + ['1970-01-01'], |
|
| 180 | + ['format' => ['Y-m-d']] |
|
| 181 | + ) |
|
| 182 | + ); |
|
| 183 | + |
|
| 184 | + $date = new \DateTime('1970-01-01'); |
|
| 185 | + $this->assertSame($date->format('m-d-Y'), $this->realContainer->get('service.call')->format('m-d-Y')); |
|
| 186 | + $this->assertSame($date->format('m-d-Y'), $this->realContainer->get('service.call2')->format('m-d-Y')); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + public function test_has() |
|
| 190 | + { |
|
| 191 | + $this->assertSame(false, $this->realContainer->has('fake')); |
|
| 192 | + $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
|
| 193 | + $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
|
| 194 | + |
|
| 195 | + $this->realContainer->addServiceInstance(new InstanceDefinition('instance.name', \DateTime::class)); |
|
| 196 | + $this->assertSame(true, $this->realContainer->has('instance.name')); |
|
| 197 | + } |
|
| 198 | 198 | |
| 199 | 199 | } |
@@ -13,134 +13,134 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class ConfigurationValidator |
| 15 | 15 | { |
| 16 | - private const ERROR_PREFIX = 'The service configuration'; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @param array $definitionList |
|
| 20 | - * @throws InvalidConfigFormatException |
|
| 21 | - */ |
|
| 22 | - public function validateDefinitions(array $definitionList) |
|
| 23 | - { |
|
| 24 | - $this->validateRoot($definitionList); |
|
| 25 | - |
|
| 26 | - if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) { |
|
| 27 | - return; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $service) { |
|
| 31 | - $this->validateService($serviceName, $service); |
|
| 32 | - } |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @param array $definitionList |
|
| 37 | - * @throws InvalidConfigFormatException |
|
| 38 | - */ |
|
| 39 | - private function validateRoot(array $definitionList) |
|
| 40 | - { |
|
| 41 | - if (false === array_key_exists(Fields::ROOT_CONFIG_NAME, $definitionList)) { |
|
| 42 | - throw new InvalidConfigFormatException( |
|
| 43 | - self::ERROR_PREFIX . ' need the root config key "' . Fields::ROOT_CONFIG_NAME . '"' |
|
| 44 | - ); |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - $field = $definitionList[Fields::ROOT_CONFIG_NAME]; |
|
| 48 | - if (false === is_array($field) && false === is_null($field)) { |
|
| 49 | - throw new InvalidConfigFormatException( |
|
| 50 | - self::ERROR_PREFIX . ' key "' . Fields::ROOT_CONFIG_NAME . '" must be an associative array' |
|
| 51 | - ); |
|
| 52 | - } |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - /** |
|
| 56 | - * @param $serviceName |
|
| 57 | - * @param $service |
|
| 58 | - * @throws InvalidConfigFormatException |
|
| 59 | - */ |
|
| 60 | - private function validateService($serviceName, $service) |
|
| 61 | - { |
|
| 62 | - if (false === is_array($service)) { |
|
| 63 | - throw new InvalidConfigFormatException( |
|
| 64 | - self::ERROR_PREFIX . ' key "' . $serviceName . '" must be an associative array' |
|
| 65 | - ); |
|
| 66 | - } |
|
| 67 | - |
|
| 68 | - $this->validateClass($service); |
|
| 69 | - $this->validateArguments($service); |
|
| 70 | - $this->validateMethods($service); |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @param $service |
|
| 75 | - * @throws InvalidConfigFormatException |
|
| 76 | - */ |
|
| 77 | - private function validateClass($service) |
|
| 78 | - { |
|
| 79 | - if (false === array_key_exists(Fields::CLASS_CONFIG_NAME, $service)) { |
|
| 80 | - throw new InvalidConfigFormatException( |
|
| 81 | - self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" is required' |
|
| 82 | - ); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - if (false === is_string($service[Fields::CLASS_CONFIG_NAME])) { |
|
| 86 | - throw new InvalidConfigFormatException( |
|
| 87 | - self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" must be a string' |
|
| 88 | - ); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * @param $service |
|
| 94 | - * @throws InvalidConfigFormatException |
|
| 95 | - */ |
|
| 96 | - private function validateArguments($service) |
|
| 97 | - { |
|
| 98 | - if (true === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $service) && |
|
| 99 | - false === is_array($service[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME]) |
|
| 100 | - ) { |
|
| 101 | - throw new InvalidConfigFormatException( |
|
| 102 | - self::ERROR_PREFIX . ' key "' . Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME . |
|
| 103 | - '" must be an array or null' |
|
| 104 | - ); |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * @param $service |
|
| 110 | - * @throws InvalidConfigFormatException |
|
| 111 | - */ |
|
| 112 | - private function validateMethods($service) |
|
| 113 | - { |
|
| 114 | - if (true === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service) && |
|
| 115 | - false === is_array($service[Fields::METHOD_CALLS_CONFIG_NAME]) |
|
| 116 | - ) { |
|
| 117 | - throw new InvalidConfigFormatException( |
|
| 118 | - self::ERROR_PREFIX . ' key "' . Fields::METHOD_CALLS_CONFIG_NAME . |
|
| 119 | - '" must be an array or null' |
|
| 120 | - ); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service)) { |
|
| 124 | - return; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - foreach ($service[Fields::METHOD_CALLS_CONFIG_NAME] as $methodName => $call) { |
|
| 128 | - $this->validateCalls($methodName, $call); |
|
| 129 | - } |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * @param $methodName |
|
| 134 | - * @param $call |
|
| 135 | - * @throws InvalidConfigFormatException |
|
| 136 | - */ |
|
| 137 | - private function validateCalls($methodName, $call) |
|
| 138 | - { |
|
| 139 | - if (false === is_array($call)) { |
|
| 140 | - throw new InvalidConfigFormatException( |
|
| 141 | - self::ERROR_PREFIX . ' key "' . $methodName . '" must be be an array' |
|
| 142 | - ); |
|
| 143 | - } |
|
| 144 | - } |
|
| 16 | + private const ERROR_PREFIX = 'The service configuration'; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @param array $definitionList |
|
| 20 | + * @throws InvalidConfigFormatException |
|
| 21 | + */ |
|
| 22 | + public function validateDefinitions(array $definitionList) |
|
| 23 | + { |
|
| 24 | + $this->validateRoot($definitionList); |
|
| 25 | + |
|
| 26 | + if (null === $definitionList[Fields::ROOT_CONFIG_NAME]) { |
|
| 27 | + return; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + foreach ($definitionList[Fields::ROOT_CONFIG_NAME] as $serviceName => $service) { |
|
| 31 | + $this->validateService($serviceName, $service); |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @param array $definitionList |
|
| 37 | + * @throws InvalidConfigFormatException |
|
| 38 | + */ |
|
| 39 | + private function validateRoot(array $definitionList) |
|
| 40 | + { |
|
| 41 | + if (false === array_key_exists(Fields::ROOT_CONFIG_NAME, $definitionList)) { |
|
| 42 | + throw new InvalidConfigFormatException( |
|
| 43 | + self::ERROR_PREFIX . ' need the root config key "' . Fields::ROOT_CONFIG_NAME . '"' |
|
| 44 | + ); |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + $field = $definitionList[Fields::ROOT_CONFIG_NAME]; |
|
| 48 | + if (false === is_array($field) && false === is_null($field)) { |
|
| 49 | + throw new InvalidConfigFormatException( |
|
| 50 | + self::ERROR_PREFIX . ' key "' . Fields::ROOT_CONFIG_NAME . '" must be an associative array' |
|
| 51 | + ); |
|
| 52 | + } |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + /** |
|
| 56 | + * @param $serviceName |
|
| 57 | + * @param $service |
|
| 58 | + * @throws InvalidConfigFormatException |
|
| 59 | + */ |
|
| 60 | + private function validateService($serviceName, $service) |
|
| 61 | + { |
|
| 62 | + if (false === is_array($service)) { |
|
| 63 | + throw new InvalidConfigFormatException( |
|
| 64 | + self::ERROR_PREFIX . ' key "' . $serviceName . '" must be an associative array' |
|
| 65 | + ); |
|
| 66 | + } |
|
| 67 | + |
|
| 68 | + $this->validateClass($service); |
|
| 69 | + $this->validateArguments($service); |
|
| 70 | + $this->validateMethods($service); |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @param $service |
|
| 75 | + * @throws InvalidConfigFormatException |
|
| 76 | + */ |
|
| 77 | + private function validateClass($service) |
|
| 78 | + { |
|
| 79 | + if (false === array_key_exists(Fields::CLASS_CONFIG_NAME, $service)) { |
|
| 80 | + throw new InvalidConfigFormatException( |
|
| 81 | + self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" is required' |
|
| 82 | + ); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + if (false === is_string($service[Fields::CLASS_CONFIG_NAME])) { |
|
| 86 | + throw new InvalidConfigFormatException( |
|
| 87 | + self::ERROR_PREFIX . ' key "' . Fields::CLASS_CONFIG_NAME . '" must be a string' |
|
| 88 | + ); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * @param $service |
|
| 94 | + * @throws InvalidConfigFormatException |
|
| 95 | + */ |
|
| 96 | + private function validateArguments($service) |
|
| 97 | + { |
|
| 98 | + if (true === array_key_exists(Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME, $service) && |
|
| 99 | + false === is_array($service[Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME]) |
|
| 100 | + ) { |
|
| 101 | + throw new InvalidConfigFormatException( |
|
| 102 | + self::ERROR_PREFIX . ' key "' . Fields::CONSTRUCTOR_PARAMS_CONFIG_NAME . |
|
| 103 | + '" must be an array or null' |
|
| 104 | + ); |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * @param $service |
|
| 110 | + * @throws InvalidConfigFormatException |
|
| 111 | + */ |
|
| 112 | + private function validateMethods($service) |
|
| 113 | + { |
|
| 114 | + if (true === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service) && |
|
| 115 | + false === is_array($service[Fields::METHOD_CALLS_CONFIG_NAME]) |
|
| 116 | + ) { |
|
| 117 | + throw new InvalidConfigFormatException( |
|
| 118 | + self::ERROR_PREFIX . ' key "' . Fields::METHOD_CALLS_CONFIG_NAME . |
|
| 119 | + '" must be an array or null' |
|
| 120 | + ); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + if (false === array_key_exists(Fields::METHOD_CALLS_CONFIG_NAME, $service)) { |
|
| 124 | + return; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + foreach ($service[Fields::METHOD_CALLS_CONFIG_NAME] as $methodName => $call) { |
|
| 128 | + $this->validateCalls($methodName, $call); |
|
| 129 | + } |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * @param $methodName |
|
| 134 | + * @param $call |
|
| 135 | + * @throws InvalidConfigFormatException |
|
| 136 | + */ |
|
| 137 | + private function validateCalls($methodName, $call) |
|
| 138 | + { |
|
| 139 | + if (false === is_array($call)) { |
|
| 140 | + throw new InvalidConfigFormatException( |
|
| 141 | + self::ERROR_PREFIX . ' key "' . $methodName . '" must be be an array' |
|
| 142 | + ); |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | 146 | } |