From e9fa0f50c5c6f11be77bf8d69ba121c9f0b54d8c Mon Sep 17 00:00:00 2001 From: Pushpak Chhajed Date: Fri, 16 Oct 2026 22:20:38 +0530 Subject: [PATCH] perf: O(1) test method lookup --- src/Factories/TestCaseFactory.php | 31 +++++++++---------------------- tests/.snapshots/success.txt | 3 ++- tests/Unit/TestSuite.php | 26 ++++++++++++++++++++++++++ tests/Visual/Parallel.php | 2 +- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index 69bece9e..3ddeb28e 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -44,6 +44,11 @@ final class TestCaseFactory */ public array $methods = []; + /** + * @var array + */ + private array $methodsByName = []; + /** * @var array */ @@ -208,35 +213,17 @@ final class TestCaseFactory } $this->methods[$method->description] = $method; + $this->methodsByName[Str::evaluable($method->description)] ??= $method; } public function hasMethod(string $methodName): bool { - foreach ($this->methods as $method) { - if ($method->description === null) { - throw ShouldNotHappen::fromMessage('The test description may not be empty.'); - } - - if ($methodName === Str::evaluable($method->description)) { - return true; - } - } - - return false; + return isset($this->methodsByName[$methodName]); } public function getMethod(string $methodName): TestCaseMethodFactory { - foreach ($this->methods as $method) { - if ($method->description === null) { - throw ShouldNotHappen::fromMessage('The test description may not be empty.'); - } - - if ($methodName === Str::evaluable($method->description)) { - return $method; - } - } - - throw ShouldNotHappen::fromMessage(sprintf('Method [%s] not found.', $methodName)); + return $this->methodsByName[$methodName] + ?? throw ShouldNotHappen::fromMessage(sprintf('Method [%s] not found.', $methodName)); } } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 9142c9ed..df2c0a77 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2204,6 +2204,7 @@ ✓ it does not allow to add the same test description twice ✓ it does not allow static closures ✓ it alerts users about tests with arguments but no input + ✓ it can look up registered test methods by evaluable name ✓ it can return an array of all test suite filenames PASS Tests\Visual\BeforeEachTestName @@ -2262,4 +2263,4 @@ ✓ pass with dataset with ('my-datas-set-value') ✓ within describe → pass with dataset with ('my-datas-set-value') - Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1603 passed (3495 assertions) \ No newline at end of file + Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1604 passed (3502 assertions) \ No newline at end of file diff --git a/tests/Unit/TestSuite.php b/tests/Unit/TestSuite.php index 18b26c83..ed6e892c 100644 --- a/tests/Unit/TestSuite.php +++ b/tests/Unit/TestSuite.php @@ -1,6 +1,7 @@ description = 'first test_with punctuation!'; + $testSuite->tests->set($firstMethod); + + $secondMethod = new TestCaseMethodFactory('foo', null); + $secondMethod->description = 'second test'; + $testSuite->tests->set($secondMethod); + + $collidingMethod = new TestCaseMethodFactory('foo', null); + $collidingMethod->description = 'second-test'; + $testSuite->tests->set($collidingMethod); + + $testCase = $testSuite->tests->get('foo'); + + expect($testCase->hasMethod('__pest_evaluable_first_test__with_punctuation_'))->toBeTrue() + ->and($testCase->getMethod('__pest_evaluable_first_test__with_punctuation_'))->toBe($firstMethod) + ->and($testCase->hasMethod('__pest_evaluable_second_test'))->toBeTrue() + ->and($testCase->getMethod('__pest_evaluable_second_test'))->toBe($secondMethod) + ->and($testCase->hasMethod('__pest_evaluable_missing'))->toBeFalse() + ->and(fn () => $testCase->getMethod('__pest_evaluable_missing'))->toThrow(ShouldNotHappen::class, 'Method [__pest_evaluable_missing] not found.'); +}); + it('can return an array of all test suite filenames', function (): void { $testSuite = new TestSuite(getcwd(), 'tests'); diff --git a/tests/Visual/Parallel.php b/tests/Visual/Parallel.php index 4c712189..3f3b9371 100644 --- a/tests/Visual/Parallel.php +++ b/tests/Visual/Parallel.php @@ -20,7 +20,7 @@ test('parallel', function () use ($run): void { $output = $run('--exclude-group=integration'); $output = implode("\n", array_slice(explode("\n", (string) $output), -10)); - $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1584 passed (3437 assertions)'; + $expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1585 passed (3444 assertions)'; expect($output) ->toContain("Tests: {$expected}")