Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
1 | <?php |
||
25 | class ContainerTest extends PHPUnit_Framework_TestCase |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * @var ContainerInterface |
||
30 | */ |
||
31 | protected $container; |
||
32 | |||
33 | /** |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $app; |
||
37 | |||
38 | protected function setUp(): void |
||
39 | { |
||
40 | $this->container = Container::app(); |
||
41 | Container::$app->setBinding(ContainerInterface::class, Container::$app); |
||
42 | |||
43 | $this->app = [ |
||
44 | 'contracts' => [ |
||
45 | ContainerInterface::class => Container::$app |
||
46 | ], |
||
47 | |||
48 | 'services' => [ |
||
49 | 'CWC' => ['ClassWithoutConstructor'], |
||
50 | 'CWP' => ['ClassWithoutParameters'], |
||
51 | 'CWDP' => ['ClassWithDefaultParameters', ['param' => '123']], |
||
52 | ] |
||
53 | ]; |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * @return mixed |
||
58 | */ |
||
59 | protected function container(): ContainerInterface |
||
60 | { |
||
61 | return $this->container; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @return array |
||
66 | */ |
||
67 | public function getApp(): array |
||
68 | { |
||
69 | return $this->app; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * Проверяем синглтон |
||
74 | */ |
||
75 | public function testCallSingleton(): void |
||
76 | { |
||
77 | $this->assertInstanceOf(Container::class, Container::app()); |
||
78 | $this->assertInstanceOf(Container::class, Container::$app); |
||
79 | } |
||
80 | |||
81 | public function testGet(): void |
||
82 | { |
||
83 | $this->assertTrue(empty($this->container()->get())); |
||
84 | } |
||
85 | |||
86 | public function testSetServices(): void |
||
87 | { |
||
88 | $this->container()->setServices($this->getApp()); |
||
89 | $this->assertInstanceOf('ClassWithoutConstructor', $this->container()->get('CWC')); |
||
90 | $this->assertInstanceOf('ClassWithoutParameters', $this->container()->get('CWP')); |
||
91 | $this->assertInstanceOf('ClassWithDefaultParameters', $this->container()->get('CWDP')); |
||
92 | } |
||
93 | |||
94 | public function testSetRaw(): void |
||
95 | { |
||
96 | $this->container()->set(ContainerTest::class, $this, 'raw'); |
||
97 | $this->assertInstanceOf(ContainerTest::class, $this->container()->get(ContainerTest::class)); |
||
98 | } |
||
99 | |||
100 | public function testGetArrayHasKey(): void |
||
101 | { |
||
102 | $this->assertArrayHasKey(ContainerTest::class, $this->container()->get()); |
||
103 | } |
||
104 | |||
105 | public function testIoCClassWithoutConstructor(): void |
||
106 | { |
||
107 | $newClassWithoutConstructor = $this->container()->new('ClassWithoutConstructor'); |
||
108 | $this->assertInstanceOf('ClassWithoutConstructor', $newClassWithoutConstructor); |
||
109 | |||
110 | $this->container()->set('ClassWithoutConstructor', $newClassWithoutConstructor); |
||
111 | $this->assertInstanceOf('ClassWithoutConstructor', $this->container()->get('ClassWithoutConstructor')); |
||
112 | } |
||
113 | |||
114 | public function testIoCwithoutParameters(): void |
||
115 | { |
||
116 | $newClassWithoutParameters = $this->container()->new('ClassWithoutParameters'); |
||
117 | $this->assertInstanceOf('ClassWithoutParameters', $newClassWithoutParameters); |
||
118 | |||
119 | $this->container()->set('ClassWithoutParameters', $newClassWithoutParameters); |
||
120 | $this->assertInstanceOf('ClassWithoutParameters', $this->container()->get('ClassWithoutParameters')); |
||
121 | } |
||
122 | |||
123 | public function testIoCwithDefaultParameters(): void |
||
124 | { |
||
125 | $newClassWithDefaultParameters = $this->container()->new('ClassWithDefaultParameters'); |
||
126 | $this->assertInstanceOf('ClassWithDefaultParameters', $newClassWithDefaultParameters); |
||
127 | |||
128 | $this->container()->set('ClassWithDefaultParameters', $newClassWithDefaultParameters); |
||
129 | $this->assertInstanceOf('ClassWithDefaultParameters', $this->container()->get('ClassWithDefaultParameters')); |
||
130 | } |
||
131 | |||
132 | public function testIoCwithDependency(): void |
||
133 | { |
||
134 | $newClassWithDependency = $this->container()->new('ClassWithDependency'); |
||
135 | $this->assertInstanceOf('ClassWithDependency', $newClassWithDependency); |
||
136 | |||
137 | $this->container()->set('ClassWithDependency', $newClassWithDependency); |
||
138 | $this->assertInstanceOf('ClassWithDependency', $this->container()->get('ClassWithDependency')); |
||
139 | } |
||
140 | |||
141 | public function testHas(): void |
||
142 | { |
||
143 | $this->assertTrue($this->container()->has(ContainerTest::class)); |
||
144 | $this->assertTrue($this->container()->has('ClassWithoutConstructor')); |
||
145 | $this->assertTrue($this->container()->has('ClassWithoutParameters')); |
||
146 | $this->assertTrue($this->container()->has('ClassWithDefaultParameters')); |
||
147 | $this->assertTrue($this->container()->has('ClassWithDependency')); |
||
148 | $this->assertFalse($this->container()->has('SomeClass')); |
||
149 | } |
||
150 | |||
151 | public function testSetParam(): void |
||
152 | { |
||
153 | $param = 'value'; |
||
154 | $this->container()->setParam('ClassWithDependency', 'param', $param); |
||
155 | $this->assertEquals($param, $this->container()->getParam('ClassWithDependency', 'param')); |
||
156 | } |
||
157 | |||
158 | public function testHasParam(): void |
||
159 | { |
||
160 | $this->assertTrue($this->container()->hasParam('ClassWithDependency', 'param')); |
||
161 | $this->assertNull($this->container()->hasParam('SomeClass', 'param')); |
||
162 | } |
||
163 | |||
164 | public function testFailureGetParam(): void |
||
165 | { |
||
166 | $this->assertNull($this->container()->getParam('SomeClass', 'param')); |
||
167 | } |
||
168 | |||
169 | public function testGetData(): void |
||
170 | { |
||
171 | Container::$app->setGet(['key' => 'value']); |
||
172 | $this->assertEquals('value', $this->container()->getGet('key')); |
||
173 | $this->assertContains('value', $this->container()->getGet()); |
||
174 | $this->assertTrue($this->container()->hasGet('key')); |
||
175 | $this->assertFalse($this->container()->hasGet('false')); |
||
176 | } |
||
177 | |||
178 | public function testPostData(): void |
||
179 | { |
||
180 | Container::$app->setPost(['key' => 'value']); |
||
181 | $this->assertEquals('value', $this->container()->getPost('key')); |
||
182 | $this->assertContains('value', $this->container()->getPost()); |
||
183 | $this->assertTrue($this->container()->hasPost('key')); |
||
184 | $this->assertFalse($this->container()->hasPost('false')); |
||
185 | } |
||
186 | |||
187 | public function testServerData(): void |
||
188 | { |
||
189 | Container::$app->setServer('key', 'value'); |
||
190 | $this->assertEquals('value', $this->container()->getServer('key')); |
||
191 | $this->assertArrayHasKey('key', $this->container()->getServer()); |
||
192 | } |
||
193 | |||
194 | public function testSessionData(): void |
||
195 | { |
||
196 | Container::$app->setSession('key', 'value'); |
||
197 | Container::$app->setSession('subKey', 'value', 'subSet'); |
||
198 | Container::$app->setSession('increment', 'value', 'increment'); |
||
199 | $this->assertEquals('value', $this->container()->getSession('key')); |
||
200 | $this->assertEquals('value', $this->container()->getSession('subKey', 'subSet')); |
||
201 | $this->assertEquals('value', $this->container()->getSession('increment', '0')); |
||
202 | $this->assertTrue($this->container()->hasSession('key')); |
||
203 | $this->assertTrue($this->container()->hasSession('subKey', 'subSet')); |
||
204 | $this->assertNull($this->container()->unsetSession('key')); |
||
205 | $this->assertNull($this->container()->unsetSession('subKey', 'subSet')); |
||
206 | $this->assertFalse($this->container()->hasSession('key')); |
||
207 | $this->assertFalse($this->container()->hasSession('subKey', 'subSet')); |
||
208 | $this->assertNull($this->container()->clearSession()); |
||
209 | } |
||
210 | |||
211 | public function testCookieData(): void |
||
212 | { |
||
213 | Container::$app->setCookie('key', 'value'); |
||
214 | $this->assertEquals('value', $this->container()->getCookie('key')); |
||
215 | $this->assertTrue($this->container()->hasCookie('key')); |
||
216 | $this->assertFalse($this->container()->hasCookie('false')); |
||
217 | } |
||
218 | |||
219 | public function testFilesData(): void |
||
220 | { |
||
221 | Container::$app->setFiles( |
||
222 | [ |
||
223 | 'upload' => ['name' => ['img' => '41146.png']], |
||
224 | 'type' => ['img' => 'image/png'], |
||
225 | ] |
||
226 | ); |
||
227 | |||
228 | $this->assertTrue($this->container()->isUploaded('img')); |
||
229 | $this->assertTrue($this->container()->isFileType('img', 'image/png')); |
||
230 | $this->assertEquals('41146.png', $this->container()->getUpload('img', 'name')); |
||
231 | } |
||
232 | } |
||
233 |