|
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\Selector\Tests\Units; |
|
11
|
|
|
|
|
12
|
|
|
use Cubiche\Core\Selector\Composite; |
|
13
|
|
|
use Cubiche\Core\Selector\Count; |
|
14
|
|
|
use Cubiche\Core\Selector\Custom; |
|
15
|
|
|
use Cubiche\Core\Selector\Key; |
|
16
|
|
|
use Cubiche\Core\Selector\Method; |
|
17
|
|
|
use Cubiche\Core\Selector\Property; |
|
18
|
|
|
use Cubiche\Core\Selector\SelectorFactoryInterface; |
|
19
|
|
|
use Cubiche\Core\Selector\Selectors; |
|
20
|
|
|
use Cubiche\Core\Selector\Value; |
|
21
|
|
|
use Cubiche\Core\Visitor\VisitorInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Selector Builder Tests Class. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
27
|
|
|
*/ |
|
28
|
|
|
class SelectorsTests extends SelectorTestCase |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* {@inheritdoc} |
|
32
|
|
|
*/ |
|
33
|
|
|
public function newDefaultTestedInstance() |
|
34
|
|
|
{ |
|
35
|
|
|
return Selectors::key('foo')->key('bar'); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function acceptVisitorDataProvider() |
|
42
|
|
|
{ |
|
43
|
|
|
return array( |
|
44
|
|
|
array($this->newMockInstance(VisitorInterface::class), 'visit', 'accept'), |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Test apply. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function testSelector() |
|
52
|
|
|
{ |
|
53
|
|
|
$this |
|
54
|
|
|
/* @var \Cubiche\Core\Selector\Selectors $builder */ |
|
55
|
|
|
->given($builder = $this->newDefaultTestedInstance()) |
|
56
|
|
|
->then() |
|
57
|
|
|
/* @var \Cubiche\Core\Selector\Composite $selector */ |
|
58
|
|
|
->object($selector = $builder->selector()) |
|
59
|
|
|
->isInstanceOf(Composite::class) |
|
60
|
|
|
/* @var \Cubiche\Core\Selector\Key $foo */ |
|
61
|
|
|
->object($foo = $selector->valueSelector()) |
|
|
|
|
|
|
62
|
|
|
->isInstanceOf(Key::class) |
|
63
|
|
|
->string($foo->name()) |
|
64
|
|
|
->isEqualTo('foo') |
|
65
|
|
|
/* @var \Cubiche\Core\Selector\Key $foo */ |
|
66
|
|
|
->object($bar = $selector->applySelector()) |
|
|
|
|
|
|
67
|
|
|
->isInstanceOf(Key::class) |
|
68
|
|
|
->string($bar->name()) |
|
69
|
|
|
->isEqualTo('bar') |
|
70
|
|
|
; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Test addSelector. |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function testAddSelector() |
|
|
|
|
|
|
77
|
|
|
{ |
|
78
|
|
|
$this |
|
79
|
|
|
/* @var \Cubiche\Core\Selector\Selectors $builder */ |
|
80
|
|
|
->given($factory = $this->newMockInstance(SelectorFactoryInterface::class)) |
|
81
|
|
|
->when(function () use ($factory) { |
|
82
|
|
|
Selectors::setFactory($factory); |
|
|
|
|
|
|
83
|
|
|
Selectors::addSelector('foo', 'bar'); |
|
84
|
|
|
}) |
|
85
|
|
|
->then() |
|
86
|
|
|
->mock($factory) |
|
87
|
|
|
->call('addSelector') |
|
88
|
|
|
->withArguments('foo', 'bar') |
|
89
|
|
|
->once() |
|
90
|
|
|
; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Test addSelector. |
|
95
|
|
|
*/ |
|
96
|
|
View Code Duplication |
public function testAddNamespace() |
|
|
|
|
|
|
97
|
|
|
{ |
|
98
|
|
|
$this |
|
99
|
|
|
/* @var \Cubiche\Core\Selector\Selectors $builder */ |
|
100
|
|
|
->given($factory = $this->newMockInstance(SelectorFactoryInterface::class)) |
|
101
|
|
|
->when(function () use ($factory) { |
|
102
|
|
|
Selectors::setFactory($factory); |
|
|
|
|
|
|
103
|
|
|
Selectors::addNamespace('foo'); |
|
104
|
|
|
}) |
|
105
|
|
|
->then() |
|
106
|
|
|
->mock($factory) |
|
107
|
|
|
->call('addNamespace') |
|
108
|
|
|
->withArguments('foo') |
|
109
|
|
|
->once() |
|
110
|
|
|
; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Test apply. |
|
115
|
|
|
*/ |
|
116
|
|
|
public function testApply() |
|
117
|
|
|
{ |
|
118
|
|
|
$this |
|
119
|
|
|
/* @var \Cubiche\Core\Selector\Selectors $builder */ |
|
120
|
|
|
->given($builder = $this->newDefaultTestedInstance()) |
|
121
|
|
|
->then() |
|
122
|
|
|
->string($builder->apply(array('foo' => array('bar' => 'baz')))) |
|
123
|
|
|
->isEqualTo('baz') |
|
124
|
|
|
; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Test acceptSelectorVisitor. |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testAcceptSelectorVisitor() |
|
131
|
|
|
{ |
|
132
|
|
|
$this |
|
133
|
|
|
->given( |
|
134
|
|
|
$visitorMock = $this->newMockVisitorInterface(), |
|
135
|
|
|
$shouldVisitMethod = 'visitComposite' |
|
136
|
|
|
) |
|
137
|
|
|
->calling($visitorMock) |
|
138
|
|
|
->methods( |
|
139
|
|
|
function ($method) use ($shouldVisitMethod) { |
|
140
|
|
|
return $method === \strtolower($shouldVisitMethod); |
|
141
|
|
|
} |
|
142
|
|
|
) |
|
143
|
|
|
->return = 25 |
|
144
|
|
|
; |
|
145
|
|
|
|
|
146
|
|
|
$this |
|
147
|
|
|
/* @var \Cubiche\Core\Selector\Selectors $builder */ |
|
148
|
|
|
->given($builder = $this->newDefaultTestedInstance()) |
|
149
|
|
|
->when($result = $builder->acceptSelectorVisitor($visitorMock)) |
|
|
|
|
|
|
150
|
|
|
->then() |
|
151
|
|
|
->mock($visitorMock) |
|
152
|
|
|
->call($shouldVisitMethod) |
|
153
|
|
|
->withArguments($builder->selector()) |
|
154
|
|
|
->once() |
|
155
|
|
|
->integer($result) |
|
156
|
|
|
->isEqualTo(25) |
|
157
|
|
|
; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Test __staticCall. |
|
162
|
|
|
* |
|
163
|
|
|
* @dataProvider staticCallDataProvider |
|
164
|
|
|
*/ |
|
165
|
|
|
public function testStaticCall(Selectors $builder, $expectedClass) |
|
166
|
|
|
{ |
|
167
|
|
|
$this |
|
168
|
|
|
->given($builder, $expectedClass) |
|
169
|
|
|
->then() |
|
170
|
|
|
->object($builder->selector()) |
|
171
|
|
|
->isInstanceOf($expectedClass) |
|
172
|
|
|
; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return string[][] |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function staticCallDataProvider() |
|
179
|
|
|
{ |
|
180
|
|
|
return array( |
|
181
|
|
|
array(Selectors::key('foo'), Key::class), |
|
182
|
|
|
array(Selectors::property('foo'), Property::class), |
|
183
|
|
|
array(Selectors::method('foo'), Method::class), |
|
184
|
|
|
array(Selectors::custom(function () { |
|
185
|
|
|
|
|
186
|
|
|
}), Custom::class), |
|
187
|
|
|
array(Selectors::count(), Count::class), |
|
188
|
|
|
array(Selectors::value('foo'), Value::class), |
|
189
|
|
|
array(Selectors::composite( |
|
190
|
|
|
Selectors::value(array()), |
|
191
|
|
|
Selectors::count() |
|
192
|
|
|
), Composite::class), |
|
193
|
|
|
); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.