VersionSpecificCodeSampleTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. use Prophecy\Prophecy;
  17. /**
  18. * @author Andreas Möller <am@localheinz.com>
  19. *
  20. * @internal
  21. *
  22. * @covers \PhpCsFixer\FixerDefinition\VersionSpecificCodeSample
  23. */
  24. final class VersionSpecificCodeSampleTest extends TestCase
  25. {
  26. public function testConstructorSetsValues(): void
  27. {
  28. $code = '<php echo $foo;';
  29. $configuration = [
  30. 'foo' => 'bar',
  31. ];
  32. $codeSample = new VersionSpecificCodeSample(
  33. $code,
  34. $this->createVersionSpecificationMock()->reveal(),
  35. $configuration
  36. );
  37. self::assertSame($code, $codeSample->getCode());
  38. self::assertSame($configuration, $codeSample->getConfiguration());
  39. }
  40. public function testConfigurationDefaultsToNull(): void
  41. {
  42. $codeSample = new VersionSpecificCodeSample(
  43. '<php echo $foo;',
  44. $this->createVersionSpecificationMock()->reveal()
  45. );
  46. self::assertNull($codeSample->getConfiguration());
  47. }
  48. /**
  49. * @dataProvider provideIsSuitableForUsesVersionSpecificationCases
  50. */
  51. public function testIsSuitableForUsesVersionSpecification(int $version, bool $isSatisfied): void
  52. {
  53. $versionSpecification = $this->createVersionSpecificationMock();
  54. $versionSpecification
  55. ->isSatisfiedBy($version)
  56. ->willReturn($isSatisfied)
  57. ;
  58. $codeSample = new VersionSpecificCodeSample(
  59. '<php echo $foo;',
  60. $versionSpecification->reveal()
  61. );
  62. self::assertSame($isSatisfied, $codeSample->isSuitableFor($version));
  63. }
  64. public static function provideIsSuitableForUsesVersionSpecificationCases(): iterable
  65. {
  66. yield 'is-satisfied' => [\PHP_VERSION_ID, true];
  67. yield 'is-not-satisfied' => [\PHP_VERSION_ID, false];
  68. }
  69. /**
  70. * @return Prophecy\ObjectProphecy|VersionSpecificationInterface
  71. */
  72. private function createVersionSpecificationMock()
  73. {
  74. return $this->prophesize(\PhpCsFixer\FixerDefinition\VersionSpecificationInterface::class);
  75. }
  76. }