mirror of
https://github.com/pestphp/pest.git
synced 2026-12-05 23:32:17 +01:00
perf: O(1) test method lookup
This commit is contained in:
@@ -44,6 +44,11 @@ final class TestCaseFactory
|
||||
*/
|
||||
public array $methods = [];
|
||||
|
||||
/**
|
||||
* @var array<string, TestCaseMethodFactory>
|
||||
*/
|
||||
private array $methodsByName = [];
|
||||
|
||||
/**
|
||||
* @var array <int, class-string>
|
||||
*/
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1604 passed (3502 assertions)
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php
|
||||
|
||||
use Pest\Exceptions\DatasetMissing;
|
||||
use Pest\Exceptions\ShouldNotHappen;
|
||||
use Pest\Exceptions\TestAlreadyExist;
|
||||
use Pest\Exceptions\TestClosureMustNotBeStatic;
|
||||
use Pest\Factories\TestCaseMethodFactory;
|
||||
@@ -43,6 +44,31 @@ it('alerts users about tests with arguments but no input', function (): void {
|
||||
sprintf('The test [%s] in [%s] expects [%d] argument(s) ([%s]), but no dataset was provided. Please chain [with()] onto the test to supply one.', 'bar', 'foo', 1, 'int $arg'),
|
||||
);
|
||||
|
||||
it('can look up registered test methods by evaluable name', function (): void {
|
||||
$testSuite = new TestSuite(getcwd(), 'tests');
|
||||
|
||||
$firstMethod = new TestCaseMethodFactory('foo', null);
|
||||
$firstMethod->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');
|
||||
|
||||
|
||||
@@ -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}")
|
||||
|
||||
Reference in New Issue
Block a user