1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Saito - The Threaded Web Forum |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
10
|
|
|
* @license http://opensource.org/licenses/MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Saito\Test; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Non-depended assumptions |
19
|
|
|
*/ |
20
|
|
|
trait AssertTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* assert contains tag |
24
|
|
|
* |
25
|
|
|
* @param array $expected expected |
26
|
|
|
* @param string $result HTML |
27
|
|
|
* @return void |
28
|
|
|
*/ |
29
|
|
|
public function assertContainsTag($expected, $result) |
30
|
|
|
{ |
31
|
|
|
do { |
32
|
|
|
$crawler = new Crawler; |
33
|
|
|
$crawler->addHtmlContent($result); |
34
|
|
|
$selector = key($expected); |
35
|
|
|
$node = $crawler->filter($selector); |
36
|
|
|
$this->assertEquals( |
|
|
|
|
37
|
|
|
1, |
38
|
|
|
$node->count(), |
39
|
|
|
"Selector '$selector' not found." |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
if (isset($expected[$selector]['attributes'])) { |
43
|
|
|
foreach ($expected[$selector]['attributes'] as $attribute => $value) { |
44
|
|
|
$this->assertEquals($value, $node->attr($attribute)); |
|
|
|
|
45
|
|
|
} |
46
|
|
|
} |
47
|
|
|
} while (next($expected)); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* tests if XPath exists in HTML Source |
52
|
|
|
* |
53
|
|
|
* @param string $html HTML |
54
|
|
|
* @param string $path XPath |
55
|
|
|
* @param int $count how many times should XPath exist in HTML |
56
|
|
|
* @return mixed |
57
|
|
|
*/ |
58
|
|
|
public function assertXPath($html, $path, $count = 1) |
59
|
|
|
{ |
60
|
|
|
$xpath = $this->_getDOMXPath($html); |
61
|
|
|
$length = $xpath->query($path)->length; |
62
|
|
|
|
63
|
|
|
return $this->assertEquals( |
|
|
|
|
64
|
|
|
$count, |
65
|
|
|
$length, |
66
|
|
|
"Failed XPath. Expected '$path' to be found $count times instead of $length." |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* assert not xpath |
72
|
|
|
* |
73
|
|
|
* @param string $html path |
74
|
|
|
* @param string $path path |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function assertNotXPath($html, $path) |
78
|
|
|
{ |
79
|
|
|
return !$this->assertXPath($html, $path, 0); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* get dom xpath |
84
|
|
|
* |
85
|
|
|
* @param string $html HTML |
86
|
|
|
* @return \DOMXPath |
87
|
|
|
*/ |
88
|
|
|
protected function _getDOMXPath($html) |
89
|
|
|
{ |
90
|
|
|
$document = new \DOMDocument; |
91
|
|
|
libxml_use_internal_errors(true); |
92
|
|
|
$document->loadHTML('<!DOCTYPE html>' . $html); |
93
|
|
|
$xpath = new \DOMXPath($document); |
94
|
|
|
libxml_clear_errors(); |
95
|
|
|
|
96
|
|
|
return $xpath; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.