-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #48 from veewee/document-uri-configurator
Add document_uri DOM configurator
- Loading branch information
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Xml\Dom\Configurator; | ||
|
||
use Closure; | ||
use DOMDocument; | ||
|
||
/** | ||
* @param non-empty-string $documentUri | ||
* @return \Closure(DOMDocument): DOMDocument | ||
*/ | ||
function document_uri(string $documentUri): Closure | ||
{ | ||
return static function (DOMDocument $document) use ($documentUri) : DOMDocument { | ||
$document->documentURI = $documentUri; | ||
|
||
return $document; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace VeeWee\Tests\Xml\Dom\Configurator; | ||
|
||
use DOMDocument; | ||
use PHPUnit\Framework\TestCase; | ||
use VeeWee\Xml\Dom\Document; | ||
use function VeeWee\Xml\Dom\Configurator\document_uri; | ||
use function VeeWee\Xml\Dom\Validator\xsd_validator; | ||
|
||
final class DocumentUriTest extends TestCase | ||
{ | ||
public function test_it_can_use_document_uri(): void | ||
{ | ||
$doc = new DOMDocument(); | ||
$doc->loadXML($xml = '<hello />'); | ||
|
||
static::assertStringStartsWith(getcwd(), $doc->documentURI); | ||
$configurator = document_uri($documentUri = 'myfile.wsdl'); | ||
|
||
$result = $configurator($doc); | ||
static::assertSame($doc, $result); | ||
static::assertSame($documentUri, $doc->documentURI); | ||
} | ||
|
||
public function test_it_uses_document_uri_in_error_reporting(): void | ||
{ | ||
$xmlFile = $this->getFixture('xsd-namespace-invalid.xml'); | ||
$xsdFile = $this->getFixture('note-nonamespace.xsd'); | ||
|
||
$doc = Document::fromXmlFile( | ||
$xmlFile, | ||
document_uri($documentUri = 'myfile.wsdl'), | ||
); | ||
$validator = xsd_validator($xsdFile); | ||
|
||
$issues = $doc->validate($validator); | ||
|
||
static::assertCount(1, $issues); | ||
static::assertSame($documentUri, ([...$issues][0])->file()); | ||
} | ||
|
||
private function getFixture(string $fixture): string | ||
{ | ||
$file = FIXTURE_DIR.'/dom/validator/xsd/'.$fixture; | ||
static::assertFileExists($file); | ||
|
||
return $file; | ||
} | ||
} |