Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 8856a32

Browse files
committed
Merge branch 'fix/#94-allow-array-as-generated-constant-value-3-0-5'
Close #94
2 parents d19970e + 35d073a commit 8856a32

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

src/Generator/ValueGenerator.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ public function isValidConstantType()
172172
$type = $this->type;
173173
}
174174

175-
// valid types for constants
176-
$scalarTypes = [
175+
$validConstantTypes = [
176+
self::TYPE_ARRAY,
177+
self::TYPE_ARRAY_LONG,
178+
self::TYPE_ARRAY_SHORT,
177179
self::TYPE_BOOLEAN,
178180
self::TYPE_BOOL,
179181
self::TYPE_NUMBER,
@@ -186,7 +188,7 @@ public function isValidConstantType()
186188
self::TYPE_NULL
187189
];
188190

189-
return in_array($type, $scalarTypes);
191+
return in_array($type, $validConstantTypes);
190192
}
191193

192194
/**

test/Generator/ValueGeneratorTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use ArrayAccess;
1313
use ArrayObject as SplArrayObject;
1414
use Zend\Code\Exception\InvalidArgumentException;
15+
use Zend\Code\Generator\PropertyGenerator;
16+
use Zend\Code\Generator\PropertyValueGenerator;
1517
use Zend\Stdlib\ArrayObject as StdlibArrayObject;
1618
use Zend\Code\Generator\ValueGenerator;
1719

@@ -64,6 +66,38 @@ public function constantsTypeProvider()
6466
];
6567
}
6668

69+
/**
70+
* @group #94
71+
* @dataProvider validConstantTypesProvider
72+
*/
73+
public function testValidConstantTypes($generator, $expectedOutput)
74+
{
75+
$propertyGenerator = new PropertyGenerator('FOO', $generator);
76+
$propertyGenerator->setConst(true);
77+
$this->assertSame($expectedOutput, $propertyGenerator->generate());
78+
}
79+
80+
/**
81+
* @return array
82+
*/
83+
public function validConstantTypesProvider()
84+
{
85+
return [
86+
[new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY, ValueGenerator::OUTPUT_SINGLE_LINE), " const FOO = array();"],
87+
[new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_LONG, ValueGenerator::OUTPUT_SINGLE_LINE), " const FOO = array();"],
88+
[new PropertyValueGenerator([], PropertyValueGenerator::TYPE_ARRAY_SHORT, ValueGenerator::OUTPUT_SINGLE_LINE), " const FOO = [];"],
89+
[new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOL), " const FOO = true;"],
90+
[new PropertyValueGenerator(true, PropertyValueGenerator::TYPE_BOOLEAN), " const FOO = true;"],
91+
[new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INT), " const FOO = 1;"],
92+
[new PropertyValueGenerator(1, PropertyValueGenerator::TYPE_INTEGER), " const FOO = 1;"],
93+
[new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_DOUBLE), " const FOO = 0.1;"],
94+
[new PropertyValueGenerator(0.1, PropertyValueGenerator::TYPE_FLOAT), " const FOO = 0.1;"],
95+
[new PropertyValueGenerator('bar', PropertyValueGenerator::TYPE_STRING), " const FOO = 'bar';"],
96+
[new PropertyValueGenerator(null, PropertyValueGenerator::TYPE_NULL), " const FOO = null;"],
97+
[new PropertyValueGenerator('PHP_EOL', PropertyValueGenerator::TYPE_CONSTANT), " const FOO = PHP_EOL;"],
98+
];
99+
}
100+
67101
/**
68102
* @return array
69103
*/

0 commit comments

Comments
 (0)