UnifiedDifferTest.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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\Differ;
  13. use PhpCsFixer\Differ\UnifiedDiffer;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Differ\UnifiedDiffer
  18. */
  19. final class UnifiedDifferTest extends AbstractDifferTestCase
  20. {
  21. public function testDiffReturnsDiff(): void
  22. {
  23. $differ = new UnifiedDiffer();
  24. $file = __FILE__;
  25. $diff = '--- '.$file.'
  26. +++ '.$file.'
  27. @@ -2,7 +2,7 @@
  28. '.'
  29. function baz($options)
  30. {
  31. - if (!array_key_exists("foo", $options)) {
  32. + if (!\array_key_exists("foo", $options)) {
  33. throw new \InvalidArgumentException();
  34. }
  35. '.'
  36. ';
  37. self::assertSame($diff, $differ->diff($this->oldCode(), $this->newCode(), new \SplFileInfo($file)));
  38. }
  39. public function testDiffAddsQuotes(): void
  40. {
  41. $differ = new UnifiedDiffer();
  42. self::assertSame(
  43. '--- "test test test.txt"
  44. +++ "test test test.txt"
  45. @@ -1 +1 @@
  46. -a
  47. +b
  48. ',
  49. $differ->diff("a\n", "b\n", $this->createSplFileInfoDouble('/foo/bar/test test test.txt'))
  50. );
  51. }
  52. public function testDiffWithoutFile(): void
  53. {
  54. $differ = new UnifiedDiffer();
  55. self::assertSame(
  56. '--- Original
  57. +++ New
  58. @@ -1 +1 @@
  59. -a
  60. \ No newline at end of file
  61. +b
  62. \ No newline at end of file
  63. ',
  64. $differ->diff('a', 'b')
  65. );
  66. }
  67. private function createSplFileInfoDouble(string $filename): \SplFileInfo
  68. {
  69. return new class($filename) extends \SplFileInfo {
  70. public function getRealPath(): string
  71. {
  72. return $this->getFilename();
  73. }
  74. };
  75. }
  76. }