From 97674cbbb96f2668a80227f5f9e524d9a3382b69 Mon Sep 17 00:00:00 2001 From: Adam Dumas Date: Mon, 13 Feb 2017 12:50:34 -0500 Subject: [PATCH] Fixed Utils::assign() bug relating to detecting missing required keys --- src/Utils.php | 2 +- tests/UtilsTest.php | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 tests/UtilsTest.php 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()); + } + } +}