<?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'));
    }

    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(), []);
    }
}