Skip to content

Commit

Permalink
Expand supported PHPUnit versions
Browse files Browse the repository at this point in the history
  • Loading branch information
aik099 committed Mar 28, 2024
1 parent 50043b2 commit a6b99e8
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: ['7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
php: ['5.6', '7.0', '7.1', '7.2', '7.3', '7.4', '8.0', '8.1', '8.2', '8.3']
min_stability: ['']
name_suffix: ['']
include:
Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
}
],
"require": {
"php": "^7.3 || ^8",
"phpspec/prophecy": "^1.18",
"phpunit/phpunit":"^9.1 || ^10.1 || ^11.0"
"php": "^5.6 || ^7 || ^8",
"phpspec/prophecy": "^1.3",
"phpunit/phpunit": "^4.0 || ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0"
},
"autoload": {
"psr-4": {
Expand Down
4 changes: 2 additions & 2 deletions fixtures/NoClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ class NoClass extends TestCase
{
use ProphecyTrait;

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

$this->assertInstanceOf(\stdClass::class, $prophecy);
}
}
59 changes: 23 additions & 36 deletions src/ProphecyTrait.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php declare(strict_types=1);
<?php

namespace Prophecy\PhpUnit;

Expand All @@ -25,13 +25,6 @@ trait ProphecyTrait
*/
private $prophet;

/**
* @var bool
*
* @internal
*/
private $prophecyAssertionsCounted = false;

/**
* @throws DoubleException
* @throws InterfaceNotFoundException
Expand All @@ -42,28 +35,36 @@ trait ProphecyTrait
*
* @not-deprecated
*/
protected function prophesize(?string $classOrInterface = null): ObjectProphecy
protected function prophesize($classOrInterface = null): ObjectProphecy
{
static $isPhpUnit9;
$isPhpUnit9 = $isPhpUnit9 ?? method_exists($this, 'recordDoubledType');
static $hasFailureTypes;

// PHPUnit 10.1.0+.
if ($hasFailureTypes === null) {
$hasFailureTypes = method_exists($this, 'registerFailureType');
}

if (! $isPhpUnit9) {
// PHPUnit 10.1
if ($hasFailureTypes) {
$this->registerFailureType(PredictionException::class);
} elseif (\is_string($classOrInterface)) {
// PHPUnit 9
\assert($this instanceof TestCase);
$this->recordDoubledType($classOrInterface);
}

return $this->getProphet()->prophesize($classOrInterface);
}

/**
* @postCondition
* @after
*/
#[PostCondition]
protected function verifyProphecyDoubles(): void
#[After]
protected function tearDownProphecy()
{
if (null !== $this->prophet) {
$this->verifyProphecyDoubles();
}

$this->prophet = null;
}

protected function verifyProphecyDoubles()
{
if ($this->prophet === null) {
return;
Expand All @@ -74,31 +75,17 @@ protected function verifyProphecyDoubles(): void
} catch (PredictionException $e) {
throw new AssertionFailedError($e->getMessage());
} finally {
$this->countProphecyAssertions();
}
}

/**
* @after
*/
#[After]
protected function tearDownProphecy(): void
{
if (null !== $this->prophet && !$this->prophecyAssertionsCounted) {
// Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects.
$this->countProphecyAssertions();
}

$this->prophet = null;
}

/**
* @internal
*/
private function countProphecyAssertions(): void
private function countProphecyAssertions()
{
\assert($this instanceof TestCase);
$this->prophecyAssertionsCounted = true;

foreach ($this->prophet->getProphecies() as $objectProphecy) {
foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
Expand All @@ -114,7 +101,7 @@ private function countProphecyAssertions(): void
/**
* @internal
*/
private function getProphet(): Prophet
private function getProphet()
{
if ($this->prophet === null) {
$this->prophet = new Prophet;
Expand Down
1 change: 1 addition & 0 deletions tests/MockFailure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Double\DateTime\P1:
but 1 were made:
- format("Y-m-d") @ fixtures/MockFailure.php:%d

%s/src/ProphecyTrait.php:%d
%s/src/ProphecyTrait.php:%d
%s/tests/run_test.php:%d

Expand Down
14 changes: 9 additions & 5 deletions tests/run_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,26 @@

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

require_once __DIR__ . '/xdebug_filter.php';
require_once dirname(__DIR__) . '/vendor/autoload.php';

function runTest(string $fixtureName): void
function runTest($fixtureName)
{
$filename = dirname(__DIR__) . '/fixtures/' . $fixtureName . '.php';
if (!file_exists($filename)) {
throw new \InvalidArgumentException('Unable to find test fixture at path ' . $filename);
}

if (class_exists(Command::class)) {
// PHPUnit 9.x
if (class_exists(Application::class)) {
// PHPUnit 10.x+.
(new Application())->run(['phpunit', $filename, '--no-configuration']);
} elseif (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']);
// PHPUnit 5.x-.
(new PHPUnit_TextUI_Command())->run(['phpunit', $filename, '--verbose', '--no-configuration'], false);
}
}
6 changes: 5 additions & 1 deletion tests/xdebug_filter.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php
if (function_exists('xdebug_set_filter')) {
if (function_exists('xdebug_set_filter')
&& defined('XDEBUG_FILTER_CODE_COVERAGE')
&& defined('XDEBUG_PATH_INCLUDE')
&& defined('XDEBUG_PATH_EXCLUDE')
) {
xdebug_set_filter(
XDEBUG_FILTER_CODE_COVERAGE,
XDEBUG_PATH_INCLUDE,
Expand Down

0 comments on commit a6b99e8

Please sign in to comment.