<NME> FunctionReflectionTest.php
<BEF> <?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/zf2 for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace ZendTest\Code\Reflection;

use PHPUnit\Framework\TestCase;
use Zend\Code\Reflection\DocBlockReflection;
use Zend\Code\Reflection\Exception\InvalidArgumentException;
use Zend\Code\Reflection\FunctionReflection;
use Zend\Code\Reflection\ParameterReflection;

use function array_shift;
use function trim;
use function uniqid;

/**
 * @group Zend_Reflection
 * @group Zend_Reflection_Function
 */
class FunctionReflectionTest extends TestCase
{
    public function testParemeterReturn()
    {
        $function = new FunctionReflection('array_splice');
        $parameters = $function->getParameters();
        self::assertCount(4, $parameters);
        self::assertInstanceOf(ParameterReflection::class, array_shift($parameters));
    }

    public function testFunctionDocBlockReturn()
    {
        require_once __DIR__ . '/TestAsset/functions.php';
        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function3');
        self::assertInstanceOf(DocBlockReflection::class, $function->getDocBlock());
    }

    public function testGetPrototypeMethod()
    {
        require_once __DIR__ . '/TestAsset/functions.php';

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function2');
        $prototype = [
            'namespace' => 'ZendTest\Code\Reflection\TestAsset',
            'name' => 'function2',
            'return' => 'string',
            'arguments' => [
                'one' => [
                    'type'     => 'string',
                    'required' => true,
                    'by_ref'   => false,
                    'default'  => null,
                ],
                'two' => [
                    'type'     => 'string',

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function6');
        $body = $function->getBody();
        $this->assertEquals("\$closure = function() { return 'bar'; };     return 'function6';", trim($body));
    }

    public function testFunctionClosureBodyReturn()
        self::assertEquals(
            'string function2(string $one, string $two = \'two\')',
            $function->getPrototype(FunctionReflection::PROTOTYPE_AS_STRING)
        );
    }

    public function testInternalFunctionBodyReturn()
    {
        $function = new FunctionReflection('array_splice');
        $this->expectException(InvalidArgumentException::class);
        $function->getBody();
    }

    public function testFunctionBodyReturn()

        $function = new FunctionReflection($function4);
        $body = $function->getBody();
        $this->assertEquals("\$closure = function() { return 'bar'; };     return 'function4';", trim($body));
    }
}

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function4');
        $body = $function->getBody();
        self::assertEquals("return 'function4';", trim($body));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function5');
        $body = $function->getBody();
        self::assertEquals("return 'function5';", trim($body));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function6');
        $body = $function->getBody();
        self::assertEquals("\$closure = function() { return 'bar'; };\n    return 'function6';", trim($body));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function7');
        $body = $function->getBody();
        self::assertEquals("return 'function7';", trim($body));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function8');
        $body = $function->getBody();
        self::assertEquals("return 'function8';", trim($body));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function9');
        $body = $function->getBody();
        self::assertEquals("return 'function9';", trim($body));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function10');
        $body = $function->getBody();
        self::assertEquals("\$closure = function() { return 'function10'; }; return \$closure();", trim($body));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function11');
        $body = $function->getBody();
        self::assertEquals("return 'function11';", trim($body));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function12');
        $body = $function->getBody() ?: '';
        self::assertEquals('', trim($body));
    }

    public function testFunctionClosureBodyReturn()
    {
        require __DIR__ . '/TestAsset/closures.php';

        $function = new FunctionReflection($function1);
        $body = $function->getBody();
        self::assertEquals("return 'function1';", trim($body));

        $function = new FunctionReflection($function2);
        $body = $function->getBody();
        self::assertEquals("return 'function2';", trim($body));

        $function = new FunctionReflection($function3);
        $body = $function->getBody();
        self::assertEquals("return 'function3';", trim($body));

        $function = new FunctionReflection($function4);
        $body = $function->getBody();
        self::assertEquals("\$closure = function() { return 'bar'; };\n    return 'function4';", trim($body));

        $function5 = $list1['closure'];
        $function = new FunctionReflection($function5);
        $body = $function->getBody();
        self::assertEquals("return 'function5';", trim($body));

        $function6 = $list2[0];
        $function = new FunctionReflection($function6);
        $body = $function->getBody();
        self::assertEquals("return 'function6';", trim($body));

        $function7 = $list3[0];
        $function = new FunctionReflection($function7);
        $body = $function->getBody();
        self::assertEquals("return \$c = function() { return 'function7'; }; return \$c();", trim($body));

        $function = new FunctionReflection($function8);
        $body = $function->getBody();
        self::assertEquals("return 'function 8';", trim($body));

        $function = new FunctionReflection($function9);
        $body = $function->getBody() ?: '';
        self::assertEquals('', trim($body));

        $function = new FunctionReflection($function10);
        $body = $function->getBody();
        self::assertEquals("return 'function10';", trim($body));
    }

    public function testInternalFunctionContentsReturn()
    {
        $function = new FunctionReflection('array_splice');

        self::assertEmpty($function->getContents());
    }

    public function testFunctionContentsReturnWithoutDocBlock()
    {
        require_once __DIR__ . '/TestAsset/functions.php';

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function1');
        $content = $function->getContents(false);
        self::assertEquals("function function1()\n{\n    return 'function1';\n}", trim($content));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function4');
        $content = $function->getContents(false);
        self::assertEquals("function function4(\$arg) {\n    return 'function4';\n}", trim($content));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function5');
        $content = $function->getContents(false);
        self::assertEquals("function function5() { return 'function5'; }", trim($content));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function6');
        $content = $function->getContents(false);
        self::assertEquals(
            "function function6()\n{\n    \$closure = function() { return 'bar'; };\n    return 'function6';\n}",
            trim($content)
        );

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function7');
        $content = $function->getContents(false);
        self::assertEquals("function function7() { return 'function7'; }", trim($content));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function8');
        $content = $function->getContents(false);
        self::assertEquals("function function8() { return 'function8'; }", trim($content));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function9');
        $content = $function->getContents(false);
        self::assertEquals("function function9() { return 'function9'; }", trim($content));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function10');
        $content = $function->getContents(false);
        self::assertEquals(
            "function function10() { \$closure = function() { return 'function10'; }; return \$closure(); }",
            trim($content)
        );

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function11');
        $content = $function->getContents(false);
        self::assertEquals("function function11() { return 'function11'; }", trim($content));

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function12');
        $content = $function->getContents(false);
        self::assertEquals('function function12() {}', trim($content));
    }

    /**
     * @group fail
     */
    public function testFunctionClosureContentsReturnWithoutDocBlock()
    {
        require __DIR__ . '/TestAsset/closures.php';

        $function = new FunctionReflection($function2);
        $content = $function->getContents(false);
        self::assertEquals("function() { return 'function2'; }", trim($content));

        $function = new FunctionReflection($function9);
        $content = $function->getContents(false);
        self::assertEquals('function() {}', trim($content));

        $function = new FunctionReflection($function10);
        $content = $function->getContents(false);
        self::assertEquals("function() { return 'function10'; }", trim($content));
    }

    public function testFunctionContentsReturnWithDocBlock()
    {
        require_once __DIR__ . '/TestAsset/functions.php';

        $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function3');
        $content = $function->getContents();
        self::assertEquals(
            "/**\n * Enter description here...\n *\n * @param string \$one\n * @param int \$two"
            . "\n * @return true\n */\nfunction function3(\$one, \$two = 2)\n{\n    return true;\n}",
            trim($content)
        );
    }

    public function testFunctionClosureContentsReturnWithDocBlock()
    {
        require __DIR__ . '/TestAsset/closures.php';

        $function = new FunctionReflection($function9);
        $content = $function->getContents();
        self::assertEquals("/**\n * closure doc block\n */\nfunction() {}", trim($content));
    }

    public function testGetContentsReturnsEmptyContentsOnEvaldCode()
    {
        $functionName = uniqid('generatedFunction');

        eval('namespace ' . __NAMESPACE__ . '; function ' . $functionName . '(){}');

        $reflectionFunction = new FunctionReflection(__NAMESPACE__ . '\\' . $functionName);

        self::assertSame('', $reflectionFunction->getContents());
    }

    public function testGetContentsReturnsEmptyContentsOnInternalCode()
    {
        $reflectionFunction = new FunctionReflection('max');

        self::assertSame('', $reflectionFunction->getContents());
    }
}

<MSG> Fix "\n" in function reflection getBody

<DFF> @@ -61,7 +61,7 @@ class FunctionReflectionTest extends \PHPUnit_Framework_TestCase
 
         $function = new FunctionReflection('ZendTest\Code\Reflection\TestAsset\function6');
         $body = $function->getBody();
-        $this->assertEquals("\$closure = function() { return 'bar'; };     return 'function6';", trim($body));
+        $this->assertEquals("\$closure = function() { return 'bar'; };\n    return 'function6';", trim($body));
     }
 
     public function testFunctionClosureBodyReturn()
@@ -82,6 +82,6 @@ class FunctionReflectionTest extends \PHPUnit_Framework_TestCase
 
         $function = new FunctionReflection($function4);
         $body = $function->getBody();
-        $this->assertEquals("\$closure = function() { return 'bar'; };     return 'function4';", trim($body));
+        $this->assertEquals("\$closure = function() { return 'bar'; };\n    return 'function4';", trim($body));
     }
 }

Fix "\n" in function reflection getBody