Skip to content

Commit 0bd7c9d

Browse files
authored
Merge pull request #89 from aldumas/assign_fix
Fixed Utils::assign() bug relating to detecting missing required keys
2 parents c2f0749 + 97674cb commit 0bd7c9d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

src/Utils.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static function undefined()
2525
public static function assign($obj, array $vars, array $requiredKeys = [])
2626
{
2727
foreach ($requiredKeys as $key) {
28-
if (!isset($key, $vars)) {
28+
if (!isset($vars[$key])) {
2929
throw new InvalidArgumentException("Key {$key} is expected to be set and not to be null");
3030
}
3131
}

tests/UtilsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
namespace GraphQL\Tests;
3+
4+
use GraphQL\Utils;
5+
6+
class UtilsTest extends \PHPUnit_Framework_TestCase
7+
{
8+
public function testAssignThrowsExceptionOnMissingRequiredKey()
9+
{
10+
$object = new \stdClass();
11+
$object->requiredKey = 'value';
12+
13+
try {
14+
Utils::assign($object, [], ['requiredKey']);
15+
$this->fail('Expected exception not thrown');
16+
} catch (\InvalidArgumentException $e) {
17+
$this->assertEquals(
18+
"Key requiredKey is expected to be set and not to be null",
19+
$e->getMessage());
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)