diff --git a/.php_cs.dist.php b/.php_cs.dist.php new file mode 100644 index 0000000..25039fb --- /dev/null +++ b/.php_cs.dist.php @@ -0,0 +1,11 @@ +setUsingCache(false) + ->getFinder() + ->in(__DIR__) + ->exclude('vendor'); + +return $config; diff --git a/composer.json b/composer.json index 996459b..d7d3260 100644 --- a/composer.json +++ b/composer.json @@ -28,6 +28,9 @@ }, "scripts": { "phpunit": "./vendor/bin/phpunit --coverage-text", - "php-cs-fixer": "./vendor/bin/php-cs-fixer fix --config .php_cs.dist.php --diff" - } -} + "php-cs-fixer": "./vendor/bin/php-cs-fixer fix --config .php_cs.dist.php --diff", + "header-stamp": "./vendor/bin/header-stamp --target=\".\" --license=./vendor/prestashop/header-stamp/assets/afl.txt --exclude=.github,vendor,tests" + }, + "author": "PrestaShop", + "license": "AFL-3.0" +} \ No newline at end of file diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..6bccac6 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,17 @@ + + + + + src + + + + + + + + ./tests + + + + diff --git a/src/ServiceContainer/Contract/IContainerLogger.php b/src/ServiceContainer/Contract/IContainerLogger.php index 7d16056..ec681db 100644 --- a/src/ServiceContainer/Contract/IContainerLogger.php +++ b/src/ServiceContainer/Contract/IContainerLogger.php @@ -1,5 +1,24 @@ + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + namespace PrestaShopCorp\LightweightContainer\ServiceContainer\Contract; interface IContainerLogger @@ -31,4 +50,4 @@ public function warn($message); * @return mixed */ public function error($message); -} \ No newline at end of file +} diff --git a/src/ServiceContainer/Contract/IServiceProvider.php b/src/ServiceContainer/Contract/IServiceProvider.php index 8637241..4272930 100644 --- a/src/ServiceContainer/Contract/IServiceProvider.php +++ b/src/ServiceContainer/Contract/IServiceProvider.php @@ -1,4 +1,5 @@ registerProvider(MyService::class, static function () use ($container) { -// return new ServicesBillingClient( -// $container->getParameter('my_module.a_parameter'), -// $container->get(AaService::class), -// $container->get(BbService::class) -// ); -// }); + // $container->registerProvider(MyService::class, static function () use ($container) { + // return new ServicesBillingClient( + // $container->getParameter('my_module.a_parameter'), + // $container->get(AaService::class), + // $container->get(BbService::class) + // ); + // }); } } diff --git a/src/ServiceContainer/ServiceContainer.php b/src/ServiceContainer/ServiceContainer.php index 9d6507d..8b99c16 100644 --- a/src/ServiceContainer/ServiceContainer.php +++ b/src/ServiceContainer/ServiceContainer.php @@ -1,4 +1,5 @@ + * @copyright Since 2007 PrestaShop SA and Contributors + * @license https://opensource.org/licenses/AFL-3.0 Academic Free License version 3.0 + */ + return [ 'my_module.a_parameter' => 'my value example', 'my_module.log_level' => 'ERROR', diff --git a/tests/src/ServiceContainer/ServiceContainerTest.php b/tests/src/ServiceContainer/ServiceContainerTest.php index 42ce7a8..5c80364 100644 --- a/tests/src/ServiceContainer/ServiceContainerTest.php +++ b/tests/src/ServiceContainer/ServiceContainerTest.php @@ -3,14 +3,77 @@ namespace PrestaShopCorp\LightweightContainer\Test\ServiceContainer; use PHPUnit\Framework\TestCase; +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 { + /** + * @var ServiceContainer + */ + private $container; + + public function setUp(): void + { + $this->container = TestServiceContainer::createInstance(__DIR__ . '/config.php'); + } + + /** + * @test + */ + public function itShouldThrowServiceNotFoundExceptionIfProviderIsMissing() + { + $this->expectException(ServiceNotFoundException::class); + + $this->container->get(TestService::class); + } + /** * @test */ - public function itShouldTestSomething() + public function itShouldThrowParameterNotFoundExceptionIfParameterNotFound() { - // TODO: implement tests + $this->expectException(ParameterNotFoundException::class); + + $this->container->getParameter('foo.bar'); + } + + /** + * @test + */ + public function itShouldGetConfigurationParameter() + { + $this->assertEquals('DEBUG', $this->container->getParameter('log_level')); + } + + /** + * @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)); + } } } diff --git a/tests/src/ServiceContainer/TestLogger.php b/tests/src/ServiceContainer/TestLogger.php new file mode 100644 index 0000000..ae48f74 --- /dev/null +++ b/tests/src/ServiceContainer/TestLogger.php @@ -0,0 +1,28 @@ + 'DEBUG', +];