From 8e418992d20ddf4aacd99a6bdff1c6e85ca3401a Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Wed, 28 Sep 2016 14:48:09 +0200 Subject: [PATCH 1/5] array is a valid constant type --- src/Generator/ValueGenerator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Generator/ValueGenerator.php b/src/Generator/ValueGenerator.php index 09c49a13..6fc698e3 100644 --- a/src/Generator/ValueGenerator.php +++ b/src/Generator/ValueGenerator.php @@ -174,6 +174,7 @@ public function isValidConstantType() // valid types for constants $scalarTypes = [ + self::TYPE_ARRAY, self::TYPE_BOOLEAN, self::TYPE_BOOL, self::TYPE_NUMBER, From 5e3be7dcb3397376b5b0e0afe46e79b144fae544 Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Thu, 29 Sep 2016 11:47:41 +0200 Subject: [PATCH 2/5] more array type --- src/Generator/ValueGenerator.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Generator/ValueGenerator.php b/src/Generator/ValueGenerator.php index 6fc698e3..28b0c97f 100644 --- a/src/Generator/ValueGenerator.php +++ b/src/Generator/ValueGenerator.php @@ -175,6 +175,8 @@ public function isValidConstantType() // valid types for constants $scalarTypes = [ self::TYPE_ARRAY, + self::TYPE_ARRAY_LONG, + self::TYPE_ARRAY_SHORT, self::TYPE_BOOLEAN, self::TYPE_BOOL, self::TYPE_NUMBER, From 3cd93e224dc0efb219f1b4e67040a23d44463eb8 Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Thu, 29 Sep 2016 11:48:17 +0200 Subject: [PATCH 3/5] rename variable to express its purpose --- src/Generator/ValueGenerator.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Generator/ValueGenerator.php b/src/Generator/ValueGenerator.php index 28b0c97f..24a2983a 100644 --- a/src/Generator/ValueGenerator.php +++ b/src/Generator/ValueGenerator.php @@ -172,8 +172,7 @@ public function isValidConstantType() $type = $this->type; } - // valid types for constants - $scalarTypes = [ + $validConstantTypes = [ self::TYPE_ARRAY, self::TYPE_ARRAY_LONG, self::TYPE_ARRAY_SHORT, @@ -189,7 +188,7 @@ public function isValidConstantType() self::TYPE_NULL ]; - return in_array($type, $scalarTypes); + return in_array($type, $validConstantTypes); } /** From 2c300f68f223cddfff648b0af69d7e395aee44c0 Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Thu, 29 Sep 2016 11:49:05 +0200 Subject: [PATCH 4/5] add test to check validContactTypes --- test/Generator/ValueGeneratorTest.php | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/test/Generator/ValueGeneratorTest.php b/test/Generator/ValueGeneratorTest.php index 1d9223ec..47dd61e6 100644 --- a/test/Generator/ValueGeneratorTest.php +++ b/test/Generator/ValueGeneratorTest.php @@ -12,6 +12,8 @@ use ArrayAccess; use ArrayObject as SplArrayObject; use Zend\Code\Exception\InvalidArgumentException; +use Zend\Code\Generator\PropertyGenerator; +use Zend\Code\Generator\PropertyValueGenerator; use Zend\Stdlib\ArrayObject as StdlibArrayObject; use Zend\Code\Generator\ValueGenerator; @@ -64,6 +66,36 @@ public function constantsTypeProvider() ]; } + /** + * @dataProvider validConstantTypesProvider + */ + public function testValidConstantTypes($generator) + { + $propertyGenerator = new PropertyGenerator('FOO', $generator); + $propertyGenerator->setConst(true); + + $this->assertInternalType('string', $propertyGenerator->generate()); + } + + public function validConstantTypesProvider() + { + return [ + [new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY)], + [new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_LONG)], + [new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_SHORT)], + [new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOL)], + [new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOLEAN)], + [new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INT)], + [new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INTEGER)], + [new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_DOUBLE)], + [new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_FLOAT)], + [new PropertyValueGenerator('bar', PropertyValueGenerator::TYPE_STRING)], + [new PropertyValueGenerator(null, PropertyValueGenerator::TYPE_NULL)], + [new PropertyValueGenerator(null, PropertyValueGenerator::TYPE_NULL)], + [new PropertyValueGenerator('PHP_EOL', PropertyValueGenerator::TYPE_CONSTANT)], + ]; + } + /** * @return array */ From ca8621d0d164ead88a29e2f6fab2237c3eaeb4e1 Mon Sep 17 00:00:00 2001 From: Bas Kamer Date: Sat, 1 Oct 2016 12:56:14 +0200 Subject: [PATCH 5/5] test expectedOutput --- test/Generator/ValueGeneratorTest.php | 30 +++++++++++++-------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/test/Generator/ValueGeneratorTest.php b/test/Generator/ValueGeneratorTest.php index 47dd61e6..5df8c987 100644 --- a/test/Generator/ValueGeneratorTest.php +++ b/test/Generator/ValueGeneratorTest.php @@ -69,30 +69,28 @@ public function constantsTypeProvider() /** * @dataProvider validConstantTypesProvider */ - public function testValidConstantTypes($generator) + public function testValidConstantTypes($generator, $expectedOutput) { $propertyGenerator = new PropertyGenerator('FOO', $generator); $propertyGenerator->setConst(true); - - $this->assertInternalType('string', $propertyGenerator->generate()); + $this->assertSame($expectedOutput, $propertyGenerator->generate()); } public function validConstantTypesProvider() { return [ - [new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY)], - [new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_LONG)], - [new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_SHORT)], - [new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOL)], - [new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOLEAN)], - [new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INT)], - [new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INTEGER)], - [new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_DOUBLE)], - [new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_FLOAT)], - [new PropertyValueGenerator('bar', PropertyValueGenerator::TYPE_STRING)], - [new PropertyValueGenerator(null, PropertyValueGenerator::TYPE_NULL)], - [new PropertyValueGenerator(null, PropertyValueGenerator::TYPE_NULL)], - [new PropertyValueGenerator('PHP_EOL', PropertyValueGenerator::TYPE_CONSTANT)], + [new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY, ValueGenerator::OUTPUT_SINGLE_LINE), " const FOO = array();"], + [new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_LONG, ValueGenerator::OUTPUT_SINGLE_LINE), " const FOO = array();"], + [new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_SHORT, ValueGenerator::OUTPUT_SINGLE_LINE), " const FOO = [];"], + [new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOL), " const FOO = true;"], + [new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOLEAN), " const FOO = true;"], + [new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INT), " const FOO = 1;"], + [new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INTEGER), " const FOO = 1;"], + [new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_DOUBLE), " const FOO = 0.1;"], + [new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_FLOAT), " const FOO = 0.1;"], + [new PropertyValueGenerator('bar', PropertyValueGenerator::TYPE_STRING), " const FOO = 'bar';"], + [new PropertyValueGenerator(null, PropertyValueGenerator::TYPE_NULL), " const FOO = null;"], + [new PropertyValueGenerator('PHP_EOL', PropertyValueGenerator::TYPE_CONSTANT), " const FOO = PHP_EOL;"], ]; }