Skip to content

Commit 2a14796

Browse files
committed
Add test
1 parent 41f9c2d commit 2a14796

4 files changed

Lines changed: 162 additions & 3 deletions

File tree

src/Common/EventListener/ExceptionListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function onKernelException(ExceptionEvent $event): void
7979
'message' => $exception->getMessage(),
8080
], 404);
8181
$event->setResponse($response);
82-
} elseif ($exception instanceof Exception) {
82+
} elseif ($exception instanceof Exception) {
8383
$response = new JsonResponse([
8484
'message' => $exception->getMessage(),
8585
], 500);

src/Messaging/Controller/AttachmentController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PhpList\RestBundle\Common\Controller\BaseController;
1212
use PhpList\RestBundle\Common\Validator\RequestValidator;
1313
use Symfony\Bridge\Doctrine\Attribute\MapEntity;
14+
use Symfony\Component\HttpFoundation\HeaderUtils;
1415
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
1516
use Symfony\Component\HttpFoundation\StreamedResponse;
1617
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
@@ -63,9 +64,9 @@ public function download(
6364

6465
$headers = [
6566
'Content-Type' => $downloadable->mimeType,
66-
'Content-Disposition' => ResponseHeaderBag::makeDisposition(
67+
'Content-Disposition' => HeaderUtils::makeDisposition(
6768
disposition: ResponseHeaderBag::DISPOSITION_ATTACHMENT,
68-
fileName: $downloadable->filename
69+
filename: $downloadable->filename
6970
),
7071
];
7172

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Integration\Messaging\Controller;
6+
7+
use PhpList\Core\Domain\Messaging\Model\Attachment;
8+
use PhpList\RestBundle\Messaging\Controller\AttachmentController;
9+
use PhpList\RestBundle\Tests\Integration\Common\AbstractTestController;
10+
use PhpList\RestBundle\Tests\Integration\Messaging\Fixtures\AttachmentFixture;
11+
12+
class AttachmentControllerTest extends AbstractTestController
13+
{
14+
private string $repoPath;
15+
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
$this->repoPath = (string) self::getContainer()->getParameter('phplist.attachment_repository_path');
20+
if (!is_dir($this->repoPath)) {
21+
mkdir($this->repoPath, 0777, true);
22+
}
23+
}
24+
25+
protected function tearDown(): void
26+
{
27+
// Clean up any test file we might have created
28+
$file = $this->repoPath . DIRECTORY_SEPARATOR . AttachmentFixture::FILENAME;
29+
if (is_file($file)) {
30+
unlink($file);
31+
}
32+
33+
parent::tearDown();
34+
}
35+
36+
public function testControllerIsAvailableViaContainer(): void
37+
{
38+
self::assertInstanceOf(
39+
AttachmentController::class,
40+
self::getContainer()->get(AttachmentController::class)
41+
);
42+
}
43+
44+
public function testDownloadReturnsFileStreamWithHeaders(): void
45+
{
46+
$this->loadFixtures([AttachmentFixture::class]);
47+
48+
// Prepare the actual file in the repository path
49+
$content = 'Hello Attachment';
50+
$file = $this->repoPath . DIRECTORY_SEPARATOR . AttachmentFixture::FILENAME;
51+
file_put_contents($file, $content);
52+
53+
self::getClient()->request(
54+
'GET',
55+
sprintf(
56+
'/api/v2/attachments/download/%d?uid=%s',
57+
AttachmentFixture::ATTACHMENT_ID,
58+
Attachment::FORWARD
59+
)
60+
);
61+
62+
$response = self::getClient()->getResponse();
63+
64+
// StreamedResponse should be 200 with correct headers
65+
self::assertSame(200, $response->getStatusCode());
66+
self::assertSame('text/plain; charset=UTF-8', $response->headers->get('Content-Type'));
67+
self::assertStringContainsString(
68+
'attachment; filename=' . AttachmentFixture::FILENAME,
69+
(string) $response->headers->get('Content-Disposition')
70+
);
71+
self::assertSame((string) strlen($content), $response->headers->get('Content-Length'));
72+
73+
$callback = $response->getCallback();
74+
ob_start();
75+
$callback();
76+
$body = ob_get_clean();
77+
78+
self::assertSame($content, $body);
79+
}
80+
81+
public function testDownloadReturnsNotFoundWhenAttachmentEntityMissing(): void
82+
{
83+
self::getClient()->request('GET', '/api/v2/attachments/download/999999?uid=' . Attachment::FORWARD);
84+
$this->assertHttpNotFound();
85+
}
86+
87+
public function testDownloadReturnsNotFoundWhenUidEmailNotFound(): void
88+
{
89+
$this->loadFixtures([AttachmentFixture::class]);
90+
91+
// Do not create the file; the uid validation happens first and should 404
92+
self::getClient()->request(
93+
'GET',
94+
sprintf(
95+
'/api/v2/attachments/download/%d?uid=%s',
96+
AttachmentFixture::ATTACHMENT_ID,
97+
'does-not-exist@example.com'
98+
)
99+
);
100+
101+
$this->assertHttpNotFound();
102+
}
103+
104+
public function testDownloadReturnsNotFoundWhenFileMissing(): void
105+
{
106+
$this->loadFixtures([AttachmentFixture::class]);
107+
108+
// Ensure no file exists
109+
$file = $this->repoPath . DIRECTORY_SEPARATOR . AttachmentFixture::FILENAME;
110+
if (is_file($file)) {
111+
unlink($file);
112+
}
113+
114+
self::getClient()->request(
115+
'GET',
116+
sprintf(
117+
'/api/v2/attachments/download/%d?uid=%s',
118+
AttachmentFixture::ATTACHMENT_ID,
119+
Attachment::FORWARD
120+
)
121+
);
122+
123+
$this->assertHttpNotFound();
124+
}
125+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace PhpList\RestBundle\Tests\Integration\Messaging\Fixtures;
6+
7+
use Doctrine\Bundle\FixturesBundle\Fixture;
8+
use Doctrine\Persistence\ObjectManager;
9+
use PhpList\Core\Domain\Messaging\Model\Attachment;
10+
use PhpList\Core\TestingSupport\Traits\ModelTestTrait;
11+
12+
class AttachmentFixture extends Fixture
13+
{
14+
use ModelTestTrait;
15+
16+
public const ATTACHMENT_ID = 1;
17+
public const FILENAME = 'attachment.txt';
18+
19+
public function load(ObjectManager $manager): void
20+
{
21+
$attachment = new Attachment(
22+
filename: self::FILENAME,
23+
remoteFile: null,
24+
mimeType: 'text/plain',
25+
description: 'Test attachment',
26+
size: null,
27+
);
28+
29+
$this->setSubjectId($attachment, self::ATTACHMENT_ID);
30+
$manager->persist($attachment);
31+
$manager->flush();
32+
}
33+
}

0 commit comments

Comments
 (0)