PHP 8 and PHPUnit 9.3 support

This commit is contained in:
Graham Campbell
2020-08-07 11:23:45 +01:00
parent 051ca73cae
commit 3cfadee2bb
11 changed files with 95 additions and 48 deletions

View File

@ -20,7 +20,7 @@ jobs:
coverage: none coverage: none
- name: Install Dependencies - name: Install Dependencies
run: composer update --no-interaction --prefer-dist --no-progress run: composer update --no-interaction --no-progress
- name: Run Rector - name: Run Rector
run: vendor/bin/rector process src --dry-run run: vendor/bin/rector process src --dry-run
@ -48,7 +48,7 @@ jobs:
coverage: none coverage: none
- name: Install Dependencies - name: Install Dependencies
run: composer update --prefer-stable --no-interaction --prefer-dist --no-progress run: composer update --prefer-stable --no-interaction --no-progress
- name: Run PHPStan - name: Run PHPStan
run: vendor/bin/phpstan analyse --no-progress run: vendor/bin/phpstan analyse --no-progress

View File

@ -6,43 +6,39 @@ jobs:
ci: ci:
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
strategy: strategy:
fail-fast: true
matrix: matrix:
os: [ubuntu-latest, macos-latest, windows-latest] os: [ubuntu-latest, macos-latest, windows-latest]
php: [7.3, 7.4] php: ['7.3', '7.4', '8.0']
dependency-version: [prefer-lowest, prefer-stable] dependency-version: [prefer-lowest, prefer-stable]
name: Tests P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }} name: PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
steps: steps:
- name: Checkout - name: Checkout
uses: actions/checkout@v2 uses: actions/checkout@v2
- name: Cache dependencies
uses: actions/cache@v1
with:
path: ~/.composer/cache/files
key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
- name: Setup PHP - name: Setup PHP
uses: shivammathur/setup-php@v2 uses: shivammathur/setup-php@v2
with: with:
php-version: ${{ matrix.php }} php-version: ${{ matrix.php }}
extensions: dom, mbstring, zip tools: composer:v2
coverage: none coverage: none
- name: Install Composer dependencies - name: Setup Problem Matches
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist run: |
echo "::add-matcher::${{ runner.tool_cache }}/php.json"
echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"
- name: Install PHP 7 dependencies
run: composer update --${{ matrix.dependency-version }} --no-interaction --no-progress
if: "matrix.php < 8"
- name: Install PHP 8 dependencies
run: composer update --${{ matrix.dependency-version }} --ignore-platform-req=php --no-interaction --no-progress
if: "matrix.php >= 8"
- name: Unit Tests - name: Unit Tests
run: php bin/pest --colors=always --exclude-group=integration run: php bin/pest --colors=always --exclude-group=integration
- name: Integration Tests - name: Integration Tests
run: php bin/pest --colors=always --group=integration run: php bin/pest --colors=always --group=integration
- name: Setup problem matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"
- name: Setup problem matchers for Pest
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

View File

@ -17,13 +17,12 @@
} }
], ],
"require": { "require": {
"php": "^7.3", "php": "^7.3 || ^8.0",
"nunomaduro/collision": "^5.0.0-BETA4", "nunomaduro/collision": "^5.0.0-BETA4",
"pestphp/pest-plugin": "^0.3", "pestphp/pest-plugin": "^0.3",
"pestphp/pest-plugin-coverage": "^0.3", "pestphp/pest-plugin-coverage": "^0.3",
"pestphp/pest-plugin-init": "^0.3", "pestphp/pest-plugin-init": "^0.3",
"phpunit/phpunit": "^9.2.6", "phpunit/phpunit": "9.3.2"
"sebastian/environment": "^5.1.2"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {
@ -45,7 +44,7 @@
"require-dev": { "require-dev": {
"illuminate/console": "^7.16.1", "illuminate/console": "^7.16.1",
"illuminate/support": "^7.16.1", "illuminate/support": "^7.16.1",
"mockery/mockery": "^1.4.0", "mockery/mockery": "^1.4.1",
"pestphp/pest-dev-tools": "dev-master" "pestphp/pest-dev-tools": "dev-master"
}, },
"minimum-stability": "dev", "minimum-stability": "dev",

View File

@ -21,3 +21,4 @@ parameters:
- "# with null as default value#" - "# with null as default value#"
- "#has parameter \\$closure with default value.#" - "#has parameter \\$closure with default value.#"
- "#has parameter \\$description with default value.#" - "#has parameter \\$description with default value.#"
- "#Method Pest\\\\Support\\\\Reflection::getParameterClassName\\(\\) has a nullable return type declaration.#"

View File

@ -6,8 +6,7 @@ namespace Pest\Actions;
use Pest\Exceptions\AttributeNotSupportedYet; use Pest\Exceptions\AttributeNotSupportedYet;
use Pest\Exceptions\FileOrFolderNotFound; use Pest\Exceptions\FileOrFolderNotFound;
use PHPUnit\TextUI\Configuration\Configuration; use PHPUnit\TextUI\XmlConfiguration\Loader;
use PHPUnit\TextUI\Configuration\Registry;
/** /**
* @internal * @internal
@ -30,9 +29,7 @@ final class ValidatesConfiguration
throw new FileOrFolderNotFound('phpunit.xml'); throw new FileOrFolderNotFound('phpunit.xml');
} }
$configuration = Registry::getInstance() $configuration = (new Loader())->load($arguments[self::CONFIGURATION_KEY])->phpunit();
->get($arguments[self::CONFIGURATION_KEY])
->phpunit();
if ($configuration->processIsolation()) { if ($configuration->processIsolation()) {
throw new AttributeNotSupportedYet('processIsolation', 'true'); throw new AttributeNotSupportedYet('processIsolation', 'true');

View File

@ -157,8 +157,15 @@ final class TestCaseFactory
} }
/** /**
* Makes a fully qualified class name * Makes a fully qualified class name from the current filename.
* from the given filename. */
public function getClassName(): string
{
return $this->makeClassFromFilename($this->filename);
}
/**
* Makes a fully qualified class name from the given filename.
*/ */
public function makeClassFromFilename(string $filename): string public function makeClassFromFilename(string $filename): string
{ {

View File

@ -9,6 +9,7 @@ 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;
/** /**
@ -91,6 +92,12 @@ 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 {
return ExecutionOrderDependency::createFromDependsAnnotation($className, $test);
}, $tests);
$this->testCaseFactory $this->testCaseFactory
->factoryProxies ->factoryProxies
->add(Backtrace::file(), Backtrace::line(), 'setDependencies', [$tests]); ->add(Backtrace::file(), Backtrace::line(), 'setDependencies', [$tests]);

View File

@ -75,14 +75,15 @@ final class Container
if ($constructor !== null) { if ($constructor !== null) {
$params = array_map( $params = array_map(
function (ReflectionParameter $param) use ($id) { function (ReflectionParameter $param) use ($id) {
$candidate = null; $candidate = Reflection::getParameterClassName($param);
if ($param->getType() !== null && $param->getType()->isBuiltin()) { if ($candidate === null) {
$candidate = $param->getName(); $type = $param->getType();
} elseif ($param->getClass() !== null) { if ($type !== null && $type->isBuiltin()) {
$candidate = $param->getClass()->getName(); $candidate = $param->getName();
} else { } else {
throw ShouldNotHappen::fromMessage(sprintf('The type of `$%s` in `%s` cannot be determined.', $id, $param->getName())); throw ShouldNotHappen::fromMessage(sprintf('The type of `$%s` in `%s` cannot be determined.', $id, $param->getName()));
}
} }
return $this->get($candidate); return $this->get($candidate);

View File

@ -76,7 +76,7 @@ final class HigherOrderMessage
Reflection::setPropertyValue($throwable, 'file', $this->filename); Reflection::setPropertyValue($throwable, 'file', $this->filename);
Reflection::setPropertyValue($throwable, 'line', $this->line); Reflection::setPropertyValue($throwable, 'line', $this->line);
if ($throwable->getMessage() === sprintf(self::UNDEFINED_METHOD, $this->methodName)) { if ($throwable->getMessage() === self::getUndefinedMethodMessage($target, $this->methodName)) {
/** @var \ReflectionClass $reflection */ /** @var \ReflectionClass $reflection */
$reflection = new ReflectionClass($target); $reflection = new ReflectionClass($target);
/* @phpstan-ignore-next-line */ /* @phpstan-ignore-next-line */
@ -87,4 +87,13 @@ final class HigherOrderMessage
throw $throwable; throw $throwable;
} }
} }
private static function getUndefinedMethodMessage(object $target, string $methodName): string
{
if (\PHP_MAJOR_VERSION >= 8) {
return sprintf(sprintf(self::UNDEFINED_METHOD, sprintf('%s::%s()', get_class($target), $methodName)));
}
return sprintf(self::UNDEFINED_METHOD, $methodName);
}
} }

View File

@ -9,6 +9,8 @@ use Pest\Exceptions\ShouldNotHappen;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;
use ReflectionFunction; use ReflectionFunction;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionProperty; use ReflectionProperty;
/** /**
@ -117,4 +119,32 @@ final class Reflection
$reflectionProperty->setAccessible(true); $reflectionProperty->setAccessible(true);
$reflectionProperty->setValue($object, $value); $reflectionProperty->setValue($object, $value);
} }
/**
* Get the class name of the given parameter's type, if possible.
*
* @see https://github.com/laravel/framework/blob/v6.18.25/src/Illuminate/Support/Reflector.php
*/
public static function getParameterClassName(ReflectionParameter $parameter): ?string
{
$type = $parameter->getType();
if (!$type instanceof ReflectionNamedType || $type->isBuiltin()) {
return null;
}
$name = $type->getName();
if (($class = $parameter->getDeclaringClass()) instanceof ReflectionClass) {
if ($name === 'self') {
return $class->getName();
}
if ($name === 'parent' && ($parent = $class->getParentClass()) instanceof ReflectionClass) {
return $parent->getName();
}
}
return $name;
}
} }

View File

@ -219,14 +219,6 @@
✓ it creates unique test case names with ('Name 1', Pest\Plugin Object (), true) #3 ✓ it creates unique test case names with ('Name 1', Pest\Plugin Object (), true) #3
✓ it creates unique test case names - count ✓ it creates unique test case names - count
PASS Tests\Features\Depends
✓ first
✓ second
✓ depends
✓ depends with ...params
✓ depends with defined arguments
✓ depends run test only once
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
@ -337,5 +329,13 @@
WARN Tests\Visual\Success WARN Tests\Visual\Success
- visual snapshot of test suite on success - visual snapshot of test suite on success
PASS Tests\Features\Depends
✓ first
✓ second
✓ depends
✓ depends with ...params
✓ depends with defined arguments
✓ depends run test only once
Tests: 6 skipped, 198 passed Tests: 6 skipped, 198 passed
Time: 5.46s Time: 5.46s