VersionSpecificCodeSampleTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\FixerDefinition;
  13. use PhpCsFixer\FixerDefinition\VersionSpecificationInterface;
  14. use PhpCsFixer\FixerDefinition\VersionSpecificCodeSample;
  15. use PhpCsFixer\Tests\TestCase;
  16. /**
  17. * @author Andreas Möller <am@localheinz.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\FixerDefinition\VersionSpecificCodeSample
  22. */
  23. final class VersionSpecificCodeSampleTest extends TestCase
  24. {
  25. public function testConstructorSetsValues(): void
  26. {
  27. $code = '<php echo $foo;';
  28. $configuration = [
  29. 'foo' => 'bar',
  30. ];
  31. $codeSample = new VersionSpecificCodeSample(
  32. $code,
  33. $this->createVersionSpecificationDouble(),
  34. $configuration
  35. );
  36. self::assertSame($code, $codeSample->getCode());
  37. self::assertSame($configuration, $codeSample->getConfiguration());
  38. }
  39. public function testConfigurationDefaultsToNull(): void
  40. {
  41. $codeSample = new VersionSpecificCodeSample(
  42. '<php echo $foo;',
  43. $this->createVersionSpecificationDouble()
  44. );
  45. self::assertNull($codeSample->getConfiguration());
  46. }
  47. /**
  48. * @dataProvider provideIsSuitableForUsesVersionSpecificationCases
  49. */
  50. public function testIsSuitableForUsesVersionSpecification(int $version, bool $isSatisfied): void
  51. {
  52. $codeSample = new VersionSpecificCodeSample(
  53. '<php echo $foo;',
  54. $this->createVersionSpecificationDouble($isSatisfied)
  55. );
  56. self::assertSame($isSatisfied, $codeSample->isSuitableFor($version));
  57. }
  58. /**
  59. * @return iterable<string, array{int, bool}>
  60. */
  61. public static function provideIsSuitableForUsesVersionSpecificationCases(): iterable
  62. {
  63. yield 'is-satisfied' => [100, true];
  64. yield 'is-not-satisfied' => [100, false];
  65. }
  66. private function createVersionSpecificationDouble(bool $isSatisfied = true): VersionSpecificationInterface
  67. {
  68. return new class($isSatisfied) implements VersionSpecificationInterface {
  69. private bool $isSatisfied;
  70. public function __construct(bool $isSatisfied)
  71. {
  72. $this->isSatisfied = $isSatisfied;
  73. }
  74. public function isSatisfiedBy(int $version): bool
  75. {
  76. return $this->isSatisfied;
  77. }
  78. };
  79. }
  80. }