Passed
Pull Request — master (#17)
by Chad
02:13
created

XmlFilterTest::validate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
1
<?php
2
3
namespace Filter;
4
5
use PHPUnit\Framework\TestCase;
6
use TraderInteractive\Exceptions\FilterException;
7
use TraderInteractive\Filter\XmlFilter;
8
9
/**
10
 * @coversDefaultClass \TraderInteractive\Filter\XmlFilter
11
 * @covers ::<private>
12
 */
13
final class XmlFilterTest extends TestCase
14
{
15
    /**
16
     * @var string
17
     */
18
    const FULL_XML = (''
19
        . '<?xml version="1.0"?>'
20
        . '<books>'
21
            . '<book id="bk101">'
22
                . '<author>Gambardella, Matthew</author>'
23
                . "<title>XML Developer's Guide</title>"
24
                . '<genre>Computer</genre>'
25
                . '<price>44.95</price>'
26
                . '<publish_date>2000-10-01</publish_date>'
27
                . '<description>An in-depth look at creating applications with XML.</description>'
28
            . '</book>'
29
            . '<book id="bk102">'
30
                . '<author>Ralls, Kim</author>'
31
                . '<title>Midnight Rain</title>'
32
                . '<genre>Fantasy</genre>'
33
                . '<price>5.95</price>'
34
                . '<publish_date>2000-12-16</publish_date>'
35
                . '<description>A former architect battles corporate zombies</description>'
36
            . '</book>'
37
        . '</books>'
38
    );
39
40
    /**
41
     * @test
42
     * @covers ::extract
43
     */
44
    public function extract()
45
    {
46
        $xpath = "/books/book[@id='bk101']";
47
        $actualXml = XmlFilter::extract(self::FULL_XML, $xpath);
48
        $expectedXml = (''
49
            . '<book id="bk101">'
50
            . '<author>Gambardella, Matthew</author>'
51
            . "<title>XML Developer's Guide</title>"
52
            . '<genre>Computer</genre>'
53
            . '<price>44.95</price>'
54
            . '<publish_date>2000-10-01</publish_date>'
55
            . '<description>An in-depth look at creating applications with XML.</description>'
56
            . '</book>'
57
        );
58
59
        $this->assertSame($expectedXml, $actualXml);
60
    }
61
62
    /**
63
     * @test
64
     * @covers ::extract
65
     */
66
    public function extractNonXmlValue()
67
    {
68
        $this->expectException(FilterException::class);
69
        $notXml = json_encode(['foo' => 'bar']);
70
        $xpath = '/books/book';
71
        XmlFilter::extract($notXml, $xpath);
72
    }
73
74
    /**
75
     * @test
76
     * @covers ::extract
77
     */
78
    public function extractNoElementFound()
79
    {
80
        $this->expectException(FilterException::class);
81
        $xpath = '/catalog/books/book';
82
        XmlFilter::extract(self::FULL_XML, $xpath);
83
    }
84
85
    /**
86
     * @test
87
     * @covers ::extract
88
     */
89
    public function extractMultipleElementFound()
90
    {
91
        $this->expectException(FilterException::class);
92
        $xpath = '/books/book';
93
        XmlFilter::extract(self::FULL_XML, $xpath);
94
    }
95
96
    /**
97
     * @test
98
     * @covers ::validate
99
     */
100
    public function validate()
101
    {
102
        $xml = self::FULL_XML;
103
        $xsdFile = __DIR__ . '/_files/books.xsd';
104
        $validatedXml = XmlFilter::validate($xml, $xsdFile);
105
        $this->assertSame($xml, $validatedXml);
106
    }
107
108
    /**
109
     * @test
110
     * @covers ::validate
111
     */
112
    public function validateXmlMissingRequiredAttribute()
113
    {
114
        $xmlMissingTitle = (''
115
            . '<books>'
116
                . '<book>'
117
                    . '<author>Gambardella, Matthew</author>'
118
                    . "<title>XML Developer's Guide</title>"
119
                    . '<genre>Computer</genre>'
120
                    . '<price>44.95</price>'
121
                    . '<publish_date>2000-10-01</publish_date>'
122
                    . '<description>An in-depth look at creating applications with XML.</description>'
123
                . '</book>'
124
            . '</books>'
125
        );
126
127
        $this->expectException(FilterException::class);
128
        $this->expectExceptionMessage("Element 'book': The attribute 'id' is required but missing");
129
        $xsdFile = __DIR__ . '/_files/books.xsd';
130
        XmlFilter::validate($xmlMissingTitle, $xsdFile);
131
    }
132
}
133