1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Date: 17.02.17 |
7
|
|
|
* Time: 13:23 |
8
|
|
|
* |
9
|
|
|
* @author : Korotkov Danila <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2016, Korotkov Danila |
11
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU GPLv3.0 |
12
|
|
|
* |
13
|
|
|
* phpunit src/tests/ContainerTraitTest --coverage-html src/tests/coverage-html |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
|
17
|
|
|
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase; |
|
|
|
|
18
|
|
|
use Rudra\ContainerInterface; |
19
|
|
|
use Rudra\Container; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ContainerTraitTest |
24
|
|
|
*/ |
25
|
|
|
class ContainerTraitTest extends PHPUnit_Framework_TestCase |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var ClassWithContainerTrait |
30
|
|
|
*/ |
31
|
|
|
protected $stub; |
32
|
|
|
|
33
|
|
|
protected function setUp(): void |
34
|
|
|
{ |
35
|
|
|
Container::app()->setBinding(ContainerInterface::class, Container::$app); |
36
|
|
|
|
37
|
|
|
$app = [ |
38
|
|
|
'contracts' => [ |
39
|
|
|
ContainerInterface::class => Container::$app |
40
|
|
|
], |
41
|
|
|
|
42
|
|
|
'services' => [ |
43
|
|
|
'validation' => ['ClassWithoutConstructor'], |
44
|
|
|
'redirect' => ['ClassWithoutParameters'], |
45
|
|
|
'db' => ['ClassWithDefaultParameters', ['param' => '123']], |
46
|
|
|
] |
47
|
|
|
]; |
48
|
|
|
|
49
|
|
|
Container::$app->setServices($app); |
50
|
|
|
|
51
|
|
|
$this->stub = new ClassWithContainerTrait(Container::$app); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testValidation(): void |
55
|
|
|
{ |
56
|
|
|
$this->assertInstanceOf(ClassWithoutConstructor::class, $this->getStub()->validation()); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testRedirect(): void |
60
|
|
|
{ |
61
|
|
|
$this->assertInstanceOf(ClassWithoutParameters::class, $this->getStub()->redirect()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testDb(): void |
65
|
|
|
{ |
66
|
|
|
$this->assertInstanceOf(ClassWithDefaultParameters::class, $this->getStub()->db()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function testNew(): void |
70
|
|
|
{ |
71
|
|
|
$newClassWithoutConstructor = $this->getStub()->new('ClassWithoutConstructor'); |
72
|
|
|
$this->assertInstanceOf('ClassWithoutConstructor', $newClassWithoutConstructor); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testSetPagination(): void |
76
|
|
|
{ |
77
|
|
|
$this->getMockBuilder('Rudra\Pagination')->getMock(); |
78
|
|
|
$this->getStub()->setPagination(['id' => 1]); |
79
|
|
|
$this->assertInstanceOf('Rudra\Pagination', $this->getStub()->pagination()); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function testPost(): void |
83
|
|
|
{ |
84
|
|
|
Container::$app->setPost(['key' => 'value']); |
85
|
|
|
$this->assertEquals('value', $this->getStub()->post('key')); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testPut(): void |
89
|
|
|
{ |
90
|
|
|
Container::$app->setPut(['key' => 'value']); |
91
|
|
|
$this->assertTrue($this->getStub()->container()->hasPut('key')); |
92
|
|
|
$this->assertEquals('value', $this->getStub()->container()->getPut('key')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function testPatch(): void |
96
|
|
|
{ |
97
|
|
|
Container::$app->setPatch(['key' => 'value']); |
98
|
|
|
$this->assertTrue($this->getStub()->container()->hasPatch('key')); |
99
|
|
|
$this->assertEquals('value', $this->getStub()->container()->getPatch('key')); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function testDelete(): void |
103
|
|
|
{ |
104
|
|
|
Container::$app->setDelete(['key' => 'value']); |
105
|
|
|
$this->assertTrue($this->getStub()->container()->hasDelete('key')); |
106
|
|
|
$this->assertEquals('value', $this->getStub()->container()->getDelete('key')); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function testSessionData(): void |
110
|
|
|
{ |
111
|
|
|
$this->getStub()->setSession('key', 'value'); |
112
|
|
|
$this->getStub()->setSession('subKey', 'value', 'subSet'); |
113
|
|
|
$this->getStub()->setSession('increment', 'value', 'increment'); |
114
|
|
|
$this->assertEquals('value', Container::$app->getSession('key')); |
115
|
|
|
$this->assertEquals('value', Container::$app->getSession('subKey', 'subSet')); |
116
|
|
|
$this->assertEquals('value', Container::$app->getSession('increment', '0')); |
117
|
|
|
$this->assertNull($this->getStub()->unsetSession('key')); |
118
|
|
|
$this->assertNull($this->getStub()->unsetSession('subKey', 'subSet')); |
119
|
|
|
$this->assertFalse(Container::$app->hasSession('key')); |
120
|
|
|
$this->assertFalse(Container::$app->hasSession('subKey', 'subSet')); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return ClassWithContainerTrait |
125
|
|
|
*/ |
126
|
|
|
public function getStub(): ClassWithContainerTrait |
127
|
|
|
{ |
128
|
|
|
return $this->stub; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: