Skip to content

[PhpUnitBridge] Fix tests with @doesNotPerformAssertions annotations #40957

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

Merged
merged 1 commit into from
May 7, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public function startTest($test)
}

if ($this->checkNumAssertions) {
$this->checkNumAssertions = $test->getTestResultObject()->isStrictAboutTestsThatDoNotTestAnything() && !$test->doesNotPerformAssertions();
$this->checkNumAssertions = $test->getTestResultObject()->isStrictAboutTestsThatDoNotTestAnything();
}

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

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Tests\FailTests;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;

/**
* This class is deliberately suffixed with *TestRisky.php so that it is ignored
* by PHPUnit. This test is designed to fail. See ../expectrisky.phpt.
*/
final class NoAssertionsTestRisky extends TestCase
{
use ExpectDeprecationTrait;

/**
* Do not remove this test in the next major version.
*
* @group legacy
*/
public function testOne()
{
$this->expectNotToPerformAssertions();
$this->expectDeprecation('foo');
@trigger_error('foo', \E_USER_DEPRECATED);
}

/**
* Do not remove this test in the next major version.
*/
public function testTwo()
{
$this->expectNotToPerformAssertions();
}
}
22 changes: 22 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Tests/expectrisky.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Test NoAssertionsTestRisky risky test
--FILE--
<?php
$test = realpath(__DIR__.'/FailTests/NoAssertionsTestRisky.php');
passthru('php '.getenv('SYMFONY_SIMPLE_PHPUNIT_BIN_DIR').'/simple-phpunit.php --fail-on-risky --colors=never '.$test);
?>
--EXPECTF--
PHPUnit %s by Sebastian Bergmann and contributors.

%ATesting Symfony\Bridge\PhpUnit\Tests\FailTests\NoAssertionsTestRisky
R. 2 / 2 (100%)

Time: %s, Memory: %s

There was 1 risky test:

1) Symfony\Bridge\PhpUnit\Tests\FailTests\NoAssertionsTestRisky::testOne
This test is annotated with "@doesNotPerformAssertions", but performed 1 assertions

OK, but incomplete, skipped, or risky tests!
Tests: 2, Assertions: 1, Risky: 1.