-
Notifications
You must be signed in to change notification settings - Fork 40
Allow installation in combination with PHPUnit < 9.1 #38
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9636333
Add custom autoloader/fall-through to PHPUnit native functionality
jrfnl 2c7179a
Add availability tests for the fall-through functionality
jrfnl 5cbdeb0
GH Actions: adjust the test workflow
jrfnl 6c66bd8
Remove the Autoloader in favour of logic in the `ProphecyTrait.php` file
jrfnl c89f65b
Update the availability test to not directly use the trait
jrfnl 0ff4d97
GH Actions: remove testing against `lowest`
jrfnl cadd8af
AvailabilityTest: declare `PHPUNIT_TESTSUITE` constant
jrfnl 4cf2dc7
Various updates for feedback
jrfnl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Prophecy\PhpUnit; | ||
|
||
use PHPUnit\Framework\AssertionFailedError; | ||
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 | ||
* | ||
* @psalm-param class-string|null $classOrInterface | ||
*/ | ||
protected function prophesize(?string $classOrInterface = null): ObjectProphecy | ||
{ | ||
if (\is_string($classOrInterface)) { | ||
\assert($this instanceof TestCase); | ||
$this->recordDoubledType($classOrInterface); | ||
} | ||
|
||
return $this->getProphet()->prophesize($classOrInterface); | ||
} | ||
|
||
/** | ||
* @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 | ||
*/ | ||
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Prophecy\PhpUnit; | ||
|
||
/** | ||
* Prophecy trait for use with PHPUnit 4.x - 9.0. | ||
* | ||
* As this trait is empty, calls to `prophesize()` will automatically fall through | ||
* to PHPUnit itself and will use the PHPUnit native `prophesize()` method. | ||
* | ||
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!} | ||
* | ||
* @mixin TestCase | ||
*/ | ||
trait ProphecyTrait | ||
{ | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,110 +1,43 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
namespace Prophecy\PhpUnit; | ||
|
||
use PHPUnit\Framework\AssertionFailedError; | ||
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; | ||
use PHPUnit\Runner\Version; | ||
use PHPUnit_Runner_Version; | ||
|
||
/** | ||
* @mixin TestCase | ||
* Load the prophecy trait. | ||
* | ||
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!} | ||
*/ | ||
trait ProphecyTrait | ||
{ | ||
/** | ||
* @var Prophet|null | ||
* | ||
* @internal | ||
*/ | ||
private $prophet; | ||
|
||
/** | ||
* @var bool | ||
* | ||
* @internal | ||
*/ | ||
private $prophecyAssertionsCounted = false; | ||
|
||
/** | ||
* @throws DoubleException | ||
* @throws InterfaceNotFoundException | ||
* | ||
* @psalm-param class-string|null $classOrInterface | ||
*/ | ||
protected function prophesize(?string $classOrInterface = null): ObjectProphecy | ||
{ | ||
if (\is_string($classOrInterface)) { | ||
\assert($this instanceof TestCase); | ||
$this->recordDoubledType($classOrInterface); | ||
} | ||
|
||
return $this->getProphet()->prophesize($classOrInterface); | ||
} | ||
|
||
/** | ||
* @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(); | ||
} | ||
/** | ||
* Retrieve the PHPUnit version id. | ||
* | ||
* As both the pre-PHPUnit 6 class, as well as the PHPUnit 6+ class contain the `id()` function, | ||
* this should work independently of whether or not another library may have aliased the class. | ||
* | ||
* @internal | ||
* | ||
* @return string Version number as a string. | ||
*/ | ||
function getPHPUnitVersion() | ||
{ | ||
if (\class_exists('\PHPUnit\Runner\Version')) { | ||
return Version::id(); | ||
} | ||
|
||
/** | ||
* @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; | ||
if (\class_exists('\PHPUnit_Runner_Version')) { | ||
return PHPUnit_Runner_Version::id(); | ||
} | ||
|
||
/** | ||
* @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())); | ||
} | ||
} | ||
} | ||
} | ||
return '0'; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
private function getProphet(): Prophet | ||
{ | ||
if ($this->prophet === null) { | ||
$this->prophet = new Prophet; | ||
} | ||
|
||
return $this->prophet; | ||
} | ||
if (\version_compare(getPHPUnitVersion(), '9.1.0', '>=')) { | ||
// PHPUnit >= 9.1.0. | ||
require_once __DIR__ . '/ProphecyTrait.actual.php'; | ||
} else { | ||
require_once __DIR__ . '/ProphecyTrait.empty.php'; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Prophecy\PhpUnit\Tests\Availability; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
use Prophecy\PhpUnit\Tests\Fixtures\Success; | ||
|
||
/** | ||
* Testing that the autoloading of the empty `ProphecyTrait` trait for PHPUnit 4.x - 9.0 works correctly. | ||
* | ||
* {@internal The code in this file must be PHP cross-version compatible for PHP 5.4 - current!} | ||
* | ||
* @coversNothing | ||
*/ | ||
final class AvailabilityTest extends TestCase | ||
jrfnl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* @before | ||
*/ | ||
protected function declareTestsuiteConstant() | ||
{ | ||
// Define the constant because our tests are running PHPUnit test cases themselves | ||
if (!\defined('PHPUNIT_TESTSUITE')) { | ||
\define('PHPUNIT_TESTSUITE', true); | ||
} | ||
} | ||
|
||
public function testSuccessfullyCallingProphesizeMethod() | ||
{ | ||
$this->assertTrue(trait_exists('Prophecy\PhpUnit\ProphecyTrait'), 'Failed to assert that the ProphecyTrait is available'); | ||
|
||
$test = new Success('testMethod'); | ||
|
||
$result = $test->run(); | ||
|
||
$this->assertSame(0, $result->errorCount(), 'Error count is not 0'); | ||
$this->assertSame(0, $result->failureCount(), 'Failure count is not 0'); | ||
$this->assertCount(1, $result, 'Result is not 1'); | ||
$this->assertSame(1, $test->getNumAssertions(), 'Number of assertions is not 1'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.