From e2cb115d46e1d07d9a3a03914e4deba5933b42fd Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Mon, 9 Sep 2024 01:39:24 +0800 Subject: [PATCH] Add test for sorting of collection methods --- .../Collection/AbstractCollectionTestCase.php | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/Collection/AbstractCollectionTestCase.php b/tests/Collection/AbstractCollectionTestCase.php index b036264..075867b 100644 --- a/tests/Collection/AbstractCollectionTestCase.php +++ b/tests/Collection/AbstractCollectionTestCase.php @@ -110,6 +110,39 @@ public function testValues(): void self::assertSame([5, 4, 7], $collection->values()->all(true)); } + public function testCollectionHasMethodsArrangedInParticularOrder(): void + { + $reflection = new \ReflectionClass($this->collection()); + $publicMethods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC); + $sortedPublicMethods = $publicMethods; + usort( + $sortedPublicMethods, + static function (\ReflectionMethod $a, \ReflectionMethod $b): int { + if ($a->isConstructor()) { + return -1; + } + + if ($b->isConstructor()) { + return 1; + } + + if ($a->isStatic() && ! $b->isStatic()) { + return -1; + } + + if (! $a->isStatic() && $b->isStatic()) { + return 1; + } + + return strcmp($a->getName(), $b->getName()); + }, + ); + $publicMethods = array_map(static fn(\ReflectionMethod $rm): string => $rm->getName(), $publicMethods); + $sortedPublicMethods = array_map(static fn(\ReflectionMethod $rm): string => $rm->getName(), $sortedPublicMethods); + + self::assertSame($sortedPublicMethods, $publicMethods); + } + /** * @template TKey * @template T