|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace PhpList\RestBundle\Tests\Integration\Subscription\Controller; |
| 6 | + |
| 7 | +use PhpList\RestBundle\Subscription\Controller\SubscriberImportController; |
| 8 | +use PhpList\RestBundle\Tests\Integration\Common\AbstractTestController; |
| 9 | +use Symfony\Component\HttpFoundation\File\UploadedFile; |
| 10 | +use Symfony\Component\HttpFoundation\Response; |
| 11 | + |
| 12 | +/** |
| 13 | + * Integration tests for the SubscriberImportController. |
| 14 | + */ |
| 15 | +class SubscriberImportControllerTest extends AbstractTestController |
| 16 | +{ |
| 17 | + private string $tempDir; |
| 18 | + |
| 19 | + protected function setUp(): void |
| 20 | + { |
| 21 | + parent::setUp(); |
| 22 | + |
| 23 | + $this->tempDir = sys_get_temp_dir(); |
| 24 | + } |
| 25 | + |
| 26 | + public function testControllerIsAvailableViaContainer(): void |
| 27 | + { |
| 28 | + self::assertInstanceOf( |
| 29 | + SubscriberImportController::class, |
| 30 | + self::getContainer()->get(SubscriberImportController::class) |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + public function testImportSubscribersWithoutSessionKeyReturnsForbiddenStatus(): void |
| 35 | + { |
| 36 | + self::getClient()->request('POST', '/api/v2/subscribers/import'); |
| 37 | + |
| 38 | + $this->assertHttpForbidden(); |
| 39 | + } |
| 40 | + |
| 41 | + public function testImportSubscribersWithoutFileReturnsBadRequestStatus(): void |
| 42 | + { |
| 43 | + $this->authenticatedJsonRequest('POST', '/api/v2/subscribers/import'); |
| 44 | + |
| 45 | + $this->assertHttpBadRequest(); |
| 46 | + $responseContent = $this->getDecodedJsonResponseContent(); |
| 47 | + self::assertSame(false, $responseContent['success']); |
| 48 | + self::assertStringContainsString('No file uploaded', $responseContent['message']); |
| 49 | + } |
| 50 | + |
| 51 | + public function testImportSubscribersWithNonCsvFileReturnsBadRequestStatus(): void |
| 52 | + { |
| 53 | + $filePath = $this->tempDir . '/test.txt'; |
| 54 | + file_put_contents($filePath, 'This is not a CSV file'); |
| 55 | + |
| 56 | + $file = new UploadedFile( |
| 57 | + $filePath, |
| 58 | + 'test.txt', |
| 59 | + 'text/plain', |
| 60 | + null, |
| 61 | + true |
| 62 | + ); |
| 63 | + |
| 64 | + $this->authenticatedJsonRequest( |
| 65 | + 'POST', |
| 66 | + '/api/v2/subscribers/import', |
| 67 | + [], |
| 68 | + ['file' => $file] |
| 69 | + ); |
| 70 | + |
| 71 | + $this->assertHttpBadRequest(); |
| 72 | + $responseContent = $this->getDecodedJsonResponseContent(); |
| 73 | + self::assertSame(false, $responseContent['success']); |
| 74 | + self::assertStringContainsString('File must be a CSV', $responseContent['message']); |
| 75 | + } |
| 76 | + |
| 77 | + public function testImportSubscribersWithValidCsvFile(): void |
| 78 | + { |
| 79 | + $filePath = $this->tempDir . '/subscribers.csv'; |
| 80 | + $csvContent = "email,name\ntest@example.com,Test User\ntest2@example.com,Test User 2"; |
| 81 | + file_put_contents($filePath, $csvContent); |
| 82 | + |
| 83 | + $file = new UploadedFile( |
| 84 | + $filePath, |
| 85 | + 'subscribers.csv', |
| 86 | + 'text/csv', |
| 87 | + null, |
| 88 | + true |
| 89 | + ); |
| 90 | + |
| 91 | + $this->authenticatedJsonRequest( |
| 92 | + 'POST', |
| 93 | + '/api/v2/subscribers/import', |
| 94 | + [], |
| 95 | + ['file' => $file] |
| 96 | + ); |
| 97 | + |
| 98 | + $response = self::getClient()->getResponse(); |
| 99 | + self::assertSame(Response::HTTP_OK, $response->getStatusCode()); |
| 100 | + |
| 101 | + $responseContent = $this->getDecodedJsonResponseContent(); |
| 102 | + self::assertSame(true, $responseContent['success']); |
| 103 | + self::assertArrayHasKey('imported', $responseContent); |
| 104 | + self::assertArrayHasKey('skipped', $responseContent); |
| 105 | + self::assertArrayHasKey('errors', $responseContent); |
| 106 | + } |
| 107 | + |
| 108 | + public function testImportSubscribersWithOptions(): void |
| 109 | + { |
| 110 | + $filePath = $this->tempDir . '/subscribers.csv'; |
| 111 | + $csvContent = "email,name\ntest@example.com,Test User"; |
| 112 | + file_put_contents($filePath, $csvContent); |
| 113 | + |
| 114 | + $file = new UploadedFile( |
| 115 | + $filePath, |
| 116 | + 'subscribers.csv', |
| 117 | + 'text/csv', |
| 118 | + null, |
| 119 | + true |
| 120 | + ); |
| 121 | + |
| 122 | + $this->authenticatedJsonRequest( |
| 123 | + 'POST', |
| 124 | + '/api/v2/subscribers/import', |
| 125 | + [ |
| 126 | + 'request_confirmation' => 'true', |
| 127 | + 'html_email' => 'false' |
| 128 | + ], |
| 129 | + ['file' => $file] |
| 130 | + ); |
| 131 | + |
| 132 | + $response = self::getClient()->getResponse(); |
| 133 | + self::assertSame(Response::HTTP_OK, $response->getStatusCode()); |
| 134 | + |
| 135 | + $responseContent = $this->getDecodedJsonResponseContent(); |
| 136 | + self::assertSame(true, $responseContent['success']); |
| 137 | + } |
| 138 | + |
| 139 | + public function testGetMethodIsNotAllowed(): void |
| 140 | + { |
| 141 | + $this->authenticatedJsonRequest('GET', '/api/v2/subscribers/import'); |
| 142 | + |
| 143 | + $this->assertHttpMethodNotAllowed(); |
| 144 | + } |
| 145 | +} |
0 commit comments