From f09b4e5a3b73965ba3464186970b48635b73edda Mon Sep 17 00:00:00 2001 From: Andrei Shapiro Date: Fri, 24 Sep 2021 09:42:22 +0000 Subject: [PATCH 1/4] feat: add equal method to dependd on property collections --- core/resource/DependsOnPropertyCollection.php | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/core/resource/DependsOnPropertyCollection.php b/core/resource/DependsOnPropertyCollection.php index 66a9ca5e0..86709b799 100644 --- a/core/resource/DependsOnPropertyCollection.php +++ b/core/resource/DependsOnPropertyCollection.php @@ -26,8 +26,27 @@ use core_kernel_classes_Property; /** - * @method core_kernel_classes_Property current() + * @method core_kernel_classes_Property|null current() */ class DependsOnPropertyCollection extends ArrayIterator { + public function isEqual(DependsOnPropertyCollection $dependsOnPropertyCollection): bool + { + return $this->isArraysEqual($this->getPropertyUris(), $dependsOnPropertyCollection->getPropertyUris()); + } + + public function getPropertyUris(): array + { + return array_map( + static function (core_kernel_classes_Property $property) { + return $property->getUri(); + }, + $this->getArrayCopy() + ); + } + + private function isArraysEqual(array $array1, array $array2): bool + { + return empty(array_diff($array1, $array2)) && empty(array_diff($array2, $array1)); + } } From 0856e937b95ba1fe5cc39b9ffc0df2cc64b8cdd8 Mon Sep 17 00:00:00 2001 From: Andrei Shapiro Date: Fri, 24 Sep 2021 10:25:06 +0000 Subject: [PATCH 2/4] test: add unit tests for DependsOnPropertyColleciton class --- .../DependsOnPropertyCollectionTest.php | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 test/unit/core/resource/DependsOnPropertyCollectionTest.php diff --git a/test/unit/core/resource/DependsOnPropertyCollectionTest.php b/test/unit/core/resource/DependsOnPropertyCollectionTest.php new file mode 100644 index 000000000..ec4debb05 --- /dev/null +++ b/test/unit/core/resource/DependsOnPropertyCollectionTest.php @@ -0,0 +1,65 @@ +sut = new DependsOnPropertyCollection(); + } + + public function testIsEqual(): void + { + $dependsOnPropertyCollection = new DependsOnPropertyCollection(); + $this->assertTrue($this->sut->isEqual($dependsOnPropertyCollection)); + + $firstProperty = $this->createMock(core_kernel_classes_Property::class); + $firstProperty + ->method('getUri') + ->willReturn('firstProperty'); + $dependsOnPropertyCollection->append($firstProperty); + $this->assertFalse($this->sut->isEqual($dependsOnPropertyCollection)); + + $secondProperty = $this->createMock(core_kernel_classes_Property::class); + $secondProperty + ->method('getUri') + ->willReturn('secondProperty'); + $this->sut->append($secondProperty); + $this->assertFalse($this->sut->isEqual($dependsOnPropertyCollection)); + + $dependsOnPropertyCollection->append($secondProperty); + $this->assertFalse($this->sut->isEqual($dependsOnPropertyCollection)); + + $this->sut->append($firstProperty); + $this->assertTrue($this->sut->isEqual($dependsOnPropertyCollection)); + } + + public function testGetPropertyUris(): void + { + $this->assertEmpty($this->sut->getPropertyUris()); + + $firstProperty = $this->createMock(core_kernel_classes_Property::class); + $firstProperty + ->method('getUri') + ->willReturn('firstProperty'); + $this->sut->append($firstProperty); + $this->assertEquals(['firstProperty'], $this->sut->getPropertyUris()); + + $secondProperty = $this->createMock(core_kernel_classes_Property::class); + $secondProperty + ->method('getUri') + ->willReturn('secondProperty'); + $this->sut->append($secondProperty); + $this->assertEquals(['firstProperty', 'secondProperty'], $this->sut->getPropertyUris()); + } +} From 418762d15c46ea394d92ca41298fa13fc7fdb80c Mon Sep 17 00:00:00 2001 From: Andrei Shapiro Date: Fri, 24 Sep 2021 12:08:33 +0000 Subject: [PATCH 3/4] refactor: update private method nmae --- core/resource/DependsOnPropertyCollection.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core/resource/DependsOnPropertyCollection.php b/core/resource/DependsOnPropertyCollection.php index 86709b799..04866050b 100644 --- a/core/resource/DependsOnPropertyCollection.php +++ b/core/resource/DependsOnPropertyCollection.php @@ -32,7 +32,7 @@ class DependsOnPropertyCollection extends ArrayIterator { public function isEqual(DependsOnPropertyCollection $dependsOnPropertyCollection): bool { - return $this->isArraysEqual($this->getPropertyUris(), $dependsOnPropertyCollection->getPropertyUris()); + return $this->areArraysEqual($this->getPropertyUris(), $dependsOnPropertyCollection->getPropertyUris()); } public function getPropertyUris(): array @@ -45,7 +45,7 @@ static function (core_kernel_classes_Property $property) { ); } - private function isArraysEqual(array $array1, array $array2): bool + private function areArraysEqual(array $array1, array $array2): bool { return empty(array_diff($array1, $array2)) && empty(array_diff($array2, $array1)); } From 7bce55a1df4a7967d0d20306636f2b10886ab960 Mon Sep 17 00:00:00 2001 From: Andrei Shapiro Date: Fri, 24 Sep 2021 14:52:44 +0000 Subject: [PATCH 4/4] test: add missed dock block --- .../DependsOnPropertyCollectionTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/unit/core/resource/DependsOnPropertyCollectionTest.php b/test/unit/core/resource/DependsOnPropertyCollectionTest.php index ec4debb05..18762b757 100644 --- a/test/unit/core/resource/DependsOnPropertyCollectionTest.php +++ b/test/unit/core/resource/DependsOnPropertyCollectionTest.php @@ -1,5 +1,23 @@