|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class PrototypeTest extends PHPUnit_Framework_TestCase |
|
|
|
|
|
|
4
|
|
|
{ |
|
5
|
|
|
public function testExtend() |
|
6
|
|
|
{ |
|
7
|
|
|
$tester = $this; |
|
8
|
|
|
|
|
9
|
|
|
$o = new Rde\Prototype; |
|
10
|
|
|
$o['sayHello'] = function($o){ |
|
|
|
|
|
|
11
|
|
|
return "Hello"; |
|
12
|
|
|
}; |
|
13
|
|
|
$o['sum'] = function($o, $a, $b) use($tester) { |
|
14
|
|
|
$tester->assertTrue(isset($o['sum']), '確認注入程序'); |
|
15
|
|
|
return $a + $b; |
|
16
|
|
|
}; |
|
17
|
|
|
|
|
18
|
|
|
$this->assertFalse(method_exists($o, 'sayHello'), '測試method_exists'); |
|
19
|
|
|
$this->assertTrue(is_callable(array($o, 'sayHelloxx')), '測試is_callable sayHelloxx(不存在)'); |
|
20
|
|
|
$this->assertFalse($o->hasDriver('sayHelloxx'), '測試hasDriver(sayHelloxx)'); |
|
21
|
|
|
$this->assertTrue(is_callable(array($o, 'sayHello')), '測試is_callable'); |
|
22
|
|
|
$this->assertTrue($o->hasDriver('sayHello'), '測試hasDriver(sayHello)'); |
|
23
|
|
|
|
|
24
|
|
|
$this->assertEquals( |
|
25
|
|
|
'Hello', |
|
26
|
|
|
$o->sayHello(), |
|
|
|
|
|
|
27
|
|
|
'檢查建構注入'); |
|
28
|
|
|
|
|
29
|
|
|
$ret = $o->extend('sayHello', function($o, $name){ |
|
30
|
|
|
return "hello {$name}"; |
|
31
|
|
|
}); |
|
32
|
|
|
|
|
33
|
|
|
$this->assertEquals($o, $ret, '檢查物件方法鍊'); |
|
34
|
|
|
|
|
35
|
|
|
$this->assertEquals( |
|
36
|
|
|
'hello abc', |
|
37
|
|
|
$o->sayHello('abc'), |
|
|
|
|
|
|
38
|
|
|
'檢查動態注入覆寫'); |
|
39
|
|
|
|
|
40
|
|
|
$this->assertEquals( |
|
41
|
|
|
9, |
|
42
|
|
|
$o->sum(1, 8), |
|
|
|
|
|
|
43
|
|
|
'檢查注入驅動處理'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function testArrayAccess() |
|
47
|
|
|
{ |
|
48
|
|
|
$tester = $this; |
|
49
|
|
|
$o = new Rde\Prototype(); |
|
50
|
|
|
|
|
51
|
|
|
$o['verify'] = function($o, array $balls) use($tester) { |
|
52
|
|
|
|
|
53
|
|
|
$tester->assertInstanceOf('\\Rde\\Prototype', $o, '檢查容器'); |
|
54
|
|
|
|
|
55
|
|
|
return 5 == count(array_unique($balls)); |
|
56
|
|
|
}; |
|
57
|
|
|
|
|
58
|
|
|
$this->assertTrue(isset($o['verify']), '檢查驅動'); |
|
59
|
|
|
|
|
60
|
|
|
$this->assertTrue( |
|
61
|
|
|
$o->verify(array(1,2,3,4,5)), |
|
|
|
|
|
|
62
|
|
|
'檢查注入驗證回傳'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @expectedException \BadMethodCallException |
|
67
|
|
|
* @expectedExceptionMessageRegExp /沒有安裝\[\w+\]處理驅動/ |
|
68
|
|
|
*/ |
|
69
|
|
|
public function testException() |
|
70
|
|
|
{ |
|
71
|
|
|
$o = new Rde\Prototype(); |
|
72
|
|
|
|
|
73
|
|
|
$o->getColde(); |
|
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
public function testToString() |
|
77
|
|
|
{ |
|
78
|
|
|
$o = new Rde\Prototype(); |
|
79
|
|
|
|
|
80
|
|
|
$this->assertEquals('['.get_class($o).']', (string) $o, '檢查預設轉型(string)'); |
|
81
|
|
|
|
|
82
|
|
|
$str = 'hello world'; |
|
83
|
|
|
$o->extend('__toString', function() use($str){ |
|
84
|
|
|
return $str; |
|
85
|
|
|
}); |
|
86
|
|
|
|
|
87
|
|
|
$this->assertEquals($str, (string) $o, '檢查自訂轉型(string)'); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.