forked from phalcon/cphalcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiTest.php
410 lines (320 loc) · 10.9 KB
/
DiTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| If you did not receive a copy of the license and are unable to |
| obtain it through the world-wide-web, please send an email |
| to license@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/
class InjectableComponent
{
public $response;
public $other;
public function __construct($response=null)
{
$this->response = $response;
}
public function setResponse($response)
{
$this->response = $response;
}
public function getResponse()
{
return $this->response;
}
}
class SimpleComponent
{
}
class SomeComponent
{
public $someProperty = false;
public function __construct($value)
{
$this->someProperty = $value;
}
}
class Service1242
{
}
class DiTest extends PHPUnit_Framework_TestCase
{
protected $_di;
public function setUp()
{
Phalcon\Di::reset();
$this->_di = new \Phalcon\Di();
}
public function testSetString()
{
$this->_di->set('request1', 'Phalcon\Http\Request');
$request = $this->_di->get('request1');
$this->assertEquals(get_class($request), 'Phalcon\Http\Request');
}
public function testSetAnonymousFunction()
{
$this->_di->set('request2', function(){
return new Phalcon\Http\Request();
});
$request = $this->_di->get('request2');
$this->assertEquals(get_class($request), 'Phalcon\Http\Request');
}
public function testSetArray()
{
$this->_di->set('request3', array(
'className' => 'Phalcon\Http\Request'
));
$request = $this->_di->get('request3');
$this->assertEquals(get_class($request), 'Phalcon\Http\Request');
}
public function testAtempt()
{
$this->_di->set('request4', function(){
return new Phalcon\Http\Request();
});
$this->_di->attempt('request4', function(){
return new stdClass();
});
$this->_di->attempt('request5', function(){
return new stdClass();
});
$request = $this->_di->get('request4');
$this->assertEquals(get_class($request), 'Phalcon\Http\Request');
$request = $this->_di->get('request5');
$this->assertEquals(get_class($request), 'stdClass');
}
public function testHas()
{
$this->_di->set('request6', function(){
return new Phalcon\Http\Request();
});
$this->assertTrue($this->_di->has('request6'));
$this->assertFalse($this->_di->has('request7'));
}
public function testShared()
{
$this->_di->set('dateObject', function(){
$object = new stdClass();
$object->date = microtime(true);
return $object;
});
$dateObject = $this->_di->getShared('dateObject');
$this->assertEquals(get_class($dateObject), 'stdClass');
$this->assertTrue($this->_di->wasFreshInstance());
$dateObject2 = $this->_di->getShared('dateObject');
$this->assertEquals(get_class($dateObject), 'stdClass');
$this->assertEquals($dateObject->date, $dateObject2->date);
}
public function testMagicCall()
{
$this->_di->set('request8', 'Phalcon\Http\Request');
$request = $this->_di->getRequest8();
$this->assertEquals(get_class($request), 'Phalcon\Http\Request');
$this->_di->setRequest9('Phalcon\Http\Request');
$request = $this->_di->get('request9');
$this->assertEquals(get_class($request), 'Phalcon\Http\Request');
}
public function testParameters()
{
$this->_di->set('someComponent1', function($v){
return new SomeComponent($v);
});
$this->_di->set('someComponent2', 'SomeComponent');
$someComponent1 = $this->_di->get('someComponent1', array(100));
$this->assertEquals($someComponent1->someProperty, 100);
$someComponent2 = $this->_di->get('someComponent2', array(50));
$this->assertEquals($someComponent2->someProperty, 50);
}
public function testGetServices()
{
$expectedServices = array(
'service1' => Phalcon\Di\Service::__set_state(array(
'_name' => 'service1',
'_definition' => 'some-service',
'_shared' => false,
'_sharedInstance' => NULL,
)),
'service2' => Phalcon\Di\Service::__set_state(array(
'_name' => 'service2',
'_definition' => 'some-other-service',
'_shared' => false,
'_sharedInstance' => NULL,
))
);
$this->_di->set('service1', 'some-service');
$this->_di->set('service2', 'some-other-service');
$this->assertEquals($this->_di->getServices(), $expectedServices);
}
public function testGetRawService()
{
$this->_di->set('service1', 'some-service');
$this->assertEquals($this->_di->getRaw('service1'), 'some-service');
}
public function testArrayAccess()
{
$this->_di['simple'] = 'SimpleComponent';
$this->assertEquals(get_class($this->_di['simple']), 'SimpleComponent');
}
public function testComplexInjection()
{
$response = new Phalcon\Http\Response();
$this->_di->set('response', $response);
//Injection of parameters in the constructor
$this->_di->set('simpleConstructor',
array(
'className' => 'InjectableComponent',
'arguments' => array(
array('type' => 'parameter', 'value' => 'response')
)
)
);
//Injection of simple setters
$this->_di->set('simpleSetters',
array(
'className' => 'InjectableComponent',
'calls' => array(
array(
'method' => 'setResponse',
'arguments' => array(
array('type' => 'parameter', 'value' => 'response'),
)
),
)
)
);
//Injection of properties
$this->_di->set('simpleProperties',
array(
'className' => 'InjectableComponent',
'properties' => array(
array(
'name' => 'response', 'value' => array('type' => 'parameter', 'value' => 'response')
),
)
)
);
//Injection of parameters in the constructor resolving the service parameter
$this->_di->set('complexConstructor',
array(
'className' => 'InjectableComponent',
'arguments' => array(
array('type' => 'service', 'name' => 'response')
)
)
);
//Injection of simple setters resolving the service parameter
$this->_di->set('complexSetters',
array(
'className' => 'InjectableComponent',
'calls' => array(
array(
'method' => 'setResponse',
'arguments' => array(
array('type' => 'service', 'name' => 'response')
)
),
)
)
);
//Injection of properties resolving the service parameter
$this->_di->set('complexProperties',
array(
'className' => 'InjectableComponent',
'properties' => array(
array(
'name' => 'response', 'value' => array('type' => 'service', 'name' => 'response')
),
)
)
);
$component = $this->_di->get('simpleConstructor');
$this->assertTrue(is_string($component->getResponse()));
$this->assertEquals($component->getResponse(), 'response');
$component = $this->_di->get('simpleSetters');
$this->assertTrue(is_string($component->getResponse()));
$this->assertEquals($component->getResponse(), 'response');
$component = $this->_di->get('simpleProperties');
$this->assertTrue(is_string($component->getResponse()));
$this->assertEquals($component->getResponse(), 'response');
$component = $this->_di->get('complexConstructor');
$this->assertTrue(is_object($component->getResponse()));
$this->assertEquals($component->getResponse(), $response);
$component = $this->_di->get('complexSetters');
$this->assertTrue(is_object($component->getResponse()));
$this->assertEquals($component->getResponse(), $response);
$component = $this->_di->get('complexProperties');
$this->assertTrue(is_object($component->getResponse()));
$this->assertEquals($component->getResponse(), $response);
}
public function testFactoryDefault()
{
$factoryDefault = new Phalcon\Di\FactoryDefault();
$request = $factoryDefault->get('request');
$this->assertEquals(get_class($request), 'Phalcon\Http\Request');
$response = $factoryDefault->get('response');
$this->assertEquals(get_class($response), 'Phalcon\Http\Response');
$filter = $factoryDefault->get('filter');
$this->assertEquals(get_class($filter), 'Phalcon\Filter');
$escaper = $factoryDefault->get('escaper');
$this->assertEquals(get_class($escaper), 'Phalcon\Escaper');
$url = $factoryDefault->get('url');
$this->assertEquals(get_class($url), 'Phalcon\Mvc\Url');
$flash = $factoryDefault->get('flash');
$this->assertEquals(get_class($flash), 'Phalcon\Flash\Direct');
$dispatcher = $factoryDefault->get('dispatcher');
$this->assertEquals(get_class($dispatcher), 'Phalcon\Mvc\Dispatcher');
$modelsManager = $factoryDefault->get('modelsManager');
$this->assertEquals(get_class($modelsManager), 'Phalcon\Mvc\Model\Manager');
$modelsMetadata = $factoryDefault->get('modelsMetadata');
$this->assertEquals(get_class($modelsMetadata), 'Phalcon\Mvc\Model\MetaData\Memory');
$router = $factoryDefault->get('router');
$this->assertEquals(get_class($router), 'Phalcon\Mvc\Router');
$session = $factoryDefault->get('session');
$this->assertEquals(get_class($session), 'Phalcon\Session\Adapter\Files');
$security = $factoryDefault->get('security');
$this->assertEquals(get_class($security), 'Phalcon\Security');
}
public function testStaticDi()
{
$di = Phalcon\Di::getDefault();
$this->assertInstanceOf('Phalcon\Di', $di);
}
public function testCrash()
{
$di = new Phalcon\Di\FactoryDefault();
$bag = $di->get('sessionBag', array('dummy'));
$this->assertTrue(true);
}
/**
* Prior to 2.0.0 exception message was "Service 'servicewhichdoesnotexist' wasn't found in the dependency injection container"
*
* @expectedException \Phalcon\Di\Exception
* @expectedExceptionMessage Service 'servicewhichdoesnotexist' wasn't found in the dependency injection container
*/
public function testGettingNonExistentServiceShouldThrowExceptionWithExpectedMessage()
{
$di = new \Phalcon\DI\FactoryDefault();
$di->get('servicewhichdoesnotexist');
}
public function testIssue1242()
{
$di = new \Phalcon\Di();
$di->set('resolved', function() { return new Service1242(); });
$di->set('notresolved', function() { return new Service1242(); });
$this->assertFalse($di->getService('resolved')->isResolved());
$this->assertFalse($di->getService('notresolved')->isResolved());
$di->get('resolved');
$this->assertTrue($di->getService('resolved')->isResolved());
$this->assertFalse($di->getService('notresolved')->isResolved());
}
}