4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Test\Formatter;
14 use Psy\Formatter\CodeFormatter;
15 use Psy\Test\Formatter\Fixtures\SomeClass;
17 class CodeFormatterTest extends \PHPUnit\Framework\TestCase
20 * @dataProvider reflectors
22 public function testFormat($reflector, $expected)
24 $formatted = CodeFormatter::format($reflector);
25 $formattedWithoutColors = \preg_replace('#' . \chr(27) . '\[\d\d?m#', '', $formatted);
27 $this->assertEquals($expected, self::trimLines($formattedWithoutColors));
28 $this->assertNotEquals($expected, self::trimLines($formatted));
31 public function reflectors()
33 $expectClass = <<<'EOS'
36 16| const SOME_CONST = 'some const';
37 17| private $someProp = 'some prop';
39 19| public function someMethod($someParam)
41 21| return 'some method';
44 24| public static function someClosure()
46 26| return function () {
47 27| return 'some closure';
53 $expectMethod = <<<'EOS'
54 > 19| public function someMethod($someParam)
56 21| return 'some method';
60 $expectClosure = <<<'EOS'
61 > 26| return function () {
62 27| return 'some closure';
67 [new \ReflectionClass('Psy\Test\Formatter\Fixtures\SomeClass'), $expectClass],
68 [new \ReflectionObject(new SomeClass()), $expectClass],
69 [new \ReflectionMethod('Psy\Test\Formatter\Fixtures\SomeClass', 'someMethod'), $expectMethod],
70 [new \ReflectionFunction(SomeClass::someClosure()), $expectClosure],
75 * @dataProvider invalidReflectors
76 * @expectedException \Psy\Exception\RuntimeException
78 public function testCodeFormatterThrowsExceptionForReflectorsItDoesntUnderstand($reflector)
80 CodeFormatter::format($reflector);
83 public function invalidReflectors()
86 [new \ReflectionExtension('json')],
87 [new \ReflectionParameter(['Psy\Test\Formatter\Fixtures\SomeClass', 'someMethod'], 'someParam')],
88 [new \ReflectionProperty('Psy\Test\Formatter\Fixtures\SomeClass', 'someProp')],
91 if (\version_compare(PHP_VERSION, '7.1.0', '>=')) {
92 $reflectors[] = [new \ReflectionClassConstant('Psy\Test\Formatter\Fixtures\SomeClass', 'SOME_CONST')];
99 * @dataProvider filenames
100 * @expectedException \Psy\Exception\RuntimeException
102 public function testCodeFormatterThrowsExceptionForMissingFile($filename)
104 $reflector = $this->getMockBuilder('ReflectionClass')
105 ->disableOriginalConstructor()
109 ->expects($this->once())
110 ->method('getFileName')
111 ->will($this->returnValue($filename));
113 CodeFormatter::format($reflector);
116 public function filenames()
118 if (\defined('HHVM_VERSION')) {
119 $this->markTestSkipped('We have issues with PHPUnit mocks on HHVM.');
122 return [[null], ['not a file']];
125 private static function trimLines($code)
127 return \rtrim(\implode("\n", \array_map('rtrim', \explode("\n", $code))));