Merge pull request #89 from aldumas/assign_fix

Fixed Utils::assign() bug relating to detecting missing required keys
This commit is contained in:
Vladimir Razuvaev 2017-02-14 02:51:33 +07:00 committed by GitHub
commit 0bd7c9d405
2 changed files with 23 additions and 1 deletions

View File

@ -25,7 +25,7 @@ class Utils
public static function assign($obj, array $vars, array $requiredKeys = []) public static function assign($obj, array $vars, array $requiredKeys = [])
{ {
foreach ($requiredKeys as $key) { foreach ($requiredKeys as $key) {
if (!isset($key, $vars)) { if (!isset($vars[$key])) {
throw new InvalidArgumentException("Key {$key} is expected to be set and not to be null"); throw new InvalidArgumentException("Key {$key} is expected to be set and not to be null");
} }
} }

22
tests/UtilsTest.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace GraphQL\Tests;
use GraphQL\Utils;
class UtilsTest extends \PHPUnit_Framework_TestCase
{
public function testAssignThrowsExceptionOnMissingRequiredKey()
{
$object = new \stdClass();
$object->requiredKey = 'value';
try {
Utils::assign($object, [], ['requiredKey']);
$this->fail('Expected exception not thrown');
} catch (\InvalidArgumentException $e) {
$this->assertEquals(
"Key requiredKey is expected to be set and not to be null",
$e->getMessage());
}
}
}