1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* Copyright (C) 2015 Michael Herold <[email protected]> |
4
|
|
|
* |
5
|
|
|
* This program is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* This program is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace hemio\html; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Description of IteratorTest |
23
|
|
|
* |
24
|
|
|
* @author Michael Herold <[email protected]> |
25
|
|
|
*/ |
26
|
|
|
class IteratorTest extends \Helpers |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
public function testRecursiveIterator() |
30
|
|
|
{ |
31
|
|
|
$doc = $this->getExample(); |
32
|
|
|
$this->assertCount(13, $doc->getRecursiveIterator()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testRecursiveIteratorFiltered() |
36
|
|
|
{ |
37
|
|
|
$doc = $this->getExample(); |
38
|
|
|
|
39
|
|
|
$select = function (Interface_\HtmlCode $child) { |
40
|
|
|
return $child instanceof \hemio\html\Div; |
41
|
|
|
}; |
42
|
|
|
|
43
|
|
|
foreach ($doc->getRecursiveIterator($select) as $child) |
44
|
|
|
$this->assertInstanceOf('\hemio\html\Div', $child); |
45
|
|
|
|
46
|
|
|
$this->assertCount(3, $doc->getRecursiveIterator($select)); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function getExample() |
50
|
|
|
{ |
51
|
|
|
$doc = new Document(new Str('Title')); |
52
|
|
|
|
53
|
|
|
$doc->getHtml() |
|
|
|
|
54
|
|
|
->addChild(new Header()) |
55
|
|
|
->addChild(new Div()); |
56
|
|
|
|
57
|
|
|
$doc->getHtml() |
|
|
|
|
58
|
|
|
->addChild(new Div()) |
59
|
|
|
->addChild(new Section()) |
60
|
|
|
->addChild(new Article()) |
61
|
|
|
->addChild(new Div()); |
62
|
|
|
|
63
|
|
|
return $doc; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
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: