|
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
|
|
|
public function testConfig(): void |
|
124
|
|
|
{ |
|
125
|
|
|
Container::$app->setConfig(['key' => ['subKey' => 'value']]); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertInternalType('array', Container::$app->config('key')); |
|
128
|
|
|
$this->assertEquals('value', Container::$app->config('key', 'subKey')); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @runInSeparateProcess |
|
133
|
|
|
*/ |
|
134
|
|
|
public function testJsonResponse(): void |
|
135
|
|
|
{ |
|
136
|
|
|
$data = ['key' => ['subKey' => 'value']]; |
|
137
|
|
|
$this->assertEquals(json_encode($data), Container::$app->jsonResponse($data)); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return ClassWithContainerTrait |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getStub(): ClassWithContainerTrait |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->stub; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.