Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Rework Context::stackContains with Closures. #52381

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bugfix as well because it was not checking the hidden data.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was already going to notify you about this after I read the other PR 😂

}

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