From ef99cffd4938759cecfe2cb19888e9ff3e942b87 Mon Sep 17 00:00:00 2001 From: Felix Schmid Date: Sun, 11 Apr 2021 18:12:42 +0200 Subject: [PATCH] [8.x] Add more readable `missing` method to Session\Store (#36937) * Add missing helper for session * Update Store.php Co-authored-by: Taylor Otwell --- src/Illuminate/Session/Store.php | 11 +++++++++++ tests/Session/SessionStoreTest.php | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Illuminate/Session/Store.php b/src/Illuminate/Session/Store.php index ff2a3b966a20..151e8b6330b5 100755 --- a/src/Illuminate/Session/Store.php +++ b/src/Illuminate/Session/Store.php @@ -193,6 +193,17 @@ public function exists($key) }); } + /** + * Determine if the given key is missing from the session data. + * + * @param string|array $key + * @return bool + */ + public function missing($key) + { + return ! $this->exists($key); + } + /** * Checks if a key is present and not null. * diff --git a/tests/Session/SessionStoreTest.php b/tests/Session/SessionStoreTest.php index 5871cc657a85..188bdce4d89f 100644 --- a/tests/Session/SessionStoreTest.php +++ b/tests/Session/SessionStoreTest.php @@ -452,6 +452,22 @@ public function testKeyExists() $this->assertFalse($session->exists(['hulk.two'])); } + public function testKeyMissing() + { + $session = $this->getSession(); + $session->put('foo', 'bar'); + $this->assertFalse($session->missing('foo')); + $session->put('baz', null); + $session->put('hulk', ['one' => true]); + $this->assertFalse($session->has('baz')); + $this->assertFalse($session->missing('baz')); + $this->assertTrue($session->missing('bogus')); + $this->assertFalse($session->missing(['foo', 'baz'])); + $this->assertTrue($session->missing(['foo', 'baz', 'bogus'])); + $this->assertFalse($session->missing(['hulk.one'])); + $this->assertTrue($session->missing(['hulk.two'])); + } + public function testRememberMethodCallsPutAndReturnsDefault() { $session = $this->getSession();