Updated from some -dev modules to alpha, beta or full releases
[yaffs-website] / vendor / psy / psysh / test / Formatter / CodeFormatterTest.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 Justin Hileman
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Psy\Test\Formatter;
13
14 use Psy\Formatter\CodeFormatter;
15 use Psy\Test\Formatter\Fixtures\SomeClass;
16
17 class CodeFormatterTest extends \PHPUnit\Framework\TestCase
18 {
19     /**
20      * @dataProvider reflectors
21      */
22     public function testFormat($reflector, $expected)
23     {
24         $formatted = CodeFormatter::format($reflector);
25         $formattedWithoutColors = preg_replace('#' . chr(27) . '\[\d\d?m#', '', $formatted);
26
27         $this->assertEquals($expected, self::trimLines($formattedWithoutColors));
28         $this->assertNotEquals($expected, self::trimLines($formatted));
29     }
30
31     public function reflectors()
32     {
33         $expectClass = <<<'EOS'
34   > 14| class SomeClass
35     15| {
36     16|     const SOME_CONST = 'some const';
37     17|     private $someProp = 'some prop';
38     18|
39     19|     public function someMethod($someParam)
40     20|     {
41     21|         return 'some method';
42     22|     }
43     23|
44     24|     public static function someClosure()
45     25|     {
46     26|         return function () {
47     27|             return 'some closure';
48     28|         };
49     29|     }
50     30| }
51 EOS;
52
53         $expectMethod = <<<'EOS'
54   > 19|     public function someMethod($someParam)
55     20|     {
56     21|         return 'some method';
57     22|     }
58 EOS;
59
60         $expectClosure = <<<'EOS'
61   > 26|         return function () {
62     27|             return 'some closure';
63     28|         };
64 EOS;
65
66         return [
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],
71         ];
72     }
73
74     /**
75      * @dataProvider invalidReflectors
76      * @expectedException \Psy\Exception\RuntimeException
77      */
78     public function testCodeFormatterThrowsExceptionForReflectorsItDoesntUnderstand($reflector)
79     {
80         CodeFormatter::format($reflector);
81     }
82
83     public function invalidReflectors()
84     {
85         $reflectors = [
86             [new \ReflectionExtension('json')],
87             [new \ReflectionParameter(['Psy\Test\Formatter\Fixtures\SomeClass', 'someMethod'], 'someParam')],
88             [new \ReflectionProperty('Psy\Test\Formatter\Fixtures\SomeClass', 'someProp')],
89         ];
90
91         if (version_compare(PHP_VERSION, '7.1.0', '>=')) {
92             $reflectors[] = [new \ReflectionClassConstant('Psy\Test\Formatter\Fixtures\SomeClass', 'SOME_CONST')];
93         }
94
95         return $reflectors;
96     }
97
98     /**
99      * @dataProvider filenames
100      * @expectedException \Psy\Exception\RuntimeException
101      */
102     public function testCodeFormatterThrowsExceptionForMissingFile($filename)
103     {
104         $reflector = $this->getMockBuilder('ReflectionClass')
105             ->disableOriginalConstructor()
106             ->getMock();
107
108         $reflector
109             ->expects($this->once())
110             ->method('getFileName')
111             ->will($this->returnValue($filename));
112
113         CodeFormatter::format($reflector);
114     }
115
116     public function filenames()
117     {
118         if (defined('HHVM_VERSION')) {
119             $this->markTestSkipped('We have issues with PHPUnit mocks on HHVM.');
120         }
121
122         return [[null], ['not a file']];
123     }
124
125     private static function trimLines($code)
126     {
127         return rtrim(implode("\n", array_map('rtrim', explode("\n", $code))));
128     }
129 }