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

Allow arrays as constant values in generated constants #94

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Generator/ValueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,10 @@ public function isValidConstantType()
$type = $this->type;
}

// valid types for constants
$scalarTypes = [
$validConstantTypes = [
self::TYPE_ARRAY,
self::TYPE_ARRAY_LONG,
self::TYPE_ARRAY_SHORT,
self::TYPE_BOOLEAN,
self::TYPE_BOOL,
self::TYPE_NUMBER,
Expand All @@ -186,7 +188,7 @@ public function isValidConstantType()
self::TYPE_NULL
];

return in_array($type, $scalarTypes);
return in_array($type, $validConstantTypes);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions test/Generator/ValueGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your data provider should probably also give the expected string outcome.

}

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)],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about nested values? Such as ['foo' => ['bar' => 'baz']]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about them? They aren't asserted by the generator afaik...

];
}

/**
* @return array
*/
Expand Down