|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\Core\Tests\Unit\Domain\Messaging\EventSubscriber; |
| 6 | + |
| 7 | +use PhpList\Core\Domain\Messaging\EventSubscriber\InjectedByHeaderSubscriber; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Symfony\Component\HttpFoundation\Request; |
| 10 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 11 | +use Symfony\Component\Mailer\Envelope; |
| 12 | +use Symfony\Component\Mailer\Event\MessageEvent; |
| 13 | +use Symfony\Component\Mime\Address; |
| 14 | +use Symfony\Component\Mime\Email; |
| 15 | +use Symfony\Component\Mime\RawMessage; |
| 16 | + |
| 17 | +class InjectedByHeaderSubscriberTest extends TestCase |
| 18 | +{ |
| 19 | + public function testNoHeaderWhenNoCurrentRequest(): void |
| 20 | + { |
| 21 | + $requestStack = new RequestStack(); |
| 22 | + $subscriber = new InjectedByHeaderSubscriber($requestStack); |
| 23 | + |
| 24 | + $email = (new Email()) |
| 25 | + |
| 26 | + |
| 27 | + ->subject('Subject') |
| 28 | + ->text('Body'); |
| 29 | + |
| 30 | + $event = new MessageEvent($email, Envelope::create($email), 'test'); |
| 31 | + |
| 32 | + $subscriber->onMessage($event); |
| 33 | + |
| 34 | + $this->assertFalse( |
| 35 | + $email->getHeaders()->has('X-phpList-Injected-By'), |
| 36 | + 'Header must not be added when there is no current Request.' |
| 37 | + ); |
| 38 | + } |
| 39 | + |
| 40 | + public function testNoHeaderWhenMessageIsNotEmail(): void |
| 41 | + { |
| 42 | + $requestStack = new RequestStack(); |
| 43 | + // Push a Request to ensure the early return is due to non-Email message, not missing request |
| 44 | + $requestStack->push(new Request(server: ['REQUEST_TIME' => time()])); |
| 45 | + |
| 46 | + $subscriber = new InjectedByHeaderSubscriber($requestStack); |
| 47 | + |
| 48 | + $raw = new RawMessage('raw'); |
| 49 | + // Create an arbitrary envelope; it does not need to match the message class |
| 50 | + $envelope = new Envelope( new Address( '[email protected]'), [ new Address( '[email protected]')]); |
| 51 | + $event = new MessageEvent($raw, $envelope, 'test'); |
| 52 | + |
| 53 | + // RawMessage has no headers; the subscriber should return early |
| 54 | + $subscriber->onMessage($event); |
| 55 | + |
| 56 | + $this->assertSame('raw', $raw->toString()); // sanity check to use the variable |
| 57 | + // Nothing to assert on headers (RawMessage has none), but the lack of exceptions is success |
| 58 | + $this->addToAssertionCount(1); |
| 59 | + } |
| 60 | + |
| 61 | + public function testNoHeaderWhenRunningInCliEvenWithRequestAndEmail(): void |
| 62 | + { |
| 63 | + // In PHPUnit, PHP_SAPI is typically "cli"; ensure we have a Request to pass other guards |
| 64 | + $request = new Request(server: [ |
| 65 | + 'REQUEST_TIME' => time(), |
| 66 | + 'REMOTE_ADDR' => '127.0.0.1', |
| 67 | + ]); |
| 68 | + $requestStack = new RequestStack(); |
| 69 | + $requestStack->push($request); |
| 70 | + |
| 71 | + $subscriber = new InjectedByHeaderSubscriber($requestStack); |
| 72 | + |
| 73 | + $email = (new Email()) |
| 74 | + |
| 75 | + |
| 76 | + ->subject('Subject') |
| 77 | + ->text('Body'); |
| 78 | + |
| 79 | + $event = new MessageEvent($email, Envelope::create($email), 'test'); |
| 80 | + |
| 81 | + $subscriber->onMessage($event); |
| 82 | + |
| 83 | + // Because tests run under CLI SAPI, the header must not be added |
| 84 | + $this->assertFalse( |
| 85 | + $email->getHeaders()->has('X-phpList-Injected-By'), |
| 86 | + 'Header must not be added when running under CLI.' |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments