php_components_pack/tests/classes/DictionaryTest.php
babaev-an ee76392d71 20250515
* [ObjectArrayBasicTrait] заменён на [ArrayBasicTrait].
* [ArrayBasicTrait] изменён метод __isset. Теперь он обрабатывается корректно.
* [ArrayBasicTrait] методы Serialize и UnSerialize теперь используют json_encode / json_decode
* [+Dictionary] Класс, описывающий словарь типа строка (ключ) -> значение любого типа (значение).
2025-05-15 23:04:04 +03:00

102 lines
2.5 KiB
PHP

<?php
namespace goodboyalex\php_components_pack\tests\classes;
use goodboyalex\php_components_pack\classes\Dictionary;
use PHPUnit\Framework\TestCase;
class DictionaryTest extends TestCase
{
public function testRemove ()
{
$this->PrepareForTest();
$dict = new Dictionary();
$dict->Add('1', 1);
$dict->Add('2', '2');
$dict->Add('3', true);
$dict->Remove('2');
$this->assertEquals(1, $dict->Get('1'));
$this->assertTrue($dict->Get('3'));
$this->assertEquals(2, $dict->count());
$this->assertFalse($dict->Has('2'));
}
private function PrepareForTest (): void
{
require_once __DIR__ . '/../../sources/interfaces/ISerializable.php';
require_once __DIR__ . '/../../sources/traits/ArrayBasicTrait.php';
require_once __DIR__ . '/../../sources/classes/Dictionary.php';
}
public function testSerialize ()
{
$this->PrepareForTest();
$dict = new Dictionary();
$dict->Add('1', 1);
$dict->Add('2', '2');
$dict->Add('3', true);
$this->assertEquals("{\"1\":1,\"2\":\"2\",\"3\":true}", $dict->Serialize());
}
public function testGet ()
{
$this->PrepareForTest();
$dict = new Dictionary();
$dict->Add('1', 1);
$dict->Add('2', '2');
$dict->Add('3', true);
$this->assertEquals(1, $dict->Get('1'));
$this->assertEquals('2', $dict->Get('2'));
$this->assertTrue($dict->Get('3'));
$this->assertEquals(3, $dict->count());
}
public function testClear ()
{
$this->PrepareForTest();
$dict = new Dictionary();
$dict->Add('1', 1);
$dict->Add('2', '2');
$dict->Add('3', true);
$dict->Clear();
$this->assertEquals(0, $dict->count());
}
public function testAdd ()
{
$this->PrepareForTest();
$dict = new Dictionary();
$dict->Add('1', 1);
$dict->Add('2', '2');
$dict->Add('3', true);
$this->assertEquals(1, $dict->Get('1'));
$this->assertEquals('2', $dict->Get('2'));
$this->assertTrue($dict->Get('3'));
$this->assertEquals(3, $dict->count());
}
public function testHas ()
{
$this->PrepareForTest();
$dict = new Dictionary();
$dict->Add('1', 1);
$dict->Add('2', '2');
$dict->Add('3', true);
$this->assertTrue($dict->Has('3'));
$this->assertFalse($dict->Has('4'));
}
}