mirror of
https://github.com/pestphp/pest.git
synced 2026-03-12 18:57:22 +01:00
Merge pull request #236 from avrahamappel/master
Add test for inheritance with depends
This commit is contained in:
@ -7,6 +7,7 @@ namespace Pest\Concerns;
|
|||||||
use Closure;
|
use Closure;
|
||||||
use Pest\Support\ExceptionTrace;
|
use Pest\Support\ExceptionTrace;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
|
use PHPUnit\Framework\ExecutionOrderDependency;
|
||||||
use PHPUnit\Util\Test;
|
use PHPUnit\Util\Test;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
|
||||||
@ -54,6 +55,24 @@ trait TestCase
|
|||||||
$this->setGroups($groups);
|
$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
|
* Returns the test case name. Note that, in Pest
|
||||||
* we ignore withDataset argument as the description
|
* we ignore withDataset argument as the description
|
||||||
|
|||||||
@ -156,14 +156,6 @@ final class TestCaseFactory
|
|||||||
return array_map($createTest, array_keys($datasets), $datasets);
|
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.
|
* Makes a fully qualified class name from the given filename.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -9,7 +9,6 @@ use Pest\Factories\TestCaseFactory;
|
|||||||
use Pest\Support\Backtrace;
|
use Pest\Support\Backtrace;
|
||||||
use Pest\Support\NullClosure;
|
use Pest\Support\NullClosure;
|
||||||
use Pest\TestSuite;
|
use Pest\TestSuite;
|
||||||
use PHPUnit\Framework\ExecutionOrderDependency;
|
|
||||||
use SebastianBergmann\Exporter\Exporter;
|
use SebastianBergmann\Exporter\Exporter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -92,19 +91,9 @@ final class TestCall
|
|||||||
*/
|
*/
|
||||||
public function depends(string ...$tests): 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
|
$this->testCaseFactory
|
||||||
->factoryProxies
|
->factoryProxies
|
||||||
->add(Backtrace::file(), Backtrace::line(), 'setDependencies', [$tests]);
|
->add(Backtrace::file(), Backtrace::line(), 'addDependencies', [$tests]);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -184,5 +184,9 @@
|
|||||||
✓ depends run test only once
|
✓ depends run test only once
|
||||||
✓ depends works with the correct test name
|
✓ depends works with the correct test name
|
||||||
|
|
||||||
Tests: 7 skipped, 106 passed
|
PASS Tests\Features\DependsInheritance
|
||||||
|
✓ it is a test
|
||||||
|
✓ it uses correct parent class
|
||||||
|
|
||||||
|
Tests: 7 skipped, 108 passed
|
||||||
|
|
||||||
22
tests/Features/DependsInheritance.php
Normal file
22
tests/Features/DependsInheritance.php
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
class InheritanceTest extends TestCase
|
||||||
|
{
|
||||||
|
public function foo()
|
||||||
|
{
|
||||||
|
return 'bar';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
uses(InheritanceTest::class);
|
||||||
|
|
||||||
|
it('is a test', function () {
|
||||||
|
expect(true)->toBeTrue();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('uses correct parent class', function () {
|
||||||
|
expect(get_parent_class($this))->toEqual(InheritanceTest::class);
|
||||||
|
expect($this->foo())->toEqual('bar');
|
||||||
|
})->depends('it is a test');
|
||||||
Reference in New Issue
Block a user