Skip to content

Commit

Permalink
Rework Context::stackContains
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Aug 5, 2024
1 parent cfe5f84 commit 0c551c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/Illuminate/Log/Context/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
13 changes: 12 additions & 1 deletion tests/Log/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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');
Expand Down

0 comments on commit 0c551c8

Please sign in to comment.