Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/NodeProcessor/Context/AbstractElementContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Netlogix\XmlProcessor\NodeProcessor\Context;

class AbstractElementContext extends NodeProcessorContext
{
private bool $selfClosing = false;

function setSelfClosing(bool $selfClosing): void
{
$this->selfClosing = $selfClosing;
}

function getSelfClosing(): bool
{
return $this->selfClosing;
}
}
2 changes: 1 addition & 1 deletion src/NodeProcessor/Context/CloseContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

namespace Netlogix\XmlProcessor\NodeProcessor\Context;

class CloseContext extends NodeProcessorContext
class CloseContext extends AbstractElementContext
{
}
2 changes: 1 addition & 1 deletion src/NodeProcessor/Context/OpenContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Netlogix\XmlProcessor\NodeProcessor\Context;

class OpenContext extends NodeProcessorContext
class OpenContext extends AbstractElementContext
{
/**
* @var array<string>
Expand Down
21 changes: 15 additions & 6 deletions src/XmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class XmlProcessor
/** @var iterable<bool> */
private iterable $parserProperties;

private bool $skipCurrentNode = false;
private bool $selfClosing = false;

/**
* @param iterable<NodeProcessorInterface> $processors
* @param iterable<bool> $parserProperties
Expand All @@ -43,7 +46,7 @@ public function __construct(
$this->context = new XmlProcessorContext(
$this->xml,
$this->processors,
fn() => $this->skipNode()
fn() => $this->skipCurrentNode = true
);
}

Expand Down Expand Up @@ -75,13 +78,12 @@ public function processFile(string $filename): void
$this->eventCloseElement();
break;
case \XMLReader::ELEMENT:
$selfClosing = $this->xml->isEmptyElement;
$this->selfClosing = $this->xml->isEmptyElement;
$this->eventOpenElement();
if ($this->shouldSkipNode()) {
$this->skipNode();
break;
if ($skip = $this->shouldSkipNode()) {
$this->xml->next();
}
if ($selfClosing) {
if ($skip || $this->selfClosing) {
$this->eventCloseElement();
}
break;
Expand All @@ -106,6 +108,10 @@ private function skipNode(): bool

private function shouldSkipNode(): bool
{
if ($this->skipCurrentNode) {
$this->skipCurrentNode = false;
return true;
}
if ($this->skipNodes === NULL) {
return false;
}
Expand Down Expand Up @@ -186,6 +192,9 @@ private function popNodePath(): void
private function createContext(string $contextClass): NodeProcessorContext
{
$context = new $contextClass($this->context, $this->nodePath);
if (method_exists($context, 'setSelfClosing')) {
$context->setSelfClosing($this->selfClosing);
}
if (method_exists($context, 'setAttributes')) {
$context->setAttributes($this->getAttributes());
}
Expand Down
66 changes: 66 additions & 0 deletions tests/Unit/NodeProcessor/Context/AbstractElementContextTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
declare(strict_types=1);

namespace Netlogix\XmlProcessor\Tests\Unit\NodeProcessor\Context;

use Netlogix\XmlProcessor\NodeProcessor\Context\AbstractElementContext;
use Netlogix\XmlProcessor\XmlProcessorContext;
use PHPUnit\Framework\TestCase;

class AbstractElementContextTest extends TestCase
{
private function getCloseContext(
?XmlProcessorContext $context = NULL,
array $nodePath = ['foo', 'bar']
): AbstractElementContext
{
return new AbstractElementContext(
$context ?? $this->getMockBuilder(XmlProcessorContext::class)
->disableOriginalConstructor()
->getMock(),
$nodePath
);
}

public function test__construct(): void
{
$nodeProcessorContext = $this->getCloseContext();
self::assertInstanceOf(AbstractElementContext::class, $nodeProcessorContext);
}

/**
* @dataProvider setSelfClosingDataProvider
*/
function testSetSelfClosing($set, $expect): void
{
$nodeProcessorContext = $this->getCloseContext();
$nodeProcessorContext->setSelfClosing($set);
self::assertEquals($expect, $nodeProcessorContext->getSelfClosing());
}

function setSelfClosingDataProvider(): \Generator
{
yield [true, true];
yield [false, false];
}

/**
* @dataProvider getSelfClosingDataProvider
*/
function testGetSelfClosing($set, $expect): void
{
$nodeProcessorContext = $this->getCloseContext();
if ($set !== NULL) {
$nodeProcessorContext->setSelfClosing($set);
}
self::assertEquals($expect, $nodeProcessorContext->getSelfClosing());
}

function getSelfClosingDataProvider(): \Generator
{
yield [NULL, false];
yield [true, true];
yield [false, false];
}

}
33 changes: 32 additions & 1 deletion tests/Unit/XmlProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@ public function testProcessFile()
[\XMLReader::SUBST_ENTITIES => true]
);
$xmlProcessor->processFile(__DIR__ . '/../Fixtures/XmlProcessorTest/test.xml');
}

public function testProcessFile_skipCurrentNode()
{
$nodeProcessor = $this->getMockForAbstractClass(NodeProcessorInterface::class);

$nodeProcessor->method('getSubscribedEvents')
->will(
$this->returnCallback(fn() => yield from [
'NodeType_' . \XMLReader::ELEMENT => function (OpenContext $context) {
$context->getXmlProcessorContext()->skipCurrentNode();
self::assertNotEquals('bar', $context->getCurrentNodeName());
},
])
);

$xmlProcessor = new XmlProcessor([
$nodeProcessor
]);

$xmlProcessor->processFile(__DIR__ . '/../Fixtures/XmlProcessorTest/test.xml');

$xmlProcessor->setSkipNodes(['foo']);
$xmlProcessor->processFile(__DIR__ . '/../Fixtures/XmlProcessorTest/test.xml');

$xmlProcessor = new XmlProcessor(
[$nodeProcessor],
[\XMLReader::SUBST_ENTITIES => true]
);
$xmlProcessor->processFile(__DIR__ . '/../Fixtures/XmlProcessorTest/test.xml');

if(!function_exists('str_end_with')){
function str_end_with(string $nodePath, string $expected){
Expand All @@ -90,8 +120,9 @@ function testCheckNodePath(string $nodePath, string $expected, bool $result): vo
self::assertSame(XmlProcessor::checkNodePath($nodePath, $expected), $result);
}

public static function checkNodePathDataProvider(): iterable
public static function checkNodePathDataProvider(): \Generator
{
yield ['', 'foo/bar', false];
yield ['/foo/bar', 'foo/bar', true];
yield ['/foo', 'foo/bar', false];
yield ['foo', 'foo/bar', false];
Expand Down