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

Expand PHPUnit version support #1

Merged
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
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": "^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 1 addition & 0 deletions fixtures/SpyFailure.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public function testMethod()

$prophecy->reveal();

// Native PHPUnit implementation don't add this to the assertion count.
$prophecy->format('Y-m-d')->shouldHaveBeenCalled();
}
}
128 changes: 5 additions & 123 deletions src/ProphecyTrait.php
Original file line number Diff line number Diff line change
@@ -1,125 +1,7 @@
<?php declare(strict_types=1);
<?php

namespace Prophecy\PhpUnit;

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\PostCondition;
use PHPUnit\Framework\TestCase;
use Prophecy\Exception\Doubler\DoubleException;
use Prophecy\Exception\Doubler\InterfaceNotFoundException;
use Prophecy\Exception\Prediction\PredictionException;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Prophecy\Prophet;

/**
* @mixin TestCase
*/
trait ProphecyTrait
{
/**
* @var Prophet|null
*
* @internal
*/
private $prophet;

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

/**
* @throws DoubleException
* @throws InterfaceNotFoundException
*
* @template T of object
* @phpstan-param class-string<T>|null $classOrInterface
* @phpstan-return ($classOrInterface is null ? ObjectProphecy<object> : ObjectProphecy<T>)
*
* @not-deprecated
*/
protected function prophesize(?string $classOrInterface = null): ObjectProphecy
{
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);
}

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

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

try {
$this->prophet->checkPredictions();
} 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
{
\assert($this instanceof TestCase);
$this->prophecyAssertionsCounted = true;

foreach ($this->prophet->getProphecies() as $objectProphecy) {
foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
foreach ($methodProphecies as $methodProphecy) {
\assert($methodProphecy instanceof MethodProphecy);

$this->addToAssertionCount(\count($methodProphecy->getCheckedPredictions()));
}
}
}
}

/**
* @internal
*/
private function getProphet(): Prophet
{
if ($this->prophet === null) {
$this->prophet = new Prophet;
}

return $this->prophet;
}
if (PHP_VERSION_ID >= 70000) {
require_once __DIR__ . '/ProphecyTrait7.php';
} else {
require_once __DIR__ . '/ProphecyTrait5.php';
}
111 changes: 111 additions & 0 deletions src/ProphecyTrait5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Prophecy\PhpUnit;

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\TestCase;
use Prophecy\Exception\Doubler\DoubleException;
use Prophecy\Exception\Doubler\InterfaceNotFoundException;
use Prophecy\Exception\Prediction\PredictionException;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Prophecy\Prophet;

/**
* @mixin TestCase
*/
trait ProphecyTrait
{
/**
* @var Prophet|null
*
* @internal
*/
private $prophet;

/**
* @throws DoubleException
* @throws InterfaceNotFoundException
*
* @template T of object
* @phpstan-param class-string<T>|null $classOrInterface
* @phpstan-return ($classOrInterface is null ? ObjectProphecy<object> : ObjectProphecy<T>)
*
* @not-deprecated
*/
protected function prophesize($classOrInterface = null)
{
static $hasFailureTypes;

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

if ($hasFailureTypes) {
$this->registerFailureType(PredictionException::class);
}

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

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

$this->prophet = null;
}

protected function verifyProphecyDoubles()
{
if ($this->prophet === null) {
return;
}

try {
$this->prophet->checkPredictions();
} catch (PredictionException $e) {
throw new AssertionFailedError($e->getMessage());
} finally {
// Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects.
$this->countProphecyAssertions();
}
}

/**
* @internal
*/
private function countProphecyAssertions()
{
\assert($this instanceof TestCase);

foreach ($this->prophet->getProphecies() as $objectProphecy) {
foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
foreach ($methodProphecies as $methodProphecy) {
\assert($methodProphecy instanceof MethodProphecy);

$this->addToAssertionCount(\count($methodProphecy->getCheckedPredictions()));
}
}
}
}

/**
* @internal
*/
private function getProphet()
{
if ($this->prophet === null) {
$this->prophet = new Prophet;
}

return $this->prophet;
}
}
111 changes: 111 additions & 0 deletions src/ProphecyTrait7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php declare(strict_types=1);

namespace Prophecy\PhpUnit;

use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\TestCase;
use Prophecy\Exception\Doubler\DoubleException;
use Prophecy\Exception\Doubler\InterfaceNotFoundException;
use Prophecy\Exception\Prediction\PredictionException;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophecy\ObjectProphecy;
use Prophecy\Prophet;

/**
* @mixin TestCase
*/
trait ProphecyTrait
{
/**
* @var Prophet|null
*
* @internal
*/
private $prophet;

/**
* @throws DoubleException
* @throws InterfaceNotFoundException
*
* @template T of object
* @phpstan-param class-string<T>|null $classOrInterface
* @phpstan-return ($classOrInterface is null ? ObjectProphecy<object> : ObjectProphecy<T>)
*
* @not-deprecated
*/
protected function prophesize($classOrInterface = null): ObjectProphecy
{
static $hasFailureTypes;

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

if ($hasFailureTypes) {
$this->registerFailureType(PredictionException::class);
}

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

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

$this->prophet = null;
}

protected function verifyProphecyDoubles()
{
if ($this->prophet === null) {
return;
}

try {
$this->prophet->checkPredictions();
} catch (PredictionException $e) {
throw new AssertionFailedError($e->getMessage());
} finally {
// Some Prophecy assertions may have been done in tests themselves even when a failure happened before checking mock objects.
$this->countProphecyAssertions();
}
}

/**
* @internal
*/
private function countProphecyAssertions()/*: void*/
{
\assert($this instanceof TestCase);

foreach ($this->prophet->getProphecies() as $objectProphecy) {
foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
foreach ($methodProphecies as $methodProphecy) {
\assert($methodProphecy instanceof MethodProphecy);

$this->addToAssertionCount(\count($methodProphecy->getCheckedPredictions()));
}
}
}
}

/**
* @internal
*/
private function getProphet(): Prophet
{
if ($this->prophet === null) {
$this->prophet = new Prophet;
}

return $this->prophet;
}
}
3 changes: 2 additions & 1 deletion tests/MockFailure.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ Double\DateTime\P1:
but 1 were made:
- format("Y-m-d") @ fixtures/MockFailure.php:%d

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

FAILURES!
Expand Down
Loading