Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/Facades/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@

use Phenix\Mail\Constants\MailerType;
use Phenix\Mail\Contracts\Mailable as MailableContract;
use Phenix\Mail\Contracts\Mailer;
use Phenix\Mail\MailManager;
use Phenix\Runtime\Facade;
use Phenix\Testing\TestMail;

/**
* @method static \Phenix\Mail\Contracts\Mailer mailer(MailerType|null $mailerType = null)
* @method static \Phenix\Mail\Contracts\Mailer using(MailerType $mailerType)
* @method static \Phenix\Mail\Contracts\Mailer to(array|string $to)
* @method static void send(\Phenix\Mail\Contracts\Mailable $mailable)
* @method static \Phenix\Mail\Contracts\Mailer fake(\Phenix\Mail\Constants\MailerType|null $mailerType = null)
* @method static Mailer mailer(MailerType|null $mailerType = null)
* @method static Mailer using(MailerType $mailerType)
* @method static Mailer to(array|string $to)
* @method static void send(MailableContract $mailable)
* @method static Mailer fake(MailerType|null $mailerType = null)
* @method static array getSendingLog(MailerType|null $mailerType = null)
* @method static void resetSendingLog(MailerType|null $mailerType = null)
* @method static TestMail expect(MailableContract|string $mailable, MailerType|null $mailerType = null)
*
* @see \Phenix\Mail\MailManager
Expand All @@ -29,11 +32,9 @@ public static function getKeyName(): string

public static function expect(MailableContract|string $mailable, MailerType|null $mailerType = null): TestMail
{
$mailerType ??= MailerType::from(Config::get('mail.default'));

return new TestMail(
$mailable,
self::mailer($mailerType)->getSendingLog()
self::getSendingLog($mailerType)
);
}
}
2 changes: 2 additions & 0 deletions src/Mail/Contracts/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public function bcc(array|string $bcc): self;
public function send(Mailable $mailable): void;

public function getSendingLog(): array;

public function resetSendingLog(): void;
}
14 changes: 14 additions & 0 deletions src/Mail/MailManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,20 @@ public function fake(MailerType|null $mailerType = null): void
$this->config->setLogTransport($mailerType);
}

public function getSendingLog(MailerType|null $mailerType = null): array
{
$mailerType ??= MailerType::from($this->config->default());

return $this->mailer($mailerType)->getSendingLog();
}

public function resetSendingLog(MailerType|null $mailerType = null): void
{
$mailerType ??= MailerType::from($this->config->default());

$this->mailer($mailerType)->resetSendingLog();
}

protected function resolveMailer(MailerType $mailer): MailerContract
{
return match ($mailer) {
Expand Down
5 changes: 5 additions & 0 deletions src/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ public function getSendingLog(): array
return $this->sendingLog;
}

public function resetSendingLog(): void
{
$this->sendingLog = [];
}

protected function serviceConfig(): array
{
return [];
Expand Down
76 changes: 76 additions & 0 deletions src/Testing/Concerns/InteractWithHeaders.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Phenix\Testing\Concerns;

use PHPUnit\Framework\Assert;

trait InteractWithHeaders
{
protected string $missingHeaderMessage = 'Response does not have a Content-Type header.';

public function getHeaders(): array
{
return $this->response->getHeaders();
}

public function getHeader(string $name): string|null
{
return $this->response->getHeader($name);
}

public function assertHeaderContains(array $needles): self
{
$needles = (array) $needles;

foreach ($needles as $header => $value) {
Assert::assertNotNull($this->response->getHeader($header));
Assert::assertEquals($value, $this->response->getHeader($header));
}

return $this;
}

public function assertIsJson(): self
{
$contentType = $this->response->getHeader('content-type');

Assert::assertNotNull($contentType, $this->missingHeaderMessage);
Assert::assertStringContainsString(
'application/json',
$contentType,
'Response does not have a JSON content type.'
);

return $this;
}

public function assertIsHtml(): self
{
$contentType = $this->response->getHeader('content-type');

Assert::assertNotNull($contentType, $this->missingHeaderMessage);
Assert::assertStringContainsString(
'text/html',
$contentType,
'Response does not have an HTML content type.'
);

return $this;
}

public function assertIsPlainText(): self
{
$contentType = $this->response->getHeader('content-type');

Assert::assertNotNull($contentType, $this->missingHeaderMessage);
Assert::assertStringContainsString(
'text/plain',
$contentType,
'Response does not have a plain text content type.'
);

return $this;
}
}
Loading