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

Update for PHPUnit 9.1 #16

Merged
merged 13 commits into from
Mar 24, 2020
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
9 changes: 3 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@ language: php
sudo: false

php:
- 5.3
- 5.4
- 5.5
- 5.6
- hhvm
- 7.3
- 7.4

install: composer install

script: phpunit --coverage-text
script: ./vendor/bin/phpunit --coverage-text
81 changes: 42 additions & 39 deletions ProphecyTestCase.php
Original file line number Diff line number Diff line change
@@ -1,51 +1,64 @@
<?php
<?php declare(strict_types=1);

sebastianbergmann marked this conversation as resolved.
Show resolved Hide resolved
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;

abstract class ProphecyTestCase extends \PHPUnit_Framework_TestCase
abstract class ProphecyTestCase extends TestCase
{
/**
* @var Prophet|null
*/
private $prophet;

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

/**
* @param string|null $classOrInterface
* @throws DoubleException
* @throws InterfaceNotFoundException
*
* @return \Prophecy\Prophecy\ObjectProphecy
* @psalm-param class-string|null $type
*/
protected function prophesize($classOrInterface = null)
protected function prophesize(?string $classOrInterface = null): ObjectProphecy
{
if (\is_string($classOrInterface)) {
$this->recordDoubledType($classOrInterface);
}

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

protected function verifyMockObjects()
protected function verifyMockObjects(): void
{
parent::verifyMockObjects();

if (null === $this->prophet) {
sebastianbergmann marked this conversation as resolved.
Show resolved Hide resolved
if ($this->prophet === null) {
return;
}

try {
$this->prophet->checkPredictions();
} catch (\Exception $e) {
/** Intentionally left empty */
}

$this->countProphecyAssertions();

if (isset($e)) {
throw $e;
} catch (PredictionException $e) {
throw new AssertionFailedError($e->getMessage());
} finally {
$this->countProphecyAssertions();
}
}

protected function tearDown()
/**
* @after
*/
protected function prophecyTearDown(): 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.
sebastianbergmann marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -55,37 +68,27 @@ protected function tearDown()
$this->prophet = null;
}

protected function onNotSuccessfulTest(\Exception $e)
{
if ($e instanceof PredictionException) {
$e = new \PHPUnit_Framework_AssertionFailedError($e->getMessage(), $e->getCode(), $e);
}

return parent::onNotSuccessfulTest($e);
}

/**
* @return Prophet
*/
private function getProphet()
{
if (null === $this->prophet) {
$this->prophet = new Prophet();
sebastianbergmann marked this conversation as resolved.
Show resolved Hide resolved
}

return $this->prophet;
}

private function countProphecyAssertions()
private function countProphecyAssertions(): void
{
$this->prophecyAssertionsCounted = true;

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

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

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

return $this->prophet;
}
}
52 changes: 30 additions & 22 deletions Tests/ProphecyTestCaseTest.php
Original file line number Diff line number Diff line change
@@ -1,63 +1,71 @@
<?php
<?php declare(strict_types=1);

sebastianbergmann marked this conversation as resolved.
Show resolved Hide resolved
namespace Prophecy\PhpUnit\Tests;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\Tests\Fixtures\Error;
use Prophecy\PhpUnit\Tests\Fixtures\MockFailure;
use Prophecy\PhpUnit\Tests\Fixtures\SpyFailure;
use Prophecy\PhpUnit\Tests\Fixtures\Success;

class ProphecyTestCaseTest extends \PHPUnit_Framework_TestCase
/**
* @covers \Prophecy\PhpUnit\ProphecyTestCase
*/
final class ProphecyTestCaseTest extends TestCase
{
protected function setUp()
protected function setUp(): void
{
// Define the constant because our tests are running PHPUnit testcases themselves
if (!defined('PHPUNIT_TESTSUITE')) {
define('PHPUNIT_TESTSUITE', true);
// Define the constant because our tests are running PHPUnit test cases themselves
if (!\defined('PHPUNIT_TESTSUITE')) {
\define('PHPUNIT_TESTSUITE', true);
}
}

public function testSuccess()
public function testSuccess(): void
{
$test = new Success('testMethod');

$result = $test->run();

$this->assertEquals(0, $result->errorCount());
$this->assertEquals(0, $result->failureCount());
$this->assertSame(0, $result->errorCount());
$this->assertSame(0, $result->failureCount());
$this->assertCount(1, $result);
$this->assertEquals(1, $test->getNumAssertions());
$this->assertSame(1, $test->getNumAssertions());
}

public function testSpyPredictionFailure()
public function testSpyPredictionFailure(): void
{
$test = new SpyFailure('testMethod');

$result = $test->run();

$this->assertEquals(0, $result->errorCount());
$this->assertEquals(1, $result->failureCount());
$this->assertSame(0, $result->errorCount());
$this->assertSame(1, $result->failureCount());
$this->assertCount(1, $result);
$this->assertEquals(1, $test->getNumAssertions());
$this->assertSame(1, $test->getNumAssertions());
}

public function testMockPredictionFailure()
public function testMockPredictionFailure(): void
{
$test = new MockFailure('testMethod');

$result = $test->run();

$this->assertEquals(0, $result->errorCount());
$this->assertEquals(1, $result->failureCount());
$this->assertSame(0, $result->errorCount());
$this->assertSame(1, $result->failureCount());
$this->assertCount(1, $result);
$this->assertEquals(1, $test->getNumAssertions());
$this->assertSame(1, $test->getNumAssertions());
}

public function testDoublingError()
public function testDoublingError(): void
{
$test = new Error('testMethod');

$result = $test->run();

$this->assertEquals(1, $result->errorCount());
$this->assertEquals(0, $result->failureCount());
$this->assertSame(1, $result->errorCount());
$this->assertSame(0, $result->failureCount());
$this->assertCount(1, $result);
$this->assertEquals(0, $test->getNumAssertions());
$this->assertSame(0, $test->getNumAssertions());
}
}
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"email": "stof@notk.org"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": ">=5.3.3",
"phpspec/prophecy": "~1.3"
},
"suggest": {
"phpunit/phpunit": "if it is not installed globally"
"php": "^7.3",
"phpspec/prophecy": "^1.3",
"phpunit/phpunit":"^9.1"
},
"autoload": {
"psr-4": {
Expand All @@ -25,7 +25,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "1.0.x-dev"
"dev-master": "2.0-dev"
}
}
}