diff --git a/core/resource/DependsOnPropertyCollection.php b/core/resource/DependsOnPropertyCollection.php index 66a9ca5e0..04866050b 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->areArraysEqual($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 areArraysEqual(array $array1, array $array2): bool + { + return empty(array_diff($array1, $array2)) && empty(array_diff($array2, $array1)); + } } diff --git a/test/unit/core/resource/DependsOnPropertyCollectionTest.php b/test/unit/core/resource/DependsOnPropertyCollectionTest.php new file mode 100644 index 000000000..18762b757 --- /dev/null +++ b/test/unit/core/resource/DependsOnPropertyCollectionTest.php @@ -0,0 +1,83 @@ +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()); + } +}