X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fpsy%2Fpsysh%2Ftest%2FFormatter%2FCodeFormatterTest.php;h=ee213f1c0aab0670743ba1a87e9b89156a47e30d;hp=4a7ccccf9c9112f9d21fcf2dedecb903adfbfd21;hb=419f97be044f1aebd0713921ee604841127e9e84;hpb=052617e40b525f8b817d84c29b1c04951f427069 diff --git a/vendor/psy/psysh/test/Formatter/CodeFormatterTest.php b/vendor/psy/psysh/test/Formatter/CodeFormatterTest.php index 4a7ccccf9..ee213f1c0 100644 --- a/vendor/psy/psysh/test/Formatter/CodeFormatterTest.php +++ b/vendor/psy/psysh/test/Formatter/CodeFormatterTest.php @@ -12,35 +12,94 @@ namespace Psy\Test\Formatter; use Psy\Formatter\CodeFormatter; +use Psy\Test\Formatter\Fixtures\SomeClass; class CodeFormatterTest extends \PHPUnit\Framework\TestCase { - private function ignoreThisMethod($arg) + /** + * @dataProvider reflectors + */ + public function testFormat($reflector, $expected) { - echo 'whot!'; + $formatted = CodeFormatter::format($reflector); + $formattedWithoutColors = preg_replace('#' . chr(27) . '\[\d\d?m#', '', $formatted); + + $this->assertEquals($expected, self::trimLines($formattedWithoutColors)); + $this->assertNotEquals($expected, self::trimLines($formatted)); } - public function testFormat() + public function reflectors() { - $expected = <<<'EOS' - > 18| private function ignoreThisMethod($arg) - 19| { - 20| echo 'whot!'; - 21| } + $expectClass = <<<'EOS' + > 14| class SomeClass + 15| { + 16| const SOME_CONST = 'some const'; + 17| private $someProp = 'some prop'; + 18| + 19| public function someMethod($someParam) + 20| { + 21| return 'some method'; + 22| } + 23| + 24| public static function someClosure() + 25| { + 26| return function () { + 27| return 'some closure'; + 28| }; + 29| } + 30| } EOS; - $formatted = CodeFormatter::format(new \ReflectionMethod($this, 'ignoreThisMethod')); - $formattedWithoutColors = preg_replace('#' . chr(27) . '\[\d\d?m#', '', $formatted); + $expectMethod = <<<'EOS' + > 19| public function someMethod($someParam) + 20| { + 21| return 'some method'; + 22| } +EOS; + + $expectClosure = <<<'EOS' + > 26| return function () { + 27| return 'some closure'; + 28| }; +EOS; + + return [ + [new \ReflectionClass('Psy\Test\Formatter\Fixtures\SomeClass'), $expectClass], + [new \ReflectionObject(new SomeClass()), $expectClass], + [new \ReflectionMethod('Psy\Test\Formatter\Fixtures\SomeClass', 'someMethod'), $expectMethod], + [new \ReflectionFunction(SomeClass::someClosure()), $expectClosure], + ]; + } + + /** + * @dataProvider invalidReflectors + * @expectedException \Psy\Exception\RuntimeException + */ + public function testCodeFormatterThrowsExceptionForReflectorsItDoesntUnderstand($reflector) + { + CodeFormatter::format($reflector); + } + + public function invalidReflectors() + { + $reflectors = [ + [new \ReflectionExtension('json')], + [new \ReflectionParameter(['Psy\Test\Formatter\Fixtures\SomeClass', 'someMethod'], 'someParam')], + [new \ReflectionProperty('Psy\Test\Formatter\Fixtures\SomeClass', 'someProp')], + ]; + + if (version_compare(PHP_VERSION, '7.1.0', '>=')) { + $reflectors[] = [new \ReflectionClassConstant('Psy\Test\Formatter\Fixtures\SomeClass', 'SOME_CONST')]; + } - $this->assertEquals($expected, rtrim($formattedWithoutColors)); - $this->assertNotEquals($expected, rtrim($formatted)); + return $reflectors; } /** * @dataProvider filenames * @expectedException \Psy\Exception\RuntimeException */ - public function testCodeFormatterThrowsException($filename) + public function testCodeFormatterThrowsExceptionForMissingFile($filename) { $reflector = $this->getMockBuilder('ReflectionClass') ->disableOriginalConstructor() @@ -62,4 +121,9 @@ EOS; return [[null], ['not a file']]; } + + private static function trimLines($code) + { + return rtrim(implode("\n", array_map('rtrim', explode("\n", $code)))); + } }