Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adds support to phpunit 9.3 and php 8.0 #128

Merged
merged 1 commit into from
Aug 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
coverage: none

- name: Install Dependencies
run: composer update --no-interaction --prefer-dist --no-progress
run: composer update --no-interaction --no-progress

- name: Run Rector
run: vendor/bin/rector process src --dry-run
Expand Down Expand Up @@ -48,7 +48,7 @@ jobs:
coverage: none

- 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
run: vendor/bin/phpstan analyse --no-progress
34 changes: 15 additions & 19 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,43 +6,39 @@ jobs:
ci:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
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]

name: Tests P${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}
name: PHP ${{ matrix.php }} - ${{ matrix.os }} - ${{ matrix.dependency-version }}

steps:

- name: Checkout
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
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: dom, mbstring, zip
tools: composer:v2
coverage: none

- name: Install Composer dependencies
run: composer update --${{ matrix.dependency-version }} --no-interaction --prefer-dist
- name: Setup Problem Matches
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
run: php bin/pest --colors=always --exclude-group=integration

- name: Integration Tests
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"
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@
}
],
"require": {
"php": "^7.3",
"php": "^7.3 || ^8.0",
"nunomaduro/collision": "^5.0.0-BETA4",
"pestphp/pest-plugin": "^0.3",
"pestphp/pest-plugin-coverage": "^0.3",
"pestphp/pest-plugin-init": "^0.3",
"phpunit/phpunit": "^9.2.6",
"sebastian/environment": "^5.1.2"
"phpunit/phpunit": "9.3.2"
GrahamCampbell marked this conversation as resolved.
Show resolved Hide resolved
},
"autoload": {
"psr-4": {
Expand All @@ -45,7 +44,7 @@
"require-dev": {
"illuminate/console": "^7.16.1",
"illuminate/support": "^7.16.1",
"mockery/mockery": "^1.4.0",
"mockery/mockery": "^1.4.1",
"pestphp/pest-dev-tools": "dev-master"
},
"minimum-stability": "dev",
Expand Down
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ parameters:
- "# with null as default value#"
- "#has parameter \\$closure with default value.#"
- "#has parameter \\$description with default value.#"
- "#Method Pest\\\\Support\\\\Reflection::getParameterClassName\\(\\) has a nullable return type declaration.#"
7 changes: 2 additions & 5 deletions src/Actions/ValidatesConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

use Pest\Exceptions\AttributeNotSupportedYet;
use Pest\Exceptions\FileOrFolderNotFound;
use PHPUnit\TextUI\Configuration\Configuration;
use PHPUnit\TextUI\Configuration\Registry;
use PHPUnit\TextUI\XmlConfiguration\Loader;

/**
* @internal
Expand All @@ -30,9 +29,7 @@ public static function in($arguments): void
throw new FileOrFolderNotFound('phpunit.xml');
}

$configuration = Registry::getInstance()
->get($arguments[self::CONFIGURATION_KEY])
->phpunit();
$configuration = (new Loader())->load($arguments[self::CONFIGURATION_KEY])->phpunit();

if ($configuration->processIsolation()) {
throw new AttributeNotSupportedYet('processIsolation', 'true');
Expand Down
11 changes: 9 additions & 2 deletions src/Factories/TestCaseFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,15 @@ public function build(TestSuite $testSuite): array
}

/**
* Makes a fully qualified class name
* from the given filename.
* 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.
*/
public function makeClassFromFilename(string $filename): string
{
Expand Down
7 changes: 7 additions & 0 deletions src/PendingObjects/TestCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Pest\Support\Backtrace;
use Pest\Support\NullClosure;
use Pest\TestSuite;
use PHPUnit\Framework\ExecutionOrderDependency;
use SebastianBergmann\Exporter\Exporter;

/**
Expand Down Expand Up @@ -91,6 +92,12 @@ public function with($data): 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
->factoryProxies
->add(Backtrace::file(), Backtrace::line(), 'setDependencies', [$tests]);
Expand Down
17 changes: 9 additions & 8 deletions src/Support/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,15 @@ private function build(string $id): object
if ($constructor !== null) {
$params = array_map(
function (ReflectionParameter $param) use ($id) {
$candidate = null;

if ($param->getType() !== null && $param->getType()->isBuiltin()) {
$candidate = $param->getName();
} elseif ($param->getClass() !== null) {
$candidate = $param->getClass()->getName();
} else {
throw ShouldNotHappen::fromMessage(sprintf('The type of `$%s` in `%s` cannot be determined.', $id, $param->getName()));
$candidate = Reflection::getParameterClassName($param);

if ($candidate === null) {
$type = $param->getType();
if ($type !== null && $type->isBuiltin()) {
$candidate = $param->getName();
} else {
throw ShouldNotHappen::fromMessage(sprintf('The type of `$%s` in `%s` cannot be determined.', $id, $param->getName()));
}
}

return $this->get($candidate);
Expand Down
11 changes: 10 additions & 1 deletion src/Support/HigherOrderMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function call(object $target)
Reflection::setPropertyValue($throwable, 'file', $this->filename);
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 */
$reflection = new ReflectionClass($target);
/* @phpstan-ignore-next-line */
Expand All @@ -87,4 +87,13 @@ public function call(object $target)
throw $throwable;
}
}

private static function getUndefinedMethodMessage(object $target, string $methodName): string
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self note: add tests about this.

{
if (\PHP_MAJOR_VERSION >= 8) {
return sprintf(sprintf(self::UNDEFINED_METHOD, sprintf('%s::%s()', get_class($target), $methodName)));
}

return sprintf(self::UNDEFINED_METHOD, $methodName);
}
}
30 changes: 30 additions & 0 deletions src/Support/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use ReflectionClass;
use ReflectionException;
use ReflectionFunction;
use ReflectionNamedType;
use ReflectionParameter;
use ReflectionProperty;

/**
Expand Down Expand Up @@ -117,4 +119,32 @@ public static function setPropertyValue(object $object, string $property, $value
$reflectionProperty->setAccessible(true);
$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;
}
}
16 changes: 8 additions & 8 deletions tests/.snapshots/success.txt
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,6 @@
✓ it creates unique test case names with ('Name 1', Pest\Plugin Object (), true) #3
✓ 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
✓ it gives access the the underlying expectException
✓ it catch exceptions
Expand Down Expand Up @@ -337,5 +329,13 @@
WARN Tests\Visual\Success
- visual snapshot of test suite on success

PASS Tests\Features\Depends
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Self note: understand why this snapshot got different.

✓ first
✓ second
✓ depends
✓ depends with ...params
✓ depends with defined arguments
✓ depends run test only once

Tests: 6 skipped, 198 passed
Time: 5.46s