init: Erste Version aus Test-App erstellen

This commit is contained in:
2022-04-27 13:16:21 +02:00
commit 0cb06e2150
7 changed files with 195 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
/.idea
/composer.lock
/vendor
/codeception.yml
/tests/*.suite.yml
/tests/_*/
+10
View File
@@ -0,0 +1,10 @@
paths:
tests: tests
output: tests/_output
data: tests/_data
support: tests/_support
envs: tests/_envs
actor_suffix: Tester
extensions:
enabled:
- Codeception\Extension\RunFailed
+37
View File
@@ -0,0 +1,37 @@
{
"name": "torsten-hettstedt/sax-xml-reader",
"description": "SAX-XML-Readertest",
"minimum-stability": "stable",
"license": "GPL-3.0-or-later",
"authors": [
{
"name": "Torsten L\u00fccke",
"email": "developer@torsten-hettstedt.de"
}
],
"require": {
"php": ">=7.4",
"ext-xml": "*"
},
"require-dev": {
"codeception/codeception": "^4.1.18",
"codeception/module-asserts": "^1.3.1",
"phpstan/phpstan": "^1.4.7",
"codeception/module-phpbrowser": "^1.0.0"
},
"autoload": {
"psr-4": {
"TorstenHettstedt\\XmlReader\\Sax\\": "src"
},
"exclude-from-classmap": [
"/example/",
"/.idea/"
]
},
"autoload-dev": {
"psr-4": {
"TorstenHettstedt\\XmlReader\\Sax\\Tests\\Unit\\": "tests/unit",
"TorstenHettstedt\\XmlReader\\Sax\\Tests\\Helper\\": "tests/_support/Helper"
}
}
}
+6
View File
@@ -0,0 +1,6 @@
parameters:
level: 6
paths:
- src
- tests/unit
- tests/_support/Helper
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace TorstenHettstedt\XmlReader\Sax;
interface XmlReaderInterfaces
{
function tag_open(string $tag_name, array $attributes): void;
function cdata(string $tag_name, string $cdata): void;
function tag_close(string $tag_name): void;
}
+75
View File
@@ -0,0 +1,75 @@
<?php
namespace TorstenHettstedt\XmlReader\Sax;
use InvalidArgumentException;
class XmlSaxParser
{
protected $parser;
protected XmlReaderInterfaces $xmlReader;
protected string $actualTagName = '';
public function __construct(XmlReaderInterfaces $xml_reader)
{
$this->xmlReader = $xml_reader;
$this->parser = xml_parser_create();
xml_set_object($this->parser, $this);
xml_set_element_handler($this->parser, "tag_open", "tag_close");
xml_set_character_data_handler($this->parser, "cdata");
}
public function __destruct()
{
xml_parser_free($this->parser);
unset($this->parser);
}
public function parse($data):void
{
xml_parse($this->parser, $data);
}
/**
* @param string $path
*
* @return void
* @throws InvalidArgumentException
*/
public function parseFile(string $path): void
{
// open xml file
if (!($handle = fopen($path, "r"))) {
throw new InvalidArgumentException('could not open XML file "' . $path . '".');
}
while ($data = fread($handle, 4096)) { // read xml file
$this->parse($data);
}
}
/** @noinspection PhpUnusedParameterInspection */
protected function tag_open($parser, string $tag_name, array $attributes):void
{
$this->actualTagName = $tag_name;
if ($this->actualTagName !== '') {
$this->xmlReader->tag_open($tag_name, $attributes);
}
}
/** @noinspection PhpUnusedParameterInspection */
protected function cdata($parser, string $cdata):void
{
if ($this->actualTagName !== '' && trim($cdata) !== '') {
$this->xmlReader->cdata($this->actualTagName, $cdata);
}
}
/** @noinspection PhpUnusedParameterInspection */
protected function tag_close($parser, string $tag_name):void
{
$this->actualTagName = '';
$this->xmlReader->tag_close($tag_name);
}
}
+48
View File
@@ -0,0 +1,48 @@
<?php
namespace TorstenHettstedt\XmlReader\Sax\Tests\Unit;
use Codeception\Stub\Expected;
use Codeception\Test\Unit;
use Exception;
use TorstenHettstedt\XmlReader\Sax\XmlReaderInterfaces;
use TorstenHettstedt\XmlReader\Sax\XmlSaxParser;
class XmlSaxParserTest extends Unit
{
const XML_DATA = <<<XML
<?xml version = "1.0" encoding = "utf-8"?>
<tutors>
<course>
<name>Android</name>
<email>contact@tutorialspoint.com</email>
</course>
<course>
<name>Java</name>
</course>
</tutors>
XML;
protected $xmlReader = null;
/**
* @throws Exception
*/
protected function setUp(): void
{
parent::setUp();
$this->xmlReader = $this->makeEmpty(XmlReaderInterfaces::class, [
'tag_open' => Expected::exactly(6),
'cdata' => Expected::exactly(3),
'tag_close' => Expected::exactly(6),
]);
}
public function testParse()
{
$reader = new XmlSaxParser($this->xmlReader);
$reader->parse(self::XML_DATA);
}
}