1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace OpenEngine\Mika\Core\Components\Di\Tests; |
4
|
|
|
|
5
|
|
|
use OpenEngine\Mika\Core\Components\Di\Di; |
6
|
|
|
use OpenEngine\Mika\Core\Components\Di\DiConfig; |
7
|
|
|
use OpenEngine\Mika\Core\Components\Di\Exceptions\ClassNotFoundException; |
8
|
|
|
use OpenEngine\Mika\Core\Components\Di\Exceptions\MissingMethodArgumentException; |
9
|
|
|
use OpenEngine\Mika\Core\Components\Di\Exceptions\ServiceNotFoundException; |
10
|
|
|
use OpenEngine\Mika\Core\Components\Di\Tests\Dummy\Bar; |
11
|
|
|
use OpenEngine\Mika\Core\Components\Di\Tests\Dummy\BarInterface; |
12
|
|
|
use OpenEngine\Mika\Core\Components\Di\Tests\Dummy\Baz; |
13
|
|
|
use OpenEngine\Mika\Core\Components\Di\Tests\Dummy\Foo; |
14
|
|
|
use OpenEngine\Mika\Core\Components\Di\Tests\Dummy\FooInterface; |
15
|
|
|
use OpenEngine\Mika\Core\Helpers\Path; |
16
|
|
|
use PHPUnit\Framework\TestCase; |
17
|
|
|
|
18
|
|
|
class DiTest extends TestCase |
19
|
|
|
{ |
20
|
|
|
public function testHasMethod(): void |
21
|
|
|
{ |
22
|
|
|
$this->assertFalse($this->getDi()->has('unknownClass')); |
23
|
|
|
$this->assertTrue($this->getDi()->has('foo')); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function testNotFoundException(): void |
27
|
|
|
{ |
28
|
|
|
$this->expectException(ServiceNotFoundException::class); |
29
|
|
|
$this->getDi()->get('unknownService'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function testClassNotFoundException(): void |
33
|
|
|
{ |
34
|
|
|
$this->expectException(ClassNotFoundException::class); |
35
|
|
|
$this->getDi()->get('service'); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function testRegister(): void |
39
|
|
|
{ |
40
|
|
|
$this->assertInstanceOf(Foo::class, $this->getDi()->get(FooInterface::class)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testCreatingMethodDepends(): void |
44
|
|
|
{ |
45
|
|
|
$depends = $this->getDi()->createMethodDepends(Foo::class, 'bar', ['login' => 'as']); |
46
|
|
|
|
47
|
|
|
$this->assertArrayHasKey('bar', $depends); |
48
|
|
|
$this->assertArrayHasKey('login', $depends); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function testMissingArgumentException(): void |
52
|
|
|
{ |
53
|
|
|
$this->expectException(MissingMethodArgumentException::class); |
54
|
|
|
$this->getDi()->createMethodDepends(Foo::class, 'bar'); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function testCreateObject(): void |
58
|
|
|
{ |
59
|
|
|
$obj = $this->getDi()->createObject(Foo::class); |
60
|
|
|
$this->assertInstanceOf(Foo::class, $obj); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function setUp() |
64
|
|
|
{ |
65
|
|
|
Path::setRoot(getenv('MIKA_ROOT_DIR')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function getDi(): Di |
69
|
|
|
{ |
70
|
|
|
$diConfig = new DiConfig(); |
71
|
|
|
$diConfig->register('foo', Foo::class); |
72
|
|
|
$diConfig->register('service', 'UnknownClass'); |
73
|
|
|
|
74
|
|
|
$diConfig->register(FooInterface::class, Foo::class); |
75
|
|
|
$diConfig->register(BarInterface::class, Bar::class); |
76
|
|
|
$diConfig->register(Baz::class); |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
return new Di($diConfig); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|