No Description

CodeFormatterTest.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2018 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\Formatter\CodeFormatter;
  12. use Psy\Test\Formatter\Fixtures\SomeClass;
  13. class CodeFormatterTest extends \PHPUnit\Framework\TestCase
  14. {
  15. /**
  16. * @dataProvider reflectors
  17. */
  18. public function testFormat($reflector, $expected)
  19. {
  20. $formatted = CodeFormatter::format($reflector);
  21. $formattedWithoutColors = \preg_replace('#' . \chr(27) . '\[\d\d?m#', '', $formatted);
  22. $this->assertEquals($expected, self::trimLines($formattedWithoutColors));
  23. $this->assertNotEquals($expected, self::trimLines($formatted));
  24. }
  25. public function reflectors()
  26. {
  27. $expectClass = <<<'EOS'
  28. > 14| class SomeClass
  29. 15| {
  30. 16| const SOME_CONST = 'some const';
  31. 17| private $someProp = 'some prop';
  32. 18|
  33. 19| public function someMethod($someParam)
  34. 20| {
  35. 21| return 'some method';
  36. 22| }
  37. 23|
  38. 24| public static function someClosure()
  39. 25| {
  40. 26| return function () {
  41. 27| return 'some closure';
  42. 28| };
  43. 29| }
  44. 30| }
  45. EOS;
  46. $expectMethod = <<<'EOS'
  47. > 19| public function someMethod($someParam)
  48. 20| {
  49. 21| return 'some method';
  50. 22| }
  51. EOS;
  52. $expectClosure = <<<'EOS'
  53. > 26| return function () {
  54. 27| return 'some closure';
  55. 28| };
  56. EOS;
  57. return [
  58. [new \ReflectionClass('Psy\Test\Formatter\Fixtures\SomeClass'), $expectClass],
  59. [new \ReflectionObject(new SomeClass()), $expectClass],
  60. [new \ReflectionMethod('Psy\Test\Formatter\Fixtures\SomeClass', 'someMethod'), $expectMethod],
  61. [new \ReflectionFunction(SomeClass::someClosure()), $expectClosure],
  62. ];
  63. }
  64. /**
  65. * @dataProvider invalidReflectors
  66. * @expectedException \Psy\Exception\RuntimeException
  67. */
  68. public function testCodeFormatterThrowsExceptionForReflectorsItDoesntUnderstand($reflector)
  69. {
  70. CodeFormatter::format($reflector);
  71. }
  72. public function invalidReflectors()
  73. {
  74. $reflectors = [
  75. [new \ReflectionExtension('json')],
  76. [new \ReflectionParameter(['Psy\Test\Formatter\Fixtures\SomeClass', 'someMethod'], 'someParam')],
  77. [new \ReflectionProperty('Psy\Test\Formatter\Fixtures\SomeClass', 'someProp')],
  78. ];
  79. if (\version_compare(PHP_VERSION, '7.1.0', '>=')) {
  80. $reflectors[] = [new \ReflectionClassConstant('Psy\Test\Formatter\Fixtures\SomeClass', 'SOME_CONST')];
  81. }
  82. return $reflectors;
  83. }
  84. /**
  85. * @dataProvider filenames
  86. * @expectedException \Psy\Exception\RuntimeException
  87. */
  88. public function testCodeFormatterThrowsExceptionForMissingFile($filename)
  89. {
  90. $reflector = $this->getMockBuilder('ReflectionClass')
  91. ->disableOriginalConstructor()
  92. ->getMock();
  93. $reflector
  94. ->expects($this->once())
  95. ->method('getFileName')
  96. ->will($this->returnValue($filename));
  97. CodeFormatter::format($reflector);
  98. }
  99. public function filenames()
  100. {
  101. if (\defined('HHVM_VERSION')) {
  102. $this->markTestSkipped('We have issues with PHPUnit mocks on HHVM.');
  103. }
  104. return [[null], ['not a file']];
  105. }
  106. private static function trimLines($code)
  107. {
  108. return \rtrim(\implode("\n", \array_map('rtrim', \explode("\n", $code))));
  109. }
  110. }