2020-10-24 19:09:20 +02:00
|
|
|
<?php
|
2024-01-14 11:32:15 +01:00
|
|
|
require_once __DIR__ . '/../Lib/Tools/JsonTool.php';
|
2020-10-24 19:09:20 +02:00
|
|
|
require_once __DIR__ . '/../Lib/Tools/JSONConverterTool.php';
|
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
|
|
|
class JSONConverterToolTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testCheckJsonIsValid(): void
|
|
|
|
{
|
|
|
|
$attribute = ['id' => 1, 'event_id' => 2, 'type' => 'ip-src', 'value' => '1.1.1.1'];
|
|
|
|
$event = ['Event' => ['id' => 2, 'info' => 'Test event']];
|
|
|
|
for ($i = 0; $i < 200; $i++) {
|
|
|
|
$event['Attribute'][] = $attribute;
|
|
|
|
}
|
|
|
|
$this->check($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckJsonIsValidWithError(): void
|
|
|
|
{
|
|
|
|
$attribute = ['id' => 1, 'event_id' => 2, 'type' => 'ip-src', 'value' => '1.1.1.1'];
|
|
|
|
$event = ['Event' => ['id' => 2, 'info' => 'Test event'], 'errors' => 'chyba'];
|
|
|
|
for ($i = 0; $i < 200; $i++) {
|
|
|
|
$event['Attribute'][] = $attribute;
|
|
|
|
}
|
|
|
|
$this->check($event);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCheckJsonIsValidSmall(): void
|
|
|
|
{
|
|
|
|
$attribute = ['id' => 1, 'event_id' => 2, 'type' => 'ip-src', 'value' => '1.1.1.1'];
|
|
|
|
$event = ['Event' => ['id' => 2, 'info' => 'Test event'], 'errors' => 'chyba'];
|
|
|
|
for ($i = 0; $i < 5; $i++) {
|
|
|
|
$event['Attribute'][] = $attribute;
|
|
|
|
}
|
|
|
|
$this->check($event);
|
|
|
|
}
|
|
|
|
|
2022-03-26 13:43:10 +01:00
|
|
|
public function testCheckJsonIsValidUnicodeSlashes(): void
|
|
|
|
{
|
|
|
|
$attribute = ['id' => 1, 'event_id' => 2, 'type' => 'ip-src', 'value' => '1.1.1.1'];
|
|
|
|
$event = ['Event' => ['id' => 2, 'info' => 'Test event ěšřžýáí \/'], 'errors' => 'chyba ě+š'];
|
|
|
|
for ($i = 0; $i < 5; $i++) {
|
|
|
|
$event['Attribute'][] = $attribute;
|
|
|
|
}
|
|
|
|
$this->check($event);
|
|
|
|
}
|
|
|
|
|
2020-10-24 19:09:20 +02:00
|
|
|
private function check(array $event): void
|
|
|
|
{
|
|
|
|
$json = '';
|
2021-12-14 22:35:44 +01:00
|
|
|
foreach (JSONConverterTool::streamConvert($event) as $part) {
|
2020-10-24 19:09:20 +02:00
|
|
|
$json .= $part;
|
|
|
|
}
|
2022-03-26 13:17:19 +01:00
|
|
|
|
2022-03-26 13:43:10 +01:00
|
|
|
// Check if result is the same without spaces
|
2022-03-26 13:17:19 +01:00
|
|
|
$jsonStreamWithoutSpaces = preg_replace("/\s+/", "", $json);
|
|
|
|
$jsonNormalWithoutSpaces = preg_replace("/\s+/", "", JSONConverterTool::convert($event));
|
|
|
|
$this->assertEquals($jsonNormalWithoutSpaces, $jsonStreamWithoutSpaces);
|
|
|
|
|
2024-01-14 11:32:15 +01:00
|
|
|
$this->assertTrue(JsonTool::isValid($json));
|
2020-10-24 19:09:20 +02:00
|
|
|
}
|
|
|
|
}
|