CliDescriptorTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\VarDumper\Tests\Command\Descriptor;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  13. use Symfony\Component\Console\Output\BufferedOutput;
  14. use Symfony\Component\VarDumper\Cloner\Data;
  15. use Symfony\Component\VarDumper\Command\Descriptor\CliDescriptor;
  16. use Symfony\Component\VarDumper\Dumper\CliDumper;
  17. class CliDescriptorTest extends TestCase
  18. {
  19. private static $timezone;
  20. private static $prevTerminalEmulator;
  21. public static function setUpBeforeClass()
  22. {
  23. self::$timezone = date_default_timezone_get();
  24. date_default_timezone_set('UTC');
  25. self::$prevTerminalEmulator = getenv('TERMINAL_EMULATOR');
  26. putenv('TERMINAL_EMULATOR');
  27. }
  28. public static function tearDownAfterClass()
  29. {
  30. date_default_timezone_set(self::$timezone);
  31. putenv('TERMINAL_EMULATOR'.(self::$prevTerminalEmulator ? '='.self::$prevTerminalEmulator : ''));
  32. }
  33. /**
  34. * @dataProvider provideContext
  35. */
  36. public function testDescribe(array $context, string $expectedOutput, bool $decorated = false)
  37. {
  38. $output = new BufferedOutput();
  39. $output->setDecorated($decorated);
  40. $descriptor = new CliDescriptor(new CliDumper(function ($s) {
  41. return $s;
  42. }));
  43. $descriptor->describe($output, new Data([[123]]), $context + ['timestamp' => 1544804268.3668], 1);
  44. $this->assertStringMatchesFormat(trim($expectedOutput), str_replace(PHP_EOL, "\n", trim($output->fetch())));
  45. }
  46. public function provideContext()
  47. {
  48. yield 'source' => [
  49. [
  50. 'source' => [
  51. 'name' => 'CliDescriptorTest.php',
  52. 'line' => 30,
  53. 'file' => '/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
  54. ],
  55. ],
  56. <<<TXT
  57. Received from client #1
  58. -----------------------
  59. -------- ---------------------------------------------------------------------------------------------------
  60. date Fri, 14 Dec 2018 16:17:48 +0000
  61. source CliDescriptorTest.php on line 30
  62. file /Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php
  63. -------- ---------------------------------------------------------------------------------------------------
  64. TXT
  65. ];
  66. yield 'source full' => [
  67. [
  68. 'source' => [
  69. 'name' => 'CliDescriptorTest.php',
  70. 'line' => 30,
  71. 'file_relative' => 'src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
  72. 'file' => '/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
  73. 'file_link' => 'phpstorm://open?file=/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php&line=30',
  74. ],
  75. ],
  76. method_exists(OutputFormatterStyle::class, 'setHref') ?
  77. <<<TXT
  78. Received from client #1
  79. -----------------------
  80. -------- --------------------------------------------------------------------------------
  81. date Fri, 14 Dec 2018 16:17:48 +0000
  82. source CliDescriptorTest.php on line 30
  83. file src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php
  84. -------- --------------------------------------------------------------------------------
  85. TXT
  86. :
  87. <<<TXT
  88. Received from client #1
  89. -----------------------
  90. -------- --------------------------------------------------------------------------------
  91. date Fri, 14 Dec 2018 16:17:48 +0000
  92. source CliDescriptorTest.php on line 30
  93. file src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php
  94. -------- --------------------------------------------------------------------------------
  95. Open source in your IDE/browser:
  96. phpstorm://open?file=/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php&line=30
  97. TXT
  98. ];
  99. if (method_exists(OutputFormatterStyle::class, 'setHref')) {
  100. yield 'source with hyperlink' => [
  101. [
  102. 'source' => [
  103. 'name' => 'CliDescriptorTest.php',
  104. 'line' => 30,
  105. 'file_relative' => 'src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php',
  106. 'file_link' => 'phpstorm://open?file=/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php&line=30',
  107. ],
  108. ],
  109. <<<TXT
  110. %A
  111. source \033]8;;phpstorm://open?file=/Users/ogi/symfony/src/Symfony/Component/VarDumper/Tests/Command/Descriptor/CliDescriptorTest.php&line=30\033\CliDescriptorTest.php on line 30\033]8;;\033%A
  112. %A
  113. TXT
  114. , true,
  115. ];
  116. }
  117. yield 'cli' => [
  118. [
  119. 'cli' => [
  120. 'identifier' => 'd8bece1c',
  121. 'command_line' => 'bin/phpunit',
  122. ],
  123. ],
  124. <<<TXT
  125. $ bin/phpunit
  126. -------------
  127. ------ ---------------------------------
  128. date Fri, 14 Dec 2018 16:17:48 +0000
  129. ------ ---------------------------------
  130. TXT
  131. ];
  132. yield 'request' => [
  133. [
  134. 'request' => [
  135. 'identifier' => 'd8bece1c',
  136. 'controller' => new Data([['FooController.php']]),
  137. 'method' => 'GET',
  138. 'uri' => 'http://localhost/foo',
  139. ],
  140. ],
  141. <<<TXT
  142. GET http://localhost/foo
  143. ------------------------
  144. ------------ ---------------------------------
  145. date Fri, 14 Dec 2018 16:17:48 +0000
  146. controller "FooController.php"
  147. ------------ ---------------------------------
  148. TXT
  149. ];
  150. }
  151. }