Skip to content

Commit

Permalink
chore: setup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hschoenenberger committed Dec 19, 2024
1 parent 5cfdd94 commit 254d568
Show file tree
Hide file tree
Showing 16 changed files with 199 additions and 14 deletions.
11 changes: 11 additions & 0 deletions .php_cs.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

$config = new PrestaShop\CodingStandards\CsFixer\Config();

$config
->setUsingCache(false)
->getFinder()
->in(__DIR__)
->exclude('vendor');

return $config;
9 changes: 6 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
17 changes: 17 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" backupStaticAttributes="false" bootstrap="vendor/autoload.php" colors="true" convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
<report>
<clover outputFile="clover.xml"/>
</report>
</coverage>
<testsuites>
<testsuite name="all">
<directory>./tests</directory>
</testsuite>
</testsuites>
<logging/>
</phpunit>
21 changes: 20 additions & 1 deletion src/ServiceContainer/Contract/IContainerLogger.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @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
Expand Down Expand Up @@ -31,4 +50,4 @@ public function warn($message);
* @return mixed
*/
public function error($message);
}
}
1 change: 1 addition & 0 deletions src/ServiceContainer/Contract/IServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
Expand Down
1 change: 1 addition & 0 deletions src/ServiceContainer/Contract/ISingletonService.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
Expand Down
15 changes: 8 additions & 7 deletions src/ServiceContainer/Provider/ExampleProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
Expand Down Expand Up @@ -32,12 +33,12 @@ class ExampleProvider implements IServiceProvider
*/
public function provide(ServiceContainer $container)
{
// $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)
// );
// });
// $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)
// );
// });
}
}
3 changes: 2 additions & 1 deletion src/ServiceContainer/ServiceContainer.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
Expand Down Expand Up @@ -87,7 +88,7 @@ public function loadConfig()
/**
* @return IContainerLogger
*/
public abstract function getLogger();
abstract public function getLogger();

/**
* @return void
Expand Down
19 changes: 19 additions & 0 deletions src/config.example.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
<?php

/**
* Copyright since 2007 PrestaShop SA and Contributors
* PrestaShop is an International Registered Trademark & Property of PrestaShop SA
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License version 3.0
* that is bundled with this package in the file LICENSE.md.
* It is also available through the world-wide-web at this URL:
* https://opensource.org/licenses/AFL-3.0
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @author PrestaShop SA and Contributors <[email protected]>
* @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',
Expand Down
67 changes: 65 additions & 2 deletions tests/src/ServiceContainer/ServiceContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
28 changes: 28 additions & 0 deletions tests/src/ServiceContainer/TestLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace PrestaShopCorp\LightweightContainer\Test\ServiceContainer;

use PrestaShopCorp\LightweightContainer\ServiceContainer\Contract\IContainerLogger;

class TestLogger implements IContainerLogger
{
public function debug($message)
{
// TODO: Implement debug() method.
}

public function info($message)
{
// TODO: Implement info() method.
}

public function warn($message)
{
// TODO: Implement warn() method.
}

public function error($message)
{
// TODO: Implement error() method.
}
}
13 changes: 13 additions & 0 deletions tests/src/ServiceContainer/TestServiceContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace PrestaShopCorp\LightweightContainer\Test\ServiceContainer;

use PrestaShopCorp\LightweightContainer\ServiceContainer\ServiceContainer;

class TestServiceContainer extends ServiceContainer
{
public function getLogger()
{
return new TestLogger();
}
}
5 changes: 5 additions & 0 deletions tests/src/ServiceContainer/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return [
'log_level' => 'DEBUG',
];

0 comments on commit 254d568

Please sign in to comment.