Skip to content

Commit

Permalink
[DX] Make use of Laravel container - step #14 (#4709)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Aug 7, 2023
1 parent 58f3e0c commit fe9d91c
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 66 deletions.
38 changes: 24 additions & 14 deletions packages-tests/Skipper/Skipper/Skip/SkipSkipperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,49 @@

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rector\Core\Kernel\RectorKernel;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Skipper\Skipper\Skipper;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\Tests\Skipper\Skipper\Skip\Source\AnotherClassToSkip;
use Rector\Tests\Skipper\Skipper\Skip\Source\NotSkippedClass;
use Rector\Tests\Skipper\Skipper\Skip\Source\SomeClassToSkip;

final class SkipSkipperTest extends TestCase
final class SkipSkipperTest extends AbstractLazyTestCase
{
private Skipper $skipper;

protected function setUp(): void
{
$rectorKernel = new RectorKernel();
$containerBuilder = $rectorKernel->createFromConfigs([__DIR__ . '/config/config.php']);
SimpleParameterProvider::setParameter(Option::SKIP, [
// classes
SomeClassToSkip::class,

$this->skipper = $containerBuilder->get(Skipper::class);
// classes only in specific paths
AnotherClassToSkip::class => ['Fixture/someFile', '*/someDirectory/*'],

// file paths
__DIR__ . '/Fixture/AlwaysSkippedPath',
'*\PathSkippedWithMask\*',
]);

$this->skipper = $this->make(Skipper::class);
}

protected function tearDown(): void
{
// null the parameter
SimpleParameterProvider::setParameter(Option::SKIP, []);
}

#[DataProvider('provideCheckerAndFile')]
#[DataProvider('provideAnythingAndFilePath')]
#[DataProvider('provideFilePath')]
public function test(string $element, string $filePath, bool $expectedSkip): void
{
$resolvedSkip = $this->skipper->shouldSkipElementAndFilePath($element, $filePath);
$this->assertSame($expectedSkip, $resolvedSkip);
}

/**
* @return Iterator<string[]|bool[]|class-string<AnotherClassToSkip>[]|class-string<NotSkippedClass>[]|class-string<SomeClassToSkip>[]>
*/
public static function provideCheckerAndFile(): Iterator
{
yield [SomeClassToSkip::class, __DIR__ . '/Fixture', true];
Expand All @@ -48,10 +61,7 @@ public static function provideCheckerAndFile(): Iterator
yield [NotSkippedClass::class, __DIR__ . '/Fixture/someOtherFile', false];
}

/**
* @return Iterator<string[]|bool[]>
*/
public static function provideAnythingAndFilePath(): Iterator
public static function provideFilePath(): Iterator
{
yield ['anything', __DIR__ . '/Fixture/AlwaysSkippedPath/some_file.txt', true];
yield ['anything', __DIR__ . '/Fixture/PathSkippedWithMask/another_file.txt', true];
Expand Down
21 changes: 0 additions & 21 deletions packages-tests/Skipper/Skipper/Skip/config/config.php

This file was deleted.

28 changes: 22 additions & 6 deletions packages-tests/Skipper/Skipper/Skipper/SkipperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,39 @@

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Rector\Core\Kernel\RectorKernel;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Skipper\Skipper\Skipper;
use Rector\Testing\PHPUnit\AbstractLazyTestCase;
use Rector\Tests\Skipper\Skipper\Skipper\Fixture\Element\FifthElement;
use Rector\Tests\Skipper\Skipper\Skipper\Fixture\Element\SixthSense;
use Rector\Tests\Skipper\Skipper\Skipper\Fixture\Element\ThreeMan;

final class SkipperTest extends TestCase
final class SkipperTest extends AbstractLazyTestCase
{
private Skipper $skipper;

protected function setUp(): void
{
$rectorKernel = new RectorKernel();
$containerBuilder = $rectorKernel->createFromConfigs([__DIR__ . '/config/config.php']);
SimpleParameterProvider::setParameter(Option::SKIP, [
// windows like path
'*\SomeSkipped\*',

$this->skipper = $containerBuilder->get(Skipper::class);
__DIR__ . '/Fixture/SomeSkippedPath',
__DIR__ . '/Fixture/SomeSkippedPathToFile/any.txt',

// elements
FifthElement::class,
SixthSense::class,
]);

$this->skipper = $this->make(Skipper::class);
}

protected function tearDown(): void
{
// cleanup configuration
SimpleParameterProvider::setParameter(Option::SKIP, []);
}

#[DataProvider('provideDataShouldSkipFileInfo')]
Expand Down
21 changes: 0 additions & 21 deletions packages-tests/Skipper/Skipper/Skipper/config/config.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ public function __construct(
*/
public function resolve(): array
{
if ($this->skippedClasses !== []) {
// skip cache in tests
if ($this->skippedClasses !== [] && ! defined('PHPUNIT_COMPOSER_INSTALL')) {
return $this->skippedClasses;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public function __construct(
*/
public function resolve(): array
{
if ($this->skippedPaths !== []) {
// disable cache in tests
if ($this->skippedPaths !== [] && ! defined('PHPUNIT_COMPOSER_INSTALL')) {
return $this->skippedPaths;
}

Expand Down
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -707,3 +707,5 @@ parameters:

# false positive
- '#Parameter \#1 \$commands of method Symfony\\Component\\Console\\Application\:\:addCommands\(\) expects array<Symfony\\Component\\Console\\Command\\Command>, iterable<Symfony\\Component\\Console\\Command\\Command> given#'

- '#Property Rector\\Core\\Console\\Command\\ListRulesCommand\:\:\$rectors \(array<Rector\\Core\\Contract\\Rector\\RectorInterface>\) does not accept array\|iterable<Rector\\Core\\Contract\\Rector\\RectorInterface>#'
4 changes: 2 additions & 2 deletions src/Console/Command/ListRulesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ public function __construct(
) {
parent::__construct();
if ($rectors instanceof RewindableGenerator) {
$rectors = $rectors->getIterator();
$rectors = iterator_to_array($rectors->getIterator());
}

$this->rectors = iterator_to_array($rectors);
$this->rectors = $rectors;
}

protected function configure(): void
Expand Down

0 comments on commit fe9d91c

Please sign in to comment.