Skip to content

Commit 1351bda

Browse files
committed
Creating ServiceContainerTest
1 parent 29b51e4 commit 1351bda

File tree

7 files changed

+181
-13
lines changed

7 files changed

+181
-13
lines changed

tests/ContainerTest.php

Lines changed: 0 additions & 13 deletions
This file was deleted.

tests/ExampleNamespace/ClassA.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace Webdevcave\Yadic\Tests\ExampleNamespace;
4+
5+
use Webdevcave\Yadic\Annotations\Provides;
6+
use Webdevcave\Yadic\Annotations\Singleton;
7+
8+
#[Singleton]
9+
#[Provides(InterfaceA::class)]
10+
#[Provides('testAlias')]
11+
class ClassA implements InterfaceA
12+
{
13+
use CountsInstancesTrait;
14+
15+
public function __construct()
16+
{
17+
static::increaseCounter();
18+
}
19+
20+
public function funcWithoutParameters(): bool
21+
{
22+
return true;
23+
}
24+
25+
public function funcWithDefaultParameter(int $x = 1): bool
26+
{
27+
return true;
28+
}
29+
30+
public function funcWithParameters(int $x, ClassC $c): bool
31+
{
32+
return true;
33+
}
34+
}

tests/ExampleNamespace/ClassB.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Webdevcave\Yadic\Tests\ExampleNamespace;
4+
5+
class ClassB
6+
{
7+
use CountsInstancesTrait;
8+
9+
public function __construct(
10+
public ClassA $a,
11+
public int $count = 2
12+
)
13+
{
14+
static::increaseCounter();
15+
}
16+
}

tests/ExampleNamespace/ClassC.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Webdevcave\Yadic\Tests\ExampleNamespace;
4+
5+
class ClassC
6+
{
7+
use CountsInstancesTrait;
8+
9+
public function __construct(
10+
public ClassA $a,
11+
public ClassB $b
12+
)
13+
{
14+
static::increaseCounter();
15+
}
16+
17+
public function sayHello()
18+
{
19+
echo "Hello World!";
20+
}
21+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Webdevcave\Yadic\Tests\ExampleNamespace;
4+
5+
trait CountsInstancesTrait
6+
{
7+
public static int $instancesCounter = 0;
8+
9+
public static function increaseCounter(): void
10+
{
11+
static::$instancesCounter ++;
12+
}
13+
14+
public static function resetCounter(): void
15+
{
16+
static::$instancesCounter = 0;
17+
}
18+
}

tests/ExampleNamespace/InterfaceA.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Webdevcave\Yadic\Tests\ExampleNamespace;
4+
5+
interface InterfaceA
6+
{
7+
8+
}

tests/ServiceContainerTest.php

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace Webdevcave\Yadic\Tests;
4+
5+
use Exception;
6+
use PHPUnit\Framework\TestCase;
7+
use Webdevcave\Yadic\ServiceContainer;
8+
use Webdevcave\Yadic\Tests\ExampleNamespace\ClassA;
9+
use Webdevcave\Yadic\Tests\ExampleNamespace\ClassB;
10+
use Webdevcave\Yadic\Tests\ExampleNamespace\ClassC;
11+
use Webdevcave\Yadic\Tests\ExampleNamespace\InterfaceA;
12+
13+
class ServiceContainerTest extends TestCase
14+
{
15+
private ?ServiceContainer $container;
16+
public function testInstancing(): void
17+
{
18+
$this->assertInstanceOf(ClassA::class, $this->container->get(ClassA::class));
19+
}
20+
21+
public function testInstancingWithParameters(): void
22+
{
23+
$object = $this->container->get(ClassB::class);
24+
25+
$this->assertInstanceOf(ClassB::class, $object);
26+
$this->assertInstanceOf(ClassA::class, $object->a);
27+
$this->assertEquals(2, $object->count);
28+
}
29+
30+
public function testProvidesAnnotation(): void
31+
{
32+
$this->container->loadDefinitionsFromDirectory(
33+
__DIR__.'/ExampleNamespace',
34+
__NAMESPACE__.'\\ExampleNamespace\\'
35+
);
36+
37+
$this->assertInstanceOf(ClassA::class, $this->container->get(InterfaceA::class));
38+
}
39+
40+
public function testSingletonAnnotation(): void
41+
{
42+
$this->container->get(ClassA::class);
43+
$this->container->get(ClassA::class);
44+
self::assertEquals(1, ClassA::$instancesCounter);
45+
46+
$this->container->get(ClassB::class);
47+
$this->container->get(ClassB::class);
48+
self::assertEquals(2, ClassB::$instancesCounter);
49+
self::assertEquals(1, ClassA::$instancesCounter);
50+
}
51+
52+
public function testInvokeFunctionWithoutParameters(): void
53+
{
54+
$a = new ClassA();
55+
56+
$this->assertTrue($this->container->invoke([$a, 'funcWithoutParameters']));
57+
}
58+
59+
public function testInvokeFunctionWithParametersError(): void
60+
{
61+
$this->expectException(Exception::class);
62+
$a = new ClassA();
63+
64+
$this->assertTrue($this->container->invoke([$a, 'funcWithParameters']));
65+
}
66+
67+
public function testInvokeFunctionWithParameters(): void
68+
{
69+
$this->assertTrue($this->container->invoke([new ClassA(), 'funcWithParameters'], ['x' => 1]));
70+
}
71+
72+
protected function setUp(): void
73+
{
74+
$this->container = new ServiceContainer();
75+
ClassA::resetCounter();
76+
ClassB::resetCounter();
77+
ClassC::resetCounter();
78+
}
79+
80+
protected function tearDown(): void
81+
{
82+
$this->container = null;
83+
}
84+
}

0 commit comments

Comments
 (0)