8000 Fix exception message in ValueGenerator for invalid values · zendframework/zend-code@6a666cb · GitHub
[go: up one dir, main page]

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

Commit 6a666cb

Browse files
committed
Fix exception message in ValueGenerator for invalid values
When resource provided we should throw exception that this type cannot be used, but we tried to get_class on resource, what is invalid.
1 parent a277be9 commit 6a666cb

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/Generator/ValueGenerator.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,10 @@ public function generate()
437437
break;
438438
case self::TYPE_OTHER:
439439
default:
440-
throw new Exception\RuntimeException(
441-
sprintf('Type "%s" is unknown or cannot be used as property default value.', get_class($value))
442-
);
440+
throw new Exception\RuntimeException(sprintf(
441+
'Type "%s" is unknown or cannot be used as property default value.',
442+
is_object($value) ? get_class($value) : gettype($value)
443+
));
443444
}
444445

445446
return $output;

test/Generator/ValueGeneratorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@
1111

1212
use ArrayAccess;
1313
use ArrayObject as SplArrayObject;
14+
use DateTime;
15+
use Generator;
1416
use PHPUnit\Framework\TestCase;
1517
use Zend\Code\Exception\InvalidArgumentException;
18+
use Zend\Code\Exception\RuntimeException;
1619
use Zend\Code\Generator\PropertyGenerator;
1720
use Zend\Code\Generator\PropertyValueGenerator;
1821
use Zend\Code\Generator\ValueGenerator;
@@ -462,4 +465,24 @@ public function getEscapedParameters()
462465
["\\'", "\\\\\\'"],
463466
];
464467
}
468+
4 A39A 69+
public function invalidValue() : Generator
470+
{
471+
yield 'object' => [new DateTime(), DateTime::class];
472+
yield 'resource' => [fopen('php://input', 'r'), 'resource'];
473+
}
474+
475+
/**
476+
* @dataProvider invalidValue
477+
*
478+
* @param mixed $value
479+
*/
480+
public function testExceptionInvalidValue($value, string $type) : void
481+
{
482+
$valueGenerator = new ValueGenerator($value);
483+
484+
$this->expectException(RuntimeException::class);
485+
$this->expectExceptionMessage('Type "'.$type.'" is unknown or cannot be used');
486+
$valueGenerator->generate();
487+
}
465488
}

0 commit comments

Comments
 (0)
0