From b6e2763731542da7dd9069d4aa884fc29f2a0dee Mon Sep 17 00:00:00 2001 From: avrahamappel Date: Thu, 17 Dec 2020 23:24:11 -0500 Subject: [PATCH] Move dependency mapping to TestCase The `TestCaseFactory::getClassName` was removed since it can have unexpected results when called too early, as it builds up the test class prematurely. It is not currently used anywhere else. --- src/Concerns/TestCase.php | 19 +++++++++++++++++++ src/Factories/TestCaseFactory.php | 8 -------- src/PendingObjects/TestCall.php | 13 +------------ 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Concerns/TestCase.php b/src/Concerns/TestCase.php index b7d29448..9581ae57 100644 --- a/src/Concerns/TestCase.php +++ b/src/Concerns/TestCase.php @@ -7,6 +7,7 @@ namespace Pest\Concerns; use Closure; use Pest\Support\ExceptionTrace; use Pest\TestSuite; +use PHPUnit\Framework\ExecutionOrderDependency; use PHPUnit\Util\Test; use Throwable; @@ -54,6 +55,24 @@ trait TestCase $this->setGroups($groups); } + /** + * Add dependencies to the test case and map them to instances of ExecutionOrderDependency. + */ + public function addDependencies(array $tests): void + { + $className = get_class($this); + + $tests = array_map(function (string $test) use ($className): ExecutionOrderDependency { + if (strpos($test, '::') === false) { + $test = "{$className}::{$test}"; + } + + return new ExecutionOrderDependency($test, null, ''); + }, $tests); + + $this->setDependencies($tests); + } + /** * Returns the test case name. Note that, in Pest * we ignore withDataset argument as the description diff --git a/src/Factories/TestCaseFactory.php b/src/Factories/TestCaseFactory.php index 5b6c2167..f3fdf031 100644 --- a/src/Factories/TestCaseFactory.php +++ b/src/Factories/TestCaseFactory.php @@ -156,14 +156,6 @@ final class TestCaseFactory return array_map($createTest, array_keys($datasets), $datasets); } - /** - * Makes a fully qualified class name from the current filename. - */ - public function getClassName(): string - { - return $this->makeClassFromFilename($this->filename); - } - /** * Makes a fully qualified class name from the given filename. */ diff --git a/src/PendingObjects/TestCall.php b/src/PendingObjects/TestCall.php index afbec8e8..76b73e28 100644 --- a/src/PendingObjects/TestCall.php +++ b/src/PendingObjects/TestCall.php @@ -9,7 +9,6 @@ use Pest\Factories\TestCaseFactory; use Pest\Support\Backtrace; use Pest\Support\NullClosure; use Pest\TestSuite; -use PHPUnit\Framework\ExecutionOrderDependency; use SebastianBergmann\Exporter\Exporter; /** @@ -92,19 +91,9 @@ final class TestCall */ public function depends(string ...$tests): TestCall { - $className = $this->testCaseFactory->getClassName(); - - $tests = array_map(function (string $test) use ($className): ExecutionOrderDependency { - if (strpos($test, '::') === false) { - $test = "{$className}::{$test}"; - } - - return new ExecutionOrderDependency($test, null, ''); - }, $tests); - $this->testCaseFactory ->factoryProxies - ->add(Backtrace::file(), Backtrace::line(), 'setDependencies', [$tests]); + ->add(Backtrace::file(), Backtrace::line(), 'addDependencies', [$tests]); return $this; }