|
| 1 | +<?php |
| 2 | + |
| 3 | +use PlinCode\KmlParser\Exceptions\KmlException; |
| 4 | +use PlinCode\KmlParser\Exceptions\KmlParserException; |
| 5 | +use PlinCode\KmlParser\KmlParser; |
| 6 | +use PlinCode\KmlParser\Validators\KmlValidator; |
| 7 | + |
| 8 | +function kmlWithLatitude(string $latitude): string |
| 9 | +{ |
| 10 | + return <<<XML |
| 11 | +<?xml version="1.0" encoding="UTF-8"?> |
| 12 | +<kml xmlns="http://www.opengis.net/kml/2.2"> |
| 13 | + <Document> |
| 14 | + <Placemark> |
| 15 | + <Point> |
| 16 | + <coordinates>7.7300965,{$latitude},0</coordinates> |
| 17 | + </Point> |
| 18 | + </Placemark> |
| 19 | + </Document> |
| 20 | +</kml> |
| 21 | +XML; |
| 22 | +} |
| 23 | + |
| 24 | +it('surfaces a validation failure with its own type and message', function () { |
| 25 | + try { |
| 26 | + (new KmlParser)->loadFromString(kmlWithLatitude('91')); |
| 27 | + } catch (KmlException $e) { |
| 28 | + expect($e)->not->toBeInstanceOf(KmlParserException::class) |
| 29 | + ->and($e->getMessage())->toBe('Invalid latitude value: 91'); |
| 30 | + |
| 31 | + return; |
| 32 | + } |
| 33 | + |
| 34 | + $this->fail('No exception was thrown.'); |
| 35 | +}); |
| 36 | + |
| 37 | +it('reports malformed XML as a parsing error', function () { |
| 38 | + try { |
| 39 | + (new KmlParser)->loadFromString('<kml><unclosed>'); |
| 40 | + } catch (KmlParserException $e) { |
| 41 | + expect($e->getMessage())->toStartWith('XML parsing error: '); |
| 42 | + |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + $this->fail('No exception was thrown.'); |
| 47 | +}); |
| 48 | + |
| 49 | +it('restores the libxml error handling mode after a successful load', function () { |
| 50 | + $before = libxml_use_internal_errors(false); |
| 51 | + |
| 52 | + (new KmlParser)->loadFromString(kmlWithLatitude('45.8635629')); |
| 53 | + |
| 54 | + expect(libxml_use_internal_errors($before))->toBeFalse(); |
| 55 | +}); |
| 56 | + |
| 57 | +it('restores the libxml error handling mode after a failed load', function () { |
| 58 | + $before = libxml_use_internal_errors(false); |
| 59 | + |
| 60 | + try { |
| 61 | + (new KmlParser)->loadFromString('<kml><unclosed>'); |
| 62 | + } catch (KmlParserException) { |
| 63 | + // The state has to be restored on the failure path too. |
| 64 | + } |
| 65 | + |
| 66 | + expect(libxml_use_internal_errors($before))->toBeFalse(); |
| 67 | +}); |
| 68 | + |
| 69 | +it('validates an already parsed document without reparsing it', function () { |
| 70 | + $xml = new SimpleXMLElement(kmlWithLatitude('91')); |
| 71 | + |
| 72 | + expect(fn () => (new KmlValidator)->validateDocument($xml)) |
| 73 | + ->toThrow(KmlException::class, 'Invalid latitude value: 91'); |
| 74 | +}); |
0 commit comments