|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\CodeStyle\PhpCsFixer; |
| 10 | + |
| 11 | +use Ibexa\CodeStyle\PhpCsFixer\InternalConfigFactory; |
| 12 | +use Ibexa\CodeStyle\PhpCsFixer\Sets\Ibexa46RuleSet; |
| 13 | +use Ibexa\CodeStyle\PhpCsFixer\Sets\Ibexa50RuleSet; |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use ReflectionClass; |
| 16 | +use ReflectionMethod; |
| 17 | + |
| 18 | +/** |
| 19 | + * @covers \Ibexa\CodeStyle\PhpCsFixer\InternalConfigFactory |
| 20 | + */ |
| 21 | +final class InternalConfigFactoryTest extends TestCase |
| 22 | +{ |
| 23 | + private InternalConfigFactory $factory; |
| 24 | + |
| 25 | + private ReflectionMethod $createRuleSetFromPackage; |
| 26 | + |
| 27 | + protected function setUp(): void |
| 28 | + { |
| 29 | + $this->factory = new InternalConfigFactory(); |
| 30 | + $reflection = new ReflectionClass(InternalConfigFactory::class); |
| 31 | + |
| 32 | + // This method is private because we don't want it to be part of the public API. |
| 33 | + // We can't test getRuleSet since it internally uses InstalledVersions::getRootPackage(), which cannot be mocked. |
| 34 | + $this->createRuleSetFromPackage = $reflection->getMethod('createRuleSetFromPackage'); |
| 35 | + $this->createRuleSetFromPackage->setAccessible(true); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @dataProvider provideRuleSetTestCases |
| 40 | + */ |
| 41 | + public function testVersionBasedRuleSetSelection(array $package, string $expectedRuleSetClass): void |
| 42 | + { |
| 43 | + $ruleSet = $this->createRuleSetFromPackage->invoke($this->factory, $package); |
| 44 | + |
| 45 | + self::assertInstanceOf($expectedRuleSetClass, $ruleSet); |
| 46 | + } |
| 47 | + |
| 48 | + public function provideRuleSetTestCases(): array |
| 49 | + { |
| 50 | + return [ |
| 51 | + 'non_ibexa_package' => [ |
| 52 | + ['name' => 'vendor/package', 'version' => '1.0.0'], |
| 53 | + Ibexa46RuleSet::class, |
| 54 | + ], |
| 55 | + 'ibexa_package_4_6' => [ |
| 56 | + ['name' => 'ibexa/core', 'version' => '4.6.0'], |
| 57 | + Ibexa46RuleSet::class, |
| 58 | + ], |
| 59 | + 'ibexa_package_5_0' => [ |
| 60 | + ['name' => 'ibexa/core', 'version' => '5.0.0'], |
| 61 | + Ibexa50RuleSet::class, |
| 62 | + ], |
| 63 | + 'ibexa_package_5_1' => [ |
| 64 | + ['name' => 'ibexa/core', 'version' => '5.1.0'], |
| 65 | + Ibexa50RuleSet::class, |
| 66 | + ], |
| 67 | + 'ibexa_package_dev_master' => [ |
| 68 | + ['name' => 'ibexa/core', 'version' => 'dev-master'], |
| 69 | + Ibexa50RuleSet::class, |
| 70 | + ], |
| 71 | + 'ibexa_package_with_pretty_version' => [ |
| 72 | + ['name' => 'ibexa/core', 'version' => '5.0.0', 'pretty_version' => '5.0.0-alpha1'], |
| 73 | + Ibexa50RuleSet::class, |
| 74 | + ], |
| 75 | + 'ibexa_package_wildcard' => [ |
| 76 | + ['name' => 'ibexa/core', 'version' => '*'], |
| 77 | + Ibexa50RuleSet::class, |
| 78 | + ], |
| 79 | + 'ibexa_package_4_6_with_suffix' => [ |
| 80 | + ['name' => 'ibexa/core', 'version' => '4.6.0-beta1'], |
| 81 | + Ibexa46RuleSet::class, |
| 82 | + ], |
| 83 | + ]; |
| 84 | + } |
| 85 | + |
| 86 | + public function testWithRuleSet(): void |
| 87 | + { |
| 88 | + $customRuleSet = new Ibexa46RuleSet(); |
| 89 | + $this->factory->withRuleSet($customRuleSet); |
| 90 | + |
| 91 | + self::assertSame($customRuleSet, $this->factory->getRuleSet()); |
| 92 | + } |
| 93 | +} |
0 commit comments