mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 159514bace | |||
| 415419a251 | |||
| 0a2d08ebe9 | |||
| a7566152fd | |||
| 7abf68e706 | |||
| f730a186ce | |||
| d534cb2c40 | |||
| 1b3ca5ba41 |
@@ -25,7 +25,7 @@ jobs:
|
||||
matrix:
|
||||
os: [ubuntu-latest, macos-latest] # windows-latest
|
||||
symfony: ['8.0']
|
||||
php: ['8.4', '8.5']
|
||||
php: ['8.4', '8.5', '8.6']
|
||||
dependency_version: [prefer-stable]
|
||||
|
||||
name: PHP ${{ matrix.php }} - Symfony ^${{ matrix.symfony }} - ${{ matrix.os }} - ${{ matrix.dependency_version }}
|
||||
|
||||
+4
-4
@@ -18,20 +18,20 @@
|
||||
],
|
||||
"require": {
|
||||
"php": "^8.4",
|
||||
"brianium/paratest": "^7.26.1",
|
||||
"brianium/paratest": "^7.27.4",
|
||||
"nunomaduro/collision": "^8.9.5",
|
||||
"nunomaduro/termwind": "^2.4.0",
|
||||
"pestphp/pest-plugin": "^5.0.0",
|
||||
"pestphp/pest-plugin-arch": "^5.0.0",
|
||||
"pestphp/pest-plugin-mutate": "^5.0.2",
|
||||
"pestphp/pest-plugin-profanity": "^5.0.1",
|
||||
"phpunit/phpunit": "^13.4.3",
|
||||
"symfony/process": "^8.1.7"
|
||||
"phpunit/phpunit": "^13.4.5",
|
||||
"symfony/process": "^8.1.9"
|
||||
},
|
||||
"conflict": {
|
||||
"filp/whoops": "<2.18.3",
|
||||
"laravel/boost": "<2.6.0",
|
||||
"phpunit/phpunit": ">13.4.3",
|
||||
"phpunit/phpunit": ">13.4.5",
|
||||
"sebastian/exporter": "<7.0.0",
|
||||
"webmozart/assert": "<1.11.0"
|
||||
},
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace Pest\Bootstrappers;
|
||||
|
||||
use Pest\Contracts\Bootstrapper;
|
||||
use Pest\Exceptions\FatalException;
|
||||
use Pest\Plugins\Concerns\HandleArguments;
|
||||
use Pest\Support\DatasetInfo;
|
||||
use Pest\Support\Str;
|
||||
use Pest\TestSuite;
|
||||
@@ -20,6 +21,8 @@ use function Pest\testDirectory;
|
||||
*/
|
||||
final class BootFiles implements Bootstrapper
|
||||
{
|
||||
use HandleArguments;
|
||||
|
||||
/**
|
||||
* @var array<int, string>
|
||||
*/
|
||||
@@ -33,6 +36,10 @@ final class BootFiles implements Bootstrapper
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
if ($this->runningInit()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$rootPath = TestSuite::getInstance()->rootPath;
|
||||
$testsPath = $rootPath.DIRECTORY_SEPARATOR.testDirectory();
|
||||
|
||||
@@ -62,6 +69,18 @@ final class BootFiles implements Bootstrapper
|
||||
$this->bootDatasets($testsPath);
|
||||
}
|
||||
|
||||
private function runningInit(): bool
|
||||
{
|
||||
$arguments = $_SERVER['argv'] ?? null;
|
||||
|
||||
if (! is_array($arguments)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var array<int, string> $arguments */
|
||||
return $this->hasArgument('--init', $arguments);
|
||||
}
|
||||
|
||||
private function load(string $filename): void
|
||||
{
|
||||
if (! Str::endsWith($filename, '.php')) {
|
||||
|
||||
@@ -24,6 +24,7 @@ use Pest\TestSuite;
|
||||
use PHPUnit\Framework\AssertionFailedError;
|
||||
use PHPUnit\Framework\Attributes\CoversClass;
|
||||
use PHPUnit\Framework\Attributes\CoversFunction;
|
||||
use PHPUnit\Framework\Attributes\CoversNothing;
|
||||
use PHPUnit\Framework\Attributes\CoversTrait;
|
||||
use PHPUnit\Framework\Attributes\Group;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
@@ -571,6 +572,16 @@ final class TestCall // @phpstan-ignore-line
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function coversNothing(): self
|
||||
{
|
||||
$this->testCaseMethod->attributes[] = new Attribute(
|
||||
CoversNothing::class,
|
||||
[],
|
||||
);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<class-string|string>|class-string ...$classes
|
||||
*/
|
||||
|
||||
+1
-1
@@ -6,7 +6,7 @@ namespace Pest;
|
||||
|
||||
function version(): string
|
||||
{
|
||||
return '5.3.3';
|
||||
return '5.3.4';
|
||||
}
|
||||
|
||||
function testDirectory(string $file = ''): string
|
||||
|
||||
@@ -17,6 +17,8 @@ use Symfony\Component\Console\Output\OutputInterface;
|
||||
*/
|
||||
final readonly class Init implements HandlesArguments
|
||||
{
|
||||
use Concerns\HandleArguments;
|
||||
|
||||
private const string INIT_OPTION = '--init';
|
||||
|
||||
private const array STUBS = [
|
||||
@@ -40,14 +42,9 @@ final readonly class Init implements HandlesArguments
|
||||
*/
|
||||
public function handleArguments(array $arguments): array
|
||||
{
|
||||
if (! array_key_exists(1, $arguments)) {
|
||||
if (! $this->hasArgument(self::INIT_OPTION, $arguments)) {
|
||||
return $arguments;
|
||||
}
|
||||
if ($arguments[1] !== self::INIT_OPTION) {
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
unset($arguments[1]);
|
||||
|
||||
$this->init();
|
||||
|
||||
|
||||
+2
-2
@@ -1571,7 +1571,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
|
||||
return [
|
||||
array_map(array_keys(...), $merged['files']),
|
||||
array_map(array_keys(...), $merged['tables']),
|
||||
array_map(static fn (array $tables): array => array_map(strval(...), array_keys($tables)), $merged['tables']),
|
||||
array_map(array_keys(...), $merged['inertia']),
|
||||
];
|
||||
}
|
||||
@@ -2291,7 +2291,7 @@ final class Tia implements AddsOutput, HandlesArguments, HandlesOriginalArgument
|
||||
foreach (array_keys($perTestUsesDatabase) as $testFile) {
|
||||
$existing = $perTestTables[$testFile] ?? [];
|
||||
$merged = array_fill_keys($existing, true) + $allTables;
|
||||
$names = array_keys($merged);
|
||||
$names = array_map(strval(...), array_keys($merged));
|
||||
sort($names);
|
||||
$perTestTables[$testFile] = $names;
|
||||
}
|
||||
|
||||
@@ -1093,7 +1093,7 @@ final class Graph
|
||||
}
|
||||
}
|
||||
|
||||
$names = array_keys($normalised);
|
||||
$names = array_map(strval(...), array_keys($normalised));
|
||||
sort($names);
|
||||
|
||||
$this->testTables[$testRel] = $names;
|
||||
|
||||
@@ -272,7 +272,7 @@ final class Recorder
|
||||
$out = [];
|
||||
|
||||
foreach ($this->perTestTables as $testFile => $tables) {
|
||||
$names = array_keys($tables);
|
||||
$names = array_map(strval(...), array_keys($tables));
|
||||
sort($names);
|
||||
$out[$testFile] = $names;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ final class Reflection
|
||||
try {
|
||||
$reflectionMethod = $reflectionClass->getMethod($method);
|
||||
|
||||
return $reflectionMethod->invoke($object, ...$args);
|
||||
return $reflectionMethod->invoke($reflectionMethod->isStatic() ? null : $object, ...$args);
|
||||
} catch (ReflectionException $exception) {
|
||||
if (method_exists($object, '__call')) {
|
||||
return $object->__call($method, $args);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
Pest Testing Framework 5.3.3.
|
||||
Pest Testing Framework 5.3.4.
|
||||
|
||||
USAGE: pest <file> [options]
|
||||
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
|
||||
Pest Testing Framework 5.3.3.
|
||||
Pest Testing Framework 5.3.4.
|
||||
|
||||
|
||||
@@ -82,6 +82,12 @@
|
||||
PASS Tests\Features\Covers\ClassCoverage
|
||||
✓ it uses the correct PHPUnit attribute for class
|
||||
|
||||
PASS Tests\Features\Covers\CoversNothing
|
||||
✓ it uses the correct PHPUnit attribute for covers nothing
|
||||
✓ describe → it uses the correct PHPUnit attribute for covers nothing
|
||||
✓ it is not risky when coverage metadata is required
|
||||
✓ it does not conflict with covers in the same file
|
||||
|
||||
PASS Tests\Features\Covers\ExceptionHandling
|
||||
✓ it throws exception if no class nor method has been found
|
||||
|
||||
@@ -1274,6 +1280,12 @@
|
||||
✓ it is not incompleted because of test with assertions
|
||||
… a "describe" group of tests → it is incompleted
|
||||
|
||||
PASS Tests\Features\Init
|
||||
✓ init creates a suite when the tests directory is missing
|
||||
✓ init creates a suite when --init is not the first argument
|
||||
✓ init creates a suite when --init is passed with a value
|
||||
✓ a missing tests directory does not crash while resolving tia state
|
||||
|
||||
PASS Tests\Features\Issue
|
||||
✓ it may be associated with an issue #1, #2
|
||||
✓ nested → it may be associated with an issue #1, #4, #5, #6, #3
|
||||
@@ -1854,6 +1866,7 @@
|
||||
✓ parseListTestsOutput → it deduplicates repeated class names from multiple test methods
|
||||
✓ parseListTestsOutput → it returns an empty list for output with no matching lines
|
||||
✓ parseListTestsOutput → it parses non-Tests namespaced classes
|
||||
✓ parseListTestsOutput → it parses module test classes living outside the tests directory
|
||||
✓ parseListTestsOutput → it parses unnamespaced top-level classes
|
||||
✓ parseListTestsOutput → it strips the P\ Pest prefix but keeps the rest of the FQCN
|
||||
✓ parseListTestsOutput → it ignores junk lines that lack the " - …::" framing
|
||||
@@ -1954,6 +1967,12 @@
|
||||
✓ it detects an added or removed non-platform dependency
|
||||
✓ it is independent of package ordering
|
||||
|
||||
PASS Tests\Unit\Plugins\Tia\NumericTableDependencies
|
||||
✓ it exports numeric table dependencies from the recorder as strings
|
||||
✓ it persists numeric tables as JSON strings and selects only their migration readers after reload
|
||||
✓ it preserves numeric worker tables through payload cleaning union and graph reload
|
||||
✓ it preserves observed and migration added numeric tables when augmenting database tests
|
||||
|
||||
PASS Tests\Unit\Plugins\Tia\Recorder
|
||||
✓ activateLinkTracking() → it tracks tables without a coverage driver
|
||||
✓ activateLinkTracking() → it tracks inertia components without a coverage driver
|
||||
@@ -2140,6 +2159,8 @@
|
||||
✓ it gets property values
|
||||
✓ it gets properties from classes
|
||||
✓ it gets methods from classes
|
||||
✓ it calls instance methods
|
||||
✓ it calls static methods without passing an object
|
||||
|
||||
PASS Tests\Unit\Support\Str
|
||||
✓ it evaluates the code with ('version()', '__pest_evaluable_version__')
|
||||
@@ -2265,4 +2286,4 @@
|
||||
✓ pass with dataset with ('my-datas-set-value')
|
||||
✓ within describe → pass with dataset with ('my-datas-set-value')
|
||||
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1606 passed (3505 assertions)
|
||||
Tests: 2 deprecated, 4 warnings, 5 incomplete, 2 notices, 40 todos, 35 skipped, 1621 passed (3582 assertions)
|
||||
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
use PHPUnit\Framework\Attributes\CoversNothing;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
$run = function (string ...$arguments): array {
|
||||
$process = new Process(
|
||||
['php', 'bin/pest', '--configuration', 'tests/Fixtures/Suites/CoversNothing.xml', ...$arguments],
|
||||
dirname(__DIR__, 3),
|
||||
['COLLISION_PRINTER' => 'DefaultPrinter', 'COLLISION_IGNORE_DURATION' => 'true', 'PAO_DISABLE' => '1'],
|
||||
);
|
||||
|
||||
$process->run();
|
||||
|
||||
return [
|
||||
'output' => removeAnsiEscapeSequences($process->getOutput().$process->getErrorOutput()),
|
||||
'code' => $process->getExitCode(),
|
||||
];
|
||||
};
|
||||
|
||||
it('uses the correct PHPUnit attribute for covers nothing', function (): void {
|
||||
$attributes = new ReflectionMethod($this, $this->name())->getAttributes();
|
||||
|
||||
expect($attributes[2]->getName())->toBe(CoversNothing::class)
|
||||
->and($attributes[2]->getArguments())->toBeEmpty();
|
||||
})->coversNothing();
|
||||
|
||||
describe('describe', function (): void {
|
||||
it('uses the correct PHPUnit attribute for covers nothing', function (): void {
|
||||
$attributes = new ReflectionMethod($this, $this->name())->getAttributes();
|
||||
|
||||
expect($attributes[2]->getName())->toBe(CoversNothing::class)
|
||||
->and($attributes[2]->getArguments())->toBeEmpty();
|
||||
});
|
||||
})->coversNothing();
|
||||
|
||||
it('is not risky when coverage metadata is required', function () use ($run): void {
|
||||
$result = $run();
|
||||
|
||||
expect($result['output'])
|
||||
->toContain('✓ covers nothing')
|
||||
->toContain('! declares no coverage metadata → This test does not define a code coverage target but is expected to do so')
|
||||
->toContain('Tests: 1 risky, 1 passed (2 assertions)')
|
||||
->and($result['code'])->toBe(1);
|
||||
})->skipOnWindows();
|
||||
|
||||
it('does not conflict with covers in the same file', function () use ($run): void {
|
||||
$result = $run('tests/Fixtures/Suites/CoversNothingWithCovers.php');
|
||||
|
||||
expect($result['output'])
|
||||
->toContain('✓ covers nothing')
|
||||
->toContain('Tests: 2 passed (2 assertions)')
|
||||
->and($result['code'])->toBe(0);
|
||||
})->skipOnWindows();
|
||||
@@ -0,0 +1,171 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
test('init creates a suite when the tests directory is missing', function (): void {
|
||||
$root = makeInitProject();
|
||||
|
||||
try {
|
||||
$process = runInitProject($root, '--init');
|
||||
|
||||
expect($process->getExitCode())->toBe(0)
|
||||
->and($process->getErrorOutput())->not->toContain('cannot be resolved')
|
||||
->and($process->getOutput())->toContain('File created.')
|
||||
->and($root.DIRECTORY_SEPARATOR.'phpunit.xml')->toBeFile()
|
||||
->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')->toBeFile()
|
||||
->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'TestCase.php')->toBeFile()
|
||||
->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Unit'.DIRECTORY_SEPARATOR.'ExampleTest.php')->toBeFile();
|
||||
} finally {
|
||||
removeInitProject($root);
|
||||
}
|
||||
});
|
||||
|
||||
test('init creates a suite when --init is not the first argument', function (): void {
|
||||
$root = makeInitProject();
|
||||
|
||||
try {
|
||||
$process = runInitProject($root, '--colors=never', '--init');
|
||||
|
||||
expect($process->getExitCode())->toBe(0)
|
||||
->and($process->getErrorOutput())->not->toContain('cannot be resolved')
|
||||
->and($process->getOutput())->toContain('File created.')
|
||||
->and($root.DIRECTORY_SEPARATOR.'phpunit.xml')->toBeFile()
|
||||
->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')->toBeFile();
|
||||
} finally {
|
||||
removeInitProject($root);
|
||||
}
|
||||
});
|
||||
|
||||
test('init creates a suite when --init is passed with a value', function (): void {
|
||||
$root = makeInitProject();
|
||||
|
||||
try {
|
||||
$process = runInitProject($root, '--init=pest');
|
||||
|
||||
expect($process->getExitCode())->toBe(0)
|
||||
->and($process->getErrorOutput())->not->toContain('cannot be resolved')
|
||||
->and($process->getOutput())->toContain('File created.')
|
||||
->and($root.DIRECTORY_SEPARATOR.'phpunit.xml')->toBeFile()
|
||||
->and($root.DIRECTORY_SEPARATOR.'tests'.DIRECTORY_SEPARATOR.'Pest.php')->toBeFile();
|
||||
} finally {
|
||||
removeInitProject($root);
|
||||
}
|
||||
});
|
||||
|
||||
test('a missing tests directory does not crash while resolving tia state', function (): void {
|
||||
$root = makeInitProject();
|
||||
|
||||
try {
|
||||
$process = runInitProject($root, '--version');
|
||||
|
||||
expect($process->getExitCode())->not->toBe(0)
|
||||
->and($process->getOutput())->toContain('does not exist')
|
||||
->and($process->getErrorOutput())->not->toContain('cannot be resolved')
|
||||
->and($process->getOutput())->not->toContain('cannot be resolved');
|
||||
} finally {
|
||||
removeInitProject($root);
|
||||
}
|
||||
});
|
||||
|
||||
function makeInitProject(): string
|
||||
{
|
||||
$root = sys_get_temp_dir().DIRECTORY_SEPARATOR.'pest-init-'.bin2hex(random_bytes(4));
|
||||
|
||||
mkdir($root.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'bin', 0777, true);
|
||||
|
||||
copy(
|
||||
dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'pest-plugins.json',
|
||||
$root.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'pest-plugins.json',
|
||||
);
|
||||
|
||||
$autoload = var_export(
|
||||
dirname(__DIR__, 2).DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'autoload.php',
|
||||
true,
|
||||
);
|
||||
|
||||
file_put_contents($root.DIRECTORY_SEPARATOR.'run.php', <<<PHP
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Kernel;
|
||||
use Pest\Panic;
|
||||
use Pest\TestSuite;
|
||||
use Symfony\Component\Console\Input\ArgvInput;
|
||||
use Symfony\Component\Console\Output\ConsoleOutput;
|
||||
|
||||
require {$autoload};
|
||||
|
||||
\$arguments = array_slice(\$argv, 1);
|
||||
|
||||
if (\$arguments === []) {
|
||||
\$arguments = ['--version'];
|
||||
}
|
||||
|
||||
\$GLOBALS['_composer_bin_dir'] = getcwd().'/vendor/bin';
|
||||
|
||||
\$_SERVER['argv'] = ['pest', ...\$arguments];
|
||||
\$_SERVER['COLLISION_PRINTER'] = 'DefaultPrinter';
|
||||
|
||||
\$input = new ArgvInput(\$_SERVER['argv']);
|
||||
\$output = new ConsoleOutput(ConsoleOutput::VERBOSITY_NORMAL, false);
|
||||
|
||||
\$testSuite = TestSuite::getInstance(getcwd(), 'tests');
|
||||
|
||||
try {
|
||||
\$kernel = Kernel::boot(\$testSuite, \$input, \$output);
|
||||
\$result = \$kernel->handle(\$_SERVER['argv'], \$_SERVER['argv']);
|
||||
\$kernel->terminate();
|
||||
} catch (Throwable \$throwable) {
|
||||
Panic::with(\$throwable);
|
||||
}
|
||||
|
||||
exit(\$result);
|
||||
PHP);
|
||||
|
||||
return $root;
|
||||
}
|
||||
|
||||
function runInitProject(string $root, string ...$command): Process
|
||||
{
|
||||
$process = new Process(
|
||||
[PHP_BINARY, 'run.php', ...$command],
|
||||
$root,
|
||||
[
|
||||
'PEST_NO_SUPPORT' => 'true',
|
||||
'COLLISION_PRINTER' => 'DefaultPrinter',
|
||||
'COLLISION_IGNORE_DURATION' => 'true',
|
||||
],
|
||||
);
|
||||
|
||||
$process->setTimeout(60);
|
||||
$process->run();
|
||||
|
||||
return $process;
|
||||
}
|
||||
|
||||
function removeInitProject(string $root): void
|
||||
{
|
||||
if (! is_dir($root)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$items = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS),
|
||||
RecursiveIteratorIterator::CHILD_FIRST,
|
||||
);
|
||||
|
||||
foreach ($items as $item) {
|
||||
if ($item->isDir() && ! $item->isLink()) {
|
||||
rmdir($item->getPathname());
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
unlink($item->getPathname());
|
||||
}
|
||||
|
||||
rmdir($root);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<?php
|
||||
|
||||
test('covers nothing', function (): void {
|
||||
expect(true)->toBeTrue();
|
||||
})->coversNothing();
|
||||
|
||||
test('declares no coverage metadata', function (): void {
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
|
||||
bootstrap="../../../vendor/autoload.php"
|
||||
colors="true"
|
||||
requireCoverageMetadata="true"
|
||||
failOnRisky="true"
|
||||
failOnWarning="true"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="default">
|
||||
<file>CoversNothing.php</file>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
use Tests\Fixtures\Covers\CoversClass1;
|
||||
|
||||
covers(CoversClass1::class);
|
||||
|
||||
test('covers the class', function (): void {
|
||||
expect(true)->toBeTrue();
|
||||
});
|
||||
|
||||
test('covers nothing', function (): void {
|
||||
expect(true)->toBeTrue();
|
||||
})->coversNothing();
|
||||
@@ -376,6 +376,28 @@ OUT;
|
||||
]);
|
||||
});
|
||||
|
||||
it('parses module test classes living outside the tests directory', function (): void {
|
||||
$output = new BufferedOutput;
|
||||
$shard = new Shard($output);
|
||||
|
||||
$reflection = new ReflectionClass($shard);
|
||||
$method = $reflection->getMethod('parseListTestsOutput');
|
||||
|
||||
$listOutput = <<<'OUT'
|
||||
INFO Available tests:
|
||||
|
||||
- P\Tests\Unit\Foo::__pest_evaluable_it_works
|
||||
- P\Modules\Example\tests\ShardModuleExample::__pest_evaluable_module_test_is_listed
|
||||
- P\Modules\Billing\Tests\Feature\InvoiceTest::__pest_evaluable_it_creates_invoices
|
||||
OUT;
|
||||
|
||||
expect($method->invoke($shard, $listOutput))->toBe([
|
||||
'Tests\\Unit\\Foo',
|
||||
'Modules\\Example\\tests\\ShardModuleExample',
|
||||
'Modules\\Billing\\Tests\\Feature\\InvoiceTest',
|
||||
]);
|
||||
});
|
||||
|
||||
it('parses unnamespaced top-level classes', function (): void {
|
||||
$output = new BufferedOutput;
|
||||
$shard = new Shard($output);
|
||||
|
||||
@@ -0,0 +1,214 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Pest\Plugins\Tia;
|
||||
use Pest\Plugins\Tia\FileState;
|
||||
use Pest\Plugins\Tia\Graph;
|
||||
use Pest\Plugins\Tia\Recorder;
|
||||
use Pest\Plugins\Tia\TableExtractor;
|
||||
use Pest\Plugins\Tia\WatchPatterns;
|
||||
use Pest\Support\Container;
|
||||
|
||||
it('exports numeric table dependencies from the recorder as strings', function (): void {
|
||||
$recorder = new Recorder;
|
||||
$recorder->activateLinkTracking();
|
||||
$recorder->beginTest('Some\Missing\TestClass', 'queries numeric tables', '/project/tests/Unit/NumericTest.php');
|
||||
|
||||
foreach (['select substring(name from 1 for 3) from users', 'select * from "123"', 'select * from "0"', 'select * from "001"', 'select * from USERS join "123" on true'] as $query) {
|
||||
foreach (TableExtractor::fromSql($query) as $table) {
|
||||
$recorder->linkTable($table);
|
||||
}
|
||||
}
|
||||
|
||||
$recorder->endTest();
|
||||
$tables = $recorder->perTestTables();
|
||||
sort($tables['/project/tests/Unit/NumericTest.php'], SORT_STRING);
|
||||
|
||||
expect($tables)->toBe([
|
||||
'/project/tests/Unit/NumericTest.php' => ['0', '001', '1', '123', 'users'],
|
||||
]);
|
||||
});
|
||||
|
||||
it('persists numeric tables as JSON strings and selects only their migration readers after reload', function (): void {
|
||||
$root = tiaNumericTableProject();
|
||||
|
||||
try {
|
||||
tiaAssertNumericTableGraph($root, [], [
|
||||
$root.'/tests/Unit/NumericTest.php' => ['123', '0', '001', '123'],
|
||||
$root.'/tests/Unit/ControlTest.php' => ['USERS', 'users'],
|
||||
], [
|
||||
'tests/Unit/NumericTest.php' => ['0', '001', '123'],
|
||||
'tests/Unit/ControlTest.php' => ['users'],
|
||||
], [
|
||||
['123', ['tests/Unit/NumericTest.php']],
|
||||
['0', ['tests/Unit/NumericTest.php']],
|
||||
['001', ['tests/Unit/NumericTest.php']],
|
||||
['users', ['tests/Unit/ControlTest.php']],
|
||||
['orders', []],
|
||||
]);
|
||||
} finally {
|
||||
tiaRemoveNumericTableProject($root);
|
||||
}
|
||||
});
|
||||
|
||||
it('preserves numeric worker tables through payload cleaning union and graph reload', function (): void {
|
||||
$root = tiaNumericTableProject();
|
||||
|
||||
try {
|
||||
$state = new FileState($root.'/state');
|
||||
$tia = new ReflectionClass(Tia::class)->newInstanceWithoutConstructor();
|
||||
new ReflectionProperty(Tia::class, 'state')->setValue($tia, $state);
|
||||
$files = [
|
||||
$root.'/tests/Unit/NumericTest.php' => [$root.'/app/Subject.php'],
|
||||
$root.'/tests/Unit/ControlTest.php' => [$root.'/app/Subject.php'],
|
||||
];
|
||||
$workers = [
|
||||
[
|
||||
$root.'/tests/Unit/NumericTest.php' => ['123', 'users'],
|
||||
$root.'/tests/Unit/ControlTest.php' => ['orders'],
|
||||
],
|
||||
[$root.'/tests/Unit/NumericTest.php' => ['0', '001', '123', 'users']],
|
||||
];
|
||||
|
||||
foreach ($workers as $slot => $tables) {
|
||||
$before = new ReflectionMethod(Tia::class, 'collectWorkerEdgesPartials')->invoke($tia);
|
||||
new ReflectionMethod(Tia::class, 'flushWorkerPartial')->invoke($tia, $files, $tables, []);
|
||||
$keys = new ReflectionMethod(Tia::class, 'collectWorkerEdgesPartials')->invoke($tia);
|
||||
$written = array_values(array_diff($keys, $before));
|
||||
expect($written)->toHaveCount(1);
|
||||
$key = $written[0];
|
||||
$raw = $state->read($key);
|
||||
expect($raw)->toBeString();
|
||||
$payload = json_decode($raw, true, 512, JSON_THROW_ON_ERROR);
|
||||
|
||||
expect($payload)->toBe(['files' => $files, 'tables' => $tables, 'inertia' => []])
|
||||
->and(new ReflectionMethod(Tia::class, 'readPartial')->invoke($tia, $key))->toBe($payload)
|
||||
->and(rename($state->pathFor($key), $state->pathFor('worker-edges-fixture-'.$slot.'.json')))->toBeTrue();
|
||||
}
|
||||
|
||||
$keys = new ReflectionMethod(Tia::class, 'collectWorkerEdgesPartials')->invoke($tia);
|
||||
expect($keys)->toHaveCount(2);
|
||||
[$mergedFiles, $mergedTables, $inertia] = new ReflectionMethod(Tia::class, 'consumePartials')->invoke($tia, $keys);
|
||||
|
||||
expect($mergedFiles)->toBe($files)
|
||||
->and($mergedTables)->toBe([
|
||||
$root.'/tests/Unit/NumericTest.php' => ['123', 'users', '0', '001'],
|
||||
$root.'/tests/Unit/ControlTest.php' => ['orders'],
|
||||
])
|
||||
->and($inertia)->toBe([])
|
||||
->and(new ReflectionMethod(Tia::class, 'collectWorkerEdgesPartials')->invoke($tia))->toBe([]);
|
||||
|
||||
tiaAssertNumericTableGraph($root, $mergedFiles, $mergedTables, [
|
||||
'tests/Unit/NumericTest.php' => ['0', '001', '123', 'users'],
|
||||
'tests/Unit/ControlTest.php' => ['orders'],
|
||||
], [
|
||||
['123', ['tests/Unit/NumericTest.php']],
|
||||
['0', ['tests/Unit/NumericTest.php']],
|
||||
['001', ['tests/Unit/NumericTest.php']],
|
||||
['users', ['tests/Unit/NumericTest.php']],
|
||||
['orders', ['tests/Unit/ControlTest.php']],
|
||||
]);
|
||||
} finally {
|
||||
tiaRemoveNumericTableProject($root);
|
||||
}
|
||||
});
|
||||
|
||||
it('preserves observed and migration added numeric tables when augmenting database tests', function (): void {
|
||||
$root = tiaNumericTableProject();
|
||||
|
||||
try {
|
||||
file_put_contents($root.'/database/migrations/create_numeric.php', "<?php Schema::create('456', function (): void {});");
|
||||
$tia = new ReflectionClass(Tia::class)->newInstanceWithoutConstructor();
|
||||
$tables = new ReflectionMethod(Tia::class, 'augmentDatabaseTestTables')->invoke($tia, [
|
||||
$root.'/tests/Unit/NumericTest.php' => ['123', '0', '001', 'users'],
|
||||
$root.'/tests/Unit/ControlTest.php' => ['orders'],
|
||||
], [$root.'/tests/Unit/NumericTest.php' => true], $root);
|
||||
|
||||
expect($tables)->toBe([
|
||||
$root.'/tests/Unit/NumericTest.php' => ['0', '001', '123', '456', 'users'],
|
||||
$root.'/tests/Unit/ControlTest.php' => ['orders'],
|
||||
]);
|
||||
|
||||
tiaAssertNumericTableGraph($root, [], $tables, [
|
||||
'tests/Unit/NumericTest.php' => ['0', '001', '123', '456', 'users'],
|
||||
'tests/Unit/ControlTest.php' => ['orders'],
|
||||
], [
|
||||
['123', ['tests/Unit/NumericTest.php']],
|
||||
['0', ['tests/Unit/NumericTest.php']],
|
||||
['001', ['tests/Unit/NumericTest.php']],
|
||||
['456', ['tests/Unit/NumericTest.php']],
|
||||
['users', ['tests/Unit/NumericTest.php']],
|
||||
['orders', ['tests/Unit/ControlTest.php']],
|
||||
]);
|
||||
} finally {
|
||||
tiaRemoveNumericTableProject($root);
|
||||
}
|
||||
});
|
||||
|
||||
function tiaNumericTableProject(): string
|
||||
{
|
||||
$root = dirname(__DIR__, 4).'/.temp/tia-numeric-'.bin2hex(random_bytes(8));
|
||||
mkdir($root.'/tests/Unit', 0755, true);
|
||||
mkdir($root.'/app', 0755, true);
|
||||
mkdir($root.'/database/migrations', 0755, true);
|
||||
file_put_contents($root.'/tests/Unit/NumericTest.php', '<?php');
|
||||
file_put_contents($root.'/tests/Unit/ControlTest.php', '<?php');
|
||||
file_put_contents($root.'/app/Subject.php', '<?php');
|
||||
|
||||
return $root;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, list<string>> $files
|
||||
* @param array<string, list<string>> $tables
|
||||
* @param array<string, list<string>> $expected
|
||||
* @param list<array{string, list<string>}> $selections
|
||||
*/
|
||||
function tiaAssertNumericTableGraph(string $root, array $files, array $tables, array $expected, array $selections): void
|
||||
{
|
||||
$graph = new Graph($root);
|
||||
$graph->replaceEdges($files);
|
||||
$graph->markKnownTestFiles(array_keys($tables));
|
||||
$graph->replaceTestTables($tables);
|
||||
$json = $graph->encode();
|
||||
expect($json)->toBeString();
|
||||
$payload = json_decode($json, true, 512, JSON_THROW_ON_ERROR);
|
||||
expect($payload['test_tables'])->toBe($expected);
|
||||
$reloaded = Graph::decode($json, $root);
|
||||
expect($reloaded)->toBeInstanceOf(Graph::class);
|
||||
|
||||
$container = Container::getInstance();
|
||||
$previous = $container->get(WatchPatterns::class);
|
||||
$patterns = new WatchPatterns;
|
||||
$container->add(WatchPatterns::class, $patterns);
|
||||
|
||||
try {
|
||||
foreach ($selections as [$table, $readers]) {
|
||||
$migration = 'database/migrations/change_'.$table.'.php';
|
||||
file_put_contents($root.'/'.$migration, "<?php Schema::table('{$table}', function (): void {});");
|
||||
|
||||
expect($reloaded->affected([$migration]))->toBe($readers);
|
||||
}
|
||||
} finally {
|
||||
$container->add(WatchPatterns::class, $previous);
|
||||
}
|
||||
}
|
||||
|
||||
function tiaRemoveNumericTableProject(string $root): void
|
||||
{
|
||||
$entries = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($root, FilesystemIterator::SKIP_DOTS),
|
||||
RecursiveIteratorIterator::CHILD_FIRST,
|
||||
);
|
||||
|
||||
foreach ($entries as $entry) {
|
||||
if ($entry->isDir()) {
|
||||
rmdir($entry->getPathname());
|
||||
} else {
|
||||
unlink($entry->getPathname());
|
||||
}
|
||||
}
|
||||
|
||||
rmdir($root);
|
||||
}
|
||||
@@ -74,3 +74,29 @@ it('gets methods from classes', function (): void {
|
||||
'getBar',
|
||||
]);
|
||||
});
|
||||
|
||||
it('calls instance methods', function (): void {
|
||||
$class = new class
|
||||
{
|
||||
private string $foo = 'bar';
|
||||
|
||||
private function getFoo(): string
|
||||
{
|
||||
return $this->foo;
|
||||
}
|
||||
};
|
||||
|
||||
expect(Reflection::call($class, 'getFoo'))->toBe('bar');
|
||||
});
|
||||
|
||||
it('calls static methods without passing an object', function (): void {
|
||||
$class = new class
|
||||
{
|
||||
private static function concat(string $a, string $b): string
|
||||
{
|
||||
return $a.$b;
|
||||
}
|
||||
};
|
||||
|
||||
expect(Reflection::call($class, 'concat', ['foo', 'bar']))->toBe('foobar');
|
||||
});
|
||||
|
||||
@@ -20,7 +20,7 @@ test('parallel', function () use ($run): void {
|
||||
$output = $run('--exclude-group=integration');
|
||||
$output = implode("\n", array_slice(explode("\n", (string) $output), -10));
|
||||
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1587 passed (3447 assertions)';
|
||||
$expected = '2 deprecated, 4 warnings, 5 incomplete, 3 notices, 40 todos, 27 skipped, 1602 passed (3524 assertions)';
|
||||
|
||||
expect($output)
|
||||
->toContain("Tests: {$expected}")
|
||||
|
||||
Reference in New Issue
Block a user