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

Commit

Permalink
Merge branch 'hotfix/179' into develop
Browse files Browse the repository at this point in the history
Forward port #179
  • Loading branch information
michalbundyra committed Dec 10, 2019
2 parents a189f10 + 8db5b4e commit 35baf63
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Expand Up @@ -44,7 +44,7 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#179](https://github.com/zendframework/zend-code/pull/179) fixes exception message when invalid value provided in `Zend\Code\Generator\ValueGenerator`.

## 3.4.0 - 2019-10-06

Expand Down
8 changes: 5 additions & 3 deletions src/Generator/ValueGenerator.php
Expand Up @@ -25,6 +25,7 @@
use function in_array;
use function is_array;
use function is_int;
use function is_object;
use function max;
use function sprintf;
use function str_repeat;
Expand Down Expand Up @@ -437,9 +438,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
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();
}
}

0 comments on commit 35baf63

Please sign in to comment.