diff --git a/src/Utils.php b/src/Utils.php index 358e6ae..7dae895 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -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"); } } diff --git a/tests/UtilsTest.php b/tests/UtilsTest.php new file mode 100644 index 0000000..cadb73d --- /dev/null +++ b/tests/UtilsTest.php @@ -0,0 +1,22 @@ +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()); + } + } +}