|
267 | 267 | "* 1 FETCH (BODY {14}\r\nHello World!\r\n)", |
268 | 268 | ], |
269 | 269 | ]); |
| 270 | + |
| 271 | +test('parses fetch response with body text then header', function () { |
| 272 | + $stream = new FakeStream; |
| 273 | + $stream->open(); |
| 274 | + |
| 275 | + // Simulating BODY[TEXT] before BODY[HEADER] |
| 276 | + $stream->feed([ |
| 277 | + '* 1 FETCH (UID 123 FLAGS (\\Seen) BODY[TEXT] {0}', |
| 278 | + '', |
| 279 | + ' BODY[HEADER] {0}', |
| 280 | + '', |
| 281 | + ')', |
| 282 | + ]); |
| 283 | + |
| 284 | + $tokenizer = new ImapTokenizer($stream); |
| 285 | + $parser = new ImapParser($tokenizer); |
| 286 | + |
| 287 | + $response = $parser->next(); |
| 288 | + |
| 289 | + expect($response)->toBeInstanceOf(UntaggedResponse::class); |
| 290 | + |
| 291 | + // Get the ListData at index 3 (the FETCH data) |
| 292 | + $data = $response->tokenAt(3); |
| 293 | + expect($data)->toBeInstanceOf(ListData::class); |
| 294 | + |
| 295 | + // Verify we can lookup UID |
| 296 | + $uid = $data->lookup('UID'); |
| 297 | + expect($uid)->not->toBeNull(); |
| 298 | + expect($uid->value)->toBe('123'); |
| 299 | + |
| 300 | + // Verify we can lookup FLAGS |
| 301 | + $flags = $data->lookup('FLAGS'); |
| 302 | + expect($flags)->not->toBeNull(); |
| 303 | + |
| 304 | + // Verify we can lookup both BODY sections |
| 305 | + $text = $data->lookup('[TEXT]'); |
| 306 | + expect($text)->not->toBeNull(); |
| 307 | + |
| 308 | + $header = $data->lookup('[HEADER]'); |
| 309 | + expect($header)->not->toBeNull(); |
| 310 | +})->issue(115); |
| 311 | + |
| 312 | +test('parses fetch response with body header then text', function () { |
| 313 | + $stream = new FakeStream; |
| 314 | + $stream->open(); |
| 315 | + |
| 316 | + // Simulating BODY[HEADER] before BODY[TEXT] |
| 317 | + $stream->feed([ |
| 318 | + '* 1 FETCH (UID 456 FLAGS (\\Seen) BODY[HEADER] {0}', |
| 319 | + '', |
| 320 | + ' BODY[TEXT] {0}', |
| 321 | + '', |
| 322 | + ')', |
| 323 | + ]); |
| 324 | + |
| 325 | + $tokenizer = new ImapTokenizer($stream); |
| 326 | + $parser = new ImapParser($tokenizer); |
| 327 | + |
| 328 | + $response = $parser->next(); |
| 329 | + |
| 330 | + expect($response)->toBeInstanceOf(UntaggedResponse::class); |
| 331 | + |
| 332 | + // Get the ListData at index 3 (the FETCH data) |
| 333 | + $data = $response->tokenAt(3); |
| 334 | + expect($data)->toBeInstanceOf(ListData::class); |
| 335 | + |
| 336 | + // Verify we can lookup UID |
| 337 | + $uid = $data->lookup('UID'); |
| 338 | + expect($uid)->not->toBeNull(); |
| 339 | + expect($uid->value)->toBe('456'); |
| 340 | + |
| 341 | + // Verify we can lookup FLAGS |
| 342 | + $flags = $data->lookup('FLAGS'); |
| 343 | + expect($flags)->not->toBeNull(); |
| 344 | + |
| 345 | + // Verify we can lookup both BODY sections |
| 346 | + $header = $data->lookup('[HEADER]'); |
| 347 | + expect($header)->not->toBeNull(); |
| 348 | + |
| 349 | + $text = $data->lookup('[TEXT]'); |
| 350 | + expect($text)->not->toBeNull(); |
| 351 | +})->issue(115); |
| 352 | + |
| 353 | +test('parses fetch response with all metadata and body parts', function () { |
| 354 | + $stream = new FakeStream; |
| 355 | + $stream->open(); |
| 356 | + |
| 357 | + // Full FETCH response with all common fields |
| 358 | + $stream->feed([ |
| 359 | + '* 1 FETCH (UID 789 RFC822.SIZE 1024 FLAGS (\\Seen \\Flagged) BODY[TEXT] {0}', |
| 360 | + '', |
| 361 | + ' BODY[HEADER] {0}', |
| 362 | + '', |
| 363 | + ')', |
| 364 | + ]); |
| 365 | + |
| 366 | + $tokenizer = new ImapTokenizer($stream); |
| 367 | + $parser = new ImapParser($tokenizer); |
| 368 | + |
| 369 | + $response = $parser->next(); |
| 370 | + |
| 371 | + expect($response)->toBeInstanceOf(UntaggedResponse::class); |
| 372 | + |
| 373 | + $data = $response->tokenAt(3); |
| 374 | + expect($data)->toBeInstanceOf(ListData::class); |
| 375 | + |
| 376 | + // Verify all lookups work correctly |
| 377 | + expect($data->lookup('UID')?->value)->toBe('789'); |
| 378 | + expect($data->lookup('RFC822.SIZE')?->value)->toBe('1024'); |
| 379 | + expect($data->lookup('FLAGS'))->not->toBeNull(); |
| 380 | + expect($data->lookup('[TEXT]'))->not->toBeNull(); |
| 381 | + expect($data->lookup('[HEADER]'))->not->toBeNull(); |
| 382 | +})->issue(115); |
0 commit comments