1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Cubiche package. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) Cubiche |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace Cubiche\Core\Delegate\Tests\Units; |
11
|
|
|
|
12
|
|
|
use Cubiche\Core\Delegate\Delegate; |
13
|
|
|
use Cubiche\Tests\TestCase; |
14
|
|
|
use Cubiche\Core\Delegate\Tests\Fixtures\FooCallable; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Delegate Tests class. |
18
|
|
|
* |
19
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class DelegateTests extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @param $value |
25
|
|
|
* |
26
|
|
|
* @return string |
27
|
|
|
*/ |
28
|
|
|
public function sampleMethod($value) |
29
|
|
|
{ |
30
|
|
|
return $value.'-sufix'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param $value |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public static function sampleStaticMethod($value) |
39
|
|
|
{ |
40
|
|
|
return $value.'-sufix'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Test fromClosure method. |
45
|
|
|
*/ |
46
|
|
|
public function testFromClosure() |
47
|
|
|
{ |
48
|
|
|
$this |
49
|
|
|
->given($closure = function () { |
50
|
|
|
|
51
|
|
|
}) |
52
|
|
|
->when($delegate = Delegate::fromClosure($closure)) |
53
|
|
|
->then() |
54
|
|
|
->object($delegate) |
55
|
|
|
->isInstanceOf(Delegate::class) |
56
|
|
|
; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Test fromMethod method. |
61
|
|
|
*/ |
62
|
|
|
public function testFromMethod() |
63
|
|
|
{ |
64
|
|
|
$this |
65
|
|
|
->when($delegate = Delegate::fromMethod($this, 'sampleMethod')) |
66
|
|
|
->then() |
67
|
|
|
->object($delegate) |
68
|
|
|
->isInstanceOf(Delegate::class) |
69
|
|
|
; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Test fromStaticMethod method. |
74
|
|
|
*/ |
75
|
|
|
public function testFromStaticMethod() |
76
|
|
|
{ |
77
|
|
|
$this |
78
|
|
|
->when($delegate = Delegate::fromStaticMethod(self::class, 'sampleStaticMethod')) |
79
|
|
|
->then() |
80
|
|
|
->object($delegate) |
81
|
|
|
->isInstanceOf(Delegate::class) |
82
|
|
|
; |
83
|
|
|
|
84
|
|
|
$this |
85
|
|
|
->when($delegate = new Delegate(self::class.'::sampleStaticMethod')) |
86
|
|
|
->then() |
87
|
|
|
->object($delegate) |
88
|
|
|
->isInstanceOf(Delegate::class) |
89
|
|
|
; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Test fromFunction method. |
94
|
|
|
*/ |
95
|
|
|
public function testFromFunction() |
96
|
|
|
{ |
97
|
|
|
$this |
98
|
|
|
->when($delegate = Delegate::fromFunction('array_filter')) |
99
|
|
|
->then() |
100
|
|
|
->object($delegate) |
101
|
|
|
->isInstanceOf(Delegate::class) |
102
|
|
|
->exception( |
103
|
|
|
function () { |
104
|
|
|
Delegate::fromFunction('foo'); |
105
|
|
|
} |
106
|
|
|
)->isInstanceOf(\InvalidArgumentException::class) |
107
|
|
|
; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Test __invoke method. |
112
|
|
|
*/ |
113
|
|
|
public function testInvoke() |
114
|
|
|
{ |
115
|
|
|
$this |
116
|
|
|
->given($closure = function ($value = null) { |
117
|
|
|
return $value; |
118
|
|
|
}) |
119
|
|
|
->when($delegate = Delegate::fromClosure($closure)) |
120
|
|
|
->then() |
121
|
|
|
->variable($delegate(5)) |
122
|
|
|
->isEqualTo(5) |
123
|
|
|
->given($delegate = Delegate::fromMethod($this, 'sampleMethod')) |
124
|
|
|
->then() |
125
|
|
|
->variable($delegate('text')) |
126
|
|
|
->isEqualTo('text-sufix') |
127
|
|
|
->given($delegate = Delegate::fromStaticMethod(self::class, 'sampleStaticMethod')) |
128
|
|
|
->then() |
129
|
|
|
->variable($delegate('text')) |
130
|
|
|
->isEqualTo('text-sufix') |
131
|
|
|
->given($delegate = Delegate::fromFunction('array_filter')) |
132
|
|
|
->then() |
133
|
|
|
->array($delegate(['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5], function ($value) { |
134
|
|
|
return $value % 2 === 0; |
135
|
|
|
})) |
136
|
|
|
->isEqualTo(['b' => 2, 'd' => 4]) |
137
|
|
|
; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Test reflection method. |
142
|
|
|
*/ |
143
|
|
|
public function testReflection() |
144
|
|
|
{ |
145
|
|
|
$this |
146
|
|
|
->given($closure = function ($value = null) { |
147
|
|
|
return $value; |
148
|
|
|
}) |
149
|
|
|
->when($reflection = Delegate::fromClosure($closure)->reflection()) |
150
|
|
|
->then() |
151
|
|
|
->object($reflection) |
152
|
|
|
->isEqualTo(new \ReflectionFunction($closure)) |
153
|
|
|
; |
154
|
|
|
|
155
|
|
|
$this |
156
|
|
|
->when($reflection = Delegate::fromMethod($this, 'sampleMethod')->reflection()) |
157
|
|
|
->then() |
158
|
|
|
->object($reflection) |
159
|
|
|
->isEqualTo(new \ReflectionMethod($this, 'sampleMethod')) |
160
|
|
|
; |
161
|
|
|
|
162
|
|
|
$this |
163
|
|
|
->given($foo = new FooCallable()) |
164
|
|
|
->when($reflection = (new Delegate($foo))->reflection()) |
165
|
|
|
->then() |
166
|
|
|
->object($reflection) |
167
|
|
|
->isEqualTo(new \ReflectionMethod($foo, '__invoke')); |
168
|
|
|
|
169
|
|
|
$this |
170
|
|
|
->when($reflection = Delegate::fromStaticMethod(self::class, 'sampleStaticMethod')->reflection()) |
171
|
|
|
->then() |
172
|
|
|
->object($reflection) |
173
|
|
|
->isEqualTo(new \ReflectionMethod(self::class.'::sampleStaticMethod')) |
174
|
|
|
; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|