Skip to content

Commit 6f092cc

Browse files
committed
Add configured tests
1 parent f2f732c commit 6f092cc

File tree

6 files changed

+94
-3
lines changed

6 files changed

+94
-3
lines changed

src/Rector/Class_/AddHasFactoryToModelsRector.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Rector\Reflection\ReflectionResolver;
1616
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1717
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
18+
use Webmozart\Assert\Assert;
1819

1920
/**
2021
* @changelog https://github.com/laravel/framework/pull/39310
@@ -26,7 +27,7 @@ final class AddHasFactoryToModelsRector extends AbstractRector implements Config
2627
private const string TRAIT_NAME = 'Illuminate\Database\Eloquent\Factories\HasFactory';
2728

2829
/**
29-
* @var mixed[]
30+
* @var string[]
3031
*/
3132
private array $allowList = [];
3233

@@ -61,6 +62,7 @@ class User extends Model
6162

6263
public function configure(array $configuration): void
6364
{
65+
Assert::allString($configuration);
6466
$this->allowList = $configuration;
6567
}
6668

@@ -91,11 +93,11 @@ public function refactor(Node $node): ?Node
9193
private function shouldSkipClass(Class_ $class): bool
9294
{
9395
if (! $this->isObjectType($class, new ObjectType('Illuminate\Database\Eloquent\Model'))) {
94-
return null;
96+
return false;
9597
}
9698

9799
if ($this->allowList !== [] && ! $this->isNames($class, $this->allowList)) {
98-
return false;
100+
return true;
99101
}
100102

101103
$classReflection = $this->reflectionResolver->resolveClassReflection($class);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace RectorLaravel\Tests\Rector\Class_\AddHasFactoryToModelsRectorConfigured;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class AddHasFactoryToModelsRectorConfiguredTest extends AbstractRectorTestCase
12+
{
13+
public static function provideData(): Iterator
14+
{
15+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
16+
}
17+
18+
/**
19+
* @test
20+
*/
21+
#[DataProvider('provideData')]
22+
public function test(string $filePath): void
23+
{
24+
$this->doTestFile($filePath);
25+
}
26+
27+
public function provideConfigFilePath(): string
28+
{
29+
return __DIR__ . '/config/configured_rule.php';
30+
}
31+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\Class_\AddHasFactoryToModelsRectorConfigured\Fixture;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class User extends Model
8+
{
9+
}
10+
11+
?>
12+
-----
13+
<?php
14+
15+
namespace RectorLaravel\Tests\Rector\Class_\AddHasFactoryToModelsRectorConfigured\Fixture;
16+
17+
use Illuminate\Database\Eloquent\Model;
18+
19+
class User extends Model
20+
{
21+
use \Illuminate\Database\Eloquent\Factories\HasFactory;
22+
}
23+
24+
?>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\Class_\AddHasFactoryToModelsRectorConfigured\Fixture;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class Person extends Model {}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace RectorLaravel\Tests\Rector\Class_\AddHasFactoryToModelsRectorConfigured\Fixture;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
class SkipIfAlreadyPresent extends Model
8+
{
9+
use \Illuminate\Database\Eloquent\Factories\HasFactory;
10+
}
11+
12+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use RectorLaravel\Rector\Class_\AddHasFactoryToModelsRector;
7+
8+
return static function (RectorConfig $rectorConfig): void {
9+
$rectorConfig->import(__DIR__ . '/../../../../../config/config.php');
10+
11+
$rectorConfig->ruleWithConfiguration(AddHasFactoryToModelsRector::class, [
12+
'RectorLaravel\Tests\Rector\Class_\AddHasFactoryToModelsRectorConfigured\Fixture\User',
13+
'RectorLaravel\Tests\Rector\Class_\AddHasFactoryToModelsRectorConfigured\Fixture\SkipIfAlreadyPresent',
14+
]);
15+
};

0 commit comments

Comments
 (0)