Skip to content

Commit

Permalink
Fixes unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
butschster committed Jun 5, 2024
1 parent f8f11e7 commit 5df3c65
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 28 deletions.
1 change: 0 additions & 1 deletion app/modules/Sentry/Interfaces/Http/JavascriptAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,3 @@ public function __invoke(EnvironmentInterface $env, string|int $project): Respon
);
}
}

2 changes: 1 addition & 1 deletion app/modules/Smtp/Application/Mail/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function __construct(
private ?string $filename,
private string $content,
private string $type,
private ?string $contentId,
private ?string $contentId = null,
) {
$this->id = (string) Uuid::uuid4();
}
Expand Down
5 changes: 4 additions & 1 deletion app/modules/Smtp/Application/Storage/AttachmentStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function __construct(

public function store(Uuid $eventUuid, array $attachments): iterable
{
$result = [];
foreach ($attachments as $attachment) {
$file = $this->bucket->write(
pathname: $eventUuid . '/' . $attachment->getFilename(),
Expand All @@ -41,12 +42,14 @@ public function store(Uuid $eventUuid, array $attachments): iterable
continue;
}

yield $attachment->getContentId() => \sprintf(
$result[$attachment->getContentId()] = \sprintf(
'/api/smtp/%s/attachments/preview/%s',
$eventUuid,
$a->getUuid(),
);
}

return $result;
}

public function deleteByEvent(Uuid $eventUuid): void
Expand Down
4 changes: 3 additions & 1 deletion app/modules/Smtp/Interfaces/TCP/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ private function dispatchMessage(Message $message, ?string $project = null): voi
$uuid = Uuid::generate();
$data = $message->jsonSerialize();


$result = $this->attachments->store(eventUuid: $uuid, attachments: $message->attachments);
// TODO: Refactor this
foreach ($this->attachments->store(eventUuid: $uuid, attachments: $message->attachments) as $cid => $url) {
foreach ($result as $cid => $url) {
$data['html'] = \str_replace("cid:$cid", $url, $data['html']);
}

Expand Down
28 changes: 6 additions & 22 deletions tests/Feature/Interfaces/TCP/Smtp/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
final class EmailTest extends TCPTestCase
{
private \Spiral\Storage\BucketInterface $bucket;
private \Mockery\MockInterface|AttachmentRepositoryInterface $accounts;
private \Mockery\MockInterface|AttachmentRepositoryInterface $attachments;

protected function setUp(): void
{
parent::setUp();

$this->bucket = $this->fakeStorage()->bucket('smtp');
$this->accounts = $this->mockContainer(AttachmentRepositoryInterface::class);
$this->attachments = $this->mockContainer(AttachmentRepositoryInterface::class);
}

public function testSendEmail(): void
Expand All @@ -44,7 +44,7 @@ public function testSendEmail(): void
);

// Assert logo-embeddable is persisted to a database
$this->accounts->shouldReceive('store')
$this->attachments->shouldReceive('store')
->once()
->with(
\Mockery::on(function (Attachment $attachment) {
Expand All @@ -59,7 +59,7 @@ public function testSendEmail(): void
);

// Assert hello.txt is persisted to a database
$this->accounts->shouldReceive('store')
$this->attachments->shouldReceive('store')
->once()
->with(
\Mockery::on(function (Attachment $attachment) {
Expand All @@ -74,7 +74,7 @@ public function testSendEmail(): void
);

// Assert hello.txt is persisted to a database
$this->accounts->shouldReceive('store')
$this->attachments->shouldReceive('store')
->once()
->with(
\Mockery::on(function (Attachment $attachment) {
Expand All @@ -88,21 +88,6 @@ public function testSendEmail(): void
}),
);


// Assert logo.svg is persisted to a database
$this->accounts->shouldReceive('store')
->once()
->with(
\Mockery::on(function (Attachment $attachment) {
$this->assertSame('logo.svg', $attachment->getFilename());
$this->assertSame('image/svg+xml', $attachment->getMime());
$this->assertSame(1206, $attachment->getSize());
$this->bucket->assertCreated($attachment->getPath());

return true;
}),
);

$client->send($email);
$this->validateMessage($id, (string) $connectionUuid);

Expand Down Expand Up @@ -173,7 +158,7 @@ private function validateMessage(string $messageId, string $uuid): void

$this->assertSame([], $parsedMessage->getBccs());

$this->assertSame(
$this->assertStringEqualsStringIgnoringLineEndings(
<<<'HTML'
<img src="cid:test-cid@buggregator">
Hello Alice.<br>This is a test message with 5 header fields and 4 lines in the message body.
Expand All @@ -182,7 +167,6 @@ private function validateMessage(string $messageId, string $uuid): void
$parsedMessage->htmlBody,
);

$this->assertSame('', $parsedMessage->htmlBody);
$this->assertStringContainsString(
"Subject: Test message\r
Date: Thu, 02 May 2024 16:01:33 +0000\r
Expand Down
11 changes: 9 additions & 2 deletions tests/Unit/Modules/Smtp/AttachmentStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function testStore(): void
filename: 'file1.txt',
content: 'Hello, world!',
type: 'text/plain',
contentId: 'file1@buggregator',
);

$attachment2 = new Attachment(
Expand All @@ -64,11 +65,13 @@ public function testStore(): void
$path1,
12,
'text/plain',
$attachment1->getId(),
$attachment1->getContentId(),
)
->once()
->andReturn($entity1 = $this->mockContainer(\Modules\Smtp\Domain\Attachment::class));

$entity1->shouldReceive('getUuid')->andReturn($uuid = Uuid::generate());

$this->bucket->shouldReceive('write')
->with($path2 = $eventUuid . '/image.png', 'image content')
->once()
Expand All @@ -93,7 +96,11 @@ public function testStore(): void
$this->attachments->shouldReceive('store')->with($entity1)->once();
$this->attachments->shouldReceive('store')->with($entity2)->once();

$this->storage->store($eventUuid, [$attachment1, $attachment2]);
$result = $this->storage->store($eventUuid, [$attachment1, $attachment2]);

$this->assertSame([
'file1@buggregator' => '/api/smtp/' . $eventUuid . '/attachments/preview/' . $uuid,
], $result);
}

public function testRemove(): void
Expand Down

0 comments on commit 5df3c65

Please sign in to comment.