diff --git a/src/Repositories/TestRepository.php b/src/Repositories/TestRepository.php index d43ed458..305e8752 100644 --- a/src/Repositories/TestRepository.php +++ b/src/Repositories/TestRepository.php @@ -45,13 +45,17 @@ final class TestRepository } /** + * Returns the filename of each test that should be run in the suite. + * * @return array */ public function getFilenames(): array { + $testsWithOnly = $this->testsUsingOnly(); + return array_values(array_map(function (TestCaseFactory $factory): string { return $factory->filename; - }, $this->state)); + }, count($testsWithOnly) > 0 ? $testsWithOnly : $this->state)); } /** @@ -95,9 +99,7 @@ final class TestRepository } } - $onlyState = array_filter($this->state, function ($testFactory): bool { - return $testFactory->only; - }); + $onlyState = $this->testsUsingOnly(); $state = count($onlyState) > 0 ? $onlyState : $this->state; @@ -110,6 +112,18 @@ final class TestRepository } } + /** + * Return all tests that have called the only method. + * + * @return array + */ + private function testsUsingOnly(): array + { + return array_filter($this->state, function ($testFactory): bool { + return $testFactory->only; + }); + } + /** * Uses the given `$testCaseClass` on the given `$paths`. * diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 54cbeb84..30b28483 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -615,6 +615,7 @@ ✓ it does not allow to add the same test description twice ✓ it alerts users about tests with arguments but no input ✓ it can return an array of all test suite filenames + ✓ it can filter the test suite filenames to those with the only method PASS Tests\Visual\Help ✓ visual snapshot of help command output @@ -648,5 +649,5 @@ ✓ it is a test ✓ it uses correct parent class - Tests: 4 incompleted, 9 skipped, 420 passed + Tests: 4 incompleted, 9 skipped, 421 passed \ No newline at end of file diff --git a/tests/Unit/TestSuite.php b/tests/Unit/TestSuite.php index 8d4cbf05..7d6c88a9 100644 --- a/tests/Unit/TestSuite.php +++ b/tests/Unit/TestSuite.php @@ -34,3 +34,18 @@ it('can return an array of all test suite filenames', function () { __FILE__, ]); }); + +it('can filter the test suite filenames to those with the only method', function () { + $testSuite = new TestSuite(getcwd(), 'tests'); + $test = function () {}; + + $testWithOnly = new \Pest\Factories\TestCaseFactory(__FILE__, 'foo', $test); + $testWithOnly->only = true; + $testSuite->tests->set($testWithOnly); + + $testSuite->tests->set(new \Pest\Factories\TestCaseFactory('Baz/Bar/Boo.php', 'bar', $test)); + + expect($testSuite->tests->getFilenames())->toEqual([ + __FILE__, + ]); +});