<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;
 */
class ValueGeneratorTest extends \PHPUnit_Framework_TestCase
{
    public function testPropertyDefaultValueConstructor()
    {
        $valueGenerator = new ValueGenerator();
        $this->isInstanceOf($valueGenerator, 'Zend\Code\Generator\ValueGenerator');
    }

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

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

    public function testPropertyDefaultValueCanHandleArray()
    {
        $expectedSource = <<<EOS
array(
    'foo',
)
EOS;

        $valueGenerator = new ValueGenerator();
        $valueGenerator->setValue(['foo']);
        $this->assertEquals($expectedSource, $valueGenerator->generate());
    }

    public function testPropertyDefaultValueCanHandleShortArray()
    {
        $expectedSource = <<<EOS
[
    'foo',
]
EOS;

        $valueGenerator = new ValueGenerator();
        $valueGenerator->setValue(['foo']);
        $valueGenerator->setType(ValueGenerator::TYPE_ARRAY_SHORT);
        $this->assertEquals($expectedSource, $valueGenerator->generate());
    }

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

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

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

    public function testPropertyDefaultValueCanHandleComplexArrayOfTypes()
    {
        $targetValue = [
            5,
            'one' => 1,
            'two' => '2',
     * @param PropertyValueGenerator $generator
     * @param string $expectedOutput
     */
    public function testValidConstantTypes(PropertyValueGenerator $generator, $expectedOutput)
    {
        $propertyGenerator = new PropertyGenerator('FOO', $generator);
        $propertyGenerator->setConst(true);
        self::assertSame($expectedOutput, $propertyGenerator->generate());
    }

    /**
            new ValueGenerator('PHP_EOL', 'constant')
        ];

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

        $valueGenerator = new ValueGenerator();
        $valueGenerator->initEnvironmentConstants();
        $valueGenerator->setValue($targetValue);
        $generatedTargetSource = $valueGenerator->generate();
        $this->assertEquals($expectedSource, $generatedTargetSource);
    }

    public function testPropertyDefaultValueCanHandleComplexShortArrayOfTypes()
    {
        $targetValue = [
            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')
        ];

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

        $valueGenerator = new ValueGenerator();
        $valueGenerator->initEnvironmentConstants();
        $valueGenerator->setValue($targetValue);
        $valueGenerator->setType(ValueGenerator::TYPE_ARRAY_SHORT);
        $generatedTargetSource = $valueGenerator->generate();
        $this->assertEquals($expectedSource, $generatedTargetSource);
    }

    public function testPropertyDefaultValueCanHandleArrayWithUnsortedKeys()
    {
        $value = [
            1 => 'a',
                '    public const FOO = PHP_EOL;',
            ],
        ];
            3 => 'e'
        ];

        $valueGenerator = new ValueGenerator();
        $valueGenerator->setValue($value);
        $expectedSource = <<<EOS
array(
    1 => 'a',
    0 => 'b',
    protected function generateArrayData($longOutput, array $value)
    {
        $shortOutput = str_replace(
)
EOS;

        $this->assertEquals($expectedSource, $valueGenerator->generate());
    }

    public function testPropertyDefaultValueCanHandleShortArrayWithUnsortedKeys()
    {
        $value = [
            1 => 'a',
            0 => 'b',
            'c',
            7 => 'd',
            3 => 'e'
        ];

        $valueGenerator = new ValueGenerator();
        $valueGenerator->setValue($value);
        $valueGenerator->setType(ValueGenerator::TYPE_ARRAY_SHORT);
        $expectedSource = <<<EOS
[
    1 => 'a',
    0 => 'b',
    'c',
    7 => 'd',
    3 => 'e',
]
EOS;

        $this->assertEquals($expectedSource, $valueGenerator->generate());
    }

    /**
        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',
                            ],
            ["\\'", "\\\\\\'"],
        ];
    }
}
                            '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> Reworked ValueGeneratorTest to use data providers

<DFF> @@ -19,72 +19,61 @@ use Zend\Code\Generator\ValueGenerator;
  */
 class ValueGeneratorTest extends \PHPUnit_Framework_TestCase
 {
-    public function testPropertyDefaultValueConstructor()
+    /**
+     * @param $longOutput
+     * @param $value
+     *
+     * @return array
+     */
+    protected function generateArrayData($longOutput, $value)
     {
-        $valueGenerator = new ValueGenerator();
-        $this->isInstanceOf($valueGenerator, 'Zend\Code\Generator\ValueGenerator');
-    }
+        $shortOutput = str_replace(
+            array('array(', ')'),
+            array('[', ']'),
+            $longOutput
+        );
 
-    public function testPropertyDefaultValueIsSettable()
-    {
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->setValue('foo');
-        $this->assertEquals('foo', $valueGenerator->getValue());
+        return [
+            'auto'        => [
+                ValueGenerator::TYPE_AUTO, $value, $longOutput
+            ],
+            'array'       => [
+                ValueGenerator::TYPE_ARRAY, $value, $longOutput
+            ],
+            'array long'  => [
+                ValueGenerator::TYPE_ARRAY_LONG, $value, $longOutput
+            ],
+            'array short' => [
+                ValueGenerator::TYPE_ARRAY_SHORT, $value, $shortOutput
+            ],
+        ];
     }
-
-    public function testPropertyDefaultValueCanHandleStrings()
+    /**
+     * Data provider for testPropertyDefaultValueCanHandleArray test
+     *
+     * @return array
+     */
+    public function simpleArrayProvider()
     {
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->setValue('foo');
-        $this->assertEquals("'foo'", $valueGenerator->generate());
-    }
+        $value = ['foo'];
 
-    public function testPropertyDefaultValueCanHandleArray()
-    {
-        $expectedSource = <<<EOS
+        $longOutput = <<<EOS
 array(
     'foo',
 )
 EOS;
 
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->setValue(['foo']);
-        $this->assertEquals($expectedSource, $valueGenerator->generate());
+        return $this->generateArrayData($longOutput, $value);
     }
 
-    public function testPropertyDefaultValueCanHandleShortArray()
-    {
-        $expectedSource = <<<EOS
-[
-    'foo',
-]
-EOS;
-
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->setValue(['foo']);
-        $valueGenerator->setType(ValueGenerator::TYPE_ARRAY_SHORT);
-        $this->assertEquals($expectedSource, $valueGenerator->generate());
-    }
-
-    public function testPropertyDefaultValueCanHandleUnquotedString()
-    {
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->setValue('PHP_EOL');
-        $valueGenerator->setType('constant');
-        $this->assertEquals('PHP_EOL', $valueGenerator->generate());
-
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->setValue(5);
-        $this->assertEquals('5', $valueGenerator->generate());
-
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->setValue(5.25);
-        $this->assertEquals('5.25', $valueGenerator->generate());
-    }
-
-    public function testPropertyDefaultValueCanHandleComplexArrayOfTypes()
+    /**
+     * Data provider for testPropertyDefaultValueCanHandleComplexArrayOfTypes test
+     *
+     * @return array
+     */
+    public function complexArrayProvider()
     {
-        $targetValue = [
+        $value = [
             5,
             'one' => 1,
             'two' => '2',
@@ -102,7 +91,7 @@ EOS;
             new ValueGenerator('PHP_EOL', 'constant')
         ];
 
-        $expectedSource = <<<EOS
+        $longOutput = <<<EOS
 array(
     5,
     'one' => 1,
@@ -122,62 +111,15 @@ array(
 )
 EOS;
 
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->initEnvironmentConstants();
-        $valueGenerator->setValue($targetValue);
-        $generatedTargetSource = $valueGenerator->generate();
-        $this->assertEquals($expectedSource, $generatedTargetSource);
+        return $this->generateArrayData($longOutput, $value);
     }
 
-    public function testPropertyDefaultValueCanHandleComplexShortArrayOfTypes()
-    {
-        $targetValue = [
-            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')
-        ];
-
-        $expectedSource = <<<EOS
-[
-    5,
-    'one' => 1,
-    'two' => '2',
-    'constant1' => __DIR__ . '/anydir1/anydir2',
-    [
-        'baz' => true,
-        'foo',
-        'bar',
-        [
-            'baz1',
-            'baz2',
-            'constant2' => ArrayObject::STD_PROP_LIST,
-        ],
-    ],
-    PHP_EOL,
-]
-EOS;
-
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->initEnvironmentConstants();
-        $valueGenerator->setValue($targetValue);
-        $valueGenerator->setType(ValueGenerator::TYPE_ARRAY_SHORT);
-        $generatedTargetSource = $valueGenerator->generate();
-        $this->assertEquals($expectedSource, $generatedTargetSource);
-    }
-
-    public function testPropertyDefaultValueCanHandleArrayWithUnsortedKeys()
+    /**
+     * Data provider for testPropertyDefaultValueCanHandleArrayWithUnsortedKeys test
+     *
+     * @return array
+     */
+    public function unsortedKeysArrayProvider()
     {
         $value = [
             1 => 'a',
@@ -187,9 +129,7 @@ EOS;
             3 => 'e'
         ];
 
-        $valueGenerator = new ValueGenerator();
-        $valueGenerator->setValue($value);
-        $expectedSource = <<<EOS
+        $longOutput = <<<EOS
 array(
     1 => 'a',
     0 => 'b',
@@ -199,33 +139,92 @@ array(
 )
 EOS;
 
-        $this->assertEquals($expectedSource, $valueGenerator->generate());
+        return $this->generateArrayData($longOutput, $value);
     }
 
-    public function testPropertyDefaultValueCanHandleShortArrayWithUnsortedKeys()
+    /**
+     * @dataProvider unsortedKeysArrayProvider
+     *
+     * @param string $type
+     * @param array $value
+     * @param string $expected
+     */
+    public function testPropertyDefaultValueCanHandleArrayWithUnsortedKeys($type, $value, $expected)
     {
-        $value = [
-            1 => 'a',
-            0 => 'b',
-            'c',
-            7 => 'd',
-            3 => 'e'
-        ];
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setType($type);
+        $valueGenerator->setValue($value);
+
+        $this->assertEquals($expected, $valueGenerator->generate());
+    }
+
+    public function testPropertyDefaultValueConstructor()
+    {
+        $valueGenerator = new ValueGenerator();
+        $this->isInstanceOf($valueGenerator, 'Zend\Code\Generator\ValueGenerator');
+    }
+
+    public function testPropertyDefaultValueIsSettable()
+    {
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue('foo');
+        $this->assertEquals('foo', $valueGenerator->getValue());
+    }
+
+    public function testPropertyDefaultValueCanHandleStrings()
+    {
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue('foo');
+        $this->assertEquals("'foo'", $valueGenerator->generate());
+    }
 
+    /**
+     * @dataProvider simpleArrayProvider
+     *
+     * @param string $type
+     * @param array $value
+     * @param string $expected
+     */
+    public function testPropertyDefaultValueCanHandleArray($type, $value, $expected)
+    {
         $valueGenerator = new ValueGenerator();
+        $valueGenerator->setType($type);
         $valueGenerator->setValue($value);
-        $valueGenerator->setType(ValueGenerator::TYPE_ARRAY_SHORT);
-        $expectedSource = <<<EOS
-[
-    1 => 'a',
-    0 => 'b',
-    'c',
-    7 => 'd',
-    3 => 'e',
-]
-EOS;
 
-        $this->assertEquals($expectedSource, $valueGenerator->generate());
+        $this->assertEquals($expected, $valueGenerator->generate());
+    }
+
+    public function testPropertyDefaultValueCanHandleUnquotedString()
+    {
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue('PHP_EOL');
+        $valueGenerator->setType('constant');
+        $this->assertEquals('PHP_EOL', $valueGenerator->generate());
+
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue(5);
+        $this->assertEquals('5', $valueGenerator->generate());
+
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->setValue(5.25);
+        $this->assertEquals('5.25', $valueGenerator->generate());
+    }
+
+    /**
+     * @dataProvider complexArrayProvider
+     *
+     * @param string $type
+     * @param array $value
+     * @param string $expected
+     */
+    public function testPropertyDefaultValueCanHandleComplexArrayOfTypes($type, $value, $expected)
+    {
+        $valueGenerator = new ValueGenerator();
+        $valueGenerator->initEnvironmentConstants();
+        $valueGenerator->setType($type);
+        $valueGenerator->setValue($value);
+
+        $this->assertEquals($expected, $valueGenerator->generate());
     }
 
     /**
@@ -251,4 +250,5 @@ EOS;
             ["\\'", "\\\\\\'"],
         ];
     }
+
 }

Reworked ValueGeneratorTest to use data providers