Skip to content
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
8 changes: 4 additions & 4 deletions src/Connection/ImapParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function next(): Data|Token|Response|null
protected function parseUntaggedResponse(): UntaggedResponse
{
// Capture the initial '*' token.
$elements[] = clone $this->currentToken;
$elements[] = $this->currentToken;

$this->advance();

Expand Down Expand Up @@ -111,7 +111,7 @@ protected function parseUntaggedResponse(): UntaggedResponse
protected function parseContinuationResponse(): ContinuationResponse
{
// Capture the initial '+' token.
$elements[] = clone $this->currentToken;
$elements[] = $this->currentToken;

$this->advance();

Expand Down Expand Up @@ -139,7 +139,7 @@ protected function parseContinuationResponse(): ContinuationResponse
protected function parseTaggedResponse(): TaggedResponse
{
// Capture the initial TAG token.
$tokens[] = clone $this->currentToken;
$tokens[] = $this->currentToken;

$this->advance();

Expand Down Expand Up @@ -253,7 +253,7 @@ protected function parseElement(): Data|Token|null
}

// Otherwise, capture the current token.
$token = clone $this->currentToken;
$token = $this->currentToken;

$this->advance();

Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/Connection/ImapParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,3 +406,58 @@
expect($data->lookup('[TEXT]')->value)->toBe("This is the email body.\r\n");
expect($data->lookup('[HEADER]')->value)->toBe("To: recipient@example.com\r\nSubject: Re: Test\r\n");
});

test('parsed responses retain independent tokens as parsing advances', function (string $prefix) {
$stream = new FakeStream;
$stream->open();
$stream->feed([
$prefix.' OK [CAPABILITY IMAP4rev1] (SAME SAME ("first" "second"))',
$prefix.' OK [CAPABILITY IDLE] (SAME SAME ("third" "fourth"))',
]);

$parser = new ImapParser(new ImapTokenizer($stream));

$first = $parser->next();
$second = $parser->next();

expect($first->toArray())->toBe([
$prefix, 'OK', ['CAPABILITY', 'IMAP4rev1'], ['SAME', 'SAME', ['first', 'second']],
]);
expect($second->toArray())->toBe([
$prefix, 'OK', ['CAPABILITY', 'IDLE'], ['SAME', 'SAME', ['third', 'fourth']],
]);

$first->tokens()[0]->value = 'changed';
$first->tokens()[3]->tokens()[0]->value = 'changed';
$first->tokens()[3]->tokens()[2]->tokens()[0]->value = 'changed';

expect($first->tokens()[3]->tokens()[1]->value)->toBe('SAME');
expect($first->tokens()[3]->tokens()[2]->tokens()[1]->value)->toBe('second');
expect($second->toArray())->toBe([
$prefix, 'OK', ['CAPABILITY', 'IDLE'], ['SAME', 'SAME', ['third', 'fourth']],
]);
expect($parser->next())->toBeNull();
})->with(['*', '+', 'A001']);

test('standalone parsed tokens remain independent as parsing advances', function () {
$stream = new FakeStream;
$stream->open();
$stream->feedRaw('"same" "same" "last"');

$parser = new ImapParser(new ImapTokenizer($stream));

$first = $parser->next();
$second = $parser->next();
$third = $parser->next();

expect($first)->not->toBe($second);
expect($first->value)->toBe('same');
expect($second->value)->toBe('same');
expect($third->value)->toBe('last');

$first->value = 'changed';

expect($second->value)->toBe('same');
expect($third->value)->toBe('last');
expect($parser->next())->toBeNull();
});
Loading