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

Fix exception message in ValueGenerator for invalid values #179

Merged
merged 1 commit into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
7 changes: 4 additions & 3 deletions src/Generator/ValueGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,9 +437,10 @@ public function generate()
break;
case self::TYPE_OTHER:
default:
throw new Exception\RuntimeException(
sprintf('Type "%s" is unknown or cannot be used as property default value.', get_class($value))
);
throw new Exception\RuntimeException(sprintf(
'Type "%s" is unknown or cannot be used as property default value.',
is_object($value) ? get_class($value) : gettype($value)
));
}

return $output;
Expand Down
23 changes: 23 additions & 0 deletions test/Generator/ValueGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

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;
Expand Down Expand Up @@ -462,4 +465,24 @@ public function getEscapedParameters()
["\\'", "\\\\\\'"],
];
}

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();
}
}