[Dictionary] добавлен метод AddRange (array $dictionary), который добавляет элементы в виде ассоциативного массива ключ => значение в словарь. [Dictionary] Добавлен метод ToArray (), который возвращает все элементы словаря в виде массива..
154 lines
3.7 KiB
PHP
154 lines
3.7 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 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'));
|
|
}
|
|
|
|
public function testKeys ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$dict = new Dictionary();
|
|
$dict->Add('1', 1);
|
|
$dict->Add('3', true);
|
|
$dict->Add('2', '2');
|
|
|
|
$array = ['1', '3', '2'];
|
|
|
|
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($array, $dict->Keys(), []);
|
|
}
|
|
|
|
public function testSort ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$dict = new Dictionary();
|
|
$dict->Add('1', 1);
|
|
$dict->Add('3', true);
|
|
$dict->Add('2', '2');
|
|
|
|
$array = ['1', '2', '3'];
|
|
|
|
$dict->Sort();
|
|
|
|
$this->assertArrayIsEqualToArrayIgnoringListOfKeys($array, $dict->Keys(), []);
|
|
}
|
|
|
|
public function testAddRange ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$dict = new Dictionary();
|
|
$dict->Add('1', 1);
|
|
|
|
$array = ['2' => '2', '3' => true, '4' => false];
|
|
|
|
$dict->AddRange($array);
|
|
|
|
$this->assertEquals(1, $dict->Get('1'));
|
|
$this->assertTrue($dict->Get('3'));
|
|
$this->assertEquals(4, $dict->count());
|
|
$this->assertFalse($dict->Get("4"));
|
|
}
|
|
|
|
public function testToArray ()
|
|
{
|
|
$this->PrepareForTest();
|
|
|
|
$dict = new Dictionary();
|
|
$dict->Add('1', 1);
|
|
$dict->Add('3', true);
|
|
$dict->Add('2', '2');
|
|
|
|
$array = $dict->ToArray();
|
|
|
|
$this->assertIsArray($array);
|
|
$this->assertEquals(1, $array['1']);
|
|
$this->assertTrue($array['3']);
|
|
$this->assertCount(3, $array);
|
|
}
|
|
} |