Skip to content

Commit 1833b5b

Browse files
feat(Log): add entry filtering methods for test mocking (#242)
Add five filtering methods to the Log class for common entry filtering use cases, particularly useful when using HAR files for test mocking: - filterEntriesByUrlPattern: Filter by regex pattern against URL - filterEntriesByMethod: Filter by HTTP method (case-insensitive) - filterEntriesByStatus: Filter by status code range (inclusive) - filterEntriesByDomain: Filter by domain (case-insensitive) - filterEntriesByContentType: Filter by content type (partial match) All methods return re-indexed arrays and support case-insensitive matching where appropriate. --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent bcb4bdd commit 1833b5b

2 files changed

Lines changed: 558 additions & 0 deletions

File tree

src/Log.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,101 @@ public function setEntries(array $entries): self
114114
return $this;
115115
}
116116

117+
/**
118+
* Filter entries by URL pattern (regex).
119+
*
120+
* @param string $pattern A regular expression pattern to match against entry URLs
121+
*
122+
* @return Entry[] Entries matching the URL pattern
123+
*/
124+
public function filterEntriesByUrlPattern(string $pattern): array
125+
{
126+
return array_values(array_filter(
127+
$this->entries,
128+
static fn (Entry $entry): bool => 1 === preg_match($pattern, (string) $entry->getRequest()->getUrl())
129+
));
130+
}
131+
132+
/**
133+
* Filter entries by HTTP method.
134+
*
135+
* @param string $method The HTTP method to filter by (GET, POST, etc.)
136+
*
137+
* @return Entry[] Entries matching the HTTP method
138+
*/
139+
public function filterEntriesByMethod(string $method): array
140+
{
141+
$normalizedMethod = strtoupper($method);
142+
143+
return array_values(array_filter(
144+
$this->entries,
145+
static fn (Entry $entry): bool => strtoupper($entry->getRequest()->getMethod()) === $normalizedMethod
146+
));
147+
}
148+
149+
/**
150+
* Filter entries by HTTP status code range.
151+
*
152+
* @param int $minStatus The minimum status code (inclusive)
153+
* @param int $maxStatus The maximum status code (inclusive)
154+
*
155+
* @return Entry[] Entries with status codes in the specified range
156+
*/
157+
public function filterEntriesByStatus(int $minStatus, int $maxStatus): array
158+
{
159+
return array_values(array_filter(
160+
$this->entries,
161+
static fn (Entry $entry): bool => $entry->getResponse()->getStatus() >= $minStatus
162+
&& $entry->getResponse()->getStatus() <= $maxStatus
163+
));
164+
}
165+
166+
/**
167+
* Filter entries by domain (host).
168+
*
169+
* Comparison is case-insensitive as domain names are case-insensitive per RFC 4343.
170+
* Note: PSR-7 URI implementations typically normalize hosts to lowercase per RFC 3986.
171+
*
172+
* @param string $domain The domain to filter by
173+
*
174+
* @return Entry[] Entries matching the domain
175+
*/
176+
public function filterEntriesByDomain(string $domain): array
177+
{
178+
$normalizedDomain = strtolower($domain);
179+
180+
return array_values(array_filter(
181+
$this->entries,
182+
static fn (Entry $entry): bool => $entry->getRequest()->getUrl()->getHost() === $normalizedDomain
183+
));
184+
}
185+
186+
/**
187+
* Filter entries by response content type (MIME type).
188+
*
189+
* Matches entries where the content type starts with the specified type.
190+
* This allows matching "application/json" when the actual type is
191+
* "application/json; charset=utf-8".
192+
*
193+
* Comparison is case-insensitive per RFC 2045.
194+
*
195+
* @param string $contentType The content type to filter by (e.g., "application/json")
196+
*
197+
* @return Entry[] Entries matching the content type
198+
*/
199+
public function filterEntriesByContentType(string $contentType): array
200+
{
201+
$normalizedType = strtolower($contentType);
202+
203+
return array_values(array_filter(
204+
$this->entries,
205+
static fn (Entry $entry): bool => str_starts_with(
206+
strtolower($entry->getResponse()->getContent()->getMimeType()),
207+
$normalizedType
208+
)
209+
));
210+
}
211+
117212
/**
118213
* Deep clone all object properties when cloning Log.
119214
*/

0 commit comments

Comments
 (0)