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

Phpunit 10 support #41

Closed
wants to merge 8 commits into from
Closed
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
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@
"minimum-stability": "dev",
"prefer-stable": true,
"require": {
"php": "^7.3 || ^8",
"phpspec/prophecy": "^1.3",
"phpunit/phpunit":"^9.1"
"php": "^8.1",
"phpspec/prophecy": "dev-allow-sebastian-comparator-5 as 1.15",
"phpunit/phpunit": "^10.0.x-dev"
},
"repositories": [
{
"type": "github",
"url": "https://github.com/Jean85/prophecy"
}
],
"autoload": {
"psr-4": {
"Prophecy\\PhpUnit\\": "src"
Expand Down
16 changes: 16 additions & 0 deletions fixtures/NoProphecy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Prophecy\PhpUnit\Tests\Fixtures;

use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class NoProphecy extends TestCase
{
use ProphecyTrait;

public function testMethod()
{
$this->assertTrue(true);
}
}
2 changes: 1 addition & 1 deletion fixtures/Error.php → fixtures/WrongCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;

class Error extends TestCase
class WrongCall extends TestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which possible naming collisions ? The namespace Prophecy\PhpUnit\Tests\Fixtures is fully ours.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting a "Cannot find class Error" with that, and the simple renaming made it go away.

{
use ProphecyTrait;

Expand Down
29 changes: 16 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Prophecy PhpUnit Test Suite">
<directory suffix="Test.php">./tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./src</directory>
</whitelist>
</filter>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.0/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResult="false"
colors="true"
>
<testsuites>
<testsuite name="Prophecy End2End PHPT Test Suite">
<directory suffix=".phpt">./tests/</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory>./src</directory>
</include>
</coverage>
</phpunit>
5 changes: 0 additions & 5 deletions src/ProphecyTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ trait ProphecyTrait
*/
protected function prophesize(?string $classOrInterface = null): ObjectProphecy
{
if (\is_string($classOrInterface)) {
\assert($this instanceof TestCase);
$this->recordDoubledType($classOrInterface);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this seems unrelated to the commit message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not. recordDoubledType does not exist anymore on TestCase, this was the only blocker that was making it fatal during execution. It's all handled with events on PHPUnit now, but the events are strictly related to PHPUnit mocks, so I don't know if its sensible to use them; maybe we could tackle that in a separate PR?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit adding it says Add test to reach 100% coverage.

anyway, I suggest that increasing test coverage to 100% should be done in a separate PR so that we already get 100% test coverage with the code using PHPUnit 9.x

Copy link
Contributor Author

@Jean85 Jean85 Jan 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit adding it says Add test to reach 100% coverage.

Probably my mistake while committing

anyway, I suggest that increasing test coverage to 100% should be done in a separate PR so that we already get 100% test coverage with the code using PHPUnit 9.x

Done in #42

}

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

Expand Down
31 changes: 31 additions & 0 deletions tests/MockFailure.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
--TEST--
A test with a mock fails due to a missing expected call
--FILE--
<?php

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

(new PHPUnit\TextUI\Application())->run(['phpunit', 'fixtures/MockFailure.php', '--no-progress'], false);
--EXPECTF--
PHPUnit 10.%s

Runtime: PHP 8.%s
Configuration: %s

Time: %s

There was 1 failure:

1) Prophecy\PhpUnit\Tests\Fixtures\MockFailure::testMethod
Some predictions failed:
Double\DateTime\P1:
Expected exactly 2 calls that match:
Double\DateTime\P1->format(exact("Y-m-d"))
but 1 were made:
- format("Y-m-d") @ fixtures/MockFailure.php:%d

%s/src/ProphecyTrait.php:%d

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
18 changes: 18 additions & 0 deletions tests/NoProphecy.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
A test without Prophecy is executed with no additional assertions counted
--FILE--
<?php

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

(new PHPUnit\TextUI\Application())->run(['phpunit', 'fixtures/NoProphecy.php', '--no-progress'], false);
--EXPECTF--
PHPUnit 10.%s

Runtime: PHP 8.%s
Configuration: %s

Time: %s

OK (1 test, 1 assertion)
76 changes: 0 additions & 76 deletions tests/ProphecyTraitTest.php

This file was deleted.

28 changes: 28 additions & 0 deletions tests/SpyFailure.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
--TEST--
A test with a spy fails due to expected call not made
--FILE--
<?php

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

(new PHPUnit\TextUI\Application())->run(['phpunit', 'fixtures/SpyFailure.php', '--no-progress'], false);
--EXPECTF--
PHPUnit 10.%s

Runtime: PHP 8.%s
Configuration: %s

Time: %s

There was 1 error:

1) Prophecy\PhpUnit\Tests\Fixtures\SpyFailure::testMethod
Prophecy\Exception\Prediction\NoCallsException: No calls have been made that match:
Double\DateTime\P1->format(exact("Y-m-d"))
but expected at least one.

%a%s/fixtures/SpyFailure.php:%d

ERRORS!
Tests: 1, Assertions: 1, Errors: 1.
18 changes: 18 additions & 0 deletions tests/Success.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
A test with a mock is executed successfully
--FILE--
<?php

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

(new PHPUnit\TextUI\Application())->run(['phpunit', 'fixtures/Success.php', '--no-progress'], false);
--EXPECTF--
PHPUnit 10.%s

Runtime: PHP 8.%s
Configuration: %s

Time: %s

OK (1 test, 1 assertion)
26 changes: 26 additions & 0 deletions tests/WrongCall.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
A test fails due to calling an unexisting method on a mock
--FILE--
<?php

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

(new PHPUnit\TextUI\Application())->run(['phpunit', 'fixtures/WrongCall.php', '--no-progress'], false);
--EXPECTF--
PHPUnit 10.%s

Runtime: PHP 8.%s
Configuration: %s

Time: %s

There was 1 error:

1) Prophecy\PhpUnit\Tests\Fixtures\WrongCall::testMethod
Prophecy\Exception\Doubler\MethodNotFoundException: Method `Double\stdClass\P1::talk()` is not defined.

%a%s/fixtures/WrongCall.php:%d

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.
13 changes: 13 additions & 0 deletions xdebug_filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
if (function_exists('xdebug_set_filter')) {
xdebug_set_filter(
XDEBUG_FILTER_CODE_COVERAGE,
XDEBUG_PATH_INCLUDE,
[ __DIR__ . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR ]
);
xdebug_set_filter(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this managed by PHPUnit already based on the configuration in phpunit.xml ? Or is it not supported for the phpt runner ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know exactly, maybe is due to PHPT; locally, as said in the commit message, with this I observed a reduction of execution time from 26 seconds to only 3.

XDEBUG_FILTER_CODE_COVERAGE,
XDEBUG_PATH_EXCLUDE,
[ __DIR__ . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR ]
);
}