mirror of
https://github.com/retailcrm/graphql-php.git
synced 2024-11-22 12:56:05 +03:00
Fixed Utils::assign() bug relating to detecting missing required keys
This commit is contained in:
parent
c2f0749d8e
commit
97674cbbb9
@ -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
22
tests/UtilsTest.php
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user