From 4a49ba5f2e86d29f8f07dc65640460822ee98bd0 Mon Sep 17 00:00:00 2001 From: binota Date: Sun, 29 Apr 2018 14:33:06 +0800 Subject: [PATCH 1/2] add tests for Support\Optional --- tests/Support/SupportOptionalTest.php | 52 +++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 tests/Support/SupportOptionalTest.php diff --git a/tests/Support/SupportOptionalTest.php b/tests/Support/SupportOptionalTest.php new file mode 100644 index 000000000000..a3aa89624e15 --- /dev/null +++ b/tests/Support/SupportOptionalTest.php @@ -0,0 +1,52 @@ +item = $expected; + + $optional = new Optional($targetObj); + + $this->assertEquals($expected, $optional->item); + } + + public function testGetNotExistItemOnObject() + { + $targetObj = new \stdClass; + + $optional = new Optional($targetObj); + + $this->assertNull($optional->item); + } + + public function testGetExistItemOnArray() + { + $expected = 'test'; + + $targetArr = [ + 'item' => $expected, + ]; + + $optional = new Optional($targetArr); + + $this->assertEquals($expected, $optional['item']); + } + + public function testGetNotExistItemOnArray() + { + $targetObj = []; + + $optional = new Optional($targetObj); + + $this->assertNull($optional['item']); + } +} From b4fdee2a8a92cf5e07593e63ffdad1f143253094 Mon Sep 17 00:00:00 2001 From: binota Date: Sun, 29 Apr 2018 14:34:19 +0800 Subject: [PATCH 2/2] implement __isset on Optional (#24031) --- src/Illuminate/Support/Optional.php | 19 +++++++++++++ tests/Support/SupportOptionalTest.php | 39 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/Illuminate/Support/Optional.php b/src/Illuminate/Support/Optional.php index 7f7faab1ce2d..101f6a7ae530 100644 --- a/src/Illuminate/Support/Optional.php +++ b/src/Illuminate/Support/Optional.php @@ -59,6 +59,25 @@ public function __call($method, $parameters) } } + /** + * Dynamically check a property exists on the underlying object. + * + * @param $name + * @return bool + */ + public function __isset($name) + { + if (is_object($this->value)) { + return isset($this->value->{$name}); + } + + if (is_array($this->value) || $this->value instanceof \ArrayObject) { + return isset($this->value[$name]); + } + + return false; + } + /** * Determine if an item exists at an offset. * diff --git a/tests/Support/SupportOptionalTest.php b/tests/Support/SupportOptionalTest.php index a3aa89624e15..26bdd4e32a73 100644 --- a/tests/Support/SupportOptionalTest.php +++ b/tests/Support/SupportOptionalTest.php @@ -28,6 +28,25 @@ public function testGetNotExistItemOnObject() $this->assertNull($optional->item); } + public function testIssetExistItemOnObject() + { + $targetObj = new \stdClass; + $targetObj->item = ''; + + $optional = new Optional($targetObj); + + $this->assertTrue(isset($optional->item)); + } + + public function testIssetNotExistItemOnObject() + { + $targetObj = new \stdClass; + + $optional = new Optional($targetObj); + + $this->assertFalse(isset($optional->item)); + } + public function testGetExistItemOnArray() { $expected = 'test'; @@ -49,4 +68,24 @@ public function testGetNotExistItemOnArray() $this->assertNull($optional['item']); } + + public function testIssetExistItemOnArray() + { + $targetArr = [ + 'item' => '', + ]; + + $optional = new Optional($targetArr); + + $this->assertTrue(isset($optional['item'])); + } + + public function testIssetNotExistItemOnArray() + { + $targetArr = []; + + $optional = new Optional($targetArr); + + $this->assertFalse(isset($optional['item'])); + } }