Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2492ccf

Browse files
bug #40957 [PhpUnitBridge] Fix tests with @doesNotPerformAssertions annotations (alexpott)
This PR was submitted for the 5.x branch but it was squashed and merged into the 5.2 branch instead. Discussion ---------- [PhpUnitBridge] Fix tests with ``@doesNotPerformAssertions`` annotations | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exist, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> If a test uses the ``@doesNotPerformAssertions`` and also has the Symfony Deprecation listener enabled because it is using the trait as well then this does not work as expected. Currently the bridge is checking this annotation/setting prior to running the test. This results in: * Tests not working as expected when `$this->expectNotToPerformAssertions()` is called during a test * If this is being used to ensure that a test does not perform an assertion then due to the bridge the test will no longer be marked as risky because we call `$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);` Commits ------- 4ad1232 [PhpUnitBridge] Fix tests with ``@doesNotPerformAssertions`` annotations
2 parents ca46f10 + 4ad1232 commit 2492ccf

File tree

3 files changed

+71
-2
lines changed

3 files changed

+71
-2
lines changed

src/Symfony/Bridge/PhpUnit/Legacy/SymfonyTestsListenerTrait.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public function startTest($test)
239239
}
240240

241241
if ($this->checkNumAssertions) {
242-
$this->checkNumAssertions = $test->getTestResultObject()->isStrictAboutTestsThatDoNotTestAnything() && !$test->doesNotPerformAssertions();
242+
$this->checkNumAssertions = $test->getTestResultObject()->isStrictAboutTestsThatDoNotTestAnything();
243243
}
244244

245245
$test->getTestResultObject()->beStrictAboutTestsThatDoNotTestAnything(false);
@@ -268,7 +268,10 @@ public function endTest($test, $time)
268268
$groups = Test::getGroups($className, $test->getName(false));
269269

270270
if ($this->checkNumAssertions) {
271-
if (!self::$expectedDeprecations && !$test->getNumAssertions() && $test->getTestResultObject()->noneSkipped()) {
271+
$assertions = \count(self::$expectedDeprecations) + $test->getNumAssertions();
272+
if ($test->doesNotPerformAssertions() && $assertions > 0) {
273+
$test->getTestResultObject()->addFailure($test, new RiskyTestError(sprintf('This test is annotated with "@doesNotPerformAssertions", but performed %s assertions', $assertions)), $time);
274+
} elseif ($assertions === 0 && $test->getTestResultObject()->noneSkipped()) {
272275
$test->getTestResultObject()->addFailure($test, new RiskyTestError('This test did not perform any assertions'), $time);
273276
}
274277

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Tests\FailTests;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
16+
17+
/**
18+
* This class is deliberately suffixed with *TestRisky.php so that it is ignored
19+
* by PHPUnit. This test is designed to fail. See ../expectrisky.phpt.
20+
*/
21+
final class NoAssertionsTestRisky extends TestCase
22+
{
23+
use ExpectDeprecationTrait;
24+
25+
/**
26+
* Do not remove this test in the next major version.
27+
*
28+
* @group legacy
29+
*/
30+
public function testOne()
31+
{
32+
$this->expectNotToPerformAssertions();
33+
$this->expectDeprecation('foo');
34+
@trigger_error('foo', \E_USER_DEPRECATED);
35+
}
36+
37+
/**
38+
* Do not remove this test in the next major version.
39+
*/
40+
public function testTwo()
41+
{
42+
$this->expectNotToPerformAssertions();
43+
}
44+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Test NoAssertionsTestRisky risky test
3+
--FILE--
4+
<?php
5+
$test = realpath(__DIR__.'/FailTests/NoAssertionsTestRisky.php');
6+
passthru('php '.getenv('SYMFONY_SIMPLE_PHPUNIT_BIN_DIR').'/simple-phpunit.php --fail-on-risky --colors=never '.$test);
7+
?>
8+
--EXPECTF--
9+
PHPUnit %s by Sebastian Bergmann and contributors.
10+
11+
%ATesting Symfony\Bridge\PhpUnit\Tests\FailTests\NoAssertionsTestRisky
12+
R. 2 / 2 (100%)
13+
14+
Time: %s, Memory: %s
15+
16+
There was 1 risky test:
17+
18+
1) Symfony\Bridge\PhpUnit\Tests\FailTests\NoAssertionsTestRisky::testOne
19+
This test is annotated with "@doesNotPerformAssertions", but performed 1 assertions
20+
21+
OK, but incomplete, skipped, or risky tests!
22+
Tests: 2, Assertions: 1, Risky: 1.

0 commit comments

Comments
 (0)