Skip to content

Commit

Permalink
Removed xml header from literal string
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathmel123 committed Aug 15, 2024
1 parent de22b87 commit 450623d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
3 changes: 2 additions & 1 deletion Classes/Common/XmlDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function toJson()
foreach ($xmlArray as $id => $value) {
$result[$id] = json_encode($value, JSON_PRETTY_PRINT);
}
return implode($result);
return trim(implode($result));
}

protected function convert(SimpleXMLElement $node): array
Expand Down Expand Up @@ -124,6 +124,7 @@ protected function convert(SimpleXMLElement $node): array
if ($node->getName() == 'p' && $node->count() > 0 && !empty($node)) {
// Add literal string, to store the node order
$literal = str_replace(array("\n", "\r"), '', trim($node->asXML()));
$literal = str_replace("<?xml version=\"1.0\"?>",'',$literal);
$result['@literal'] = $literal;
}
}
Expand Down
41 changes: 32 additions & 9 deletions Tests/Unit/Common/XmlDocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,52 @@ public function xmlStringNotEmpty(): void
/**
* @test
*/
public function writeTestFile()
public function testFluidInterface()
{
$file = fopen('Tests/Testfiles/testout.json', 'w');
fwrite($file, $this->subject->setSplitSymbols(['meiHead'])->setXmlId(false)->toJson());
self::assertJson($this->subject->setXmlId(true)->toJson());
}

self::assertFileExists('Tests/Testfiles/testout.json');
/**
* @test
*/
public function testXmlIdIsOmited()
{
self::assertFalse(str_contains($this->subject->setXmlId(false)->toJson(), '@xmlId'));
}

/**
* @test
*/
public function testFluidInterface()

public function xmlIdIsIncluded()
{
self::assertStringContainsString("@xml:id", $this->subject->setXmlId(true)->toJson());
}

/**
* @test
**/

public function testMixedContentIsIncluded()
{
self::assertJson($this->subject->setXmlId(true)->toJson());
$mixedContentString = '<p> I am <b> mixed </b> content </p>';
$subject = XmlDocument::from($mixedContentString);
$subject->setLiteralString(true);
$expected = '"@literal": "<p> I am <b> mixed <\/b> content <\/p>"';
self::assertStringContainsString($expected, $subject->toJson());
}

/**
* @test
*/
public function testXmlIdIsOmited()

public function testMixedContentIsNotIncluded()
{
$this->subject->setXmlId(false);
self::assertFalse(str_contains($this->subject->toJson(), '@xmlId'));
$this->subject->setLiteralString(false);
self::assertFalse(str_contains($this->subject->toJson(), "@literal"));
}


/**
* @test
*/
Expand All @@ -99,4 +120,6 @@ public function testPlainText()
$expected = '{"@value": "I am plain text"}';
self::assertSame(json_decode($subject->toJson(), true), json_decode($expected, true));
}


}

0 comments on commit 450623d

Please sign in to comment.