Uses Collision ^7.0

This commit is contained in:
Nuno Maduro
2022-09-15 01:07:15 +01:00
parent eab944023c
commit 3ff95faaaa
47 changed files with 646 additions and 308 deletions

View File

@ -9,13 +9,21 @@ use Pest\Support\ChainableClosure;
use Pest\Support\ExceptionTrace;
use Pest\Support\Reflection;
use Pest\TestSuite;
use PHPUnit\Framework\TestCase;
use Throwable;
/**
* @internal
*
* @mixin TestCase
*/
trait Testable
{
/**
* Test method description.
*/
private static string $__description;
/**
* The Test Case "test" closure.
*/
@ -118,14 +126,6 @@ trait Testable
: $hook;
}
/**
* Gets the Test Case filename.
*/
public static function __getFilename(): string
{
return self::$__filename;
}
/**
* This method is called before the first test of this Test Case is run.
*/
@ -211,6 +211,13 @@ trait Testable
*/
private function __resolveTestArguments(array $arguments): array
{
$method = TestSuite::getInstance()->tests->get(self::$__filename)->getMethod($this->name());
if ($this->dataName()) {
self::$__description = $method->description . ' with ' . $this->dataName();
} else {
self::$__description = $method->description;
}
if (count($arguments) !== 1) {
return $arguments;
}
@ -246,8 +253,16 @@ trait Testable
/**
* Gets the Test Case name that should be used by printers.
*/
public function getPrintableTestCaseName(): string
public static function getPrintableTestCaseName(): string
{
return ltrim(self::class, 'P\\');
}
/**
* Gets the Test Case name that should be used by printers.
*/
public static function getPrintableTestCaseMethodName(): string
{
return self::$__description;
}
}

View File

@ -12,9 +12,9 @@ interface HandlesArguments
/**
* Adds arguments before of the Test Suite execution.
*
* @param array<int, string> $argv
* @param array<int, string> $arguments
*
* @return array<int, string>
*/
public function handleArguments(array $argv): array;
public function handleArguments(array $arguments): array;
}

View File

@ -19,7 +19,7 @@ final class ShouldNotHappen extends RuntimeException
{
$message = $exception->getMessage();
parent::__construct(sprintf(<<<EOF
parent::__construct(sprintf(<<<'EOF'
This should not happen - please create an new issue here: https://github.com/pestphp/pest.

View File

@ -186,6 +186,7 @@ final class Expectation
foreach ($values as $key => $item) {
if ($callbacks[$key] instanceof Closure) {
call_user_func($callbacks[$key], new self($item), new self($keys[$key]));
continue;
}
@ -220,6 +221,7 @@ final class Expectation
if (is_callable($callback)) {
$callback(new self($this->value));
continue;
}

View File

@ -37,7 +37,7 @@ final class HigherOrderExpectation
*/
public function __construct(private Expectation $original, mixed $value)
{
$this->expectation = $this->expect($value);
$this->expectation = $this->expect($value);
}
/**

View File

@ -121,7 +121,17 @@ final class TestCaseFactory
$filename = str_replace('\\\\', '\\', addslashes((string) realpath($filename)));
$rootPath = TestSuite::getInstance()->rootPath;
$relativePath = str_replace($rootPath . DIRECTORY_SEPARATOR, '', $filename);
$relativePath = dirname(ucfirst($relativePath)) . DIRECTORY_SEPARATOR . basename($relativePath, '.php');
$basename = basename($relativePath, '.php');
$dotPos = strpos($basename, '.');
if ($dotPos !== false) {
$basename = substr($basename, 0, $dotPos);
}
$relativePath = dirname(ucfirst($relativePath)) . DIRECTORY_SEPARATOR . $basename;
$relativePath = str_replace(DIRECTORY_SEPARATOR, '\\', $relativePath);
// Strip out any %-encoded octets.
@ -132,6 +142,7 @@ final class TestCaseFactory
$relativePath = (string) preg_replace('/[^A-Za-z0-9\\\\]/', '', $relativePath);
$classFQN = 'P\\' . $relativePath;
if (class_exists($classFQN)) {
return;
}
@ -174,27 +185,28 @@ final class TestCaseFactory
));
$classAttributesCode = implode('', array_map(
static fn (string $attribute) => sprintf("\n %s", $attribute),
static fn (string $attribute) => sprintf("\n%s", $attribute),
array_unique($classAttributes),
));
try {
eval("
namespace $namespace;
$classCode = <<<PHP
namespace $namespace;
use Pest\Repositories\DatasetsRepository as __PestDatasets;
use Pest\TestSuite as __PestTestSuite;
use Pest\Repositories\DatasetsRepository as __PestDatasets;
use Pest\TestSuite as __PestTestSuite;
$classAttributesCode
#[\AllowDynamicProperties]
final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {
$traitsCode
$classAttributesCode
#[\AllowDynamicProperties]
final class $className extends $baseClass implements $hasPrintableTestCaseClassFQN {
$traitsCode
private static \$__filename = '$filename';
private static \$__filename = '$filename';
$methodsCode
}
PHP;
$methodsCode
}
");
eval($classCode);
} catch (ParseError $caught) {
throw new RuntimeException(sprintf('Unable to create test case for test file at %s', $filename), 1, $caught);
}

View File

@ -91,7 +91,6 @@ final class TestCaseMethodFactory
return function () use ($testCase, $method, $closure): mixed { // @phpstan-ignore-line
/* @var TestCase $this */
$testCase->proxies->proxy($this);
$method->proxies->proxy($this);
@ -148,28 +147,29 @@ final class TestCaseMethodFactory
}
$annotations = implode('', array_map(
static fn ($annotation) => sprintf("\n * %s", $annotation), $annotations,
static fn ($annotation) => sprintf("\n * %s", $annotation), $annotations,
));
$attributes = implode('', array_map(
static fn ($attribute) => sprintf("\n %s", $attribute), $attributes,
));
return <<<EOF
return <<<PHP
/**$annotations
*/
$attributes
public function $methodName()
{
\$test = \Pest\TestSuite::getInstance()->tests->get(self::\$__filename)->getMethod(\$this->name())->getClosure(\$this);
return \$this->__runTest(
\$this->__test,
\$test,
...func_get_args(),
);
}
$datasetsCode
EOF;
PHP;
}
/**

View File

@ -44,10 +44,10 @@ final class PestDatasetCommand extends Command
/** @var string $name */
$name = $this->argument('name');
$relativePath = sprintf(testDirectory('DatasetsRepository/%s.php'), ucfirst($name));
$relativePath = sprintf(testDirectory('DatasetsRepository/%s.php'), ucfirst($name));
/* @phpstan-ignore-next-line */
$target = base_path($relativePath);
$target = base_path($relativePath);
if (File::exists($target)) {
throw new InvalidConsoleArgument(sprintf('%s already exist', $target));

View File

@ -41,8 +41,8 @@ final class PestInstallCommand extends Command
TestSuite::getInstance(base_path(), $this->option('test-directory'));
/* @phpstan-ignore-next-line */
$pest = base_path(testDirectory('Pest.php'));
$stubs = 'stubs/Laravel';
$pest = base_path(testDirectory('Pest.php'));
$stubs = 'stubs/Laravel';
if (File::exists($pest)) {
throw new InvalidConsoleArgument(sprintf('%s already exist', $pest));

View File

@ -19,6 +19,20 @@ trait HandleArguments
return in_array($argument, $arguments, true);
}
/**
* Adds the given argument and value to the list of arguments.
*
* @param array<int, string> $arguments
*
* @return array<int, string>
*/
public function pushArgument(string $argument, array $arguments): array
{
$arguments[] = $argument;
return $arguments;
}
/**
* Pops the given argument from the arguments.
*

View File

@ -14,7 +14,7 @@ final class Environment implements HandlesArguments
/**
* The continuous integration environment.
*/
public const CI = 'ci';
public const CI = 'ci';
/**
* The local environment.

27
src/Plugins/Printer.php Normal file
View File

@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Pest\Plugins;
use Pest\Contracts\Plugins\HandlesArguments;
/**
* @internal
*/
final class Printer implements HandlesArguments
{
use Concerns\HandleArguments;
/**
* {@inheritDoc}
*/
public function handleArguments(array $arguments): array
{
if (! array_key_exists('COLLISION_PRINTER', $_SERVER)) {
return $arguments;
}
return $this->pushArgument('--no-output', $arguments);
}
}

View File

@ -57,6 +57,11 @@ final class DatasetsRepository
self::$withs[$filename . '>>>' . $description] = $with;
}
public static function has(string $filename, string $description): bool
{
return array_key_exists($filename . '>>>' . $description, self::$withs);
}
/**
* @return Closure|iterable<int|string, mixed>|never
*
@ -107,7 +112,7 @@ final class DatasetsRepository
$values = array_merge($values, $datasetCombinationElement['values']);
}
$datasetDescriptions[] = $description . ' with ' . implode(' / ', $partialDescriptions);
$datasetDescriptions[] = implode(' / ', $partialDescriptions);
$datasetValues[] = $values;
}

View File

@ -56,7 +56,7 @@ final class ExceptionTrace
$cleanedTrace = [];
foreach ($trace as $item) {
if (key_exists('file', $item) && mb_strpos($item['file'], 'vendor/pestphp/pest/') > 0) {
if (array_key_exists('file', $item) && mb_strpos($item['file'], 'vendor/pestphp/pest/') > 0) {
continue;
}

View File

@ -13,6 +13,7 @@ abstract class Printer implements \PHPUnit\Util\Printer
private $stream;
private bool $isPhpStream;
private bool $isOpen;
private function __construct(string $out)

View File

@ -12,11 +12,4 @@ use PHPUnit\Framework\TestCase;
*/
class IgnorableTestCase extends TestCase
{
/**
* @test
*/
public function fake(): void
{
self::markTestIncomplete();
}
}

View File

@ -70,12 +70,12 @@ final class TestSuite
string $rootPath,
public string $testPath)
{
$this->beforeAll = new BeforeAllRepository();
$this->beforeEach = new BeforeEachRepository();
$this->tests = new TestRepository();
$this->afterEach = new AfterEachRepository();
$this->afterAll = new AfterAllRepository();
$this->retryTempRepository = new TempRepository('retry');
$this->beforeAll = new BeforeAllRepository();
$this->beforeEach = new BeforeEachRepository();
$this->tests = new TestRepository();
$this->afterEach = new AfterEachRepository();
$this->afterAll = new AfterAllRepository();
$this->retryTempRepository = new TempRepository('retry');
$this->rootPath = (string) realpath($rootPath);
}