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/ContainerTraitTest --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\ClassWithContainerTrait; |
18
|
|
|
use Rudra\Tests\Stub\ClassWithDefaultParameters; |
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 ContainerTraitTest |
25
|
|
|
*/ |
26
|
|
|
class ContainerTraitTest extends PHPUnit_Framework_TestCase |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ClassWithContainerTrait |
31
|
|
|
*/ |
32
|
|
|
protected $stub; |
33
|
|
|
/** |
34
|
|
|
* @var ContainerInterface |
35
|
|
|
*/ |
36
|
|
|
protected $container; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $stubNamespace = 'Rudra\\Tests\\Stub\\'; |
42
|
|
|
|
43
|
|
|
protected function setUp(): void |
44
|
|
|
{ |
45
|
|
|
$this->container = Container::app(); |
46
|
|
|
$this->container->setBinding(ContainerInterface::class, $this->container); |
47
|
|
|
|
48
|
|
|
$app = [ |
49
|
|
|
'contracts' => [ |
50
|
|
|
ContainerInterface::class => $this->container |
51
|
|
|
], |
52
|
|
|
|
53
|
|
|
'services' => [ |
54
|
|
|
'validation' => [ClassWithoutConstructor::class], |
55
|
|
|
'redirect' => [ClassWithoutParameters::class], |
56
|
|
|
'db' => [ClassWithDefaultParameters::class, ['param' => '123']], |
57
|
|
|
] |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
$this->container->setServices($app); |
61
|
|
|
|
62
|
|
|
$this->stub = new ClassWithContainerTrait($this->container); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testValidation(): void |
66
|
|
|
{ |
67
|
|
|
$this->assertInstanceOf(ClassWithoutConstructor::class, $this->getStub()->validation()); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testRedirect(): void |
71
|
|
|
{ |
72
|
|
|
$this->assertInstanceOf(ClassWithoutParameters::class, $this->getStub()->redirect()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function testDb(): void |
76
|
|
|
{ |
77
|
|
|
$this->assertInstanceOf(ClassWithDefaultParameters::class, $this->getStub()->db()); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function testNew(): void |
81
|
|
|
{ |
82
|
|
|
$newClassWithoutConstructor = $this->getStub()->new(ClassWithoutConstructor::class); |
83
|
|
|
$this->assertInstanceOf(ClassWithoutConstructor::class, $newClassWithoutConstructor); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function testSetPagination(): void |
87
|
|
|
{ |
88
|
|
|
$this->getMockBuilder('Rudra\Pagination')->getMock(); |
89
|
|
|
$this->getStub()->setPagination(['id' => 1]); |
90
|
|
|
$this->assertInstanceOf('Rudra\Pagination', $this->getStub()->pagination()); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function testPost(): void |
94
|
|
|
{ |
95
|
|
|
$this->container->setPost(['key' => 'value']); |
96
|
|
|
$this->assertEquals('value', $this->getStub()->post('key')); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function testPut(): void |
100
|
|
|
{ |
101
|
|
|
$this->container->setPut(['key' => 'value']); |
102
|
|
|
$this->assertTrue($this->getStub()->container()->hasPut('key')); |
103
|
|
|
$this->assertEquals('value', $this->getStub()->container()->getPut('key')); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function testPatch(): void |
107
|
|
|
{ |
108
|
|
|
$this->container->setPatch(['key' => 'value']); |
109
|
|
|
$this->assertTrue($this->getStub()->container()->hasPatch('key')); |
110
|
|
|
$this->assertEquals('value', $this->getStub()->container()->getPatch('key')); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function testDelete(): void |
114
|
|
|
{ |
115
|
|
|
$this->container->setDelete(['key' => 'value']); |
116
|
|
|
$this->assertTrue($this->getStub()->container()->hasDelete('key')); |
117
|
|
|
$this->assertEquals('value', $this->getStub()->container()->getDelete('key')); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testSessionData(): void |
121
|
|
|
{ |
122
|
|
|
$this->getStub()->setSession('key', 'value'); |
123
|
|
|
$this->getStub()->setSession('subKey', 'value', 'subSet'); |
124
|
|
|
$this->getStub()->setSession('increment', 'value', 'increment'); |
125
|
|
|
$this->assertEquals('value', $this->container->getSession('key')); |
126
|
|
|
$this->assertEquals('value', $this->container->getSession('subKey', 'subSet')); |
127
|
|
|
$this->assertEquals('value', $this->container->getSession('increment', '0')); |
128
|
|
|
$this->assertNull($this->getStub()->unsetSession('key')); |
129
|
|
|
$this->assertNull($this->getStub()->unsetSession('subKey', 'subSet')); |
130
|
|
|
$this->assertFalse($this->container->hasSession('key')); |
131
|
|
|
$this->assertFalse($this->container->hasSession('subKey', 'subSet')); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
public function testConfig(): void |
135
|
|
|
{ |
136
|
|
|
$this->container->setConfig(['key' => ['subKey' => 'value']]); |
137
|
|
|
|
138
|
|
|
$this->assertInternalType('array', $this->container->config('key')); |
139
|
|
|
$this->assertEquals('value', $this->container->config('key', 'subKey')); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @runInSeparateProcess |
144
|
|
|
*/ |
145
|
|
|
public function testJsonResponse(): void |
146
|
|
|
{ |
147
|
|
|
$data = ['key' => ['subKey' => 'value']]; |
148
|
|
|
|
149
|
|
|
ob_start(); |
150
|
|
|
$this->container->jsonResponse($data); |
151
|
|
|
$json = ob_get_clean(); |
152
|
|
|
|
153
|
|
|
$this->assertEquals(json_encode($data), $json); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return ClassWithContainerTrait |
158
|
|
|
*/ |
159
|
|
|
public function getStub(): ClassWithContainerTrait |
160
|
|
|
{ |
161
|
|
|
return $this->stub; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|