Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
641ab3e
commit b1960e3
Showing
17 changed files
with
194 additions
and
15 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
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,89 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Donatorsky\XmlTemplate\Reader\Tests\Feature\XmlTemplateReader; | ||
|
||
use Assert\InvalidArgumentException; | ||
use Donatorsky\XmlTemplate\Reader\Models\Contracts\NodeInterface; | ||
use Donatorsky\XmlTemplate\Reader\Models\Node; | ||
use Donatorsky\XmlTemplate\Reader\XmlTemplateReader; | ||
|
||
/** | ||
* @covers \Donatorsky\XmlTemplate\Reader\XmlTemplateReader | ||
* @coversDefaultClass \Donatorsky\XmlTemplate\Reader\XmlTemplateReader | ||
*/ | ||
class CastToModeTest extends AbstractXmlTemplateReaderTest | ||
{ | ||
private const XML_VALID = 'configuration-cast-to-valid'; | ||
|
||
private const XML_INVALID_NON_EXISTENT_CLASS = 'configuration-cast-to-invalid-non-existent-class'; | ||
|
||
private const XML_INVALID_UNSUPPORTED_CLASS = 'configuration-cast-to-invalid-unsupported-class'; | ||
|
||
public function testSingleNodeAsCollection(): void | ||
{ | ||
$xmlTemplateReader = new XmlTemplateReader(self::getTemplateXml(self::XML_VALID)); | ||
|
||
$node = $xmlTemplateReader->read( | ||
<<<'XML' | ||
<root> | ||
<casted/> | ||
<uncasted/> | ||
<children/> | ||
<children/> | ||
<children/> | ||
</root> | ||
XML | ||
); | ||
|
||
self::assertInstanceOf(Node::class, $node); | ||
|
||
$rootNodeRelationsMap = $node->getRelations(); | ||
|
||
self::assertTrue($rootNodeRelationsMap->has('casted')); | ||
self::assertInstanceOf(CastedRelationNode::class, $rootNodeRelationsMap->get('casted')); | ||
|
||
self::assertTrue($rootNodeRelationsMap->has('uncasted')); | ||
self::assertInstanceOf(Node::class, $rootNodeRelationsMap->get('casted')); | ||
|
||
$rootNodeChildrenMap = $node->getChildren(); | ||
|
||
self::assertTrue($rootNodeChildrenMap->has('children')); | ||
|
||
$children = $rootNodeChildrenMap->get('children'); | ||
self::assertCount(3, $children); | ||
|
||
foreach ($children as $child) { | ||
self::assertInstanceOf(CastedChildNode::class, $child); | ||
} | ||
} | ||
|
||
public function testFailToCastToNonExistentClass(): void | ||
{ | ||
$xmlTemplateReader = new XmlTemplateReader(self::getTemplateXml(self::XML_INVALID_NON_EXISTENT_CLASS)); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage('The "root/nonExistentClass" node\'s tpl:castTo attribute value "\Donatorsky\XmlTemplate\Reader\Tests\Feature\XmlTemplateReader\NonExistentNodeClass" refers to non-existent class FQN'); | ||
|
||
$xmlTemplateReader->preloadTemplate(); | ||
} | ||
|
||
public function testFailToCastToUnsupportedClass(): void | ||
{ | ||
$xmlTemplateReader = new XmlTemplateReader(self::getTemplateXml(self::XML_INVALID_UNSUPPORTED_CLASS)); | ||
|
||
$this->expectException(InvalidArgumentException::class); | ||
$this->expectExceptionMessage(sprintf('The "root/unsupportedClass" node\'s tpl:castTo attribute value "\stdClass" refers to a class that does not implement "%s" interface', NodeInterface::class)); | ||
|
||
$xmlTemplateReader->preloadTemplate(); | ||
} | ||
} | ||
|
||
class CastedRelationNode extends Node | ||
{ | ||
} | ||
|
||
class CastedChildNode extends Node | ||
{ | ||
} |
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
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
7 changes: 7 additions & 0 deletions
7
...XmlTemplateReader/resources/configuration-cast-to-invalid-non-existent-class-template.xml
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,7 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<template xmlns:tpl="http://www.w3.org/2001/XMLSchema-instance" | ||
tpl:noNamespaceSchemaLocation="../../../../src/xml-template-reader.xsd"> | ||
<root> | ||
<nonExistentClass tpl:castTo="\Donatorsky\XmlTemplate\Reader\Tests\Feature\XmlTemplateReader\NonExistentNodeClass" /> | ||
</root> | ||
</template> |
7 changes: 7 additions & 0 deletions
7
.../XmlTemplateReader/resources/configuration-cast-to-invalid-unsupported-class-template.xml
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,7 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<template xmlns:tpl="http://www.w3.org/2001/XMLSchema-instance" | ||
tpl:noNamespaceSchemaLocation="../../../../src/xml-template-reader.xsd"> | ||
<root> | ||
<unsupportedClass tpl:castTo="\stdClass" /> | ||
</root> | ||
</template> |
10 changes: 10 additions & 0 deletions
10
tests/Feature/XmlTemplateReader/resources/configuration-cast-to-valid-template.xml
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,10 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<template xmlns:tpl="http://www.w3.org/2001/XMLSchema-instance" | ||
tpl:noNamespaceSchemaLocation="../../../../src/xml-template-reader.xsd"> | ||
<root> | ||
<casted tpl:castTo="\Donatorsky\XmlTemplate\Reader\Tests\Feature\XmlTemplateReader\CastedRelationNode" /> | ||
<uncasted /> | ||
<children tpl:castTo="\Donatorsky\XmlTemplate\Reader\Tests\Feature\XmlTemplateReader\CastedChildNode" | ||
tpl:type="collection" /> | ||
</root> | ||
</template> |
7 changes: 7 additions & 0 deletions
7
...Feature/XmlTemplateReader/resources/configuration-collect-attributes-invalid-template.xml
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,7 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<template xmlns:tpl="http://www.w3.org/2001/XMLSchema-instance" | ||
tpl:noNamespaceSchemaLocation="../../../../src/xml-template-reader.xsd"> | ||
<root> | ||
<invalid tpl:collectAttributes="invalid value" /> | ||
</root> | ||
</template> |
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
tests/Feature/XmlTemplateReader/resources/configuration-contents-invalid-template.xml
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,7 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<template xmlns:tpl="http://www.w3.org/2001/XMLSchema-instance" | ||
tpl:noNamespaceSchemaLocation="../../../../src/xml-template-reader.xsd"> | ||
<root> | ||
<invalid tpl:contents="invalid value" /> | ||
</root> | ||
</template> |
File renamed without changes.
File renamed without changes.
7 changes: 7 additions & 0 deletions
7
tests/Feature/XmlTemplateReader/resources/configuration-type-invalid-template.xml
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,7 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<template xmlns:tpl="http://www.w3.org/2001/XMLSchema-instance" | ||
tpl:noNamespaceSchemaLocation="../../../../src/xml-template-reader.xsd"> | ||
<root> | ||
<invalid tpl:type="invalid value" /> | ||
</root> | ||
</template> |
File renamed without changes.