Completed
Push — master ( 4025c4...856f8c )
by Nikita
02:41
created

XMLAssertsTrait::toXPath()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
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);
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like assertGreaterThan() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
38
        } else {
39
            $this->assertSame($count, $list->length, $message);
0 ignored issues
show
Bug introduced by
It seems like assertSame() must be provided by classes using this trait. How about adding it as abstract method to this trait?

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

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). 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.

Loading history...
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