|
| 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\"?>\n" |
| 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>\n" |
| 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 | + $this->expectExceptionMessage('String could not be parsed as XML'); |
| 70 | + $notXml = json_encode(['foo' => 'bar']); |
| 71 | + $xpath = '/books/book'; |
| 72 | + XmlFilter::extract($notXml, $xpath); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @test |
| 77 | + * @covers ::extract |
| 78 | + */ |
| 79 | + public function extractNoElementFound() |
| 80 | + { |
| 81 | + $xpath = '/catalog/books/book'; |
| 82 | + $this->expectException(FilterException::class); |
| 83 | + $this->expectExceptionMessage(sprintf(XmlFilter::EXTRACT_NO_ELEMENT_FOUND_ERROR_FORMAT, $xpath)); |
| 84 | + XmlFilter::extract(self::FULL_XML, $xpath); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @test |
| 89 | + * @covers ::extract |
| 90 | + */ |
| 91 | + public function extractMultipleElementFound() |
| 92 | + { |
| 93 | + $xpath = '/books/book'; |
| 94 | + $this->expectException(FilterException::class); |
| 95 | + $this->expectExceptionMessage(sprintf(XmlFilter::EXTRACT_MULTIPLE_ELEMENTS_FOUND_ERROR_FORMAT, $xpath)); |
| 96 | + XmlFilter::extract(self::FULL_XML, $xpath); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @test |
| 101 | + * @covers ::validate |
| 102 | + */ |
| 103 | + public function validate() |
| 104 | + { |
| 105 | + $xml = self::FULL_XML; |
| 106 | + $xsdFile = __DIR__ . '/_files/books.xsd'; |
| 107 | + $validatedXml = XmlFilter::validate($xml, $xsdFile); |
| 108 | + $this->assertSame($xml, $validatedXml); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @test |
| 113 | + * @covers ::validate |
| 114 | + */ |
| 115 | + public function validateXmlMissingRequiredAttribute() |
| 116 | + { |
| 117 | + $xmlMissingId = ('' |
| 118 | + . '<books>' |
| 119 | + . '<book>' |
| 120 | + . '<author>Gambardella, Matthew</author>' |
| 121 | + . "<title>XML Developer's Guide</title>" |
| 122 | + . '<genre>Computer</genre>' |
| 123 | + . '<price>44.95</price>' |
| 124 | + . '<publish_date>2000-10-01</publish_date>' |
| 125 | + . '<description>An in-depth look at creating applications with XML.</description>' |
| 126 | + . '</book>' |
| 127 | + . '</books>' |
| 128 | + ); |
| 129 | + |
| 130 | + $this->expectException(FilterException::class); |
| 131 | + $this->expectExceptionMessage("Element 'book': The attribute 'id' is required but missing"); |
| 132 | + $xsdFile = __DIR__ . '/_files/books.xsd'; |
| 133 | + XmlFilter::validate($xmlMissingId, $xsdFile); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @test |
| 138 | + * @covers ::validate |
| 139 | + */ |
| 140 | + public function validateEmptyXml() |
| 141 | + { |
| 142 | + $emptyXml = ''; |
| 143 | + $this->expectException(FilterException::class); |
| 144 | + $this->expectExceptionMessage('String could not be parsed as XML'); |
| 145 | + $xsdFile = __DIR__ . '/_files/books.xsd'; |
| 146 | + XmlFilter::validate($emptyXml, $xsdFile); |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * @test |
| 151 | + * @covers ::filter |
| 152 | + */ |
| 153 | + public function filter() |
| 154 | + { |
| 155 | + $filteredXml = XmlFilter::filter(self::FULL_XML); |
| 156 | + $this->assertSame(self::FULL_XML, $filteredXml); |
| 157 | + } |
| 158 | +} |
0 commit comments