1 | <?php |
||
7 | class ArrayLoaderTest extends \PHPUnit_Framework_TestCase |
||
8 | { |
||
9 | private $loader; |
||
10 | |||
11 | protected function setUp() |
||
12 | { |
||
13 | $this->loader = new ArrayLoader(); |
||
14 | } |
||
15 | |||
16 | public function testSupports() |
||
17 | { |
||
18 | $this->assertFalse($this->loader->supports(__DIR__)); |
||
19 | $this->assertFalse($this->loader->supports(__FILE__)); |
||
20 | $this->assertFalse($this->loader->supports('string')); |
||
21 | $this->assertFalse($this->loader->supports(array('wrong_root'))); |
||
22 | $this->assertFalse($this->loader->supports(array('features'))); |
||
23 | $this->assertTrue($this->loader->supports(array('features' => array()))); |
||
24 | $this->assertTrue($this->loader->supports(array('feature' => array()))); |
||
25 | } |
||
26 | |||
27 | public function testLoadEmpty() |
||
28 | { |
||
29 | $this->assertEquals(array(), $this->loader->load(array('features' => array()))); |
||
30 | } |
||
31 | |||
32 | public function testLoadFeatures() |
||
33 | { |
||
34 | $features = $this->loader->load(array( |
||
35 | 'features' => array( |
||
36 | array( |
||
37 | 'title' => 'First feature', |
||
38 | 'line' => 3, |
||
39 | ), |
||
40 | array( |
||
41 | 'description' => 'Second feature description', |
||
42 | 'language' => 'ru', |
||
43 | 'tags' => array('some', 'tags') |
||
44 | ) |
||
45 | ), |
||
46 | )); |
||
47 | |||
48 | $this->assertEquals(2, count($features)); |
||
49 | |||
50 | $this->assertEquals(3, $features[0]->getLine()); |
||
51 | $this->assertEquals('First feature', $features[0]->getTitle()); |
||
52 | $this->assertNull($features[0]->getDescription()); |
||
53 | $this->assertNull($features[0]->getFile()); |
||
54 | $this->assertEquals('en', $features[0]->getLanguage()); |
||
55 | $this->assertFalse($features[0]->hasTags()); |
||
56 | |||
57 | $this->assertEquals(1, $features[1]->getLine()); |
||
58 | $this->assertNull($features[1]->getTitle()); |
||
59 | $this->assertEquals('Second feature description', $features[1]->getDescription()); |
||
60 | $this->assertNull($features[1]->getFile()); |
||
61 | $this->assertEquals('ru', $features[1]->getLanguage()); |
||
62 | $this->assertEquals(array('some', 'tags'), $features[1]->getTags()); |
||
63 | } |
||
64 | |||
65 | public function testLoadScenarios() |
||
66 | { |
||
67 | $features = $this->loader->load(array( |
||
68 | 'features' => array( |
||
69 | array( |
||
70 | 'title' => 'Feature', |
||
71 | 'scenarios' => array( |
||
72 | array( |
||
73 | 'title' => 'First scenario', |
||
74 | 'line' => 2 |
||
75 | ), |
||
76 | array( |
||
77 | 'tags' => array('second', 'scenario', 'tags') |
||
78 | ), |
||
79 | array( |
||
80 | 'tags' => array('third', 'scenario'), |
||
81 | 'line' => 3 |
||
82 | ) |
||
83 | ) |
||
84 | ) |
||
85 | ), |
||
86 | )); |
||
87 | |||
88 | $this->assertEquals(1, count($features)); |
||
89 | |||
90 | $scenarios = $features[0]->getScenarios(); |
||
91 | |||
92 | $this->assertEquals(3, count($scenarios)); |
||
93 | |||
94 | $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[0]); |
||
95 | $this->assertEquals('First scenario', $scenarios[0]->getTitle()); |
||
96 | $this->assertFalse($scenarios[0]->hasTags()); |
||
97 | $this->assertEquals(2, $scenarios[0]->getLine()); |
||
98 | |||
99 | $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[1]); |
||
100 | $this->assertNull($scenarios[1]->getTitle()); |
||
101 | $this->assertEquals(array('second', 'scenario', 'tags'), $scenarios[1]->getTags()); |
||
102 | $this->assertEquals(1, $scenarios[1]->getLine()); |
||
103 | |||
104 | $this->assertInstanceOf('Behat\Gherkin\Node\ScenarioNode', $scenarios[2]); |
||
105 | $this->assertNull($scenarios[2]->getTitle()); |
||
106 | $this->assertEquals(array('third', 'scenario'), $scenarios[2]->getTags()); |
||
107 | $this->assertEquals(3, $scenarios[2]->getLine()); |
||
108 | } |
||
109 | |||
110 | public function testLoadOutline() |
||
111 | { |
||
112 | $features = $this->loader->load(array( |
||
113 | 'features' => array( |
||
114 | array( |
||
115 | 'title' => 'Feature', |
||
116 | 'scenarios' => array( |
||
117 | array( |
||
118 | 'type' => 'outline', |
||
119 | 'title' => 'First outline', |
||
120 | 'line' => 2 |
||
121 | ), |
||
122 | array( |
||
123 | 'type' => 'outline', |
||
124 | 'tags' => array('second', 'outline', 'tags') |
||
125 | ) |
||
126 | ) |
||
127 | ) |
||
128 | ), |
||
129 | )); |
||
130 | |||
131 | $this->assertEquals(1, count($features)); |
||
132 | |||
133 | $outlines = $features[0]->getScenarios(); |
||
134 | |||
135 | $this->assertEquals(2, count($outlines)); |
||
136 | |||
137 | $this->assertInstanceOf('Behat\Gherkin\Node\OutlineNode', $outlines[0]); |
||
138 | $this->assertEquals('First outline', $outlines[0]->getTitle()); |
||
139 | $this->assertFalse($outlines[0]->hasTags()); |
||
140 | $this->assertEquals(2, $outlines[0]->getLine()); |
||
141 | |||
142 | $this->assertInstanceOf('Behat\Gherkin\Node\OutlineNode', $outlines[1]); |
||
143 | $this->assertNull($outlines[1]->getTitle()); |
||
144 | $this->assertEquals(array('second', 'outline', 'tags'), $outlines[1]->getTags()); |
||
145 | $this->assertEquals(1, $outlines[1]->getLine()); |
||
146 | } |
||
147 | |||
148 | public function testOutlineExamples() |
||
149 | { |
||
150 | $features = $this->loader->load(array( |
||
151 | 'features' => array( |
||
152 | array( |
||
153 | 'title' => 'Feature', |
||
154 | 'scenarios' => array( |
||
155 | array( |
||
156 | 'type' => 'outline', |
||
157 | 'title' => 'First outline', |
||
158 | 'line' => 2, |
||
159 | 'examples' => array( |
||
160 | array('user', 'pass'), |
||
161 | array('ever', 'sdsd'), |
||
162 | array('anto', 'fdfd') |
||
163 | ) |
||
164 | ), |
||
165 | array( |
||
166 | 'type' => 'outline', |
||
167 | 'tags' => array('second', 'outline', 'tags') |
||
168 | ) |
||
169 | ) |
||
170 | ) |
||
171 | ), |
||
172 | )); |
||
173 | |||
174 | $this->assertEquals(1, count($features)); |
||
175 | |||
176 | $scenarios = $features[0]->getScenarios(); |
||
177 | $scenario = $scenarios[0]; |
||
178 | |||
179 | $this->assertEquals( |
||
180 | array(array('user' => 'ever', 'pass' => 'sdsd'), array('user' => 'anto', 'pass' => 'fdfd')), |
||
181 | $scenario->getExampleTable()->getHash() |
||
|
|||
182 | ); |
||
183 | } |
||
184 | |||
185 | public function testLoadBackground() |
||
186 | { |
||
187 | $features = $this->loader->load(array( |
||
188 | 'features' => array( |
||
189 | array( |
||
190 | ), |
||
191 | array( |
||
192 | 'background' => array() |
||
193 | ), |
||
194 | array( |
||
195 | 'background' => array( |
||
196 | 'line' => 2 |
||
197 | ) |
||
198 | ), |
||
199 | ) |
||
200 | )); |
||
201 | |||
202 | $this->assertEquals(3, count($features)); |
||
203 | |||
204 | $this->assertFalse($features[0]->hasBackground()); |
||
205 | $this->assertTrue($features[1]->hasBackground()); |
||
206 | $this->assertEquals(0, $features[1]->getBackground()->getLine()); |
||
207 | $this->assertTrue($features[2]->hasBackground()); |
||
208 | $this->assertEquals(2, $features[2]->getBackground()->getLine()); |
||
209 | } |
||
210 | |||
211 | public function testLoadSteps() |
||
290 | |||
291 | public function testLoadStepArguments() |
||
367 | |||
368 | public function testSingleFeatureArray() |
||
369 | { |
||
370 | $features = $this->loader->load(array( |
||
371 | 'feature' => array( |
||
372 | 'title' => 'Some feature' |
||
373 | ) |
||
374 | )); |
||
375 | |||
379 | } |
||
380 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: