Skip to content

Commit

Permalink
feat: more tests in progress (#2)
Browse files Browse the repository at this point in the history
* feat: more tests in progress

* feat: more tests in progress

* feat: phpstan

* refactor: get parameter with default
  • Loading branch information
hschoenenberger authored Dec 24, 2024
1 parent d8fc064 commit 75eebcf
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 33 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down
23 changes: 6 additions & 17 deletions src/ServiceContainer/ServiceContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}

/**
Expand Down
104 changes: 89 additions & 15 deletions tests/src/ServiceContainer/ServiceContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/**
Expand Down Expand Up @@ -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();

Expand All @@ -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));
}
}
11 changes: 11 additions & 0 deletions tests/src/ServiceContainer/TestService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace PrestaShopCorp\LightweightContainer\Test\ServiceContainer;

class TestService
{
public function init()
{
// empty method
}
}
20 changes: 20 additions & 0 deletions tests/src/ServiceContainer/TestSingletonService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace PrestaShopCorp\LightweightContainer\Test\ServiceContainer;

use PrestaShopCorp\LightweightContainer\ServiceContainer\Contract\ISingletonService;
use PrestaShopCorp\LightweightContainer\ServiceContainer\ServiceContainer;

class TestSingletonService implements ISingletonService
{
private static $instance;

public static function getInstance(ServiceContainer $serviceContainer)
{
if (self::$instance === null) {
self::$instance = new TestSingletonService();
}

return self::$instance;
}
}

0 comments on commit 75eebcf

Please sign in to comment.