Skip to content

Commit

Permalink
Merge pull request #48 from veewee/document-uri-configurator
Browse files Browse the repository at this point in the history
Add document_uri DOM configurator
  • Loading branch information
veewee committed Dec 29, 2022
2 parents b0a298d + cf7bf38 commit 6123139
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
16 changes: 16 additions & 0 deletions docs/dom.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,22 @@ Document::fromXmlFile(
```


#### document_uri

Allows you to keep track of the document uri, even if you are using an in-memory string.
Internally, it sets `DOMDocument::$documentURI`, which gets used as `file` in the [error-handling issues component](./error-handling.md#issues).

```php
use VeeWee\Xml\Dom\Document;
use function VeeWee\Xml\Dom\Configurator\document_uri;

$wsdl = 'http://myservice.com?wsdl';
Document::fromXmlString(
$loadFromHttp($wsdl),
document_uri($wsdl)
);
```

#### loader

The loader configurator takes a [loader](#loaders) to specify the source of the DOM Document.
Expand Down
21 changes: 21 additions & 0 deletions src/Xml/Dom/Configurator/document_uri.php
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;
};
}
1 change: 1 addition & 0 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require_once __DIR__.'/Xml/Dom/Builder/xmlns_attributes.php';
require_once __DIR__.'/Xml/Dom/Configurator/canonicalize.php';
require_once __DIR__.'/Xml/Dom/Configurator/comparable.php';
require_once __DIR__.'/Xml/Dom/Configurator/document_uri.php';
require_once __DIR__.'/Xml/Dom/Configurator/loader.php';
require_once __DIR__.'/Xml/Dom/Configurator/normalize.php';
require_once __DIR__.'/Xml/Dom/Configurator/optimize_namespaces.php';
Expand Down
52 changes: 52 additions & 0 deletions tests/Xml/Dom/Configurator/DocumentUriTest.php
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;
}
}

0 comments on commit 6123139

Please sign in to comment.