Skip to content

Commit a1bd35c

Browse files
committed
Properly break out in FakeMessageQuery
1 parent 7cd317e commit a1bd35c

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/Testing/FakeMessageQuery.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,40 @@ public function append(string $message, mixed $flags = null): int
8181
*/
8282
public function each(callable $callback, int $chunkSize = 10, int $startChunk = 1): void
8383
{
84-
$this->get()->each($callback);
84+
$this->chunk(function (MessageCollection $messages) use ($callback) {
85+
foreach ($messages as $key => $message) {
86+
if ($callback($message, $key) === false) {
87+
return false;
88+
}
89+
}
90+
}, $chunkSize, $startChunk);
8591
}
8692

8793
/**
8894
* {@inheritDoc}
8995
*/
9096
public function chunk(callable $callback, int $chunkSize = 10, int $startChunk = 1): void
9197
{
92-
$this->get()->chunk($chunkSize)->each($callback);
98+
$page = $startChunk;
99+
100+
$messages = $this->get();
101+
102+
$chunks = $messages->chunk($chunkSize);
103+
104+
foreach ($chunks as $chunk) {
105+
if ($page < $startChunk) {
106+
$page++;
107+
108+
continue;
109+
}
110+
111+
// If the callback returns false, break out.
112+
if ($callback($chunk, $page) === false) {
113+
break;
114+
}
115+
116+
$page++;
117+
}
93118
}
94119

95120
/**

0 commit comments

Comments
 (0)