Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Jan 28, 2017
1 parent 8a54fdf commit 1ee71b5
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 35 deletions.
9 changes: 7 additions & 2 deletions src/SendTestMail.php
Expand Up @@ -5,6 +5,7 @@
use Mail;
use Exception;
use Illuminate\Console\Command;
use Validator;

class SendTestMail extends Command
{
Expand All @@ -26,9 +27,13 @@ public function handle()
$this->comment('Mail sent!');
}

public function guardAgainstInvalidArguments(): void
public function guardAgainstInvalidArguments()
{
if (! validate($this->argument('recipient'), 'email')) {
$validator = Validator::make(
['email' => $this->argument('recipient')],
['email' => 'email']);

if (! $validator->passes()) {
throw new Exception("`{$this->argument('recipient')}` is not a valid e-mail address");
}
}
Expand Down
35 changes: 4 additions & 31 deletions tests/MailableFactoryTest.php
Expand Up @@ -22,42 +22,15 @@ public function setUp()
}

/** @test */
public function it_test()
public function it_can_create_a_mailable()
{
$mailableClass = new class extends BaseMailable
{
/** @var int */
public $myInteger = null;

/** @var string */
public $myString = null;

/** @var bool */
public $myBool = null;

/** @var TestModel */
public $myModel = null;

public function __construct(
int $myInteger = null,
string $myString = null,
bool $myBool = null,
TestModel $myModel = null
)
{
$this->myInteger = $myInteger;
$this->myString = $myString;
$this->myBool = $myBool;
$this->myModel = $myModel;
}
};

$mailableClassInstance = $this->mailableFactory->getInstance(
get_class($mailableClass),
TestMailable::class,
'recepient@mail.com'
);

$this->assertInstanceOf(get_class($mailableClass), $mailableClassInstance);
$this->assertInstanceOf(TestMailable::class, $mailableClassInstance);

$this->assertTrue(is_int($mailableClassInstance->myInteger));
$this->assertTrue(is_string($mailableClassInstance->myString));
$this->assertTrue(is_bool($mailableClassInstance->myBool));
Expand Down
43 changes: 43 additions & 0 deletions tests/SendTestMailTest.php
@@ -0,0 +1,43 @@
<?php

namespace Spatie\MailableTest\Test;

use Exception;
use Mail;
use Artisan;

class SendTestMailTest extends TestCase
{
/** @test */
public function it_can_send_a_mail()
{
Mail::fake();

TestModel::create(['name' => 'my name']);

Artisan::call('mail:send-test', [
'mailableClass' => TestMailable::class,
'recipient' => 'recepient@mail.com'
]);

Mail::assertSent(TestMailable::class, function (TestMailable $mail) {
$this->assertCount(1, $mail->to);
$this->assertEquals('recepient@mail.com', $mail->to[0]['address']);
$this->assertCount(0, $mail->cc);
$this->assertCount(0, $mail->bcc);

return true;
});
}

/** @test */
public function it_will_throw_an_exception_when_passing_an_invalid_mail_address()
{
$this->expectException(Exception::class);

Artisan::call('mail:send-test', [
'mailableClass' => TestMailable::class,
'recipient' => 'invalid-mail-address'
]);
}
}
27 changes: 25 additions & 2 deletions tests/BaseMailable.php → tests/TestMailable.php
Expand Up @@ -7,9 +7,32 @@
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

abstract class BaseMailable extends Mailable implements ShouldQueue
class TestMailable extends Mailable
{
use Queueable, SerializesModels;
/** @var int */
public $myInteger;

/** @var string */
public $myString;

/** @var bool */
public $myBool;

/** @var TestModel */
public $myModel;

public function __construct(
int $myInteger,
string $myString,
bool $myBool,
TestModel $myModel
)
{
$this->myInteger = $myInteger;
$this->myString = $myString;
$this->myBool = $myBool;
$this->myModel = $myModel;
}

/**
* Build the message.
Expand Down

0 comments on commit 1ee71b5

Please sign in to comment.