319 lines
8.8 KiB
PHP
319 lines
8.8 KiB
PHP
<?php
|
|
|
|
namespace goodboyalex\php_components_pack\tests\classes;
|
|
|
|
use goodboyalex\php_components_pack\classes\File;
|
|
use goodboyalex\php_components_pack\classes\JsonReWriter;
|
|
use goodboyalex\php_components_pack\exceptions\JsonException;
|
|
use goodboyalex\php_components_pack\tests\data\A;
|
|
use goodboyalex\php_components_pack\tests\data\C;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class JsonReWriterTest extends TestCase
|
|
{
|
|
public function testIsKeyExists ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
try {
|
|
$json->Write("test/subtest/AAA", "123");
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$this->assertTrue($json->IsKeyExists("test/subtest/AAA"));
|
|
}
|
|
|
|
private function PrepareForTest (): void
|
|
{
|
|
require_once __DIR__ . '/../../sources/exceptions/JsonException.php';
|
|
require_once __DIR__ . '/../../sources/traits/JsonReWriter/JsonReWriterDeleteTrait.php';
|
|
require_once __DIR__ . '/../../sources/traits/JsonReWriter/JsonReWriterKeyTrait.php';
|
|
require_once __DIR__ . '/../../sources/traits/JsonReWriter/JsonReWriterLoadSaveTrait.php';
|
|
require_once __DIR__ . '/../../sources/traits/JsonReWriter/JsonReWriterReadTrait.php';
|
|
require_once __DIR__ . '/../../sources/traits/JsonReWriter/JsonReWriterWriteTrait.php';
|
|
require_once __DIR__ . '/../../sources/classes/JsonReWriter.php';
|
|
require_once __DIR__ . '/../data/A.php';
|
|
require_once __DIR__ . '/../data/C.php';
|
|
}
|
|
|
|
public function testReadWriteInt ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
try {
|
|
$json->Write("test/test", 123);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$this->assertEquals(123, $json->ReadInt("test/test"));
|
|
}
|
|
|
|
public function testReadWriteBool ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
try {
|
|
$json->Write("test/test1", false);
|
|
$json->Write("test/test2", true);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$this->assertTrue($json->ReadBool("test/test2"));
|
|
$this->assertFalse($json->ReadBool("test/test1"));
|
|
}
|
|
|
|
public function testReadWriteString ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
try {
|
|
$json->Write("test/test", "test string");
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$this->assertEquals("test string", $json->ReadString("test/test", "test this"));
|
|
}
|
|
|
|
public function testSaveToFile ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$fileName = __DIR__ . "/test.json";
|
|
|
|
if (file_exists($fileName))
|
|
unlink($fileName);
|
|
|
|
$json = new JsonReWriter();
|
|
|
|
try {
|
|
$json->Write("test/subtest/AAA", "123");
|
|
$json->Write("test/subtest/BBB", 1.23);
|
|
$json->Write("test1/test", 123);
|
|
$json->Write("test2/test", true);
|
|
$json->WriteArray("test3/test/res", [1, 2, 3]);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$json->SaveToFile($fileName);
|
|
|
|
$this->assertFileExists($fileName);
|
|
|
|
$size = File::FileSize($fileName)->Value;
|
|
|
|
$this->assertEquals(268, $size);
|
|
|
|
unlink($fileName);
|
|
}
|
|
|
|
public function testReadWriteArray ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
|
|
try {
|
|
$json->Write("test/subtest/AAA", "123");
|
|
$json->Write("test/subtest/BBB", 1.23);
|
|
$json->Write("test1/test", 123);
|
|
$json->Write("test2/test", true);
|
|
$json->WriteArray("test3/test/res", [1, 2, 3]);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$this->assertArrayIsEqualToArrayIgnoringListOfKeys([1, 2, 3], $json->ReadArray("test3/test/res"), []);
|
|
}
|
|
|
|
public function testClear ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
|
|
try {
|
|
$json->Write("test/subtest/AAA", "123");
|
|
$json->Write("test/subtest/BBB", 1.23);
|
|
$json->Write("test1/test", 123);
|
|
$json->Write("test2/test", true);
|
|
$json->WriteArray("test3/test/res", [1, 2, 3]);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$json->Clear();
|
|
|
|
$this->assertCount(0, $json->GetKeys());
|
|
}
|
|
|
|
public function testReadWriteObject ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
|
|
$class = new A("test", 123, true);
|
|
|
|
try {
|
|
$json->WriteObject("test", $class);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$this->assertEquals("test", $json->ReadObject("test", new A())->a);
|
|
}
|
|
|
|
public function testLoadFromFile ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$fileName = __DIR__ . "/test.json";
|
|
|
|
if (file_exists($fileName))
|
|
unlink($fileName);
|
|
|
|
$json = new JsonReWriter();
|
|
|
|
try {
|
|
$json->Write("test/subtest/AAA", "123");
|
|
$json->Write("test/subtest/BBB", 1.23);
|
|
$json->Write("test1/test", 123);
|
|
$json->Write("test2/test", true);
|
|
$json->WriteArray("test3/test/res", [1, 2, 3]);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$json->SaveToFile($fileName);
|
|
|
|
unset($json);
|
|
|
|
$json = new JsonReWriter();
|
|
|
|
$json->LoadFromFile($fileName);
|
|
|
|
unlink($fileName);
|
|
|
|
$this->assertEquals(123, $json->ReadInt("test1/test"));
|
|
}
|
|
|
|
public function testDeleteKey ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
|
|
try {
|
|
$json->Write("test/subtest/AAA", "123");
|
|
$json->Write("test/subtest/BBB", 1.23);
|
|
$json->Write("test1/test", 123);
|
|
$json->Write("test2/test", true);
|
|
$json->WriteArray("test3/test/res", [1, 2, 3]);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$json->DeleteKey("test3/test/res");
|
|
|
|
$this->assertFalse($json->IsKeyExists("test3/test/res"));
|
|
}
|
|
|
|
public function testReadWrite ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
try {
|
|
$json->Write("test/subtest/AAA", "123");
|
|
$json->Write("test/subtest/BBB", 1.23);
|
|
$json->Write("test1/test", 123);
|
|
$json->Write("test2/test", true);
|
|
$json->Write("test3/test/res", json_encode([1, 2, 3]));
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$this->assertTrue($json->IsKeyExists("test/subtest/AAA"));
|
|
$this->assertTrue($json->IsKeyExists("test1/test"));
|
|
$this->assertEquals(1.23, (float)$json->Read("test/subtest/BBB"));
|
|
}
|
|
|
|
public function testGetKeys ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
try {
|
|
$json->Write("test/subtest/AAA", "123");
|
|
$json->Write("test/subtest/BBB", 1.23);
|
|
$json->Write("test1/test", 123);
|
|
$json->Write("test2/test", true);
|
|
$json->WriteArray("test3/test/res", [1, 2, 3]);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$this->assertCount(11, $json->GetKeys());
|
|
$this->assertCount(2, $json->GetKeys("test/subtest"));
|
|
}
|
|
|
|
public function testReadWriteSerializable ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$serializableClass = new C("test", 123, true);
|
|
|
|
$json = new JsonReWriter();
|
|
try {
|
|
$json->WriteSerializable("test", $serializableClass);
|
|
|
|
/**
|
|
* @var C $unSerializableClass Получаем объект из файла
|
|
*/
|
|
$unSerializableClass = $json->ReadSerializable("test", "goodboyalex\\php_components_pack\\tests\\data\\C");
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
return;
|
|
}
|
|
|
|
$this->assertEquals($serializableClass->stringC, $unSerializableClass->stringC);
|
|
$this->assertEquals($serializableClass->intC, $unSerializableClass->intC);
|
|
$this->assertEquals($serializableClass->boolC, $unSerializableClass->boolC);
|
|
}
|
|
|
|
public function testReadWriteFloat ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$json = new JsonReWriter();
|
|
try {
|
|
$json->Write("test", 1.23);
|
|
}
|
|
catch (JsonException $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
|
|
$this->assertEquals(1.23, $json->ReadFloat("test", 0.2));
|
|
}
|
|
} |