|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestBundle\Tests\Integration\Messaging\Controller; |
| 6 | + |
| 7 | +use PhpList\RestBundle\Messaging\Controller\EmailForwardController; |
| 8 | +use PhpList\RestBundle\Tests\Integration\Common\AbstractTestController; |
| 9 | +use PhpList\RestBundle\Tests\Integration\Identity\Fixtures\AdministratorFixture; |
| 10 | +use PhpList\RestBundle\Tests\Integration\Identity\Fixtures\AdministratorTokenFixture; |
| 11 | +use PhpList\RestBundle\Tests\Integration\Messaging\Fixtures\MessageFixture; |
| 12 | +use Symfony\Component\HttpFoundation\Response; |
| 13 | + |
| 14 | +class EmailForwardControllerTest extends AbstractTestController |
| 15 | +{ |
| 16 | + public function testControllerIsAvailableViaContainer(): void |
| 17 | + { |
| 18 | + self::assertInstanceOf( |
| 19 | + EmailForwardController::class, |
| 20 | + self::getContainer()->get(EmailForwardController::class) |
| 21 | + ); |
| 22 | + } |
| 23 | + |
| 24 | + public function testForwardWithInvalidPayloadReturnsUnprocessableEntity(): void |
| 25 | + { |
| 26 | + $this->loadFixtures([AdministratorFixture::class, AdministratorTokenFixture::class, MessageFixture::class]); |
| 27 | + |
| 28 | + // Missing required 'recipients' field should trigger 422 from RequestValidator |
| 29 | + $this->authenticatedJsonRequest('POST', '/api/v2/email-forward/1', content: json_encode([ ])); |
| 30 | + |
| 31 | + $this->assertHttpUnprocessableEntity(); |
| 32 | + } |
| 33 | + |
| 34 | + public function testForwardWithValidDataButNotReceivedEmail(): void |
| 35 | + { |
| 36 | + $this->loadFixtures([AdministratorFixture::class, AdministratorTokenFixture::class, MessageFixture::class]); |
| 37 | + |
| 38 | + $payload = json_encode([ |
| 39 | + 'recipients' => ['friend1@example.com'], |
| 40 | + 'uid' => 'fwd-123', |
| 41 | + 'note' => null, |
| 42 | + 'from_name' => 'Alice', |
| 43 | + 'from_email' => 'alice@example.com', |
| 44 | + ]); |
| 45 | + |
| 46 | + $this->authenticatedJsonRequest('POST', '/api/v2/email-forward/1', content: $payload); |
| 47 | + |
| 48 | + $response = self::getClient()->getResponse(); |
| 49 | + $this->assertHttpUnprocessableEntity(); |
| 50 | + self::assertStringContainsString('application/json', (string)$response->headers); |
| 51 | + |
| 52 | + $data = $this->getDecodedJsonResponseContent(); |
| 53 | + self::assertIsArray($data); |
| 54 | + self::assertArrayHasKey('message', $data); |
| 55 | + self::assertStringContainsString('Cannot forward: user has not received this message', $data['message']); |
| 56 | + } |
| 57 | + |
| 58 | + public function testForwardWithInvalidEmailInRecipientsReturnsUnprocessableEntity(): void |
| 59 | + { |
| 60 | + $this->loadFixtures([AdministratorFixture::class, AdministratorTokenFixture::class, MessageFixture::class]); |
| 61 | + |
| 62 | + $payload = json_encode([ |
| 63 | + 'recipients' => ['not-an-email'], |
| 64 | + ]); |
| 65 | + |
| 66 | + $this->authenticatedJsonRequest('POST', '/api/v2/email-forward/1', content: $payload); |
| 67 | + |
| 68 | + $this->assertHttpUnprocessableEntity(); |
| 69 | + } |
| 70 | + |
| 71 | + public function testForwardWithInvalidIdReturnsNotFound(): void |
| 72 | + { |
| 73 | + $this->loadFixtures([AdministratorFixture::class, AdministratorTokenFixture::class]); |
| 74 | + |
| 75 | + $this->authenticatedJsonRequest('POST', '/api/v2/email-forward/9999', content: json_encode([ |
| 76 | + 'recipients' => ['friend@example.com'], |
| 77 | + ])); |
| 78 | + |
| 79 | + $this->assertHttpNotFound(); |
| 80 | + } |
| 81 | +} |
0 commit comments