Skip to content

Commit

Permalink
Merge pull request #89 from aldumas/assign_fix
Browse files Browse the repository at this point in the history
Fixed Utils::assign() bug relating to detecting missing required keys
  • Loading branch information
vladar authored Feb 13, 2017
2 parents c2f0749 + 97674cb commit 0bd7c9d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static function undefined()
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");
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/UtilsTest.php
Original file line number Diff line number Diff line change
@@ -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());
}
}
}

0 comments on commit 0bd7c9d

Please sign in to comment.