diff --git a/README.md b/README.md index f81bd2f..9f71f65 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ class MyModuleServiceContainer extends ServiceContainer { if (null === $this->logger) { $this->logger = LoggerFactory::create( - $this->getParameterWithDefault( + $this->getParameter( 'ps_accounts.log_level', LoggerFactory::ERROR ) diff --git a/src/ServiceContainer/ServiceContainer.php b/src/ServiceContainer/ServiceContainer.php index 9979058..2d25d2b 100644 --- a/src/ServiceContainer/ServiceContainer.php +++ b/src/ServiceContainer/ServiceContainer.php @@ -164,32 +164,21 @@ public function has($name) /** * @param string $name + * @param mixed $default * - * @return string + * @return mixed * * @throws ParameterNotFoundException */ - public function getParameter($name) + public function getParameter($name, $default = null) { if (array_key_exists($name, $this->config)) { return $this->config[$name]; } - throw new ParameterNotFoundException('Configuration parameter "' . $name . '" not found.'); - } - - /** - * @param string $name - * @param string $default - * - * @return string - */ - public function getParameterWithDefault($name, $default) - { - if (array_key_exists($name, $this->config)) { - return $this->config[$name]; + if (func_num_args() > 1) { + return $default; } - - return $default; + throw new ParameterNotFoundException('Configuration parameter "' . $name . '" not found.'); } /** diff --git a/tests/src/ServiceContainer/ServiceContainerTest.php b/tests/src/ServiceContainer/ServiceContainerTest.php index 40ee7e4..1176033 100644 --- a/tests/src/ServiceContainer/ServiceContainerTest.php +++ b/tests/src/ServiceContainer/ServiceContainerTest.php @@ -7,14 +7,6 @@ use PrestaShopCorp\LightweightContainer\ServiceContainer\Exception\ServiceNotFoundException; use PrestaShopCorp\LightweightContainer\ServiceContainer\ServiceContainer; -class TestService -{ - public function init() - { - // empty method - } -} - class ServiceContainerTest extends TestCase { /** @@ -50,19 +42,83 @@ public function itShouldThrowParameterNotFoundExceptionIfParameterNotFound() /** * @test */ - public function itShouldGetConfigurationParameter() + public function itShouldGetParameter() { + $this->assertTrue($this->container->hasParameter('log_level')); $this->assertEquals('DEBUG', $this->container->getParameter('log_level')); } + /** + * @test + */ + public function itShouldGetParameterDefaultValue() + { + $default = 'foo'; + + $this->assertFalse($this->container->hasParameter('not_found')); + $this->assertEquals($default, $this->container->getParameter('not_found', $default)); + } + + /** + * @test + */ + public function itShouldGetParameterDefaultNullValue() + { + $default = null; + + $this->assertFalse($this->container->hasParameter('not_found')); + $this->assertEquals($default, $this->container->getParameter('not_found', $default)); + } + + /** + * @test + */ + public function itShouldNotGetParameterDefaultValue() + { + $default = 'foo'; + + $this->assertTrue($this->container->hasParameter('log_level')); + $this->assertEquals('DEBUG', $this->container->getParameter('log_level', $default)); + } + + /** + * @test + */ + public function itShouldSetService() + { + $service = new TestService(); + $this->container->set(TestService::class, $service); + + $this->assertTrue($this->container->has(TestService::class)); + $this->assertSame($service, $this->container->get(TestService::class)); + } + + /** + * @test + */ + public function itShouldReplaceService() + { + $service = new TestService(); + $this->container->set(TestService::class, $service); + + $this->assertTrue($this->container->has(TestService::class)); + $this->assertSame($service, $this->container->get(TestService::class)); + + $service2 = new TestService(); + $this->container->set(TestService::class, $service2); + + $this->assertTrue($this->container->has(TestService::class)); + $this->assertSame($service2, $this->container->get(TestService::class)); + + $this->assertNotSame($service, $service2); + } + /** * @test */ public function itShouldInstantiateServiceOnlyOnce() { $service = $this->createMock(TestService::class); - $service->expects($this->once())->method('init'); - $this->container->registerProvider(TestService::class, static function () use ($service) { $service->init(); @@ -71,9 +127,27 @@ public function itShouldInstantiateServiceOnlyOnce() $this->assertTrue($this->container->hasProvider(TestService::class)); - $i = 0; - while ($i++ < 3) { - $this->assertInstanceOf(TestService::class, $this->container->get(TestService::class)); - } + $service->expects($this->once())->method('init'); + + $instance1 = $this->container->get(TestService::class); + $instance2 = $this->container->get(TestService::class); + + $this->assertTrue($this->container->has(TestService::class)); + + $this->assertInstanceOf(TestService::class, $instance1); + $this->assertInstanceOf(TestService::class, $instance2); + + $this->assertSame($instance1, $instance2); + } + + /** + * @test + */ + public function itShouldInstantiateSingletonService() + { + $instance = $this->container->get(TestSingletonService::class); + + $this->assertInstanceOf(TestSingletonService::class, $instance); + $this->assertTrue($this->container->has(TestSingletonService::class)); } } diff --git a/tests/src/ServiceContainer/TestService.php b/tests/src/ServiceContainer/TestService.php new file mode 100644 index 0000000..0cc00f7 --- /dev/null +++ b/tests/src/ServiceContainer/TestService.php @@ -0,0 +1,11 @@ +