implemented support for PHPUnit's @depends

This commit is contained in:
Adrian Nürnberger
2020-06-19 19:50:54 +02:00
parent 283d8f3e03
commit d0a74931dd
7 changed files with 120 additions and 12 deletions

View File

@ -53,6 +53,11 @@ trait TestCase
$this->setGroups($groups); $this->setGroups($groups);
} }
public function addDependencies(array $tests): void
{
$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
@ -130,16 +135,16 @@ trait TestCase
/** /**
* Runs the test. * Runs the test.
*/ */
public function __test(): void public function __test()
{ {
$this->__callClosure($this->__test, func_get_args()); return $this->__callClosure($this->__test, func_get_args());
} }
private function __callClosure(Closure $closure, array $arguments): void private function __callClosure(Closure $closure, array $arguments)
{ {
ExceptionTrace::ensure(function () use ($closure, $arguments) { return ExceptionTrace::ensure(function () use ($closure, $arguments) {
call_user_func_array(Closure::bind($closure, $this, get_class($this)), $arguments); return call_user_func_array(Closure::bind($closure, $this, get_class($this)), $arguments);
}); })->getValue();
} }
public function getPrintableTestCaseName(): string public function getPrintableTestCaseName(): string

View File

@ -132,10 +132,11 @@ final class TestCaseFactory
$proxies = $this->proxies; $proxies = $this->proxies;
$factoryTest = $this->test; $factoryTest = $this->test;
$test = function () use ($chains, $proxies, $factoryTest): void { $test = function () use ($chains, $proxies, $factoryTest) {
$proxies->proxy($this); $proxies->proxy($this);
$chains->chain($this); $chains->chain($this);
call_user_func(Closure::bind($factoryTest, $this, get_class($this)), ...func_get_args());
return call_user_func(Closure::bind($factoryTest, $this, get_class($this)), ...func_get_args());
}; };
$className = $this->makeClassFromFilename($this->filename); $className = $this->makeClassFromFilename($this->filename);

View File

@ -84,6 +84,20 @@ final class TestCall
return $this; return $this;
} }
public function dependsOn(string ...$tests): TestCall
{
$this->testCaseFactory
->factoryProxies
->add(Backtrace::file(), Backtrace::line(), 'addDependencies', [$tests]);
return $this;
}
public function depends(string ...$tests): TestCall
{
return $this->dependsOn(...$tests);
}
/** /**
* Makes the test suite only this test case. * Makes the test suite only this test case.
*/ */

View File

@ -18,10 +18,10 @@ final class ExceptionTrace
* Ensures the given closure reports * Ensures the given closure reports
* the good execution context. * the good execution context.
*/ */
public static function ensure(Closure $closure): void public static function ensure(Closure $closure): Mixed
{ {
try { try {
$closure(); return new Mixed($closure());
} catch (Throwable $throwable) { } catch (Throwable $throwable) {
if (Str::startsWith($message = $throwable->getMessage(), self::UNDEFINED_METHOD)) { if (Str::startsWith($message = $throwable->getMessage(), self::UNDEFINED_METHOD)) {
$message = str_replace(self::UNDEFINED_METHOD, 'Call to undefined method ', $message); $message = str_replace(self::UNDEFINED_METHOD, 'Call to undefined method ', $message);

32
src/Support/Mixed.php Normal file
View File

@ -0,0 +1,32 @@
<?php
declare(strict_types=1);
namespace Pest\Support;
/**
* @internal
*/
final class Mixed
{
/**
* @var mixed
*/
private $value;
/**
* @param mixed $value
*/
public function __construct($value)
{
$this->value = $value;
}
/**
* @return mixed
*/
public function getValue()
{
return $this->value;
}
}

View File

@ -42,6 +42,15 @@
✓ eager registered wrapped datasets did the job right ✓ eager registered wrapped datasets did the job right
✓ lazy named datasets with (Bar Object (...)) ✓ lazy named datasets with (Bar Object (...))
PASS Tests\Features\Depends
✓ first
✓ second
✓ dependsOn
✓ dependsOn with ...params
✓ dependsOn with defined arguments
✓ dependsOn run test only once
✓ depends alias for dependsOn
PASS Tests\Features\Exceptions PASS Tests\Features\Exceptions
✓ it gives access the the underlying expectException ✓ it gives access the the underlying expectException
✓ it catch exceptions ✓ it catch exceptions
@ -144,5 +153,5 @@
WARN Tests\Visual\Success WARN Tests\Visual\Success
s visual snapshot of test suite on success s visual snapshot of test suite on success
Tests: 6 skipped, 79 passed Tests: 6 skipped, 86 passed
Time: 3.44s Time: 2.61s

View File

@ -0,0 +1,47 @@
<?php
$runCounter = 0;
test('first', function () use (&$runCounter) {
assertTrue(true);
$runCounter++;
return 'first';
});
test('second', function () use (&$runCounter) {
assertTrue(true);
$runCounter++;
return 'second';
});
test('dependsOn', function () {
assertEquals(
['first', 'second'],
func_get_args()
);
})->dependsOn('first', 'second');
test('dependsOn with ...params', function (string ...$params) {
assertEquals(
['first', 'second'],
$params
);
})->dependsOn('first', 'second');
test('dependsOn with defined arguments', function (string $first, string $second) {
assertEquals('first', $first);
assertEquals('second', $second);
})->dependsOn('first', 'second');
test('dependsOn run test only once', function () use (&$runCounter) {
assertEquals(2, $runCounter);
})->dependsOn('first', 'second');
test('depends alias for dependsOn', function (string ...$params) {
assertEquals(
['first', 'second'],
$params
);
})->depends('first', 'second');