1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Taisiya\PropelBundle; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @target \PHPUnit_Framework_Assert |
7
|
|
|
*/ |
8
|
|
|
trait XMLAssertsTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @param string|\DOMDocument $xml |
12
|
|
|
* @param string $version |
13
|
|
|
* @param string $encoding |
14
|
|
|
* @param string $message |
15
|
|
|
*/ |
16
|
|
|
public function assertXmlHasProlog($xml, $version = '1.0', $encoding = 'UTF-8', string $message = ''): void |
17
|
|
|
{ |
18
|
|
|
$this->assertSame(0, strpos($xml, '<?xml version="'.$version.'" encoding="'.$encoding.'"?>'), $message); |
|
|
|
|
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string|\DOMDocument $xml |
23
|
|
|
* @param string $query |
24
|
|
|
* @param int|null $count |
25
|
|
|
* @param string $message |
26
|
|
|
*/ |
27
|
|
|
public function assertXmlHasElements($xml, string $query, int $count = null, string $message = '') |
28
|
|
|
{ |
29
|
|
|
if ($message === '') { |
30
|
|
|
$message = 'XML has elements (XPath query is \''.$query.'\')'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
$xpath = $this->toXPath($xml); |
34
|
|
|
$list = $xpath->query($query); |
35
|
|
|
|
36
|
|
|
if ($count === null) { |
37
|
|
|
$this->assertGreaterThan(0, $list->length, $message); |
|
|
|
|
38
|
|
|
} else { |
39
|
|
|
$this->assertSame($count, $list->length, $message); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string|\DOMDocument $xml |
45
|
|
|
* @throws \InvalidArgumentException |
46
|
|
|
* @return \DOMXPath |
47
|
|
|
*/ |
48
|
|
|
private function toXPath($xml): \DOMXPath |
49
|
|
|
{ |
50
|
|
|
if ($xml instanceof \DOMDocument) { |
51
|
|
|
$dom = $xml; |
52
|
|
|
} elseif (is_string($xml)) { |
53
|
|
|
$dom = new \DOMDocument('1.0', 'UTF-8'); |
54
|
|
|
if (!$dom->loadXML($xml)) { |
55
|
|
|
throw new \InvalidArgumentException('Couldn\'t load XML string to DOM object'); |
56
|
|
|
} |
57
|
|
|
} else { |
58
|
|
|
throw new \InvalidArgumentException('XML must be a string or DOMDocument'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return new \DOMXPath($dom); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
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.