bluetree-service /
data
| 1 | <?php |
||||
| 2 | /** |
||||
| 3 | * test Xml |
||||
| 4 | * |
||||
| 5 | * @package BlueData |
||||
| 6 | * @subpackage Test |
||||
| 7 | * @author MichaĆ Adamiak <[email protected]> |
||||
| 8 | * @copyright bluetree-service |
||||
| 9 | */ |
||||
| 10 | namespace Test; |
||||
| 11 | |||||
| 12 | use BlueData\Data\Xml; |
||||
| 13 | use PHPUnit\Framework\TestCase; |
||||
| 14 | |||||
| 15 | class XmlTest extends TestCase |
||||
| 16 | { |
||||
| 17 | const XML_TEST_FILE = 'tests/data/test.xml'; |
||||
| 18 | const XML_EXPECTED = 'tests/data/expected.xml'; |
||||
| 19 | const XML_SOURCE = 'tests/data/source.xml'; |
||||
| 20 | const XML_DTD = 'tests/data/source_dtd.xml'; |
||||
| 21 | const XML_DTD_ID = 'tests/data/source_ID.xml'; |
||||
| 22 | const XML_BROKEN = 'tests/data/source_broken.xml'; |
||||
| 23 | const XML_NO_EXISTS = 'none_exists.xml'; |
||||
| 24 | |||||
| 25 | /** |
||||
| 26 | * @var string |
||||
| 27 | */ |
||||
| 28 | protected $xmlTestFilePath; |
||||
| 29 | |||||
| 30 | public function setUp() |
||||
| 31 | { |
||||
| 32 | $this->tearDown(); |
||||
| 33 | } |
||||
| 34 | |||||
| 35 | /** |
||||
| 36 | * test creating new xml object |
||||
| 37 | */ |
||||
| 38 | public function testXmlCreation() |
||||
| 39 | { |
||||
| 40 | $xml = new Xml(); |
||||
| 41 | $this->assertEquals('1.0', $xml->version); |
||||
| 42 | $this->assertEquals('UTF-8', $xml->encoding); |
||||
| 43 | |||||
| 44 | $xml = new Xml([ |
||||
| 45 | 'version' => '1.0', |
||||
| 46 | 'encoding' =>'iso-8859-1' |
||||
| 47 | ]); |
||||
| 48 | $this->assertEquals('1.0', $xml->version); |
||||
| 49 | $this->assertEquals('iso-8859-1', $xml->encoding); |
||||
| 50 | } |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * test search nodes by attribute name |
||||
| 54 | */ |
||||
| 55 | public function testSearchByAttributeAccess() |
||||
| 56 | { |
||||
| 57 | $xml = $this->createSimpleXml(); |
||||
| 58 | |||||
| 59 | $list = $xml->searchByAttribute($xml->childNodes, 'attr'); |
||||
| 60 | |||||
| 61 | $this->assertArrayHasKey('a', $list); |
||||
| 62 | $this->assertArrayHasKey('b', $list); |
||||
| 63 | $this->assertArrayHasKey('c', $list); |
||||
| 64 | } |
||||
| 65 | |||||
| 66 | /** |
||||
| 67 | * test loading xml data from file |
||||
| 68 | */ |
||||
| 69 | public function testFileLoading() |
||||
| 70 | { |
||||
| 71 | $xml = new Xml; |
||||
| 72 | $loaded = $xml->loadXmlFile(self::XML_SOURCE); |
||||
| 73 | |||||
| 74 | $this->assertTrue($loaded); |
||||
| 75 | $this->assertFalse($xml->hasErrors()); |
||||
| 76 | |||||
| 77 | $root = $xml->documentElement; |
||||
| 78 | $this->assertEquals( |
||||
| 79 | 'lorem ipsum', |
||||
| 80 | $root->getElementsByTagName('sub')->item(0)->nodeValue |
||||
| 81 | ); |
||||
| 82 | } |
||||
| 83 | |||||
| 84 | public function testFileLoadingWithParse() |
||||
| 85 | { |
||||
| 86 | $xml = new Xml; |
||||
| 87 | $loaded = $xml->loadXmlFile(self::XML_DTD, true); |
||||
| 88 | |||||
| 89 | $this->assertTrue($loaded); |
||||
| 90 | $this->assertFalse($xml->hasErrors()); |
||||
| 91 | } |
||||
| 92 | |||||
| 93 | public function testLoadingNoneExistingFile() |
||||
| 94 | { |
||||
| 95 | $this->assertFileNotExists(self::XML_NO_EXISTS, 'test file exists'); |
||||
| 96 | |||||
| 97 | $xml = new Xml; |
||||
| 98 | $loaded = $xml->loadXmlFile(self::XML_NO_EXISTS); |
||||
| 99 | |||||
| 100 | $this->assertFalse($loaded); |
||||
| 101 | $this->assertTrue($xml->hasErrors()); |
||||
| 102 | $this->assertEquals('file_not_exists', $xml->getError()); |
||||
| 103 | } |
||||
| 104 | |||||
| 105 | public function testLoadingBrokenXml() |
||||
| 106 | { |
||||
| 107 | $xml = new Xml; |
||||
| 108 | $loaded = $xml->loadXmlFile(self::XML_BROKEN); |
||||
| 109 | |||||
| 110 | $this->assertFalse($loaded); |
||||
| 111 | $this->assertTrue($xml->hasErrors()); |
||||
| 112 | $this->assertEquals('loading_file_error', $xml->getError()); |
||||
| 113 | } |
||||
| 114 | |||||
| 115 | public function testLoadingNoneValidXml() |
||||
| 116 | { |
||||
| 117 | $xml = new Xml; |
||||
| 118 | $loaded = $xml->loadXmlFile(self::XML_SOURCE, true); |
||||
| 119 | |||||
| 120 | $this->assertFalse($loaded); |
||||
| 121 | $this->assertTrue($xml->hasErrors()); |
||||
| 122 | $this->assertEquals('parse_file_error', $xml->getError()); |
||||
| 123 | |||||
| 124 | $this->assertFalse($xml->clearErrors()->hasErrors()); |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | public function testSaveXmlAsString() |
||||
| 128 | { |
||||
| 129 | $xml = $this->createSimpleXml(); |
||||
| 130 | $xmlString = $xml->saveXmlFile(); |
||||
| 131 | |||||
| 132 | $this->assertEquals(file_get_contents(self::XML_EXPECTED), $xmlString); |
||||
| 133 | |||||
| 134 | ob_start(function ($output) { |
||||
| 135 | $this->assertEquals(file_get_contents(self::XML_EXPECTED), $output); |
||||
| 136 | }); |
||||
| 137 | echo $xml; |
||||
| 138 | ob_end_flush(); |
||||
| 139 | } |
||||
| 140 | |||||
| 141 | public function testSaveXmlAsFile() |
||||
| 142 | { |
||||
| 143 | $val = $this->createSimpleXml() |
||||
| 144 | ->saveXmlFile(self::XML_TEST_FILE); |
||||
| 145 | |||||
| 146 | $this->assertTrue($val); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 147 | $this->assertFileExists(self::XML_TEST_FILE); |
||||
| 148 | } |
||||
| 149 | |||||
| 150 | public function testSaveXmlWithError() |
||||
| 151 | { |
||||
| 152 | $xml = $this->createSimpleXml(); |
||||
| 153 | $val = $xml->saveXmlFile(self::XML_NO_EXISTS . '/\\'); |
||||
| 154 | |||||
| 155 | $this->assertFalse($val); |
||||
|
0 ignored issues
–
show
It seems like
$val can also be of type string; however, parameter $condition of PHPUnit_Framework_Assert::assertFalse() does only seem to accept boolean, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 156 | $this->assertEquals($xml->getError(), 'save_file_error'); |
||||
| 157 | $this->assertTrue($xml->hasErrors()); |
||||
| 158 | $this->assertFileNotExists(self::XML_NO_EXISTS . '/\\'); |
||||
| 159 | } |
||||
| 160 | |||||
| 161 | public function testThatIdExists() |
||||
| 162 | { |
||||
| 163 | $xml = new Xml; |
||||
| 164 | $loaded = $xml->loadXmlFile(self::XML_DTD_ID); |
||||
| 165 | |||||
| 166 | $this->assertTrue($loaded); |
||||
| 167 | $this->assertFalse($xml->hasErrors()); |
||||
| 168 | |||||
| 169 | $this->assertTrue($xml->checkId('sub-id-2')); |
||||
| 170 | $this->assertFalse($xml->checkId('unknown')); |
||||
| 171 | } |
||||
| 172 | |||||
| 173 | public function testGetElementById() |
||||
| 174 | { |
||||
| 175 | $xml = new Xml; |
||||
| 176 | $loaded = $xml->loadXmlFile(self::XML_DTD_ID); |
||||
| 177 | |||||
| 178 | $this->assertTrue($loaded); |
||||
| 179 | $this->assertFalse($xml->hasErrors()); |
||||
| 180 | |||||
| 181 | $this->assertEquals('data 1', $xml->getId('sub-id-2')->nodeValue); |
||||
| 182 | } |
||||
| 183 | |||||
| 184 | public function tearDown() |
||||
| 185 | { |
||||
| 186 | if (file_exists(self::XML_TEST_FILE)) { |
||||
| 187 | unlink(self::XML_TEST_FILE); |
||||
| 188 | } |
||||
| 189 | } |
||||
| 190 | |||||
| 191 | /** |
||||
| 192 | * @return Xml |
||||
| 193 | */ |
||||
| 194 | protected function createSimpleXml() |
||||
| 195 | { |
||||
| 196 | $xml = new Xml; |
||||
| 197 | $root = $xml->createElement('root'); |
||||
| 198 | |||||
| 199 | $testNode = $xml->createElement('test'); |
||||
| 200 | $testNode->setAttribute('attr', 'a'); |
||||
| 201 | $root->appendChild($testNode); |
||||
| 202 | |||||
| 203 | $testNode = $xml->createElement('test'); |
||||
| 204 | $testNode->setAttribute('attr', 'b'); |
||||
| 205 | $root->appendChild($testNode); |
||||
| 206 | |||||
| 207 | $testNode = $xml->createElement('test'); |
||||
| 208 | $testNode->setAttribute('attr', 'c'); |
||||
| 209 | $root->appendChild($testNode); |
||||
| 210 | |||||
| 211 | $xml->appendChild($root); |
||||
| 212 | |||||
| 213 | return $xml; |
||||
| 214 | } |
||||
| 215 | } |
||||
| 216 |