Skip to content
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 @@ -36,9 +36,9 @@
namespace Infection\Tests\Architecture\PHPat\Selector;

use Infection\Tests\Architecture\PHPat\Selector\Support\ConcreteClassReflection;
use Infection\Tests\Architecture\PHPat\Selector\Support\PHPUnitTestClassAnalysis;
use PHPat\Selector\SelectorInterface;
use PHPStan\Reflection\ClassReflection;
use PHPUnit\Framework\TestCase;
use function str_ends_with;

final class ConcretePHPUnitTestClass implements SelectorInterface
Expand All @@ -52,6 +52,6 @@ public function matches(ClassReflection $classReflection): bool
{
return ConcreteClassReflection::isConcreteClass($classReflection)
&& str_ends_with($classReflection->getName(), 'Test')
&& $classReflection->isSubclassOf(TestCase::class);
&& PHPUnitTestClassAnalysis::isPHPUnitTestCase($classReflection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

namespace Infection\Tests\Architecture\PHPat\Selector;

use Infection\Tests\Architecture\PHPat\Selector\Support\PHPUnitTestClassAnalysis;
use Infection\Tests\Architecture\PHPat\Selector\Support\PHPUnitTestIoRequirements;
use PHPat\Selector\SelectorInterface;
use PHPStan\Reflection\ClassReflection;
Expand All @@ -57,6 +58,6 @@ public function matches(ClassReflection $classReflection): bool
&& InfectionSelector::concretePHPUnitTestClass()->matches($classReflection)
&& $this->ioRequirements->hasCoveredClass($classReflection)
&& !$this->ioRequirements->requiresIntegrationGroup($classReflection)
&& $this->ioRequirements->hasIntegrationGroup($classReflection);
&& PHPUnitTestClassAnalysis::belongsToIntegrationGroup($classReflection);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

namespace Infection\Tests\Architecture\PHPat\Selector;

use Infection\Tests\Architecture\PHPat\Selector\Support\PHPUnitTestClassAnalysis;
use Infection\Tests\Architecture\PHPat\Selector\Support\PHPUnitTestIoRequirements;
use PHPat\Selector\SelectorInterface;
use PHPStan\Reflection\ClassReflection;
Expand All @@ -56,6 +57,6 @@ public function matches(ClassReflection $classReflection): bool
return InfectionSelector::phpunitTestCode()->matches($classReflection)
&& InfectionSelector::concretePHPUnitTestClass()->matches($classReflection)
&& $this->ioRequirements->requiresIntegrationGroup($classReflection)
&& !$this->ioRequirements->hasIntegrationGroup($classReflection);
&& !PHPUnitTestClassAnalysis::belongsToIntegrationGroup($classReflection);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Tests\Architecture\PHPat\Selector\Support;

use PHPStan\BetterReflection\Reflection\Adapter\ReflectionAttribute;
use PHPStan\Reflection\ClassReflection;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

final class PHPUnitTestClassAnalysis
{
private const string GROUP_NAME = 'integration';

public static function isPHPUnitTestCase(ClassReflection $classReflection): bool
{
return $classReflection->isSubclassOf(TestCase::class);
}

public static function belongsToIntegrationGroup(ClassReflection $classReflection): bool
{
foreach (self::getAttributes($classReflection) as $groupAttribute) {
if (self::isIntegrationGroup($groupAttribute)) {
return true;
}
}

return false;
}

/**
* @see Group
*/
private static function isIntegrationGroup(ReflectionAttribute $attribute): bool
{
if ($attribute->getName() !== Group::class) {
return false;
}

$arguments = $attribute->getArguments();
$groupName = $arguments[0] ?? $arguments['name'] ?? null;

return $groupName === self::GROUP_NAME;
}

/**
* @return iterable<ReflectionAttribute>
*/
private static function getAttributes(ClassReflection $classReflection): iterable
{
$attributes = $classReflection->getNativeReflection()->getAttributes();

foreach ($attributes as $attribute) {
if ($attribute instanceof ReflectionAttribute) {
yield $attribute;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,13 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use function str_starts_with;

final class PHPUnitTestIoRequirements
{
private const string COVERS_ATTRIBUTE_NAMESPACE = 'PHPUnit\\Framework\\Attributes\\Covers';

private const string GROUP_NAME = 'integration';

/**
* @var array<class-string, bool>
*/
Expand Down Expand Up @@ -89,17 +86,6 @@ public function hasCoveredClass(ClassReflection $testCaseReflection): bool
return $this->getCoveredClassNames($testCaseReflection) !== null;
}

public function hasIntegrationGroup(ClassReflection $classReflection): bool
{
foreach (self::getAttributes($classReflection) as $groupAttribute) {
if (self::isIntegrationGroup($groupAttribute)) {
return true;
}
}

return false;
}

/**
* Check the test case code.
*/
Expand Down Expand Up @@ -217,21 +203,6 @@ private function classUsesIo(ClassReflection $classReflection): bool
&& $this->analyser->analyse($classReflection, analyseNonConcreteClasses: true)->usesIo;
}

/**
* @see Group
*/
private static function isIntegrationGroup(ReflectionAttribute $attribute): bool
{
if ($attribute->getName() !== Group::class) {
return false;
}

$arguments = $attribute->getArguments();
$groupName = $arguments[0] ?? $arguments['name'] ?? null;

return $groupName === self::GROUP_NAME;
}

/**
* @return iterable<ReflectionAttribute>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Tests\Architecture\PHPat\Selector\Support\Fixtures;

use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;

#[Group('unit')]
final class PHPUnitTestWithUnitGroupFixture extends TestCase
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* This code is licensed under the BSD 3-Clause License.
*
* Copyright (c) 2017, Maks Rafalko
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

declare(strict_types=1);

namespace Infection\Tests\Architecture\PHPat\Selector\Support;

use Infection\Command\ConfigureCommand;
use Infection\Tests\Architecture\PHPat\Selector\PHPUnitTestNotRequiringIoWithIntegrationGroup\Fixtures\FixtureWithCoveredClassWithoutIoAndIntegrationGroupTest;
use Infection\Tests\Architecture\PHPat\Selector\SelectorTestCase;
use Infection\Tests\Architecture\PHPat\Selector\Support\Fixtures\PHPUnitTestWithUnitGroupFixture;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;

#[CoversClass(PHPUnitTestClassAnalysis::class)]
final class PHPUnitTestClassAnalysisTest extends SelectorTestCase
{
/**
* @param class-string $className
*/
#[DataProvider('phpUnitTestCaseProvider')]
public function test_it_detects_phpunit_test_cases(
string $className,
bool $expected,
): void {
$actual = PHPUnitTestClassAnalysis::isPHPUnitTestCase(
$this->createClassReflection($className),
);

$this->assertSame($expected, $actual);
}

public static function phpUnitTestCaseProvider(): iterable
{
yield 'PHPUnit test case' => [
self::class,
true,
];

yield 'source class' => [
ConfigureCommand::class,
false,
];
}

/**
* @param class-string $className
*/
#[DataProvider('integrationGroupProvider')]
public function test_it_detects_integration_group(
string $className,
bool $expected,
): void {
$actual = PHPUnitTestClassAnalysis::belongsToIntegrationGroup(
$this->createClassReflection($className),
);

$this->assertSame($expected, $actual);
}

public static function integrationGroupProvider(): iterable
{
yield 'positional integration group' => [
FixtureWithCoveredClassWithoutIoAndIntegrationGroupTest::class,
true,
];

yield 'non-integration group' => [
PHPUnitTestWithUnitGroupFixture::class,
false,
];

yield 'no group' => [
self::class,
false,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function test_it_detects_phpunit_test_io_requirements(
);
$this->assertSame(
$expectedHasIntegrationGroup,
$ioRequirements->hasIntegrationGroup($classReflection),
PHPUnitTestClassAnalysis::belongsToIntegrationGroup($classReflection),
);
}

Expand Down
Loading