colly_wyx
2018-05-18 50a53f7db63c5729b0ddbc93367117cf35ccf03c
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
<?php
/**
 * PhpUnderControl_PhalApiDI_Test
 *
 * 针对 ../PhalApi/DI.php PhalApi_DI 类的PHPUnit单元测试
 *
 * @author: dogstar 20141004
 */
 
require_once dirname(__FILE__) . '/test_env.php';
 
if (!class_exists('PhalApi_DI')) {
    require dirname(__FILE__) . '/../PhalApi/DI.php';
}
 
class PhpUnderControl_PhalApiDI_Test extends PHPUnit_Framework_TestCase
{
    public $coreDI;
 
    protected function setUp()
    {
        parent::setUp();
 
        $this->coreDI = new PhalApi_DI();
    }
 
    protected function tearDown()
    {
    }
 
 
    /**
     * @group testOne
     */ 
    public function testOne()
    {
        $rs = DI();
    }
 
    public function testSetterAndGetter()
    {
        $this->coreDI->set('name', 'dogstar');
        $this->assertEquals($this->coreDI->get('name'), 'dogstar');
 
        $arr = array(1, 5, 7);
        $this->coreDI->set('nameArr', $arr);
        $this->assertEquals($this->coreDI->get('nameArr'), $arr);
    }
 
    public function testMagicFunction()
    {
        $this->coreDI->setName('dogstar');
        $this->assertEquals($this->coreDI->getName(), 'dogstar');
 
        $this->assertEquals($this->coreDI->getNameDefault('2013'), '2013');
 
        $this->assertEquals($this->coreDI->getNameNull(), null);
 
        $this->coreDI->setNameSetNull();
        $this->assertEquals($this->coreDI->getNameSetNull(), null);
    }
 
    public function testClassSettterAndGetter()
    {
        $this->coreDI->name2 = 'dogstar';
        $this->assertEquals($this->coreDI->name2, 'dogstar');
 
        $this->coreDI->nameAgain = 'dogstarAgain';
        $this->assertEquals($this->coreDI->nameAgain, 'dogstarAgain');
 
        $this->assertNull($this->coreDI->nameNull);
 
    }
 
    public function testMixed()
    {
        $this->coreDI->name1 = 'dogstar1';
        $this->assertEquals($this->coreDI->name1, 'dogstar1');
        $this->assertEquals($this->coreDI->getName1('2013'), 'dogstar1');
        $this->assertEquals($this->coreDI->name1, 'dogstar1');
 
        $this->coreDI->setName1('dogstar2');
        $this->assertEquals($this->coreDI->name1, 'dogstar2');
        $this->assertEquals($this->coreDI->getName1('2013'), 'dogstar2');
        $this->assertEquals($this->coreDI->name1, 'dogstar2');
 
        $this->coreDI->set('name1', 'dogstar3');
        $this->assertEquals($this->coreDI->name1, 'dogstar3');
        $this->assertEquals($this->coreDI->getName1('2013'), 'dogstar3');
        $this->assertEquals($this->coreDI->name1, 'dogstar3');
 
    }
 
    public function testAnonymousFunction()
    {
        $this->coreDI->set('name', function(){
            return new Demo(2014);   
        });
 
        $this->assertEquals($this->coreDI->name->mark, 2014);
 
        $mark = 2015;
        $this->coreDI->set('name1', function() use ($mark){
            return new Demo($mark);
        });
        $this->assertEquals($this->coreDI->name1->mark, $mark);
 
        $this->coreDI->name3 = function(){
            return new Demo(2015);
        };
        $this->assertEquals($this->coreDI->getName3()->mark, 2015);
    }
 
    public function testLazyLoadClass()
    {
        $this->coreDI->setName('Demo2');
        $this->assertEquals($this->coreDI->name instanceof Demo2, true);
        $this->assertEquals($this->coreDI->name->number, 3);
        $this->assertEquals($this->coreDI->name->number, 3);
        $this->coreDI->name->number = 9;
        $this->assertEquals($this->coreDI->name->number, 9);
        $this->assertEquals($this->coreDI->getName()->number, 9);
    }
 
    public function testArrayIndex()
    {
        $this->coreDI['name'] = 'dogstar';
        $this->assertEquals($this->coreDI->name, 'dogstar');
 
        $this->coreDI[2014] = 'horse';
        $this->assertEquals($this->coreDI->get2014(), 'horse');
        $this->assertEquals($this->coreDI[2014], 'horse');
        $this->assertEquals($this->coreDI->get(2014), 'horse');
    }
 
    /**
     * hope nobody use DI as bellow
     */
    public function testException()
    {
        try {
            $this->coreDI[array(1)] = 1;
            $this->fail('no way');
        } catch (Exception $ex) {
        }
 
        try {
            $this->coreDI->set(array(1), array(1));
            $this->fail('no way');
        } catch (Exception $ex) {
        }
 
        try {
            $this->coreDI->get(array(1), array(1));
            $this->fail('no way');
        } catch (Exception $ex) {
        }
    }
 
    public function testSetAgainAndAgain()
    {
        $this->coreDI['name'] = function () {
            return 'dogstar';
        };
        $this->assertEquals('dogstar', $this->coreDI['name']);
 
        $this->coreDI['name'] = 'dogstar2';
        $this->assertEquals('dogstar2', $this->coreDI['name']);
 
        $this->coreDI['name'] = function () {
            return 'dogstar3';
        };
        $this->assertEquals('dogstar3', $this->coreDI['name']);
 
        $this->coreDI['name'] = 'dogstar4';
        $this->assertEquals('dogstar4', $this->coreDI['name']);
 
        $this->coreDI['name'] = 'dogstar5';
        $this->assertEquals('dogstar5', $this->coreDI['name']);
 
    }
 
    public function testSetAgainAndAgainByProperty()
    {
        $this->coreDI->name = 'name';
        $this->assertSame('name', $this->coreDI->name);
 
        $this->coreDI->name = 'name2';
        $this->assertSame('name2', $this->coreDI->name);
 
        $this->coreDI->name = array('name3');
        $this->assertSame(array('name3'), $this->coreDI->name);
    }
 
    public function testSetAgainAndAgainBySetter()
    {
        $this->coreDI->set('name', 'value');
        $this->assertSame('value', $this->coreDI->name);
 
        $this->coreDI->set('name', 'value2');
        $this->assertSame('value2', $this->coreDI->name);
    }
 
    public function testSetAgainAndAgainByMagic()
    {
        $this->coreDI->setName('value');
        $this->assertSame('value', $this->coreDI->name);
 
        $this->coreDI->setName('value2');
        $this->assertSame('value2', $this->coreDI->name);
    }
 
    public function testGetAgainAndAgain()
    {
        $times = 0;
 
        $this->coreDI['name'] = function () use (&$times) {
            $times ++;
            return 'dogstar';
        };
 
        $this->assertEquals('dogstar', $this->coreDI['name']);
        $this->assertEquals('dogstar', $this->coreDI['name']);
        $this->assertEquals('dogstar', $this->coreDI['name']);
        $this->assertEquals('dogstar', $this->coreDI['name']);
 
        $this->assertEquals(1, $times);
    }
 
    public function testNumericKey()
    {
        $this->coreDI[0] = 0;
        $this->coreDI[1] = 10;
        $this->coreDI[2] = 20;
 
        $this->assertSame(0, $this->coreDI[0]);
        $this->assertSame(0, $this->coreDI->get(0));
        $this->assertSame(0, $this->coreDI->get0());
 
        $this->assertSame(10, $this->coreDI[1]);
        $this->assertSame(20, $this->coreDI->get(2));
        $this->assertSame(20, $this->coreDI->get2());
    }
 
    public function testMultiLevel()
    {
        $this->coreDI->dogstar = new PhalApi_DI();
        $this->coreDI->dogstar->name = "dogstar";
        $this->coreDI->dogstar->age = "?";
        $this->coreDI->dogstar->id = 1;
 
        $this->assertSame('dogstar', $this->coreDI->dogstar->name);
        $this->assertSame('?', $this->coreDI->dogstar->age);
        $this->assertSame(1, $this->coreDI->dogstar->id = 1);
    }
 
    public function testWithClassName()
    {
        $this->coreDI->key = 'PhalApi_DI';
        $this->assertInstanceOf('PhalApi_DI', $this->coreDI->key);
    }
 
    public function testArrayUsage()
    {
        $this->coreDI[0] = 0;
        $this->coreDI[1] = 1;
        $this->coreDI[2] = 2;
 
        foreach ($this->coreDI as $key => $value) {
            $this->assertSame($key, $value);
        }
    }
 
    /**
     * @expectedException Exception
     */
    public function testIllegalMethod()
    {
        $this->coreDI->doSomeThingWrong();
    }
 
    public function testMultiSet()
    {
        $this->coreDI->set('key1', 'value1')
            ->set('key2', 'value2')
            ->set('key2', 'value2')
            ->set('key3', 'value3');
 
        $this->coreDI->setKey4('value4')
            ->setKey5('value5')
            ->setkey6('value6');
 
        $this->assertSame('value2', $this->coreDI->key2);
        $this->assertSame('value6', $this->coreDI['key6']);
    }
 
    public function testOneWithInstanceNull()
    {
        $oldInstance = PhalApi_DI_Mock::getInstance();
 
        PhalApi_DI_Mock::setInstance(null);
        $newDI = PhalApi_DI::one();
 
        if (!isset($newDI['tmp'])) {
            $newDI['tmp'] = '2015';
 
            $this->assertEquals('2015', $newDI['tmp']);
            unset($newDI['tmp']);
        }
 
        $this->assertEquals(null, $newDI['tmp']);
 
        PhalApi_DI_Mock::setInstance($oldInstance);
    }
}
 
class Demo
{
    public $hasConstruct = false;
    public $hasInitialized = false;
 
    public $mark = null;
 
    public function __construct($mark)
    {
        //echo "Demo::__construct()\n";
 
        $this->mark = $mark;
    }
 
    public function onConstruct()
    {
        $this->hasConstruct = true;
        //echo "Demo::onConstruct()\n";
    }
 
    public function onInitialize()
    {  
        $this->hasInitialize = true;
        //echo "Demo:: onInitialize()\n";
    }
}
 
 
class Demo2 extends Demo
{
    public $number = 1;
 
    public function __construct()
    {
        //echo "Demo2::__construct()\n";   
    }
 
    public function onConstruct()
    {
        //echo "Demo2::onConstruct()\n";
        $this->number = 2;
        parent::onConstruct();
    }
 
    public function onInitialize()
    {
        //echo "Demo2::onInitialize()\n";
        $this->number = 3;
        parent::onInitialize();
    }
 
    public function onInit()
    {
        $this->onInitialize();
    }
}
 
class PhalApi_DI_Mock extends PhalApi_DI {
 
    public static function getInstance(){
        return PhalApi_DI::$instance;
    }
 
    public static function setInstance($instance) {
        PhalApi_DI::$instance = $instance;
    }
}