Skip to content

Commit 6dfea2b

Browse files
[PHPUnit12] Drop ConstraintValidatorTestCase from AllowMockObjectsWhereParentClassRector triggers (#678)
* [PHPUnit12] Add fixture reproducing AllowMockObjects false positive on ConstraintValidatorTestCase Refs rectorphp/rector#9650 * [PHPUnit12] Drop ConstraintValidatorTestCase from AllowMockObjectsWhereParentClassRector triggers The Symfony fix in symfony/symfony#62745 (shipped in symfony/validator 6.4.31 / 7.3.9 / 7.4.3 / 8.0.3) removed the underlying need for the attribute on this lineage. Rule now fires only on TypeTestCase. Includes: code sample updated to TypeTestCase, orphaned enum constant removed, existing fixture converted to TypeTestCase, stub added so the converted fixture autoloads, and a transitive-inheritance skip fixture. Closes rectorphp/rector#9650 --------- Co-authored-by: Aleksei Kankov <akankov@gmail.com>
1 parent 635e5f1 commit 6dfea2b

6 files changed

Lines changed: 42 additions & 14 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWhereParentClassRector\Fixture;
4+
5+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
6+
7+
final class SkipConstraintValidatorTestCase extends ConstraintValidatorTestCase
8+
{
9+
private \PHPUnit\Framework\MockObject\MockObject $someMock;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWhereParentClassRector\Fixture;
4+
5+
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
6+
7+
abstract class IntermediateConstraintValidatorBase extends ConstraintValidatorTestCase
8+
{
9+
}
10+
11+
final class SkipConstraintValidatorTestCaseViaIntermediate extends IntermediateConstraintValidatorBase
12+
{
13+
private \PHPUnit\Framework\MockObject\MockObject $someMock;
14+
}

rules-tests/PHPUnit120/Rector/Class_/AllowMockObjectsWhereParentClassRector/Fixture/some_class.php.inc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWhereParentClassRector\Fixture;
44

5-
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
5+
use Symfony\Component\Form\Test\TypeTestCase;
66

7-
final class SomeClass extends ConstraintValidatorTestCase
7+
final class SomeClass extends TypeTestCase
88
{
99
private \PHPUnit\Framework\MockObject\MockObject $someMock;
1010
}
@@ -15,10 +15,10 @@ final class SomeClass extends ConstraintValidatorTestCase
1515

1616
namespace Rector\PHPUnit\Tests\PHPUnit120\Rector\Class_\AllowMockObjectsWhereParentClassRector\Fixture;
1717

18-
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
18+
use Symfony\Component\Form\Test\TypeTestCase;
1919

2020
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
21-
final class SomeClass extends ConstraintValidatorTestCase
21+
final class SomeClass extends TypeTestCase
2222
{
2323
private \PHPUnit\Framework\MockObject\MockObject $someMock;
2424
}

rules/PHPUnit120/Rector/Class_/AllowMockObjectsWhereParentClassRector.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ final class AllowMockObjectsWhereParentClassRector extends AbstractRector
3030
/**
3131
* @var string[]
3232
*/
33-
private const array PARENT_CLASSES = [
34-
PHPUnitClassName::SYMFONY_CONSTRAINT_VALIDATOR_TEST_CASE,
35-
PHPUnitClassName::SYMFONY_TYPE_TEST_CASE,
36-
];
33+
private const array PARENT_CLASSES = [PHPUnitClassName::SYMFONY_TYPE_TEST_CASE];
3734

3835
public function __construct(
3936
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
@@ -75,18 +72,18 @@ public function getRuleDefinition(): RuleDefinition
7572
[
7673
new CodeSample(
7774
<<<'CODE_SAMPLE'
78-
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
75+
use Symfony\Component\Form\Test\TypeTestCase;
7976
80-
final class SomeTest extends ConstraintValidatorTestCase
77+
final class SomeTest extends TypeTestCase
8178
{
8279
}
8380
CODE_SAMPLE
8481
,
8582
<<<'CODE_SAMPLE'
86-
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
83+
use Symfony\Component\Form\Test\TypeTestCase;
8784
8885
#[\PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations]
89-
final class SomeTest extends ConstraintValidatorTestCase
86+
final class SomeTest extends TypeTestCase
9087
{
9188
}
9289
CODE_SAMPLE

src/Enum/PHPUnitClassName.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ final class PHPUnitClassName
2727

2828
public const string STUB = 'PHPUnit\Framework\MockObject\Stub';
2929

30-
public const string SYMFONY_CONSTRAINT_VALIDATOR_TEST_CASE = 'Symfony\Component\Validator\Test\ConstraintValidatorTestCase';
31-
3230
public const string SYMFONY_TYPE_TEST_CASE = 'Symfony\Component\Form\Test\TypeTestCase';
3331

3432
/**
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Symfony\Component\Form\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
abstract class TypeTestCase extends TestCase
8+
{
9+
}

0 commit comments

Comments
 (0)