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