<NME> ValueGeneratorTest.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\Generator;

use ArrayAccess;
use ArrayObject as SplArrayObject;
use DateTime;
use Generator;
use PHPUnit\Framework\TestCase;
use Zend\Code\Exception\InvalidArgumentException;
use Zend\Code\Exception\RuntimeException;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Code\Generator\PropertyValueGenerator;
use Zend\Code\Generator\ValueGenerator;
/**
 * @namespace
 */
namespace ZendTest\Code\Generator\Property;
use Zend\Code\Generator;

/**
 * @category   Zend
 * @covers \Zend\Code\Generator\ValueGenerator
 */
class ValueGeneratorTest extends TestCase
{
    public function testDefaultInstance()
 * @group Zend_CodeGenerator
 * @group Zend_CodeGenerator_Php
 */
class PhpPropertyValueTest extends \PHPUnit_Framework_TestCase
{

    public function testPropertyDefaultValueConstructor()
    {
        $propDefaultValue = new Php\PhpPropertyValue();
        $this->isInstanceOf($propDefaultValue, 'Zend\Code\Generator\Property\DefaultValue');
    }

    public function testPropertyDefaultValueIsSettable()
    {
        $propDefaultValue = new Php\PhpPropertyValue();
        $propDefaultValue->setValue('foo');
        $this->assertEquals('foo', $propDefaultValue->getValue());
        //$this->assertEquals('\'foo\';', $propDefaultValue->generate());
    }

    public function testPropertyDefaultValueCanHandleStrings()
    {
        $propDefaultValue = new Php\PhpPropertyValue();
        $propDefaultValue->setValue('foo');
        $this->assertEquals('\'foo\';', $propDefaultValue->generate());
    }

    public function testPropertyDefaultValueCanHandleArray()
    {
        $propDefaultValue = new Php\PhpPropertyValue();
        $propDefaultValue->setValue(array('foo'));
        $this->assertEquals('array(\'foo\');', $propDefaultValue->generate());
    }

    public function testPropertyDefaultValueCanHandleUnquotedString()
    {
        $propDefaultValue = new Php\PhpPropertyValue();
        $propDefaultValue->setValue('PHP_EOL');
        $propDefaultValue->setType('constant');
        $this->assertEquals('PHP_EOL;', $propDefaultValue->generate());

        $propDefaultValue = new Php\PhpPropertyValue();
        $propDefaultValue->setValue(5);
        $this->assertEquals('5;', $propDefaultValue->generate());

        $propDefaultValue = new Php\PhpPropertyValue();
        $propDefaultValue->setValue(5.25);
        $this->assertEquals('5.25;', $propDefaultValue->generate());
    }

    public function testPropertyDefaultValueCanHandleComplexArrayOfTypes()
        $propertyGenerator->setConst(true);
        self::assertSame($expectedOutput, $propertyGenerator->generate());
    }

    /**
     * @return array
     */
    public function validConstantTypes()
    {
        return [
                    'baz2'
                    )
                ),
            new Php\PhpValue(array('value' => 'PHP_EOL', 'type' => 'constant'))
            );

        $expectedSource = <<<EOS
                    PropertyValueGenerator::TYPE_ARRAY_LONG,
                    ValueGenerator::OUTPUT_SINGLE_LINE
                ),
                '    public const FOO = array();',
            ],
            [
                new PropertyValueGenerator(
                    [],
                    PropertyValueGenerator::TYPE_ARRAY_SHORT,
                    ValueGenerator::OUTPUT_SINGLE_LINE
                ),
                '    public const FOO = [];',
            ],
        );
EOS;

        $propDefaultValue = new Php\PhpPropertyValue();
        $propDefaultValue->setValue($targetValue);
        $generatedTargetSource = $propDefaultValue->generate();
        $this->assertEquals($expectedSource, $generatedTargetSource);

    }
                new PropertyValueGenerator('PHP_EOL', PropertyValueGenerator::TYPE_CONSTANT),
                '    public const FOO = PHP_EOL;',
            ],
        ];
    }

    /**
     * @param string $longOutput
     * @param array $value
     * @return array
     */
    protected function generateArrayData($longOutput, array $value)
    {
        $shortOutput = str_replace(
            ['array(', ')'],
            ['[', ']'],
            $longOutput
        );

        return [
            'auto'        => [
                ValueGenerator::TYPE_AUTO,
                $value,
                $shortOutput,
            ],
            'array'       => [
                ValueGenerator::TYPE_ARRAY,
                $value,
                $shortOutput,
            ],
            'array long'  => [
                ValueGenerator::TYPE_ARRAY_LONG,
                $value,
                $longOutput,
            ],
            'array short' => [
                ValueGenerator::TYPE_ARRAY_SHORT,
                $value,
                $shortOutput,
            ],
        ];
    }

    /**
     * Data provider for testPropertyDefaultValueCanHandleArray test
     *
     * @return array
     */
    public function simpleArray()
    {
        $value = ['foo'];

        $longOutput = <<<EOS
array(
    'foo',
)
EOS;

        return $this->generateArrayData($longOutput, $value);
    }

    /**
     * Data provider for testPropertyDefaultValueCanHandleComplexArrayOfTypes test
     *
     * @return array
     */
    public function complexArray()
    {
        $value = [
            5,
            'one' => 1,
            'two' => '2',
            'constant1' => "__DIR__ . '/anydir1/anydir2'",
            [
                'baz' => true,
                'foo',
                'bar',
                [
                    'baz1',
                    'baz2',
                    'constant2' => 'ArrayObject::STD_PROP_LIST',
                ],
            ],
            new ValueGenerator('PHP_EOL', 'constant'),
        ];

        $longOutput = <<<EOS
array(
    5,
    'one' => 1,
    'two' => '2',
    'constant1' => __DIR__ . '/anydir1/anydir2',
    array(
        'baz' => true,
        'foo',
        'bar',
        array(
            'baz1',
            'baz2',
            'constant2' => ArrayObject::STD_PROP_LIST,
        ),
    ),
    PHP_EOL,
)
EOS;

        return $this->generateArrayData($longOutput, $value);
    }

    /**
     * Data provider for testPropertyDefaultValueCanHandleComplexArrayWCustomIndentOfTypes test
     */
    public function complexArrayWCustomIndent(): array
    {
        $value = [
            '5bcf08a0a5d20' => [
                '5bcf08a0a5d65' => [
                    '5bcf08a0a5d9f' => [
                        '5bcf08a0a5dd8' => [
                            '5bcf08a0a5e11' => [
                                '5bcf08a0a5e4f' => '5bcf08a0a5e8c',
                                '5bcf08a0a5eca' => '5bcf08a0a5f05',
                                '5bcf08a0a5f43' => '5bcf08a0a5f7f',
                                '5bcf08a0a5fbd' => '5bcf08a0a5ff8',
                            ],
                        ],
                        '5bcf08a0a603a' => [],
                        '5bcf08a0a6062' => '5bcf08a0a609f',
                        '5bcf08a0a60dc' => [
                            '5bcf08a0a611b' => '5bcf08a0a6158',
                            '5bcf08a0a6197' => [
                                '5bcf08a0a61d7' => '5bcf08a0a6212',
                                '5bcf08a0a6250' => '5bcf08a0a628c',
                                '5bcf08a0a62cb' => '5bcf08a0a6306',
                            ],
                            '5bcf08a0a6345' => [
                                '5bcf08a0a637e' => '5bcf08a0a63b4',
                                '5bcf08a0a63ee' => '5bcf08a0a642a',
                            ],
                            '5bcf08a0a6449' => '5bcf08a0a6485',
                        ],
                    ],
                ], '5bcf08a0a64c8' => '5bcf08a0a6540',
                '5bcf08a0a657f' => '5bcf08a0a65bf',
            ],
        ];

        $longOutput = <<<EOS
array(
	'5bcf08a0a5d20' => array(
		'5bcf08a0a5d65' => array(
			'5bcf08a0a5d9f' => array(
				'5bcf08a0a5dd8' => array(
					'5bcf08a0a5e11' => array(
						'5bcf08a0a5e4f' => '5bcf08a0a5e8c',
						'5bcf08a0a5eca' => '5bcf08a0a5f05',
						'5bcf08a0a5f43' => '5bcf08a0a5f7f',
						'5bcf08a0a5fbd' => '5bcf08a0a5ff8',
					),
				),
				'5bcf08a0a603a' => array(
					
				),
				'5bcf08a0a6062' => '5bcf08a0a609f',
				'5bcf08a0a60dc' => array(
					'5bcf08a0a611b' => '5bcf08a0a6158',
					'5bcf08a0a6197' => array(
						'5bcf08a0a61d7' => '5bcf08a0a6212',
						'5bcf08a0a6250' => '5bcf08a0a628c',
						'5bcf08a0a62cb' => '5bcf08a0a6306',
					),
					'5bcf08a0a6345' => array(
						'5bcf08a0a637e' => '5bcf08a0a63b4',
						'5bcf08a0a63ee' => '5bcf08a0a642a',
					),
					'5bcf08a0a6449' => '5bcf08a0a6485',
				),
			),
		),
		'5bcf08a0a64c8' => '5bcf08a0a6540',
		'5bcf08a0a657f' => '5bcf08a0a65bf',
	),
)
EOS;

        return $this->generateArrayData($longOutput, $value);
    }

    /**
     * Data provider for testPropertyDefaultValueCanHandleArrayWithUnsortedKeys test
     *
     * @return array
     */
    public function unsortedKeysArray()
    {
        $value = [
            1 => 'a',
            0 => 'b',
            'c',
            7 => 'd',
            3 => 'e',
        ];

        $longOutput = <<<EOS
array(
    1 => 'a',
    0 => 'b',
    'c',
    7 => 'd',
    3 => 'e',
)
EOS;

        return $this->generateArrayData($longOutput, $value);
    }

    /**
     * @dataProvider unsortedKeysArray
     *
     * @param string $type
     * @param array $value
     * @param string $expected
     */
    public function testPropertyDefaultValueCanHandleArrayWithUnsortedKeys($type, array $value, $expected)
    {
        $valueGenerator = new ValueGenerator();
        $valueGenerator->setType($type);
        $valueGenerator->setValue($value);

        self::assertEquals($expected, $valueGenerator->generate());
    }

    public function testPropertyDefaultValueConstructor()
    {
        $valueGenerator = new ValueGenerator();
        self::assertInstanceOf(ValueGenerator::class, $valueGenerator);
    }

    public function testPropertyDefaultValueIsSettable()
    {
        $valueGenerator = new ValueGenerator();
        $valueGenerator->setValue('foo');
        self::assertEquals('foo', $valueGenerator->getValue());
    }

    public function testPropertyDefaultValueCanHandleStrings()
    {
        $valueGenerator = new ValueGenerator();
        $valueGenerator->setValue('foo');
        self::assertEquals("'foo'", $valueGenerator->generate());
    }

    /**
     * @dataProvider simpleArray
     *
     * @param string $type
     * @param array $value
     * @param string $expected
     */
    public function testPropertyDefaultValueCanHandleArray($type, array $value, $expected)
    {
        $valueGenerator = new ValueGenerator();
        $valueGenerator->setType($type);
        $valueGenerator->setValue($value);

        self::assertEquals($expected, $valueGenerator->generate());
    }

    public function testPropertyDefaultValueCanHandleUnquotedString()
    {
        $valueGenerator = new ValueGenerator();
        $valueGenerator->setValue('PHP_EOL');
        $valueGenerator->setType('constant');
        self::assertEquals('PHP_EOL', $valueGenerator->generate());

        $valueGenerator = new ValueGenerator();
        $valueGenerator->setValue(5);
        self::assertEquals('5', $valueGenerator->generate());

        $valueGenerator = new ValueGenerator();
        $valueGenerator->setValue(5.25);
        self::assertEquals('5.25', $valueGenerator->generate());
    }

    /**
     * @dataProvider complexArray
     *
     * @param string $type
     * @param array $value
     * @param string $expected
     */
    public function testPropertyDefaultValueCanHandleComplexArrayOfTypes($type, array $value, $expected)
    {
        $valueGenerator = new ValueGenerator();
        $valueGenerator->initEnvironmentConstants();
        $valueGenerator->setType($type);
        $valueGenerator->setValue($value);

        self::assertEquals($expected, $valueGenerator->generate());
    }

    /**
     * @dataProvider complexArrayWCustomIndent
     */
    public function testPropertyDefaultValueCanHandleComplexArrayWCustomIndentOfTypes(
        string $type,
        array $value,
        string $expected
    ) : void {
        $valueGenerator = new ValueGenerator();
        $valueGenerator->setType($type);
        $valueGenerator->setValue($value);
        $valueGenerator->setIndentation("\t");

        self::assertEquals($expected, $valueGenerator->generate());
    }

    /**
     * @group 6023
     *
     * @dataProvider getEscapedParameters
     *
     * @param string $input
     * @param string $expectedEscapedValue
     */
    public function testEscaping($input, $expectedEscapedValue)
    {
        self::assertSame($expectedEscapedValue, ValueGenerator::escape($input, false));
    }

    /**
     * Data provider for escaping tests
     *
     * @return string[][]
     */
    public function getEscapedParameters()
    {
        return [
            ['\\', '\\\\'],
            ["'", "\\'"],
            ["\\'", "\\\\\\'"],
        ];
    }

    public function invalidValue() : Generator
    {
        yield 'object' => [new DateTime(), DateTime::class];
        yield 'resource' => [fopen('php://input', 'r'), 'resource'];
    }

    /**
     * @dataProvider invalidValue
     *
     * @param mixed $value
     */
    public function testExceptionInvalidValue($value, string $type) : void
    {
        $valueGenerator = new ValueGenerator($value);

        $this->expectException(RuntimeException::class);
        $this->expectExceptionMessage('Type "'.$type.'" is unknown or cannot be used');
        $valueGenerator->generate();
    }
}

<MSG> Zend\Code
  - refactoring of code generator

<DFF> @@ -22,8 +22,8 @@
 /**
  * @namespace
  */
-namespace ZendTest\Code\Generator\Property;
-use Zend\Code\Generator;
+namespace ZendTest\Code\Generator;
+use Zend\Code\Generator\ValueGenerator;
 
 /**
  * @category   Zend
@@ -35,51 +35,51 @@ use Zend\Code\Generator;
  * @group Zend_CodeGenerator
  * @group Zend_CodeGenerator_Php
  */
-class PhpPropertyValueTest extends \PHPUnit_Framework_TestCase
+class ValueGeneratorTest extends \PHPUnit_Framework_TestCase
 {
 
     public function testPropertyDefaultValueConstructor()
     {
-        $propDefaultValue = new Php\PhpPropertyValue();
-        $this->isInstanceOf($propDefaultValue, 'Zend\Code\Generator\Property\DefaultValue');
+        $valueGenerator = new ValueGenerator();
+        $this->isInstanceOf($valueGenerator, 'Zend\Code\Generator\ValueGenerator');
     }
 
     public function testPropertyDefaultValueIsSettable()
     {
-        $propDefaultValue = new Php\PhpPropertyValue();
-        $propDefaultValue->setValue('foo');
-        $this->assertEquals('foo', $propDefaultValue->getValue());
-        //$this->assertEquals('\'foo\';', $propDefaultValue->generate());
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue('foo');
+        $this->assertEquals('foo', $valueGenerator->getValue());
+        //$this->assertEquals('\'foo\';', $valueGenerator->generate());
     }
 
     public function testPropertyDefaultValueCanHandleStrings()
     {
-        $propDefaultValue = new Php\PhpPropertyValue();
-        $propDefaultValue->setValue('foo');
-        $this->assertEquals('\'foo\';', $propDefaultValue->generate());
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue('foo');
+        $this->assertEquals('\'foo\'', $valueGenerator->generate());
     }
 
     public function testPropertyDefaultValueCanHandleArray()
     {
-        $propDefaultValue = new Php\PhpPropertyValue();
-        $propDefaultValue->setValue(array('foo'));
-        $this->assertEquals('array(\'foo\');', $propDefaultValue->generate());
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue(array('foo'));
+        $this->assertEquals('array(\'foo\')', $valueGenerator->generate());
     }
 
     public function testPropertyDefaultValueCanHandleUnquotedString()
     {
-        $propDefaultValue = new Php\PhpPropertyValue();
-        $propDefaultValue->setValue('PHP_EOL');
-        $propDefaultValue->setType('constant');
-        $this->assertEquals('PHP_EOL;', $propDefaultValue->generate());
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue('PHP_EOL');
+        $valueGenerator->setType('constant');
+        $this->assertEquals('PHP_EOL', $valueGenerator->generate());
 
-        $propDefaultValue = new Php\PhpPropertyValue();
-        $propDefaultValue->setValue(5);
-        $this->assertEquals('5;', $propDefaultValue->generate());
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue(5);
+        $this->assertEquals('5', $valueGenerator->generate());
 
-        $propDefaultValue = new Php\PhpPropertyValue();
-        $propDefaultValue->setValue(5.25);
-        $this->assertEquals('5.25;', $propDefaultValue->generate());
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue(5.25);
+        $this->assertEquals('5.25', $valueGenerator->generate());
     }
 
     public function testPropertyDefaultValueCanHandleComplexArrayOfTypes()
@@ -96,7 +96,7 @@ class PhpPropertyValueTest extends \PHPUnit_Framework_TestCase
                     'baz2'
                     )
                 ),
-            new Php\PhpValue(array('value' => 'PHP_EOL', 'type' => 'constant'))
+            new ValueGenerator(array('value' => 'PHP_EOL', 'type' => 'constant'))
             );
 
         $expectedSource = <<<EOS
@@ -116,9 +116,9 @@ array(
         );
 EOS;
 
-        $propDefaultValue = new Php\PhpPropertyValue();
-        $propDefaultValue->setValue($targetValue);
-        $generatedTargetSource = $propDefaultValue->generate();
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue($targetValue);
+        $generatedTargetSource = $valueGenerator->generate();
         $this->assertEquals($expectedSource, $generatedTargetSource);
 
     }

Zend\Code   - refactoring of code generator