Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use PHPUnit\Framework\TestCase;

class NonAssertingSetUp extends TestCase
{
protected function setUp(): void
{
$this->value = 1;
}

public function testSomething()
{
$value = 5;
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use PHPUnit\Framework\TestCase;

class NonAssertingSetUp extends TestCase
{
protected function setUp(): void
{
$this->value = 1;
}

#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function testSomething()
{
$value = 5;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithNonAssertingMethod;

class ParentTestCallWithoutAssert extends AbstractClassWithNonAssertingMethod
{
public function testSomething()
{
parent::testSomething();
}
}

?>
-----
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithNonAssertingMethod;

class ParentTestCallWithoutAssert extends AbstractClassWithNonAssertingMethod
{
#[\PHPUnit\Framework\Attributes\DoesNotPerformAssertions]
public function testSomething()
{
parent::testSomething();
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use PHPUnit\Framework\TestCase;

class SkipAssertingAfterAnnotation extends TestCase
{
/**
* @after
*/
protected function cleanup(): void
{
$this->assertTrue(true);
}

public function testSomething()
{
$value = 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\TestCase;

class SkipAssertingBeforeAttribute extends TestCase
{
#[Before]
protected function prepare(): void
{
$this->assertTrue(true);
}

public function testSomething()
{
$value = 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use PHPUnit\Framework\TestCase;

class SkipAssertingOwnPostConditions extends TestCase
{
protected function assertPostConditions(): void
{
$this->assertTrue(true);
}

public function testSomething()
{
$value = 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use PHPUnit\Framework\TestCase;

class SkipAssertingSetUp extends TestCase
{
protected function setUp(): void
{
$this->assertTrue(true);
}

public function testSomething()
{
$value = 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithAssertingTearDown;

class SkipAssertingTearDownInParent extends AbstractClassWithAssertingTearDown
{
public function testSomething()
{
$value = 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

class SkipExpectUserDeprecationMessageTest extends \PHPUnit\Framework\TestCase
{
public function testExpectUserDeprecationMessage()
{
$this->expectUserDeprecationMessage('Since foo 1.0: Bar is deprecated.');

@trigger_error('Since foo 1.0: Bar is deprecated.', E_USER_DEPRECATED);
}

public function testExpectUserDeprecationMessageMatches()
{
$this->expectUserDeprecationMessageMatches('#^Since foo 1\.0#');

@trigger_error('Since foo 1.0: Bar is deprecated.', E_USER_DEPRECATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithAssertingMethod;

class SkipParentTestCallWithAssert extends AbstractClassWithAssertingMethod
{
public function testSomething()
{
parent::testSomething();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Fixture;

use Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source\AbstractClassWithAssertingPostConditions;

class SkipPostConditionsParentCall extends AbstractClassWithAssertingPostConditions
{
protected function assertPostConditions(): void
{
parent::assertPostConditions();
}

public function testSomething()
{
$value = 5;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source;

use PHPUnit\Framework\TestCase;

abstract class AbstractClassWithAssertingMethod extends TestCase
{
public function testSomething()
{
$this->assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source;

use PHPUnit\Framework\TestCase;

abstract class AbstractClassWithAssertingPostConditions extends TestCase
{
protected function assertPostConditions(): void
{
$this->assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source;

use PHPUnit\Framework\TestCase;

abstract class AbstractClassWithAssertingTearDown extends TestCase
{
protected function tearDown(): void
{
$this->assertTrue(true);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Rector\PHPUnit\Tests\PHPUnit60\Rector\ClassMethod\AddDoesNotPerformAssertionToNonAssertingTestRector\Source;

use PHPUnit\Framework\TestCase;

abstract class AbstractClassWithNonAssertingMethod extends TestCase
{
public function testSomething()
{
$value = 1 + 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rector\PHPUnit\Enum\PHPUnitAttribute;
use Rector\PHPUnit\Enum\PHPUnitClassName;
use Rector\PHPUnit\NodeAnalyzer\AssertCallAnalyzer;
use Rector\PHPUnit\NodeAnalyzer\LifecycleMethodAssertAnalyzer;
use Rector\PHPUnit\NodeAnalyzer\MockedVariableAnalyzer;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\Rector\AbstractRector;
Expand Down Expand Up @@ -57,6 +58,7 @@ public function __construct(
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly ReflectionResolver $reflectionResolver,
private readonly ReflectionProvider $reflectionProvider,
private readonly LifecycleMethodAssertAnalyzer $lifecycleMethodAssertAnalyzer,
) {
}

Expand Down Expand Up @@ -164,6 +166,11 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
return true;
}

// assertions in lifecycle methods count toward every test of the class
if ($this->lifecycleMethodAssertAnalyzer->hasAssertingLifecycleMethod($classMethod)) {
return true;
}

return $this->mockedVariableAnalyzer->containsMockAsUsedVariable($classMethod);
}

Expand Down
8 changes: 8 additions & 0 deletions src/Enum/PHPUnitAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,12 @@ final class PHPUnitAttribute
public const string ALLOW_MOCK_OBJECTS_WITHOUT_EXPECTATIONS = 'PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations';

public const string DATA_PROVIDER = 'PHPUnit\Framework\Attributes\DataProvider';

public const string BEFORE = 'PHPUnit\Framework\Attributes\Before';

public const string AFTER = 'PHPUnit\Framework\Attributes\After';

public const string PRE_CONDITION = 'PHPUnit\Framework\Attributes\PreCondition';

public const string POST_CONDITION = 'PHPUnit\Framework\Attributes\PostCondition';
}
Loading
Loading