Files
myep/tests/BusProNet/XmlParser/BookingInsurancesParserTest.php
T

101 lines
3.5 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Tests\BusProNet\XmlParser;
use App\BusProNet\XmlCrawlerFactory;
use App\BusProNet\XmlParser\BookingInsurancesParser;
use PHPUnit\Framework\TestCase;
class BookingInsurancesParserTest extends TestCase
{
public function testParseInsurancesFromBookingXml(): void
{
$xml = <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<ergebnis>
<versicherungen>
<versicherung idversicherung="177240" bezeichnung="Reiseschutz Platin Auto/Bahn/Bus (Europa)" anzahl="1"
zuordnung="1" gesamtpreis="45,00" einzelpreis="45,00"></versicherung>
</versicherungen>
</ergebnis>
XML;
$crawler = XmlCrawlerFactory::create($xml);
$insurancesData = $crawler->filterXPath('//versicherungen/versicherung');
$parser = new BookingInsurancesParser();
$insurances = $parser->parse($insurancesData);
$this->assertCount(1, $insurances);
$this->assertArrayHasKey(177240, $insurances);
$insurance = $insurances[177240];
$this->assertEquals(177240, $insurance->id);
$this->assertEquals('Reiseschutz Platin Auto/Bahn/Bus (Europa)', $insurance->label);
$this->assertEquals(45.0, $insurance->price);
$this->assertEquals([0], $insurance->mapping); // zuordnung="1" maps to participant index 0
$this->assertEquals([0 => 45.0], $insurance->individualPrice);
}
public function testParseMultipleInsurances(): void
{
$xml = <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<ergebnis>
<versicherungen>
<versicherung idversicherung="100" bezeichnung="Insurance A" anzahl="1"
zuordnung="1" gesamtpreis="45,00" einzelpreis="45,00"></versicherung>
<versicherung idversicherung="200" bezeichnung="Insurance B" anzahl="1"
zuordnung="2" gesamtpreis="30,00" einzelpreis="30,00"></versicherung>
</versicherungen>
</ergebnis>
XML;
$crawler = XmlCrawlerFactory::create($xml);
$insurancesData = $crawler->filterXPath('//versicherungen/versicherung');
$parser = new BookingInsurancesParser();
$insurances = $parser->parse($insurancesData);
$this->assertCount(2, $insurances);
// First insurance
$insurance1 = $insurances[100];
$this->assertEquals(100, $insurance1->id);
$this->assertEquals('Insurance A', $insurance1->label);
$this->assertEquals(45.0, $insurance1->price);
$this->assertEquals([0], $insurance1->mapping); // zuordnung="1" maps to index 0
$this->assertEquals([0 => 45.0], $insurance1->individualPrice);
// Second insurance
$insurance2 = $insurances[200];
$this->assertEquals(200, $insurance2->id);
$this->assertEquals('Insurance B', $insurance2->label);
$this->assertEquals(30.0, $insurance2->price);
$this->assertEquals([1], $insurance2->mapping); // zuordnung="2" maps to index 1
$this->assertEquals([1 => 30.0], $insurance2->individualPrice);
}
public function testParseEmptyInsurancesReturnsEmptyArray(): void
{
$xml = <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<ergebnis>
<versicherungen>
</versicherungen>
</ergebnis>
XML;
$crawler = XmlCrawlerFactory::create($xml);
$insurancesData = $crawler->filterXPath('//versicherungen/versicherung');
$parser = new BookingInsurancesParser();
$insurances = $parser->parse($insurancesData);
$this->assertCount(0, $insurances);
$this->assertIsArray($insurances);
}
}