SignatureFormatterTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2023 Justin Hileman
  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 Psy\Test\Formatter;
  11. use Psy\CodeCleaner\CodeCleanerPass;
  12. use Psy\Formatter\SignatureFormatter;
  13. use Psy\Reflection\ReflectionClassConstant;
  14. use Psy\Reflection\ReflectionConstant_;
  15. use Psy\Test\Formatter\Fixtures\BoringTrait;
  16. /**
  17. * @group isolation-fail
  18. */
  19. class SignatureFormatterTest extends \Psy\Test\TestCase
  20. {
  21. const FOO = 'foo value';
  22. private static $bar = 'bar value';
  23. private function someFakeMethod(array $one, $two = 'TWO', \Reflector $three = null)
  24. {
  25. }
  26. private function anotherFakeMethod(array $one = [], $two = 2, $three = null)
  27. {
  28. }
  29. /**
  30. * @dataProvider signatureReflectors
  31. */
  32. public function testFormat($reflector, $expected)
  33. {
  34. $this->assertSame($expected, \strip_tags(SignatureFormatter::format($reflector)));
  35. }
  36. public function signatureReflectors()
  37. {
  38. $values = [
  39. [
  40. ReflectionClassConstant::create($this, 'FOO'),
  41. 'const FOO = "foo value"',
  42. ],
  43. [
  44. new \ReflectionMethod($this, 'someFakeMethod'),
  45. 'private function someFakeMethod(array $one, $two = \'TWO\', Reflector $three = null)',
  46. ],
  47. [
  48. new \ReflectionProperty($this, 'bar'),
  49. 'private static $bar',
  50. ],
  51. [
  52. new \ReflectionClass(CodeCleanerPass::class),
  53. 'abstract class Psy\CodeCleaner\CodeCleanerPass '
  54. .'extends PhpParser\NodeVisitorAbstract '
  55. .'implements PhpParser\NodeVisitor',
  56. ],
  57. [
  58. new \ReflectionClass(BoringTrait::class),
  59. 'trait Psy\Test\Formatter\Fixtures\BoringTrait',
  60. ],
  61. [
  62. new \ReflectionMethod(BoringTrait::class, 'boringMethod'),
  63. 'public function boringMethod($one = 1)',
  64. ],
  65. [
  66. new ReflectionConstant_('E_ERROR'),
  67. 'define("E_ERROR", 1)',
  68. ],
  69. [
  70. new ReflectionConstant_('PHP_VERSION'),
  71. 'define("PHP_VERSION", "'.\PHP_VERSION.'")',
  72. ],
  73. [
  74. new ReflectionConstant_('__LINE__'),
  75. 'define("__LINE__", null)', // @todo show this as `unknown` in red or something?
  76. ],
  77. [
  78. new \ReflectionMethod($this, 'anotherFakeMethod'),
  79. 'private function anotherFakeMethod(array $one = [], $two = 2, $three = null)',
  80. ],
  81. ];
  82. if (\version_compare(\PHP_VERSION, '8.0', '>=')) {
  83. $values[] = [new \ReflectionFunction('implode'), 'function implode(array|string $separator, array $array = null): string'];
  84. $values[] = [new \ReflectionFunction('array_chunk'), 'function array_chunk(array $array, int $length, bool $preserve_keys = false): array'];
  85. } else {
  86. $values[] = [new \ReflectionFunction('implode'), 'function implode($glue, $pieces)'];
  87. $values[] = [new \ReflectionFunction('array_chunk'), 'function array_chunk($arg, $size, $preserve_keys = unknown)'];
  88. }
  89. return $values;
  90. }
  91. public function testSignatureFormatterThrowsUnknownReflectorExpeption()
  92. {
  93. $this->expectException(\InvalidArgumentException::class);
  94. $refl = $this->getMockBuilder(\Reflector::class)->getMock();
  95. SignatureFormatter::format($refl);
  96. $this->fail();
  97. }
  98. }