From d0fe7423bb7af78cc694ffe829f5859bd7e9d742 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Fri, 20 Dec 2024 09:32:06 +0100 Subject: [PATCH 1/4] feat: more tests in progress --- .../ServiceContainer/ServiceContainerTest.php | 95 +++++++++++++++---- tests/src/ServiceContainer/TestService.php | 11 +++ .../ServiceContainer/TestSingletonService.php | 19 ++++ 3 files changed, 109 insertions(+), 16 deletions(-) create mode 100644 tests/src/ServiceContainer/TestService.php create mode 100644 tests/src/ServiceContainer/TestSingletonService.php diff --git a/tests/src/ServiceContainer/ServiceContainerTest.php b/tests/src/ServiceContainer/ServiceContainerTest.php index 40ee7e4..5a34f69 100644 --- a/tests/src/ServiceContainer/ServiceContainerTest.php +++ b/tests/src/ServiceContainer/ServiceContainerTest.php @@ -3,18 +3,11 @@ namespace PrestaShopCorp\LightweightContainer\Test\ServiceContainer; use PHPUnit\Framework\TestCase; +use PrestaShopCorp\LightweightContainer\ServiceContainer\Contract\ISingletonService; use PrestaShopCorp\LightweightContainer\ServiceContainer\Exception\ParameterNotFoundException; use PrestaShopCorp\LightweightContainer\ServiceContainer\Exception\ServiceNotFoundException; use PrestaShopCorp\LightweightContainer\ServiceContainer\ServiceContainer; -class TestService -{ - public function init() - { - // empty method - } -} - class ServiceContainerTest extends TestCase { /** @@ -50,30 +43,100 @@ 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->getParameterWithDefault('not_found', $default)); + } + + /** + * @test + */ + public function itShouldNotGetParameterDefaultValue() + { + $default = 'foo'; + + $this->assertTrue($this->container->hasParameter('log_level')); + $this->assertEquals('DEBUG', $this->container->getParameterWithDefault('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(); - return $service; }); $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..d87e4e5 --- /dev/null +++ b/tests/src/ServiceContainer/TestService.php @@ -0,0 +1,11 @@ + Date: Fri, 20 Dec 2024 09:34:05 +0100 Subject: [PATCH 2/4] feat: more tests in progress --- tests/src/ServiceContainer/ServiceContainerTest.php | 2 +- tests/src/ServiceContainer/TestService.php | 2 +- tests/src/ServiceContainer/TestSingletonService.php | 5 +++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/src/ServiceContainer/ServiceContainerTest.php b/tests/src/ServiceContainer/ServiceContainerTest.php index 5a34f69..5058c00 100644 --- a/tests/src/ServiceContainer/ServiceContainerTest.php +++ b/tests/src/ServiceContainer/ServiceContainerTest.php @@ -3,7 +3,6 @@ namespace PrestaShopCorp\LightweightContainer\Test\ServiceContainer; use PHPUnit\Framework\TestCase; -use PrestaShopCorp\LightweightContainer\ServiceContainer\Contract\ISingletonService; use PrestaShopCorp\LightweightContainer\ServiceContainer\Exception\ParameterNotFoundException; use PrestaShopCorp\LightweightContainer\ServiceContainer\Exception\ServiceNotFoundException; use PrestaShopCorp\LightweightContainer\ServiceContainer\ServiceContainer; @@ -111,6 +110,7 @@ public function itShouldInstantiateServiceOnlyOnce() $service = $this->createMock(TestService::class); $this->container->registerProvider(TestService::class, static function () use ($service) { $service->init(); + return $service; }); diff --git a/tests/src/ServiceContainer/TestService.php b/tests/src/ServiceContainer/TestService.php index d87e4e5..0cc00f7 100644 --- a/tests/src/ServiceContainer/TestService.php +++ b/tests/src/ServiceContainer/TestService.php @@ -8,4 +8,4 @@ public function init() { // empty method } -} \ No newline at end of file +} diff --git a/tests/src/ServiceContainer/TestSingletonService.php b/tests/src/ServiceContainer/TestSingletonService.php index 8b23db1..1bb87b9 100644 --- a/tests/src/ServiceContainer/TestSingletonService.php +++ b/tests/src/ServiceContainer/TestSingletonService.php @@ -7,13 +7,14 @@ class TestSingletonService implements ISingletonService { - static private $instance = null; + private static $instance; public static function getInstance(ServiceContainer $serviceContainer) { if (self::$instance === null) { self::$instance = new TestSingletonService(); } + return self::$instance; } -} \ No newline at end of file +} From bb38ca23548b818bb4f96620315c4e11ba0ffa01 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Tue, 24 Dec 2024 09:20:52 +0100 Subject: [PATCH 3/4] feat: phpstan --- src/ServiceContainer/ServiceContainer.php | 23 +++++-------------- .../ServiceContainer/ServiceContainerTest.php | 15 ++++++++++-- 2 files changed, 19 insertions(+), 19 deletions(-) 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 5058c00..1176033 100644 --- a/tests/src/ServiceContainer/ServiceContainerTest.php +++ b/tests/src/ServiceContainer/ServiceContainerTest.php @@ -56,7 +56,18 @@ public function itShouldGetParameterDefaultValue() $default = 'foo'; $this->assertFalse($this->container->hasParameter('not_found')); - $this->assertEquals($default, $this->container->getParameterWithDefault('not_found', $default)); + $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)); } /** @@ -67,7 +78,7 @@ public function itShouldNotGetParameterDefaultValue() $default = 'foo'; $this->assertTrue($this->container->hasParameter('log_level')); - $this->assertEquals('DEBUG', $this->container->getParameterWithDefault('log_level', $default)); + $this->assertEquals('DEBUG', $this->container->getParameter('log_level', $default)); } /** From 6e4299ac771074361095c1cc16478aae57d34d68 Mon Sep 17 00:00:00 2001 From: hschoenenberger Date: Tue, 24 Dec 2024 09:22:26 +0100 Subject: [PATCH 4/4] refactor: get parameter with default --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 )