-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
836a1fe
commit 8ac8036
Showing
4 changed files
with
174 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace yii1tech\mailer\transport; | ||
|
||
use Symfony\Component\Mailer\SentMessage; | ||
use Symfony\Component\Mailer\Transport\AbstractTransport; | ||
use Symfony\Component\Mime\RawMessage; | ||
|
||
/** | ||
* ArrayTransport stores incoming messages in the internal list, instead of sending them. | ||
* | ||
* This transport can be useful while writing unit tests. | ||
* | ||
* In {@see \yii1tech\mailer\Mailer::getTransport()} this transport will be created for DSN 'array' or 'array://'. | ||
* | ||
* @author Paul Klimov <[email protected]> | ||
* @since 1.0 | ||
*/ | ||
class ArrayTransport extends AbstractTransport | ||
{ | ||
/** | ||
* @var \Symfony\Component\Mime\RawMessage[]|\Symfony\Component\Mime\Email[]|\yii1tech\mailer\TemplatedEmail[] | ||
*/ | ||
private $sentMessages = []; | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
protected function doSend(SentMessage $message): void | ||
{ | ||
$this->sentMessages[] = $message->getOriginalMessage(); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function __toString(): string | ||
{ | ||
return 'array://'; | ||
} | ||
|
||
/** | ||
* @return \Symfony\Component\Mime\RawMessage[]|\Symfony\Component\Mime\Email[]|\yii1tech\mailer\TemplatedEmail[] sent messages. | ||
*/ | ||
public function getSentMessages(): array | ||
{ | ||
return $this->sentMessages; | ||
} | ||
|
||
/** | ||
* @return \Symfony\Component\Mime\RawMessage|\Symfony\Component\Mime\Email|\yii1tech\mailer\TemplatedEmail|null last sent message. | ||
*/ | ||
public function getLastSentMessage(): ?RawMessage | ||
{ | ||
if (empty($this->sentMessages)) { | ||
return null; | ||
} | ||
|
||
$sentMessages = $this->sentMessages; | ||
|
||
return array_pop($sentMessages); | ||
} | ||
|
||
/** | ||
* Clears the internal list of sent messages. | ||
* | ||
* @return static self reference. | ||
*/ | ||
public function clearSentMessages(): self | ||
{ | ||
$this->sentMessages = []; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Filters the internal list of sent messages according to the given callback. | ||
* | ||
* Callback signature: | ||
* | ||
* ``` | ||
* function (\Symfony\Component\Mailer\SentMessage $message): bool | ||
* ``` | ||
* | ||
* The callback is applied over each sent message. | ||
* In case the callback returns `true` the message will be returned in result set. | ||
* | ||
* @param callable $filterCallback filter callback. | ||
* @return \Symfony\Component\Mime\RawMessage[]|\Symfony\Component\Mime\Email[]|\yii1tech\mailer\TemplatedEmail[] sent messages. | ||
*/ | ||
public function filterSentMessages(callable $filterCallback): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($this->sentMessages as $message) { | ||
if (call_user_func($filterCallback, $message)) { | ||
$result[] = $message; | ||
} | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace yii1tech\mailer\test\transport; | ||
|
||
use Symfony\Component\Mime\Email; | ||
use Yii; | ||
use yii1tech\mailer\Mailer; | ||
use yii1tech\mailer\test\TestCase; | ||
use yii1tech\mailer\transport\ArrayTransport; | ||
|
||
class ArrayTransportTest extends TestCase | ||
{ | ||
public function testSend(): void | ||
{ | ||
$transport = new ArrayTransport(); | ||
|
||
/** @var Mailer $mailer */ | ||
$mailer = Yii::createComponent([ | ||
'class' => Mailer::class, | ||
'transport' => $transport, | ||
]); | ||
|
||
$email = new Email(); | ||
$email->from('[email protected]'); | ||
$email->addTo('[email protected]'); | ||
$email->subject('Test subject'); | ||
$email->text('Test body'); | ||
|
||
$mailer->send($email); | ||
|
||
$this->assertCount(1, $transport->getSentMessages()); | ||
|
||
$sentMessage = $transport->getLastSentMessage(); | ||
$this->assertSame('[email protected]', $sentMessage->getFrom()[0]->getAddress()); | ||
|
||
$messages = $transport->filterSentMessages(function (Email $message) { | ||
return $message->getSubject() === 'Test subject'; | ||
}); | ||
$this->assertCount(1, $messages); | ||
|
||
$messages = $transport->filterSentMessages(function (Email $message) { | ||
return $message->getSubject() === 'Fake'; | ||
}); | ||
$this->assertCount(0, $messages); | ||
} | ||
} |