@@ -10,10 +10,10 @@ |
||
| 10 | 10 | interface ServiceInstanceInterface |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** @return string */ |
|
| 14 | - public function getServiceName(); |
|
| 13 | + /** @return string */ |
|
| 14 | + public function getServiceName(); |
|
| 15 | 15 | |
| 16 | - /** @return mixed */ |
|
| 17 | - public function getInstance(); |
|
| 16 | + /** @return mixed */ |
|
| 17 | + public function getInstance(); |
|
| 18 | 18 | |
| 19 | 19 | } |
@@ -9,28 +9,28 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class InstanceDefinition implements ServiceInstanceInterface |
| 11 | 11 | { |
| 12 | - /** @var string */ |
|
| 13 | - private $serviceName; |
|
| 12 | + /** @var string */ |
|
| 13 | + private $serviceName; |
|
| 14 | 14 | |
| 15 | - /** @var mixed */ |
|
| 16 | - private $instance; |
|
| 15 | + /** @var mixed */ |
|
| 16 | + private $instance; |
|
| 17 | 17 | |
| 18 | - public function __construct(string $serviceName, $instance) |
|
| 19 | - { |
|
| 20 | - $this->serviceName = $serviceName; |
|
| 21 | - $this->instance = $instance; |
|
| 22 | - } |
|
| 18 | + public function __construct(string $serviceName, $instance) |
|
| 19 | + { |
|
| 20 | + $this->serviceName = $serviceName; |
|
| 21 | + $this->instance = $instance; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** @return string */ |
|
| 25 | - public function getServiceName() |
|
| 26 | - { |
|
| 27 | - return $this->serviceName; |
|
| 28 | - } |
|
| 24 | + /** @return string */ |
|
| 25 | + public function getServiceName() |
|
| 26 | + { |
|
| 27 | + return $this->serviceName; |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | - /** @return mixed */ |
|
| 31 | - public function getInstance() |
|
| 32 | - { |
|
| 33 | - return $this->instance; |
|
| 34 | - } |
|
| 30 | + /** @return mixed */ |
|
| 31 | + public function getInstance() |
|
| 32 | + { |
|
| 33 | + return $this->instance; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | 36 | } |
@@ -10,16 +10,16 @@ |
||
| 10 | 10 | interface ServiceDefinitionInterface |
| 11 | 11 | { |
| 12 | 12 | |
| 13 | - /** @return string */ |
|
| 14 | - public function getServiceName(); |
|
| 13 | + /** @return string */ |
|
| 14 | + public function getServiceName(); |
|
| 15 | 15 | |
| 16 | - /** @return string */ |
|
| 17 | - public function getClass(); |
|
| 16 | + /** @return string */ |
|
| 17 | + public function getClass(); |
|
| 18 | 18 | |
| 19 | - /** @return array */ |
|
| 20 | - public function getConstructArgs(); |
|
| 19 | + /** @return array */ |
|
| 20 | + public function getConstructArgs(); |
|
| 21 | 21 | |
| 22 | - /** @return array */ |
|
| 23 | - public function getCalls(); |
|
| 22 | + /** @return array */ |
|
| 23 | + public function getCalls(); |
|
| 24 | 24 | |
| 25 | 25 | } |
@@ -2,7 +2,7 @@ discard block |
||
| 2 | 2 | declare( strict_types = 1 ); |
| 3 | 3 | |
| 4 | 4 | if ( PHP_SAPI !== 'cli' ) { |
| 5 | - die( 'Not an entry point' ); |
|
| 5 | + die( 'Not an entry point' ); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | error_reporting( -1 ); |
@@ -10,7 +10,7 @@ discard block |
||
| 10 | 10 | ini_set( 'display_errors', '1' ); |
| 11 | 11 | |
| 12 | 12 | if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
| 13 | - die( 'You need to install this package with Composer before you can run the tests' ); |
|
| 13 | + die( 'You need to install this package with Composer before you can run the tests' ); |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | require_once __DIR__ . '/../vendor/autoload.php'; |
@@ -1,16 +1,16 @@ |
||
| 1 | 1 | <?php |
| 2 | -declare( strict_types = 1 ); |
|
| 2 | +declare(strict_types=1); |
|
| 3 | 3 | |
| 4 | -if ( PHP_SAPI !== 'cli' ) { |
|
| 5 | - die( 'Not an entry point' ); |
|
| 4 | +if (PHP_SAPI !== 'cli') { |
|
| 5 | + die('Not an entry point'); |
|
| 6 | 6 | } |
| 7 | 7 | |
| 8 | 8 | error_reporting( -1 ); |
| 9 | 9 | |
| 10 | -ini_set( 'display_errors', '1' ); |
|
| 10 | +ini_set('display_errors', '1'); |
|
| 11 | 11 | |
| 12 | -if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) { |
|
| 13 | - die( 'You need to install this package with Composer before you can run the tests' ); |
|
| 12 | +if (!is_readable(__DIR__ . '/../vendor/autoload.php')) { |
|
| 13 | + die('You need to install this package with Composer before you can run the tests'); |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | require_once __DIR__ . '/../vendor/autoload.php'; |
@@ -9,62 +9,62 @@ |
||
| 9 | 9 | */ |
| 10 | 10 | class ServiceDefinition implements ServiceDefinitionInterface |
| 11 | 11 | { |
| 12 | - /** @var string */ |
|
| 13 | - private $serviceName; |
|
| 12 | + /** @var string */ |
|
| 13 | + private $serviceName; |
|
| 14 | 14 | |
| 15 | - /** @var string */ |
|
| 16 | - private $class; |
|
| 15 | + /** @var string */ |
|
| 16 | + private $class; |
|
| 17 | 17 | |
| 18 | - /** @var array */ |
|
| 19 | - private $constructArgs; |
|
| 18 | + /** @var array */ |
|
| 19 | + private $constructArgs; |
|
| 20 | 20 | |
| 21 | - /** @var array */ |
|
| 22 | - private $calls; |
|
| 21 | + /** @var array */ |
|
| 22 | + private $calls; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * RepositoryFactory constructor. |
|
| 26 | - * |
|
| 27 | - * @param string $serviceName |
|
| 28 | - * @param string $class |
|
| 29 | - * @param array $args |
|
| 30 | - * @param array $calls |
|
| 31 | - */ |
|
| 32 | - public function __construct(string $serviceName, string $class, array $args = [], array $calls = []) |
|
| 33 | - { |
|
| 34 | - $this->serviceName = $serviceName; |
|
| 35 | - $this->class = $class; |
|
| 36 | - $this->constructArgs = $args; |
|
| 37 | - $this->calls = $calls; |
|
| 38 | - } |
|
| 24 | + /** |
|
| 25 | + * RepositoryFactory constructor. |
|
| 26 | + * |
|
| 27 | + * @param string $serviceName |
|
| 28 | + * @param string $class |
|
| 29 | + * @param array $args |
|
| 30 | + * @param array $calls |
|
| 31 | + */ |
|
| 32 | + public function __construct(string $serviceName, string $class, array $args = [], array $calls = []) |
|
| 33 | + { |
|
| 34 | + $this->serviceName = $serviceName; |
|
| 35 | + $this->class = $class; |
|
| 36 | + $this->constructArgs = $args; |
|
| 37 | + $this->calls = $calls; |
|
| 38 | + } |
|
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * @return string |
|
| 42 | - */ |
|
| 43 | - public function getClass() |
|
| 44 | - { |
|
| 45 | - return $this->class; |
|
| 46 | - } |
|
| 40 | + /** |
|
| 41 | + * @return string |
|
| 42 | + */ |
|
| 43 | + public function getClass() |
|
| 44 | + { |
|
| 45 | + return $this->class; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** @return string */ |
|
| 49 | - public function getServiceName() |
|
| 50 | - { |
|
| 51 | - return $this->serviceName; |
|
| 52 | - } |
|
| 48 | + /** @return string */ |
|
| 49 | + public function getServiceName() |
|
| 50 | + { |
|
| 51 | + return $this->serviceName; |
|
| 52 | + } |
|
| 53 | 53 | |
| 54 | - /** |
|
| 55 | - * @return array |
|
| 56 | - */ |
|
| 57 | - public function getConstructArgs() |
|
| 58 | - { |
|
| 59 | - return $this->constructArgs; |
|
| 60 | - } |
|
| 54 | + /** |
|
| 55 | + * @return array |
|
| 56 | + */ |
|
| 57 | + public function getConstructArgs() |
|
| 58 | + { |
|
| 59 | + return $this->constructArgs; |
|
| 60 | + } |
|
| 61 | 61 | |
| 62 | - /** |
|
| 63 | - * @return array |
|
| 64 | - */ |
|
| 65 | - public function getCalls() |
|
| 66 | - { |
|
| 67 | - return $this->calls; |
|
| 68 | - } |
|
| 62 | + /** |
|
| 63 | + * @return array |
|
| 64 | + */ |
|
| 65 | + public function getCalls() |
|
| 66 | + { |
|
| 67 | + return $this->calls; |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | 70 | } |
@@ -14,158 +14,158 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | class ServiceContainer implements ContainerInterface |
| 16 | 16 | { |
| 17 | - /** @var ServiceDefinitionInterface[] */ |
|
| 18 | - private $services; |
|
| 19 | - |
|
| 20 | - /** @var array */ |
|
| 21 | - private $serviceInstances; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * ServiceContainer constructor. |
|
| 25 | - */ |
|
| 26 | - public function __construct() |
|
| 27 | - { |
|
| 28 | - $this->serviceInstances = []; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @param ServiceDefinitionInterface $serviceDefinition |
|
| 33 | - * @return ServiceContainer |
|
| 34 | - */ |
|
| 35 | - public function addService(ServiceDefinitionInterface $serviceDefinition) |
|
| 36 | - { |
|
| 37 | - $this->services[$serviceDefinition->getServiceName()] = $serviceDefinition; |
|
| 38 | - |
|
| 39 | - return $this; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @param ServiceInstanceInterface $definition |
|
| 44 | - * @return ServiceContainer |
|
| 45 | - * @throws DuplicatedServiceException |
|
| 46 | - */ |
|
| 47 | - public function addServiceInstance(ServiceInstanceInterface $definition) |
|
| 48 | - { |
|
| 49 | - if (true === array_key_exists($definition->getServiceName(), $this->serviceInstances)) { |
|
| 50 | - throw new DuplicatedServiceException( |
|
| 51 | - 'The service' . $definition->getServiceName() . ' is already registered in the container' |
|
| 52 | - ); |
|
| 53 | - } |
|
| 54 | - |
|
| 55 | - $this->serviceInstances[$definition->getServiceName()] = $definition->getInstance(); |
|
| 56 | - |
|
| 57 | - return $this; |
|
| 58 | - } |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * @param string $index |
|
| 62 | - * @param bool $new |
|
| 63 | - * @return mixed|null |
|
| 64 | - */ |
|
| 65 | - public function get($index, $new = false) |
|
| 66 | - { |
|
| 67 | - // if service is shared true and already exists we return it |
|
| 68 | - if (isset($this->serviceInstances[$index]) && false === $new) { |
|
| 69 | - return $this->serviceInstances[$index]; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - // is service not defined return null |
|
| 73 | - if (!isset($this->services[$index])) { |
|
| 74 | - return null; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - // we create a new instance |
|
| 78 | - $instance = $this->create($this->services[$index]); |
|
| 79 | - |
|
| 80 | - // if shared true we set it in the current instances |
|
| 81 | - if (false === $new) { |
|
| 82 | - $this->serviceInstances[$index] = $instance; |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - return $instance; |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - /** |
|
| 89 | - * Returns true if the container can return an entry for the given identifier. |
|
| 90 | - * Returns false otherwise. |
|
| 91 | - * |
|
| 92 | - * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
| 93 | - * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
| 94 | - * |
|
| 95 | - * @param string $index Identifier of the entry to look for. |
|
| 96 | - * |
|
| 97 | - * @return bool |
|
| 98 | - */ |
|
| 99 | - public function has($index) |
|
| 100 | - { |
|
| 101 | - // if service is shared true and already exists we return it |
|
| 102 | - if (isset($this->serviceInstances[$index])) { |
|
| 103 | - return true; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - // is service not defined return null |
|
| 107 | - if (isset($this->services[$index])) { |
|
| 108 | - return true; |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - return false; |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - /** |
|
| 115 | - * @param ServiceDefinitionInterface $serviceDefinition |
|
| 116 | - * @return mixed |
|
| 117 | - */ |
|
| 118 | - private function create(ServiceDefinitionInterface $serviceDefinition) |
|
| 119 | - { |
|
| 120 | - // we get the class name in a var for the new later |
|
| 121 | - $newClass = $serviceDefinition->getClass(); |
|
| 122 | - |
|
| 123 | - // we get the construct args |
|
| 124 | - $args = $this->checkArgsServices($serviceDefinition->getConstructArgs()); |
|
| 125 | - $instance = new $newClass(...$args); |
|
| 126 | - |
|
| 127 | - // we call the injection methods after we instance the object |
|
| 128 | - $calls = $this->checkCallsServices($serviceDefinition->getCalls()); |
|
| 129 | - foreach ($calls as $call => $params) { |
|
| 130 | - call_user_func_array([$instance, $call], $params); |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - // we create the new instance |
|
| 134 | - return $instance; |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - /** |
|
| 138 | - * @param array $args |
|
| 139 | - * @return array |
|
| 140 | - */ |
|
| 141 | - private function checkArgsServices(array $args) |
|
| 142 | - { |
|
| 143 | - // we check if any of the construct args are services themself and we create them |
|
| 144 | - foreach ($args as $k => $arg) { |
|
| 145 | - if (true === $this->has($arg)) { |
|
| 146 | - $args[$k] = $this->get($arg); |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - return $args; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * @param array $calls |
|
| 155 | - * @return array |
|
| 156 | - */ |
|
| 157 | - private function checkCallsServices(array $calls) |
|
| 158 | - { |
|
| 159 | - // we check if any of the calls args are services themself and we create them |
|
| 160 | - foreach ($calls as $callArgs) { |
|
| 161 | - foreach ($callArgs as $k => $arg) { |
|
| 162 | - if (true === $this->has($arg)) { |
|
| 163 | - $args[$k] = $this->get($arg); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return $calls; |
|
| 169 | - } |
|
| 17 | + /** @var ServiceDefinitionInterface[] */ |
|
| 18 | + private $services; |
|
| 19 | + |
|
| 20 | + /** @var array */ |
|
| 21 | + private $serviceInstances; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * ServiceContainer constructor. |
|
| 25 | + */ |
|
| 26 | + public function __construct() |
|
| 27 | + { |
|
| 28 | + $this->serviceInstances = []; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @param ServiceDefinitionInterface $serviceDefinition |
|
| 33 | + * @return ServiceContainer |
|
| 34 | + */ |
|
| 35 | + public function addService(ServiceDefinitionInterface $serviceDefinition) |
|
| 36 | + { |
|
| 37 | + $this->services[$serviceDefinition->getServiceName()] = $serviceDefinition; |
|
| 38 | + |
|
| 39 | + return $this; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @param ServiceInstanceInterface $definition |
|
| 44 | + * @return ServiceContainer |
|
| 45 | + * @throws DuplicatedServiceException |
|
| 46 | + */ |
|
| 47 | + public function addServiceInstance(ServiceInstanceInterface $definition) |
|
| 48 | + { |
|
| 49 | + if (true === array_key_exists($definition->getServiceName(), $this->serviceInstances)) { |
|
| 50 | + throw new DuplicatedServiceException( |
|
| 51 | + 'The service' . $definition->getServiceName() . ' is already registered in the container' |
|
| 52 | + ); |
|
| 53 | + } |
|
| 54 | + |
|
| 55 | + $this->serviceInstances[$definition->getServiceName()] = $definition->getInstance(); |
|
| 56 | + |
|
| 57 | + return $this; |
|
| 58 | + } |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * @param string $index |
|
| 62 | + * @param bool $new |
|
| 63 | + * @return mixed|null |
|
| 64 | + */ |
|
| 65 | + public function get($index, $new = false) |
|
| 66 | + { |
|
| 67 | + // if service is shared true and already exists we return it |
|
| 68 | + if (isset($this->serviceInstances[$index]) && false === $new) { |
|
| 69 | + return $this->serviceInstances[$index]; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + // is service not defined return null |
|
| 73 | + if (!isset($this->services[$index])) { |
|
| 74 | + return null; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + // we create a new instance |
|
| 78 | + $instance = $this->create($this->services[$index]); |
|
| 79 | + |
|
| 80 | + // if shared true we set it in the current instances |
|
| 81 | + if (false === $new) { |
|
| 82 | + $this->serviceInstances[$index] = $instance; |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + return $instance; |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + /** |
|
| 89 | + * Returns true if the container can return an entry for the given identifier. |
|
| 90 | + * Returns false otherwise. |
|
| 91 | + * |
|
| 92 | + * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
|
| 93 | + * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
|
| 94 | + * |
|
| 95 | + * @param string $index Identifier of the entry to look for. |
|
| 96 | + * |
|
| 97 | + * @return bool |
|
| 98 | + */ |
|
| 99 | + public function has($index) |
|
| 100 | + { |
|
| 101 | + // if service is shared true and already exists we return it |
|
| 102 | + if (isset($this->serviceInstances[$index])) { |
|
| 103 | + return true; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + // is service not defined return null |
|
| 107 | + if (isset($this->services[$index])) { |
|
| 108 | + return true; |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + return false; |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + /** |
|
| 115 | + * @param ServiceDefinitionInterface $serviceDefinition |
|
| 116 | + * @return mixed |
|
| 117 | + */ |
|
| 118 | + private function create(ServiceDefinitionInterface $serviceDefinition) |
|
| 119 | + { |
|
| 120 | + // we get the class name in a var for the new later |
|
| 121 | + $newClass = $serviceDefinition->getClass(); |
|
| 122 | + |
|
| 123 | + // we get the construct args |
|
| 124 | + $args = $this->checkArgsServices($serviceDefinition->getConstructArgs()); |
|
| 125 | + $instance = new $newClass(...$args); |
|
| 126 | + |
|
| 127 | + // we call the injection methods after we instance the object |
|
| 128 | + $calls = $this->checkCallsServices($serviceDefinition->getCalls()); |
|
| 129 | + foreach ($calls as $call => $params) { |
|
| 130 | + call_user_func_array([$instance, $call], $params); |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + // we create the new instance |
|
| 134 | + return $instance; |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + /** |
|
| 138 | + * @param array $args |
|
| 139 | + * @return array |
|
| 140 | + */ |
|
| 141 | + private function checkArgsServices(array $args) |
|
| 142 | + { |
|
| 143 | + // we check if any of the construct args are services themself and we create them |
|
| 144 | + foreach ($args as $k => $arg) { |
|
| 145 | + if (true === $this->has($arg)) { |
|
| 146 | + $args[$k] = $this->get($arg); |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + return $args; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * @param array $calls |
|
| 155 | + * @return array |
|
| 156 | + */ |
|
| 157 | + private function checkCallsServices(array $calls) |
|
| 158 | + { |
|
| 159 | + // we check if any of the calls args are services themself and we create them |
|
| 160 | + foreach ($calls as $callArgs) { |
|
| 161 | + foreach ($callArgs as $k => $arg) { |
|
| 162 | + if (true === $this->has($arg)) { |
|
| 163 | + $args[$k] = $this->get($arg); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return $calls; |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | 171 | } |
@@ -18,119 +18,119 @@ |
||
| 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 | - protected function setUp() |
|
| 31 | - { |
|
| 32 | - $this->container = $this->getMockBuilder(ServiceContainer::class)->getMock(); |
|
| 33 | - $this->realContainer = new ServiceContainer(); |
|
| 34 | - $this->realContainer->addService(new ServiceDefinition(self::DEFAULT_SERVICE_NANE, \DateTime::class)); |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - public function testConstructor() |
|
| 38 | - { |
|
| 39 | - $this->assertAttributeEquals([], 'serviceInstances', $this->container); |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @covers \Wonderland\Container\ServiceContainer::addService |
|
| 44 | - */ |
|
| 45 | - public function test_AddService() |
|
| 46 | - { |
|
| 47 | - $definition = $this->getMockBuilder(ServiceDefinition::class) |
|
| 48 | - ->setConstructorArgs([self::SERVICE_NAME, \DateTime::class]) |
|
| 49 | - ->getMock(); |
|
| 50 | - ; |
|
| 51 | - |
|
| 52 | - $definition->expects($this->once()) |
|
| 53 | - ->method('getServiceName') |
|
| 54 | - ->willReturn(self::SERVICE_NAME); |
|
| 55 | - |
|
| 56 | - $this->container->expects($this->once()) |
|
| 57 | - ->method('addService') |
|
| 58 | - ->willReturn($this->container); |
|
| 59 | - |
|
| 60 | - $this->assertSame($this->container, $this->container->addService($definition)); |
|
| 61 | - $this->assertSame($this->realContainer, $this->realContainer->addService($definition)); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @throws DuplicatedServiceException |
|
| 66 | - */ |
|
| 67 | - public function test_AddServiceInstance() |
|
| 68 | - { |
|
| 69 | - $definition = $this->getMockBuilder(InstanceDefinition::class) |
|
| 70 | - ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
|
| 71 | - ->getMock(); |
|
| 72 | - |
|
| 73 | - $definition->expects($this->atLeast(2)) |
|
| 74 | - ->method('getServiceName') |
|
| 75 | - ->willReturn(self::SERVICE_NAME); |
|
| 76 | - |
|
| 77 | - $this->container->expects($this->once()) |
|
| 78 | - ->method('addServiceInstance') |
|
| 79 | - ->willReturn($this->container); |
|
| 80 | - |
|
| 81 | - $this->assertSame($this->container, $this->container->addServiceInstance($definition)); |
|
| 82 | - $this->assertSame($this->realContainer, $this->realContainer->addServiceInstance($definition)); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * @throws DuplicatedServiceException |
|
| 87 | - */ |
|
| 88 | - public function test_AddServiceInstanceException() |
|
| 89 | - { |
|
| 90 | - $this->expectException(DuplicatedServiceException::class); |
|
| 91 | - |
|
| 92 | - $definition = $this->getMockBuilder(InstanceDefinition::class) |
|
| 93 | - ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
|
| 94 | - ->getMock(); |
|
| 95 | - |
|
| 96 | - $definition->expects($this->atLeast(2)) |
|
| 97 | - ->method('getServiceName') |
|
| 98 | - ->willReturn(self::SERVICE_NAME); |
|
| 99 | - |
|
| 100 | - $this->realContainer->addServiceInstance($definition); |
|
| 101 | - $this->realContainer->addServiceInstance($definition); |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - public function test_get() |
|
| 105 | - { |
|
| 106 | - $this->assertSame(null, $this->realContainer->get('test')); |
|
| 107 | - $this->assertSame(\DateTime::class, get_class($this->realContainer->get(self::DEFAULT_SERVICE_NANE))); |
|
| 108 | - $instance = $this->realContainer->get(self::DEFAULT_SERVICE_NANE); |
|
| 109 | - $this->assertSame(\DateTime::class, get_class($instance)); |
|
| 110 | - $this->assertSame($instance, $this->realContainer->get(self::DEFAULT_SERVICE_NANE)); |
|
| 111 | - |
|
| 112 | - $this->realContainer->addServiceInstance( |
|
| 113 | - new InstanceDefinition('call.format', 'd-m-Y') |
|
| 114 | - ); |
|
| 115 | - |
|
| 116 | - $this->realContainer->addService( |
|
| 117 | - new ServiceDefinition( |
|
| 118 | - 'service.call', |
|
| 119 | - \DateTime::class, |
|
| 120 | - ['1970-01-01'], |
|
| 121 | - ['format' => ['call.format']] |
|
| 122 | - ) |
|
| 123 | - ); |
|
| 124 | - |
|
| 125 | - $date = new \DateTime('1970-01-01'); |
|
| 126 | - $this->assertSame($date->format('m-d-Y'), $this->realContainer->get('service.call')->format('m-d-Y')); |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - public function test_has() |
|
| 130 | - { |
|
| 131 | - $this->assertSame(false, $this->realContainer->has('fake')); |
|
| 132 | - $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
|
| 133 | - $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
|
| 134 | - } |
|
| 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 | + protected function setUp() |
|
| 31 | + { |
|
| 32 | + $this->container = $this->getMockBuilder(ServiceContainer::class)->getMock(); |
|
| 33 | + $this->realContainer = new ServiceContainer(); |
|
| 34 | + $this->realContainer->addService(new ServiceDefinition(self::DEFAULT_SERVICE_NANE, \DateTime::class)); |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + public function testConstructor() |
|
| 38 | + { |
|
| 39 | + $this->assertAttributeEquals([], 'serviceInstances', $this->container); |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @covers \Wonderland\Container\ServiceContainer::addService |
|
| 44 | + */ |
|
| 45 | + public function test_AddService() |
|
| 46 | + { |
|
| 47 | + $definition = $this->getMockBuilder(ServiceDefinition::class) |
|
| 48 | + ->setConstructorArgs([self::SERVICE_NAME, \DateTime::class]) |
|
| 49 | + ->getMock(); |
|
| 50 | + ; |
|
| 51 | + |
|
| 52 | + $definition->expects($this->once()) |
|
| 53 | + ->method('getServiceName') |
|
| 54 | + ->willReturn(self::SERVICE_NAME); |
|
| 55 | + |
|
| 56 | + $this->container->expects($this->once()) |
|
| 57 | + ->method('addService') |
|
| 58 | + ->willReturn($this->container); |
|
| 59 | + |
|
| 60 | + $this->assertSame($this->container, $this->container->addService($definition)); |
|
| 61 | + $this->assertSame($this->realContainer, $this->realContainer->addService($definition)); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @throws DuplicatedServiceException |
|
| 66 | + */ |
|
| 67 | + public function test_AddServiceInstance() |
|
| 68 | + { |
|
| 69 | + $definition = $this->getMockBuilder(InstanceDefinition::class) |
|
| 70 | + ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
|
| 71 | + ->getMock(); |
|
| 72 | + |
|
| 73 | + $definition->expects($this->atLeast(2)) |
|
| 74 | + ->method('getServiceName') |
|
| 75 | + ->willReturn(self::SERVICE_NAME); |
|
| 76 | + |
|
| 77 | + $this->container->expects($this->once()) |
|
| 78 | + ->method('addServiceInstance') |
|
| 79 | + ->willReturn($this->container); |
|
| 80 | + |
|
| 81 | + $this->assertSame($this->container, $this->container->addServiceInstance($definition)); |
|
| 82 | + $this->assertSame($this->realContainer, $this->realContainer->addServiceInstance($definition)); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * @throws DuplicatedServiceException |
|
| 87 | + */ |
|
| 88 | + public function test_AddServiceInstanceException() |
|
| 89 | + { |
|
| 90 | + $this->expectException(DuplicatedServiceException::class); |
|
| 91 | + |
|
| 92 | + $definition = $this->getMockBuilder(InstanceDefinition::class) |
|
| 93 | + ->setConstructorArgs([self::SERVICE_NAME, new \DateTime()]) |
|
| 94 | + ->getMock(); |
|
| 95 | + |
|
| 96 | + $definition->expects($this->atLeast(2)) |
|
| 97 | + ->method('getServiceName') |
|
| 98 | + ->willReturn(self::SERVICE_NAME); |
|
| 99 | + |
|
| 100 | + $this->realContainer->addServiceInstance($definition); |
|
| 101 | + $this->realContainer->addServiceInstance($definition); |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + public function test_get() |
|
| 105 | + { |
|
| 106 | + $this->assertSame(null, $this->realContainer->get('test')); |
|
| 107 | + $this->assertSame(\DateTime::class, get_class($this->realContainer->get(self::DEFAULT_SERVICE_NANE))); |
|
| 108 | + $instance = $this->realContainer->get(self::DEFAULT_SERVICE_NANE); |
|
| 109 | + $this->assertSame(\DateTime::class, get_class($instance)); |
|
| 110 | + $this->assertSame($instance, $this->realContainer->get(self::DEFAULT_SERVICE_NANE)); |
|
| 111 | + |
|
| 112 | + $this->realContainer->addServiceInstance( |
|
| 113 | + new InstanceDefinition('call.format', 'd-m-Y') |
|
| 114 | + ); |
|
| 115 | + |
|
| 116 | + $this->realContainer->addService( |
|
| 117 | + new ServiceDefinition( |
|
| 118 | + 'service.call', |
|
| 119 | + \DateTime::class, |
|
| 120 | + ['1970-01-01'], |
|
| 121 | + ['format' => ['call.format']] |
|
| 122 | + ) |
|
| 123 | + ); |
|
| 124 | + |
|
| 125 | + $date = new \DateTime('1970-01-01'); |
|
| 126 | + $this->assertSame($date->format('m-d-Y'), $this->realContainer->get('service.call')->format('m-d-Y')); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + public function test_has() |
|
| 130 | + { |
|
| 131 | + $this->assertSame(false, $this->realContainer->has('fake')); |
|
| 132 | + $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
|
| 133 | + $this->assertSame(true, $this->realContainer->has(self::DEFAULT_SERVICE_NANE)); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | 136 | } |