Skip to content

Commit

Permalink
Merge branch '3.x' of github.com:pestphp/pest-plugin-mutate into 3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
nunomaduro committed Sep 5, 2024
2 parents cd48e4d + 784dff9 commit 3bbb7cd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/Support/MutationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ private function doesNotContainClassToMutate(string $contents, array $classesToM
$namespace = preg_quote(implode('\\', $parts));
$classOrNamespace = preg_quote($classOrNamespace);

if (preg_match("/namespace\\s+$namespace/", $contents) === 1 && preg_match("/class\\s+$class.*/", $contents) === 1) {
if (preg_match("/namespace\\s+$namespace/", $contents) === 1 && preg_match("/(?:class|trait)\\s+$class.*/", $contents) === 1) {
return false;
}

if (preg_match("/class\\s+$class\[{\\s*\]/", $contents) === 1) {
if (preg_match("/(?:class|trait)\\s+$class\[{\\s*\]/", $contents) === 1) {
return false;
}

Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/Classes/SizeHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

namespace Tests\Fixtures\Classes;

use Tests\Fixtures\Traits\SizeHelperTrait;

class SizeHelper
{
use SizeHelperTrait;

public static function isBig(int $size): bool
{
return $size >= 100;
Expand Down
13 changes: 13 additions & 0 deletions tests/Fixtures/Traits/SizeHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Tests\Fixtures\Traits;

trait SizeHelperTrait
{
public static function isSmall(int $size): bool
{
return $size < 100;
}
}
20 changes: 20 additions & 0 deletions tests/Unit/MutationGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Finder\SplFileInfo;
use Tests\Fixtures\Classes\AgeHelper;
use Tests\Fixtures\Classes\SizeHelper;
use Tests\Fixtures\Traits\SizeHelperTrait;

beforeEach(function (): void {
$this->generator = new MutationGenerator;
Expand Down Expand Up @@ -102,6 +103,25 @@ classesToMutate: $classes,
[[SizeHelper::class, AgeHelper::class], 2],
]);

it('generates mutations for the given file if it contains the given trait', function (array $classes, int $expectedCount): void {
$mutations = $this->generator->generate(
file: new SplFileInfo(dirname(__DIR__).'/Fixtures/Traits/SizeHelperTrait.php', '', ''),
mutators: [SmallerToSmallerOrEqual::class],
classesToMutate: $classes,
);

expect($mutations)
->toBeArray()
->toHaveCount($expectedCount);
})->with([
[[SizeHelperTrait::class], 1],
[[SizeHelper::class], 0],
[[SizeHelperTrait::class, SizeHelper::class], 1],
[['SizeHelperTrait'], 1],
[['Tests\\Fixtures\\Traits\\SizeHelperTrai'], 1],
[['Invalid\\Namespace\\SizeHelperTrait'], 0],
]);

it('ignores lines with the ignore annotation', function (): void {
$mutations = ($this->generate)(<<<'PHP'
<?php
Expand Down
44 changes: 22 additions & 22 deletions tests/Unit/Support/FileFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

it('finds all files in a directory', function (): void {
expect(FileFinder::files(['tests/Fixtures'], []))
->toHaveCount(2)
->getIterator()->current()->getRealPath()->toEndWith('AgeHelper.php');
->toHaveCount(3)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelperTrait.php');

expect(FileFinder::files([getcwd().'/tests/Fixtures'], []))
->toHaveCount(2)
->getIterator()->current()->getRealPath()->toEndWith('AgeHelper.php');
->toHaveCount(3)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelperTrait.php');
});

it('finds files by path', function (): void {
Expand All @@ -22,71 +22,71 @@

it('excludes a file by full path', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['tests/Fixtures/Classes/AgeHelper.php']))
->toHaveCount(1)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelper.php');
->toHaveCount(2)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelperTrait.php');
});

it('excludes a file by relative path', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['Classes/AgeHelper.php']))
->toHaveCount(1)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelper.php');
->toHaveCount(2)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelperTrait.php');
});

it('excludes a file by pattern', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['Classes/Age*.php']))
->toHaveCount(1)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelper.php');
->toHaveCount(2)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelperTrait.php');
});

it('excludes a file by pattern ending with an asterisk', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['Classes/Age*']))
->toHaveCount(1)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelper.php');
->toHaveCount(2)
->getIterator()->current()->getRealPath()->toEndWith('SizeHelperTrait.php');
});

it('does not exclude files with an incomplete path', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['Classes/Age']))
->toHaveCount(2);
->toHaveCount(3);
});

it('excludes a directory by full path', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['tests/Fixtures/Classes']))
expect(FileFinder::files(['tests/Fixtures'], ['tests/Fixtures/Classes', 'tests/Fixtures/Traits']))
->toBeEmpty();
});

it('excludes a directory by relative path', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['Classes']))
expect(FileFinder::files(['tests/Fixtures'], ['Classes', 'Traits']))
->toBeEmpty();

expect(FileFinder::files(['tests/Fixtures'], ['Classes/']))
expect(FileFinder::files(['tests/Fixtures'], ['Classes/', 'Traits/']))
->toBeEmpty();
});

it('excludes a directory by pattern', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['tests/*/Classes']))
expect(FileFinder::files(['tests/Fixtures'], ['tests/*/Classes', 'tests/*/Traits']))
->toBeEmpty();
});

it('excludes a directory by pattern with multiple wildcards', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['*/*/Classes']))
expect(FileFinder::files(['tests/Fixtures'], ['*/*/Classes', '*/*/Traits']))
->toBeEmpty();
});

it('excludes a directory by pattern with double asterisk wildcard', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['**/Classes']))
expect(FileFinder::files(['tests/Fixtures'], ['**/Classes', '**/Traits']))
->toBeEmpty();
});

it('excludes by absolute path', function (): void {
expect(FileFinder::files(['tests/Fixtures'], ['/tests/Fixtures/Classes']))
expect(FileFinder::files(['tests/Fixtures'], ['/tests/Fixtures/Classes', '/tests/Fixtures/Traits']))
->toBeEmpty();

expect(FileFinder::files(['tests/Fixtures'], [getcwd().'/tests/Fixtures/Classes']))
expect(FileFinder::files(['tests/Fixtures'], [getcwd().'/tests/Fixtures/Classes', getcwd().'/tests/Fixtures/Traits']))
->toBeEmpty();

expect(FileFinder::files(['tests/Fixtures'], ['/tests/Fixtures/Invalid']))
->not->toBeEmpty();

expect(FileFinder::files(['tests/Fixtures'], ['/tests/Fixtures/Classes/AgeHelper.php']))
->toHaveCount(1);
->toHaveCount(2);
});

0 comments on commit 3bbb7cd

Please sign in to comment.