-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from gacela-project/feature/adding-unit-tests
Adding unit tests to Container and Locator
- Loading branch information
Showing
5 changed files
with
178 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GacelaTest\Fixtures; | ||
|
||
final class StringValue implements StringValueInterface | ||
{ | ||
private string $value = ''; | ||
|
||
public function setValue(string $value): void | ||
{ | ||
$this->value = $value; | ||
} | ||
|
||
public function value(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GacelaTest\Fixtures; | ||
|
||
interface StringValueInterface | ||
{ | ||
public function value(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GacelaTest\Unit\Framework\Container; | ||
|
||
use Gacela\Framework\Container\Container; | ||
use Gacela\Framework\Container\Exception\ContainerKeyNotFoundException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class ContainerTest extends TestCase | ||
{ | ||
private Container $container; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->container = new Container(); | ||
} | ||
|
||
public function test_get_non_existing_service(): void | ||
{ | ||
$this->expectException(ContainerKeyNotFoundException::class); | ||
$this->container->get('unknown-service_name'); | ||
} | ||
|
||
public function test_has_service(): void | ||
{ | ||
$this->container->set('service_name', 'value'); | ||
|
||
self::assertTrue($this->container->has('service_name')); | ||
self::assertFalse($this->container->has('unknown-service_name')); | ||
} | ||
|
||
public function test_remove_existing_service(): void | ||
{ | ||
$this->container->set('service_name', 'value'); | ||
$this->container->remove('service_name'); | ||
|
||
$this->expectException(ContainerKeyNotFoundException::class); | ||
$this->container->get('service_name'); | ||
} | ||
|
||
public function test_resolve_service_as_raw_string(): void | ||
{ | ||
$this->container->set('service_name', 'value'); | ||
|
||
$resolvedService = $this->container->get('service_name'); | ||
self::assertSame('value', $resolvedService); | ||
|
||
$cachedResolvedService = $this->container->get('service_name'); | ||
self::assertSame('value', $cachedResolvedService); | ||
} | ||
|
||
public function test_resolve_service_as_function(): void | ||
{ | ||
$this->container->set('service_name', static fn (): string => 'value'); | ||
|
||
$resolvedService = $this->container->get('service_name'); | ||
self::assertSame('value', $resolvedService); | ||
|
||
$cachedResolvedService = $this->container->get('service_name'); | ||
self::assertSame('value', $cachedResolvedService); | ||
} | ||
|
||
public function test_resolve_service_as_callable_class(): void | ||
{ | ||
$this->container->set('service_name', new class() { | ||
public function __invoke(): string | ||
{ | ||
return 'value'; | ||
} | ||
}); | ||
|
||
$resolvedService = $this->container->get('service_name'); | ||
self::assertSame('value', $resolvedService); | ||
|
||
$cachedResolvedService = $this->container->get('service_name'); | ||
self::assertSame('value', $cachedResolvedService); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace GacelaTest\Unit\Framework\Container; | ||
|
||
use Gacela\Framework\Container\Locator; | ||
use GacelaTest\Fixtures\StringValue; | ||
use GacelaTest\Fixtures\StringValueInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class LocatorTest extends TestCase | ||
{ | ||
private Locator $locator; | ||
|
||
public function setUp(): void | ||
{ | ||
Locator::resetInstance(); | ||
$this->locator = Locator::getInstance(); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
Locator::resetInstance(); | ||
} | ||
|
||
public function test_get_singleton_from_concrete_class(): void | ||
{ | ||
/** @var StringValue $stringValue */ | ||
$stringValue = $this->locator->get(StringValue::class); | ||
self::assertInstanceOf(StringValue::class, $stringValue); | ||
self::assertSame('', $stringValue->value()); | ||
$stringValue->setValue('updated value'); | ||
|
||
/** @var StringValue $stringValue2 */ | ||
$stringValue2 = $this->locator->get(StringValue::class); | ||
self::assertSame('updated value', $stringValue2->value()); | ||
} | ||
|
||
public function test_get_singleton_from_interface(): void | ||
{ | ||
/** @var StringValue $stringValue */ | ||
$stringValue = $this->locator->get(StringValueInterface::class); | ||
self::assertInstanceOf(StringValue::class, $stringValue); | ||
self::assertSame('', $stringValue->value()); | ||
$stringValue->setValue('updated value'); | ||
|
||
/** @var StringValue $stringValue2 */ | ||
$stringValue2 = $this->locator->get(StringValueInterface::class); | ||
self::assertSame('updated value', $stringValue2->value()); | ||
} | ||
|
||
public function test_get_singleton_from_string(): void | ||
{ | ||
/** @var string $stringValue */ | ||
$stringValue = $this->locator->get('NonExistingClass'); | ||
self::assertSame('NonExistingClass', $stringValue); | ||
} | ||
} |