Skip to content

Commit

Permalink
Merge pull request #45 from Jean85/phpunit-10-support
Browse files Browse the repository at this point in the history
Add support for Phpunit 10
  • Loading branch information
stof authored Apr 21, 2023
2 parents 9f26c22 + 14b4960 commit c19e18f
Show file tree
Hide file tree
Showing 12 changed files with 72 additions and 6 deletions.
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']);
}
}

0 comments on commit c19e18f

Please sign in to comment.