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

Add support for Phpunit 10 #45

Merged
merged 1 commit into from
Apr 21, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
phpunit.xml
.*.cache
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 2.1.0 / TBA

Add support for PHPUnit 10.1+ (10.0 is not supported)
Bump requirement to Prophecy 1.18+

# 2.0.2 / 2023-04-18

Add the `@not-deprecated` annotation to avoid phpstan reporting `prophesize` as deprecated due to the TestCase deprecation.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"prefer-stable": true,
"require": {
"php": "^7.3 || ^8",
"phpspec/prophecy": "^1.3",
"phpunit/phpunit":"^9.1"
"phpspec/prophecy": "^1.18",
"phpunit/phpunit":"^9.1 || ^10.1"
},
"autoload": {
"psr-4": {
Expand Down
18 changes: 18 additions & 0 deletions fixtures/NoClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Prophecy\PhpUnit\Tests\Fixtures;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class NoClass extends TestCase
{
use ProphecyTrait;

public function testProphesizeWithoutArguments(): void
{
$prophecy = $this->prophesize()->reveal();

$this->assertInstanceOf(\stdClass::class, $prophecy);
}
}
9 changes: 8 additions & 1 deletion src/ProphecyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,14 @@ trait ProphecyTrait
*/
protected function prophesize(?string $classOrInterface = null): ObjectProphecy
{
if (\is_string($classOrInterface)) {
static $isPhpUnit9;
$isPhpUnit9 = $isPhpUnit9 ?? method_exists($this, 'recordDoubledType');

if (! $isPhpUnit9) {
// PHPUnit 10.1
$this->registerFailureType(PredictionException::class);
} elseif (\is_string($classOrInterface)) {
// PHPUnit 9
\assert($this instanceof TestCase);
$this->recordDoubledType($classOrInterface);
}
Expand Down
2 changes: 2 additions & 0 deletions tests/MockFailure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require_once __DIR__ . '/run_test.php';
--EXPECTF--
PHPUnit %s

Runtime: %s

F %s 1 / 1 (100%)

Time: %s
Expand Down
18 changes: 18 additions & 0 deletions tests/NoClass.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
A test with a Prophecy called without any argument
--FILE--
<?php

require_once __DIR__ . '/run_test.php';

\Prophecy\PhpUnit\Tests\runTest('NoClass');
--EXPECTF--
PHPUnit %s

Runtime: %s

. %s 1 / 1 (100%)

Time: %s

OK (1 test, 1 assertion)
2 changes: 2 additions & 0 deletions tests/NoProphecy.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require_once __DIR__ . '/run_test.php';
--EXPECTF--
PHPUnit %s

Runtime: %s

. %s 1 / 1 (100%)

Time: %s
Expand Down
6 changes: 4 additions & 2 deletions tests/SpyFailure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ require_once __DIR__ . '/run_test.php';
--EXPECTF--
PHPUnit %s

Runtime: %s

F %s 1 / 1 (100%)

Time: %a

There was 1 failure:

1) Prophecy\PhpUnit\Tests\Fixtures\SpyFailure::testMethod
No calls have been made that match:
%ao calls have been made that match:
Double\DateTime\P1->format(exact("Y-m-d"))
but expected at least one.

%s/tests/run_test.php:%d
%a/tests/run_test.php:%d

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
2 changes: 2 additions & 0 deletions tests/Success.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require_once __DIR__ . '/run_test.php';
--EXPECTF--
PHPUnit %s

Runtime: %s

. %s 1 / 1 (100%)

Time: %s
Expand Down
2 changes: 2 additions & 0 deletions tests/WrongCall.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ require_once __DIR__ . '/run_test.php';
--EXPECTF--
PHPUnit %s

Runtime: %s

E %s 1 / 1 (100%)

Time: %a
Expand Down
9 changes: 8 additions & 1 deletion tests/run_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Prophecy\PhpUnit\Tests;

use PHPUnit\TextUI\Application;
use PHPUnit\TextUI\Command;

require_once __DIR__ . '/xdebug_filter.php';
Expand All @@ -14,5 +15,11 @@ function runTest(string $fixtureName): void
throw new \InvalidArgumentException('Unable to find test fixture at path ' . $filename);
}

(new Command())->run(['phpunit', $filename], false);
if (class_exists(Command::class)) {
// PHPUnit 9.x
(new Command())->run(['phpunit', $filename, '--verbose', '--no-configuration'], false);
} else {
// PHPUnit 10.x
(new Application())->run(['phpunit', $filename, '--no-configuration']);
}
}