Fixed Utils::assign() bug relating to detecting missing required keys

This commit is contained in:
Adam Dumas 2017-02-13 12:50:34 -05:00
parent c2f0749d8e
commit 97674cbbb9
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 = [])
{
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");
}
}

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