Skip to content

Commit

Permalink
Add command send-test-mail
Browse files Browse the repository at this point in the history
  • Loading branch information
spawnia authored Dec 18, 2023
1 parent 1c3cdf8 commit 4520682
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 13 deletions.
22 changes: 12 additions & 10 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@master
uses: actions/checkout@v4

- name: "Install PHP with extensions"
uses: shivammathur/setup-php@v2
with:
coverage: none
extensions: mbstring
php-version: 8.1
php-version: 8.3

- name: "Validate composer.json and composer.lock"
run: composer validate --strict
Expand All @@ -39,7 +39,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

Expand All @@ -48,7 +48,7 @@ jobs:
with:
coverage: none
extensions: mbstring
php-version: 8.1
php-version: 8.3

- name: "Install locked dependencies with composer"
run: composer install --no-interaction --no-progress --no-suggest
Expand All @@ -68,7 +68,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@v2
uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}

Expand All @@ -77,7 +77,7 @@ jobs:
with:
coverage: none
extensions: mbstring
php-version: 8.1
php-version: 8.3

- name: "Install dependencies with composer"
run: composer install --no-interaction --no-progress --no-suggest
Expand All @@ -100,12 +100,13 @@ jobs:
php-version:
- "8.1"
- "8.2"
- "8.3"
laravel-version:
- ^9.51
- ^10

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
Expand All @@ -131,6 +132,7 @@ jobs:
php-version:
- "8.1"
- "8.2"
- "8.3"
dependencies:
- lowest
- highest
Expand All @@ -150,7 +152,7 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@master
uses: actions/checkout@v4

- name: "Install PHP with extensions"
uses: shivammathur/setup-php@v2
Expand Down Expand Up @@ -190,14 +192,14 @@ jobs:

steps:
- name: "Checkout"
uses: actions/checkout@master
uses: actions/checkout@v4

- name: "Install PHP with extensions"
uses: shivammathur/setup-php@v2
with:
coverage: pcov
extensions: mbstring
php-version: 8.1
php-version: 8.3

- name: "Install dependencies with composer"
run: composer install --no-interaction --no-progress --no-suggest
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ See [GitHub releases](https://github.com/mll-lab/laravel-utils/releases).

## Unreleased

## v4.7.0

### Added

- Add command `send-test-mail`

## v4.6.0

### Added
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@
"ergebnis/composer-normalize": "^2",
"jangregor/phpstan-prophecy": "^1",
"jbzoo/mermaid-php": "^2.3",
"larastan/larastan": "^2",
"laravel/framework": "^9 || ^10",
"mll-lab/graphql-php-scalars": "^4 || ^5",
"mll-lab/php-cs-fixer-config": "^5",
"mll-lab/rector-config": "^2",
"nunomaduro/larastan": "^2",
"orchestra/testbench": "^7.7 || ^8",
"phpstan/extension-installer": "^1",
"phpstan/phpstan-deprecation-rules": "^1",
Expand Down Expand Up @@ -72,6 +72,7 @@
"providers": [
"MLL\\LaravelUtils\\LaravelUtilsServiceProvider",
"MLL\\LaravelUtils\\Database\\DatabaseServiceProvider",
"MLL\\LaravelUtils\\Mail\\MailServiceProvider",
"MLL\\LaravelUtils\\ModelStates\\ModelStatesServiceProvider"
]
}
Expand Down
2 changes: 1 addition & 1 deletion php.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM php:8.1-cli
FROM php:8.3-cli

WORKDIR /workdir

Expand Down
15 changes: 15 additions & 0 deletions src/Mail/MailServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace MLL\LaravelUtils\Mail;

use Illuminate\Support\ServiceProvider;

class MailServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->commands([
SendTestMailCommand::class,
]);
}
}
36 changes: 36 additions & 0 deletions src/Mail/SendTestMailCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php declare(strict_types=1);

namespace MLL\LaravelUtils\Mail;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Mail;

class SendTestMailCommand extends Command
{
protected $signature = '
send-test-mail
{--from= : Sender}
{--to= : Recipient}
';

public function handle(): void
{
$from = $this->option('from');
if (! is_string($from)) {
$fromType = gettype($from);
throw new \UnexpectedValueException("Expected option --from to be string, got {$fromType}.");
}

$to = $this->option('to');
if (! is_string($to)) {
$toType = gettype($to);
throw new \UnexpectedValueException("Expected option --to to be string, got {$toType}.");
}

$mail = (new TestMail())
->from($from)
->to($to);

Mail::send($mail);
}
}
15 changes: 15 additions & 0 deletions src/Mail/TestMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php declare(strict_types=1);

namespace MLL\LaravelUtils\Mail;

use Illuminate\Mail\Mailable;

class TestMail extends Mailable
{
/** @return $this */
public function build(): self
{
return $this->subject('This is a test mail sent by php artisan send-test-mail')
->html(/** @lang HTML */ '<p>This is a test mail sent by php artisan send-test-mail</p>');
}
}
31 changes: 31 additions & 0 deletions tests/Mail/SendTestMailCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types=1);

namespace MLL\LaravelUtils\Tests\Mail;

use Illuminate\Support\Facades\Mail;
use MLL\LaravelUtils\Mail\TestMail;
use MLL\LaravelUtils\Tests\TestCase;

final class SendTestMailCommandTest extends TestCase
{
public function testSendsTestMail(): void
{
$mailFake = Mail::fake();

$from = '[email protected]';
$to = '[email protected]';

$this->artisan('send-test-mail', [
'--from' => $from,
'--to' => $to,
]);

$sent = $mailFake->sent(TestMail::class);
self::assertCount(1, $sent);

$testMail = $sent->firstOrFail();
assert($testMail instanceof TestMail);
$testMail->assertFrom($from);
$testMail->assertTo($to);
}
}
2 changes: 1 addition & 1 deletion tests/ModelStates/StateTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);

namespace MLL\LaravelUtils\ModelStates\Tests;
namespace MLL\LaravelUtils\Tests\ModelStates;

use App\ModelStates\ModelStates\StateA;
use App\ModelStates\ModelStates\StateB;
Expand Down
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MLL\LaravelUtils\Tests;

use MLL\LaravelUtils\LaravelUtilsServiceProvider;
use MLL\LaravelUtils\Mail\MailServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
Expand All @@ -20,6 +21,7 @@ protected function getPackageProviders($app): array
{
return [
LaravelUtilsServiceProvider::class,
MailServiceProvider::class,
];
}
}

0 comments on commit 4520682

Please sign in to comment.