1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @author : Korotkov Danila <[email protected]> |
7
|
|
|
* @copyright Copyright (c) 2016, Korotkov Danila |
8
|
|
|
* @license http://www.gnu.org/licenses/gpl.html GNU GPLv3.0 |
9
|
|
|
* |
10
|
|
|
* phpunit src/tests/ContainerTest --coverage-html src/tests/coverage-html |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Rudra\Tests; |
14
|
|
|
|
15
|
|
|
use Rudra\Container; |
16
|
|
|
use Rudra\Interfaces\ContainerInterface; |
17
|
|
|
use Rudra\Tests\Stub\ClassWithDefaultParameters; |
18
|
|
|
use Rudra\Tests\Stub\ClassWithDependency; |
19
|
|
|
use Rudra\Tests\Stub\ClassWithoutConstructor; |
20
|
|
|
use Rudra\Tests\Stub\ClassWithoutParameters; |
21
|
|
|
use PHPUnit\Framework\TestCase as PHPUnit_Framework_TestCase; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class ContainerTest |
25
|
|
|
*/ |
26
|
|
|
class ContainerTest extends PHPUnit_Framework_TestCase |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ContainerInterface |
31
|
|
|
*/ |
32
|
|
|
protected $container; |
33
|
|
|
|
34
|
|
|
protected function setUp(): void |
35
|
|
|
{ |
36
|
|
|
$this->container = Container::app(); |
37
|
|
|
$this->container->setBinding(ContainerInterface::class, $this->container); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Проверяем синглтон |
42
|
|
|
*/ |
43
|
|
|
public function testCallSingleton(): void |
44
|
|
|
{ |
45
|
|
|
$this->assertInstanceOf(Container::class, Container::app()); |
46
|
|
|
$this->assertInstanceOf(Container::class, $this->container); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function testGet(): void |
50
|
|
|
{ |
51
|
|
|
$this->assertTrue(empty($this->container->get())); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function testSetServices(): void |
55
|
|
|
{ |
56
|
|
|
$this->container->setServices([ |
57
|
|
|
'contracts' => [ |
58
|
|
|
ContainerInterface::class => $this->container |
59
|
|
|
], |
60
|
|
|
|
61
|
|
|
'services' => [ |
62
|
|
|
'CWC' => [ClassWithoutConstructor::class], |
63
|
|
|
'CWP' => [ClassWithoutParameters::class], |
64
|
|
|
'CWDP' => [ClassWithDefaultParameters::class, ['param' => '123']], |
65
|
|
|
] |
66
|
|
|
]); |
67
|
|
|
$this->assertInstanceOf(ClassWithoutConstructor::class, $this->container->get('CWC')); |
68
|
|
|
$this->assertInstanceOf(ClassWithoutParameters::class, $this->container->get('CWP')); |
69
|
|
|
$this->assertInstanceOf(ClassWithDefaultParameters::class, $this->container->get('CWDP')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function testSetRaw(): void |
73
|
|
|
{ |
74
|
|
|
$this->container->set(ContainerTest::class, $this, 'raw'); |
75
|
|
|
$this->assertInstanceOf(ContainerTest::class, $this->container->get(ContainerTest::class)); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function testGetArrayHasKey(): void |
79
|
|
|
{ |
80
|
|
|
$this->assertArrayHasKey(ContainerTest::class, $this->container->get()); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function testIoCClassWithoutConstructor(): void |
84
|
|
|
{ |
85
|
|
|
$newClassWithoutConstructor = $this->container->new(ClassWithoutConstructor::class); |
86
|
|
|
$this->assertInstanceOf(ClassWithoutConstructor::class, $newClassWithoutConstructor); |
87
|
|
|
|
88
|
|
|
$this->container->set('ClassWithoutConstructor', $newClassWithoutConstructor); |
89
|
|
|
$this->assertInstanceOf(ClassWithoutConstructor::class, $this->container->get('ClassWithoutConstructor')); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function testIoCwithoutParameters(): void |
93
|
|
|
{ |
94
|
|
|
$newClassWithoutParameters = $this->container->new(ClassWithoutParameters::class); |
95
|
|
|
$this->assertInstanceOf(ClassWithoutParameters::class, $newClassWithoutParameters); |
96
|
|
|
|
97
|
|
|
$this->container->set('ClassWithoutParameters', $newClassWithoutParameters); |
98
|
|
|
$this->assertInstanceOf(ClassWithoutParameters::class, $this->container->get('ClassWithoutParameters')); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function testIoCwithDefaultParameters(): void |
102
|
|
|
{ |
103
|
|
|
$newClassWithDefaultParameters = $this->container->new(ClassWithDefaultParameters::class); |
104
|
|
|
$this->assertInstanceOf(ClassWithDefaultParameters::class, $newClassWithDefaultParameters); |
105
|
|
|
|
106
|
|
|
$this->container->set('ClassWithDefaultParameters', $newClassWithDefaultParameters); |
107
|
|
|
$this->assertInstanceOf(ClassWithDefaultParameters::class, $this->container->get('ClassWithDefaultParameters')); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function testIoCwithDependency(): void |
111
|
|
|
{ |
112
|
|
|
$newClassWithDependency = $this->container->new(ClassWithDependency::class); |
113
|
|
|
$this->assertInstanceOf(ClassWithDependency::class, $newClassWithDependency); |
114
|
|
|
|
115
|
|
|
$this->container->set('ClassWithDependency', $newClassWithDependency); |
116
|
|
|
$this->assertInstanceOf(ClassWithDependency::class, $this->container->get('ClassWithDependency')); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function testHas(): void |
120
|
|
|
{ |
121
|
|
|
$this->assertTrue($this->container->has(ContainerTest::class)); |
122
|
|
|
$this->assertTrue($this->container->has('ClassWithoutConstructor')); |
123
|
|
|
$this->assertTrue($this->container->has('ClassWithoutParameters')); |
124
|
|
|
$this->assertTrue($this->container->has('ClassWithDefaultParameters')); |
125
|
|
|
$this->assertTrue($this->container->has('ClassWithDependency')); |
126
|
|
|
$this->assertFalse($this->container->has('SomeClass')); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function testSetParam(): void |
130
|
|
|
{ |
131
|
|
|
$param = 'value'; |
132
|
|
|
$this->container->setParam('ClassWithDependency', 'param', $param); |
133
|
|
|
$this->assertEquals($param, $this->container->getParam('ClassWithDependency', 'param')); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function testHasParam(): void |
137
|
|
|
{ |
138
|
|
|
$this->assertTrue($this->container->hasParam('ClassWithDependency', 'param')); |
139
|
|
|
$this->assertNull($this->container->hasParam('SomeClass', 'param')); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function testFailureGetParam(): void |
143
|
|
|
{ |
144
|
|
|
$this->assertNull($this->container->getParam('SomeClass', 'param')); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function testGetData(): void |
148
|
|
|
{ |
149
|
|
|
$this->container->setGet(['key' => 'value']); |
150
|
|
|
$this->assertEquals('value', $this->container->getGet('key')); |
151
|
|
|
$this->assertContains('value', $this->container->getGet()); |
152
|
|
|
$this->assertTrue($this->container->hasGet('key')); |
153
|
|
|
$this->assertFalse($this->container->hasGet('false')); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testPostData(): void |
157
|
|
|
{ |
158
|
|
|
$this->container->setPost(['key' => 'value']); |
159
|
|
|
$this->assertEquals('value', $this->container->getPost('key')); |
160
|
|
|
$this->assertContains('value', $this->container->getPost()); |
161
|
|
|
$this->assertTrue($this->container->hasPost('key')); |
162
|
|
|
$this->assertFalse($this->container->hasPost('false')); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function testServerData(): void |
166
|
|
|
{ |
167
|
|
|
$this->container->setServer('key', 'value'); |
168
|
|
|
$this->assertEquals('value', $this->container->getServer('key')); |
169
|
|
|
$this->assertArrayHasKey('key', $this->container->getServer()); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
public function testSessionData(): void |
173
|
|
|
{ |
174
|
|
|
$this->container->setSession('key', 'value'); |
175
|
|
|
$this->container->setSession('subKey', 'value', 'subSet'); |
176
|
|
|
$this->container->setSession('increment', 'value', 'increment'); |
177
|
|
|
$this->assertEquals('value', $this->container->getSession('key')); |
178
|
|
|
$this->assertEquals('value', $this->container->getSession('subKey', 'subSet')); |
179
|
|
|
$this->assertEquals('value', $this->container->getSession('increment', '0')); |
180
|
|
|
$this->assertTrue($this->container->hasSession('key')); |
181
|
|
|
$this->assertTrue($this->container->hasSession('subKey', 'subSet')); |
182
|
|
|
$this->assertNull($this->container->unsetSession('key')); |
183
|
|
|
$this->assertNull($this->container->unsetSession('subKey', 'subSet')); |
184
|
|
|
$this->assertFalse($this->container->hasSession('key')); |
185
|
|
|
$this->assertFalse($this->container->hasSession('subKey', 'subSet')); |
186
|
|
|
$this->assertNull($this->container->clearSession()); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
public function testCookieData(): void |
190
|
|
|
{ |
191
|
|
|
$this->container->setCookie('key', 'value'); |
192
|
|
|
$this->assertEquals('value', $this->container->getCookie('key')); |
193
|
|
|
$this->assertTrue($this->container->hasCookie('key')); |
194
|
|
|
$this->assertFalse($this->container->hasCookie('false')); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function testFilesData(): void |
198
|
|
|
{ |
199
|
|
|
$this->container->setFiles( |
200
|
|
|
[ |
201
|
|
|
'upload' => ['name' => ['img' => '41146.png']], |
202
|
|
|
'type' => ['img' => 'image/png'], |
203
|
|
|
] |
204
|
|
|
); |
205
|
|
|
|
206
|
|
|
$this->assertTrue($this->container->isUploaded('img')); |
207
|
|
|
$this->assertTrue($this->container->isFileType('img', 'image/png')); |
208
|
|
|
$this->assertEquals('41146.png', $this->container->getUpload('img', 'name')); |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
|