From 0c551c804cfcd3a892a5895c5fb8b1f748e78cc6 Mon Sep 17 00:00:00 2001 From: Tim MacDonald Date: Mon, 5 Aug 2024 10:34:24 +1000 Subject: [PATCH] Rework `Context::stackContains` --- src/Illuminate/Log/Context/Repository.php | 4 ++-- tests/Log/ContextTest.php | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Log/Context/Repository.php b/src/Illuminate/Log/Context/Repository.php index 445fa470bb8..0bd2ca68428 100644 --- a/src/Illuminate/Log/Context/Repository.php +++ b/src/Illuminate/Log/Context/Repository.php @@ -333,7 +333,7 @@ public function stackContains(string $key, mixed $value, bool $strict = false): } if ($value instanceof Closure) { - return $value($this->data[$key]); + return collect($this->data[$key])->contains($value); } return in_array($value, $this->data[$key], $strict); @@ -360,7 +360,7 @@ public function hiddenStackContains(string $key, mixed $value, bool $strict = fa } if ($value instanceof Closure) { - return $value($this->data[$key]); + return collect($this->hidden[$key])->contains($value); } return in_array($value, $this->hidden[$key], $strict); diff --git a/tests/Log/ContextTest.php b/tests/Log/ContextTest.php index bec5df63a7a..f0eb661e55b 100644 --- a/tests/Log/ContextTest.php +++ b/tests/Log/ContextTest.php @@ -217,8 +217,10 @@ public function test_it_can_check_if_value_is_in_context_stack() public function test_it_can_check_if_value_is_in_context_stack_with_closures() { Context::push('foo', 'bar', ['lorem'], 123); + Context::pushHidden('baz'); - $this->assertTrue(Context::stackContains('foo', fn ($stack) => in_array('bar', $stack, true))); + $this->assertTrue(Context::stackContains('foo', fn ($value) => $value === 'bar')); + $this->assertFalse(Context::stackContains('foo', fn ($value) => $value === 'baz')); } public function test_it_can_check_if_value_is_in_hidden_context_stack() @@ -230,6 +232,15 @@ public function test_it_can_check_if_value_is_in_hidden_context_stack() $this->assertFalse(Context::hiddenStackContains('foo', 'doesNotExist')); } + public function test_it_can_check_if_value_is_in_hidden_context_stack_with_closures() + { + Context::pushHidden('foo', 'baz'); + Context::push('foo', 'bar', ['lorem'], 123); + + $this->assertTrue(Context::hiddenStackContains('foo', fn ($value) => $value === 'baz')); + $this->assertFalse(Context::hiddenStackContains('foo', fn ($value) => $value === 'bar')); + } + public function test_it_cannot_check_if_hidden_value_is_in_non_hidden_context_stack() { Context::pushHidden('foo', 'bar', 'lorem');