553 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			553 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace goodboyalex\php_components_pack\tests\classes;
 | |
| 
 | |
| use goodboyalex\php_components_pack\classes\ObjectArray;
 | |
| use goodboyalex\php_components_pack\enums\ObjectArraySerializeMethod;
 | |
| use goodboyalex\php_components_pack\models\ObjectArraySerializeOptions;
 | |
| use goodboyalex\php_components_pack\tests\data\A;
 | |
| use goodboyalex\php_components_pack\tests\data\B;
 | |
| use goodboyalex\php_components_pack\tests\data\C;
 | |
| use goodboyalex\php_components_pack\tests\data\D;
 | |
| use PHPUnit\Framework\TestCase;
 | |
| use stdClass;
 | |
| 
 | |
| class ObjectArrayTest extends TestCase
 | |
| {
 | |
|     public function testSort ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
|         $b_Array = new ObjectArray($array);
 | |
| 
 | |
|         $a_Array->Sort('a');
 | |
|         $b_Array->Sort('b');
 | |
| 
 | |
|         $this->assertEquals('a', $a_Array[0]->a);
 | |
|         $this->assertEquals('b', $a_Array[1]->a);
 | |
|         $this->assertEquals('c', $a_Array[2]->a);
 | |
| 
 | |
|         $this->assertEquals('1', $b_Array[0]->b);
 | |
|         $this->assertEquals('2', $b_Array[1]->b);
 | |
|         $this->assertEquals('3', $b_Array[2]->b);
 | |
| 
 | |
|     }
 | |
| 
 | |
|     private function PrepareForTest (): void
 | |
|     {
 | |
|         require_once __DIR__ . '/../data/A.php';
 | |
|         require_once __DIR__ . '/../data/B.php';
 | |
|         require_once __DIR__ . '/../data/C.php';
 | |
|         require_once __DIR__ . '/../data/D.php';
 | |
|         require_once __DIR__ . '/../../sources/interfaces/ISerializable.php';
 | |
|         require_once __DIR__ . '/../../sources/traits/ArrayBasicTrait.php';
 | |
|         require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArrayConstantsTrait.php';
 | |
|         require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArrayLINQTrait.php';
 | |
|         require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArraySearchAndSortTrait.php';
 | |
|         require_once __DIR__ . '/../../sources/traits/ObjectArray/ObjectArraySpecialTrait.php';
 | |
|         require_once __DIR__ . '/../../sources/classes/ObjectArray.php';
 | |
|     }
 | |
| 
 | |
|     public function testSearch ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $sr = $a_Array->Search('a', 'c');
 | |
| 
 | |
|         $this->assertEquals(2, $sr->b);
 | |
|     }
 | |
| 
 | |
|     public function testGetRows ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $sr = $a_Array->GetRows(fn (A $a): bool => $a->b < 3);
 | |
| 
 | |
|         $sr->Sort("b");
 | |
| 
 | |
|         $this->assertCount(2, $sr);
 | |
|         $this->assertEquals('1', $sr[0]->b);
 | |
|         $this->assertEquals('2', $sr[1]->b);
 | |
|     }
 | |
| 
 | |
|     public function testGetValue ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
|         $sr = $a_Array->GetValue("b", fn (A $a): bool => $a->b < 2);
 | |
| 
 | |
|         $this->assertNotNull($sr);
 | |
|         $this->assertEquals(1, $sr);
 | |
|     }
 | |
| 
 | |
|     public function testCount ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $cnt = $a_Array->Count();
 | |
|         $this->assertEquals(3, $cnt);
 | |
| 
 | |
|         $cnt = $a_Array->Count(fn (A $a): bool => $a->b < 3);
 | |
|         $this->assertEquals(2, $cnt);
 | |
|     }
 | |
| 
 | |
|     public function testDelete ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $a_Array->Delete(fn (A $a): bool => $a->b == 3);
 | |
| 
 | |
|         $cnt = $a_Array->Count();
 | |
|         $this->assertEquals(2, $cnt);
 | |
|     }
 | |
| 
 | |
|     public function testClear ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $a_Array->Clear();
 | |
| 
 | |
|         $cnt = $a_Array->Count();
 | |
|         $this->assertEquals(0, $cnt);
 | |
|     }
 | |
| 
 | |
|     public function testMerge ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $array2 = [
 | |
|             new A("d", 4, true),
 | |
|             new A("e", 5, false),
 | |
|             new A("f", 6, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $a_Array->Merge($array2);
 | |
| 
 | |
|         $cnt = $a_Array->Count();
 | |
|         $this->assertEquals(6, $cnt);
 | |
| 
 | |
|         $sr = $a_Array->Search('a', 'f');
 | |
| 
 | |
|         $this->assertEquals(6, $sr->b);
 | |
|     }
 | |
| 
 | |
|     public function testMaxBy ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $sr = $a_Array->MaxBy(fn (A $a) => $a->b);
 | |
| 
 | |
|         $this->assertEquals("a", $sr->a);
 | |
|     }
 | |
| 
 | |
|     public function testGetColumn ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $sr = $a_Array->GetColumn('a', fn (A $a): bool => $a->b < 3);
 | |
| 
 | |
|         $this->assertIsArray($sr);
 | |
|         $this->assertSame(['c', 'b'], $sr);
 | |
|     }
 | |
| 
 | |
|     public function testToArray ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $sr = $a_Array->ToArray();
 | |
| 
 | |
|         $this->assertIsArray($sr);
 | |
|         $this->assertSame($array, $sr);
 | |
| 
 | |
|     }
 | |
| 
 | |
|     public function testMinBy ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $sr = $a_Array->MinBy(fn (A $a) => $a->b);
 | |
| 
 | |
|         $this->assertEquals("b", $sr->a);
 | |
|     }
 | |
| 
 | |
|     public function testIsExist ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $srTrue = $a_Array->IsExist(fn (A $a) => $a->b == 1);
 | |
|         $srFalse = $a_Array->IsExist(fn (A $a) => $a->b == 5);
 | |
| 
 | |
|         $this->assertTrue($srTrue);
 | |
|         $this->assertFalse($srFalse);
 | |
|     }
 | |
| 
 | |
|     public function testUpdate ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $a_Array->Update([new A("d", 3, true)], fn (A $a) => $a->a == "a");
 | |
| 
 | |
|         $sr = $a_Array->Search("a", "d");
 | |
| 
 | |
|         $this->assertEquals("3", $sr->b);
 | |
|         $this->assertTrue($sr->c);
 | |
|     }
 | |
| 
 | |
|     public function testGetRow ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $sr = $a_Array->GetRow(fn (A $a): bool => $a->b == 3);
 | |
| 
 | |
|         $this->assertInstanceOf(A::class, $sr);
 | |
|         $this->assertEquals('a', $sr->a);
 | |
|         $this->assertTrue($sr->c);
 | |
|     }
 | |
| 
 | |
|     public function testFirst ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $sr = $a_Array->First();
 | |
| 
 | |
|         $this->assertNotNull($sr);
 | |
|         $this->assertInstanceOf(A::class, $sr);
 | |
|         $this->assertEquals('a', $sr->a);
 | |
|         $this->assertTrue($sr->c);
 | |
|     }
 | |
| 
 | |
|     public function testLast ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         $sr = $a_Array->Last();
 | |
| 
 | |
|         $this->assertNotNull($sr);
 | |
|         $this->assertInstanceOf(A::class, $sr);
 | |
|         $this->assertEquals('b', $sr->a);
 | |
|         $this->assertTrue($sr->c);
 | |
|     }
 | |
| 
 | |
|     public function testSkip ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|             new A("d", 5, true),
 | |
|             new A("e", 4, true),
 | |
|             new A("f", 6, false)
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         // Для начала отсортируем по b
 | |
|         $a_Array->Sort("b");
 | |
| 
 | |
|         $b_Array = $a_Array->Skip(2);
 | |
| 
 | |
|         $this->assertNotNull($b_Array);
 | |
|         $this->assertEquals(4, $b_Array->Count());
 | |
|         $this->assertEquals(3, $b_Array->First()->b);
 | |
|         $this->assertEquals(6, $b_Array->Last()->b);
 | |
|     }
 | |
| 
 | |
|     public function testTake ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|             new A("d", 5, true),
 | |
|             new A("e", 4, true),
 | |
|             new A("f", 6, false)
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         // Для начала отсортируем по b
 | |
|         $a_Array->Sort("b");
 | |
| 
 | |
|         $b_Array = $a_Array->Take(3);
 | |
| 
 | |
|         $this->assertNotNull($b_Array);
 | |
|         $this->assertEquals(3, $b_Array->Count());
 | |
|         $this->assertEquals(1, $b_Array->First()->b);
 | |
|         $this->assertEquals(3, $b_Array->Last()->b);
 | |
|     }
 | |
| 
 | |
|     public function testSkipAndTake ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|             new A("d", 5, true),
 | |
|             new A("e", 4, true),
 | |
|             new A("f", 6, false)
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray($array);
 | |
| 
 | |
|         // Для начала отсортируем по b
 | |
|         $a_Array->Sort("b");
 | |
| 
 | |
|         $b_Array = $a_Array->SkipAndTake(2, 3);
 | |
| 
 | |
|         $this->assertNotNull($b_Array);
 | |
|         $this->assertEquals(3, $b_Array->Count());
 | |
|         $this->assertEquals(3, $b_Array->First()->b);
 | |
|         $this->assertEquals(5, $b_Array->Last()->b);
 | |
|     }
 | |
| 
 | |
|     public function testAdd ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $obj = new A("a", 3, true);
 | |
| 
 | |
|         $a_Array = new ObjectArray([]);
 | |
| 
 | |
|         $a_Array->Add($obj);
 | |
| 
 | |
|         $b_Array = $a_Array->GetRow(fn (A $a) => $a->a == "a");
 | |
| 
 | |
|         $this->assertEquals(3, $b_Array->b);
 | |
|     }
 | |
| 
 | |
|     public function testAddRange ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         $array = [
 | |
|             new A("a", 3, true),
 | |
|             new A("c", 2, false),
 | |
|             new A("b", 1, true),
 | |
|             new A("d", 5, true),
 | |
|             new A("e", 4, true),
 | |
|             new A("f", 6, false)
 | |
|         ];
 | |
| 
 | |
|         $a_Array = new ObjectArray();
 | |
|         $a_Array->AddRange($array);
 | |
| 
 | |
|         $this->assertEquals(6, $a_Array->Count());
 | |
| 
 | |
|         $array2 = [
 | |
|             new A("g", 3, true),
 | |
|             new A("h", 2, false),
 | |
|             new A("i", 1, true),
 | |
|             new A("k", 5, true),
 | |
|             new A("l", 4, true),
 | |
|             new A("m", 6, false)
 | |
|         ];
 | |
| 
 | |
|         $objectArray = new ObjectArray($array2);
 | |
| 
 | |
|         $a_Array->AddRange($objectArray);
 | |
| 
 | |
|         $this->assertEquals(12, $a_Array->Count());
 | |
|     }
 | |
| 
 | |
|     public function testSerializeEx ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         // Создаём тестовые классы
 | |
|         $class1 = new D ('test_string1', 12345, true, new A("test_string_A1", 6789, false),
 | |
|             new B("test_string_B1", 9876, "false"), new C("test_string_C1", 54321, true));
 | |
|         $class2 = new D ('test_string2', 123456, false, new A("test_string_A1", 678910, true),
 | |
|             new B("test_string_B2", 98765, "true"), new C("test_string_C2", 543210, false));
 | |
|         $class = new D ('test_string3', 123450, true, new A("test_string_A2", 67890, false),
 | |
|             new B("test_string_B3", 90876, "false"), new C("test_string_C3", 543201, true));
 | |
| 
 | |
|         // Создаём массив объектов
 | |
|         $objectArray = new ObjectArray([$class1, $class2, $class]);
 | |
| 
 | |
|         // Сериализуем
 | |
|         $serialized1 =
 | |
|             $objectArray->SerializeEx(new ObjectArraySerializeOptions(ObjectArraySerializeMethod::Serialize));
 | |
|         $serialized2 =
 | |
|             $objectArray->SerializeEx(new ObjectArraySerializeOptions(ObjectArraySerializeMethod::JsonEncode));
 | |
|         $serialized3 =
 | |
|             $objectArray->SerializeEx(new ObjectArraySerializeOptions(ObjectArraySerializeMethod::JsonEncodeWithToArray,
 | |
|                 D::CLOSURE_TO_ARRAY()));
 | |
| 
 | |
|         // Сохраняем в файл
 | |
|         file_put_contents(__DIR__ . 'serialized1.txt', $serialized1);
 | |
|         file_put_contents(__DIR__ . 'serialized2.txt', $serialized2);
 | |
|         file_put_contents(__DIR__ . 'serialized3.txt', $serialized3);
 | |
| 
 | |
|         // Проверяем, что всё хорошо
 | |
|         $this->assertNotEmpty($serialized1);
 | |
|         $this->assertNotEmpty($serialized2);
 | |
|         $this->assertNotEmpty($serialized3);
 | |
|     }
 | |
| 
 | |
|     public function testUnSerializeEx ()
 | |
|     {
 | |
|         $this->PrepareForTest();
 | |
| 
 | |
|         // Загружаем данные
 | |
|         $serialized1 = file_get_contents(__DIR__ . 'serialized1.txt');
 | |
|         $serialized2 = file_get_contents(__DIR__ . 'serialized2.txt');
 | |
|         $serialized3 = file_get_contents(__DIR__ . 'serialized3.txt');
 | |
| 
 | |
|         // Десериализуем
 | |
|         $objectArray1 = ObjectArray::UnSerializeEx($serialized1,
 | |
|             new ObjectArraySerializeOptions(ObjectArraySerializeMethod::Serialize));
 | |
|         $objectArray2 = ObjectArray::UnSerializeEx($serialized2,
 | |
|             new ObjectArraySerializeOptions(ObjectArraySerializeMethod::JsonEncode));
 | |
|         $objectArray3 = ObjectArray::UnSerializeEx($serialized3,
 | |
|             new ObjectArraySerializeOptions(ObjectArraySerializeMethod::JsonEncodeWithToArray,
 | |
|                 onClassFrom: D::CLOSURE_FROM_ARRAY()));
 | |
| 
 | |
|         // Проверяем, что всё хорошо
 | |
|         $this->assertEquals('test_string_A1',
 | |
|             $objectArray1->GetRow(fn (D $value) => $value->stringD == 'test_string1')->a->a);
 | |
|         $this->assertEquals('test_string_B2',
 | |
|             $objectArray2->GetRow(fn (stdClass $value) => $value->stringD == 'test_string2')->b->a);
 | |
|         $this->assertEquals('test_string_C3',
 | |
|             $objectArray3->GetRow(fn (D $value) => $value->stringD == 'test_string3')->c->stringC);
 | |
|     }
 | |
| } |