Skip to content

Commit 1f90a5b

Browse files
committed
wip
1 parent 05019d7 commit 1f90a5b

File tree

10 files changed

+124
-15
lines changed

10 files changed

+124
-15
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
},
2727
"require-dev": {
2828
"phpunit/phpunit": "^7.0",
29+
"orchestra/testbench": "3.7.*",
2930
"zendframework/zend-mail": "^2.10"
3031
},
3132
"autoload": {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class CreateMailboxInboundEmails extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up()
13+
{
14+
Schema::create('mailbox_inbound_emails', function (Blueprint $table) {
15+
$table->increments('id');
16+
$table->nullableTimestamps();
17+
});
18+
}
19+
/**
20+
* Reverse the migrations.
21+
*/
22+
public function down()
23+
{
24+
Schema::dropIfExists('mailbox_inbound_emails');
25+
}
26+
}

phpunit.xml.dist

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,7 @@
2626
<log type="coverage-text" target="build/coverage.txt"/>
2727
<log type="coverage-clover" target="build/logs/clover.xml"/>
2828
</logging>
29+
<php>
30+
<env name="DB_CONNECTION" value="testing"/>
31+
</php>
2932
</phpunit>

src/Drivers/Log.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
namespace BeyondCode\Mailbox\Drivers;
44

5-
use BeyondCode\Mailbox\Facades\Mailbox;
65
use BeyondCode\Mailbox\InboundEmail;
7-
use Illuminate\Log\Logger;
6+
use BeyondCode\Mailbox\Facades\Mailbox;
7+
use Illuminate\Log\Events\MessageLogged;
88

99
class Log
1010
{
11-
public function __construct(Logger $logger)
12-
{
13-
$logger->listen(function ($log) {
1411

15-
});
16-
}
17-
18-
public function processInboundEmail(InboundEmail $email)
12+
public function processLog(MessageLogged $log)
1913
{
20-
Mailbox::callMailboxes($email);
14+
$email = new InboundEmail([
15+
'message' => $log->message
16+
]);
17+
18+
if ($email->isValid()) {
19+
Mailbox::callMailboxes($email);
20+
}
2121
}
2222
}

src/InboundEmail.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,9 @@ public function forward($recipients)
116116
'text' => $this->text()
117117
]);
118118
}
119+
120+
public function isValid(): bool
121+
{
122+
return $this->from() !== '' && ($this->text() !== '' || $this->html() !== '');
123+
}
119124
}

src/MailboxServiceProvider.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace BeyondCode\Mailbox;
44

5+
use BeyondCode\Mailbox\Drivers\Log;
6+
use Illuminate\Log\Logger;
7+
use ZBateson\MailMimeParser\Message;
58
use Illuminate\Support\ServiceProvider;
9+
use Illuminate\Log\Events\MessageLogged;
610

711
class MailboxServiceProvider extends ServiceProvider
812
{
@@ -11,10 +15,10 @@ class MailboxServiceProvider extends ServiceProvider
1115
*/
1216
public function boot()
1317
{
14-
if ($this->app->runningInConsole()) {
18+
if (! class_exists('CreateMailboxInboundEmails')) {
1519
$this->publishes([
16-
__DIR__.'/../config/mailbox.php' => config_path('mailbox.php'),
17-
], 'config');
20+
__DIR__.'/../database/migrations/create_mailbox_inbound_emails_table.php.stub' => database_path('migrations/'.date('Y_m_d_His', time()).'_create_mailbox_inbound_emails_table.php'),
21+
], 'migrations');
1822
}
1923
}
2024

@@ -28,5 +32,14 @@ public function register()
2832
$this->app->singleton('mailbox', function () {
2933
return new MailboxRouter($this->app);
3034
});
35+
36+
if (config('mail.driver') === 'log') {
37+
$this->registerLogDriver();
38+
}
39+
}
40+
41+
protected function registerLogDriver()
42+
{
43+
$this->app['events']->listen(MessageLogged::class, [new Log, 'processLog']);
3144
}
3245
}

tests/Drivers/LogTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Tests\Drivers;
4+
5+
use BeyondCode\Mailbox\Facades\Mailbox;
6+
use BeyondCode\Mailbox\InboundEmail;
7+
use BeyondCode\Mailbox\Tests\TestCase;
8+
use Illuminate\Mail\Mailable;
9+
use Illuminate\Support\Facades\Mail;
10+
11+
class LogTest extends TestCase
12+
{
13+
14+
protected function getPackageProviders($app)
15+
{
16+
$app['config']['mail.driver'] = 'log';
17+
18+
return parent::getPackageProviders($app);
19+
}
20+
21+
/** @test */
22+
public function it_catches_logged_mails()
23+
{
24+
Mailbox::from('{name}@beyondco.de', function(InboundEmail $email, $name) {
25+
$this->assertSame($name, 'example');
26+
$this->assertSame($email->from(), '[email protected]');
27+
$this->assertSame($email->subject(), 'This is a subject');
28+
});
29+
30+
Mail::to('[email protected]')->send(new TestMail);
31+
}
32+
}
33+
34+
class TestMail extends Mailable
35+
{
36+
public function build()
37+
{
38+
$this->from('[email protected]')
39+
->subject('This is a subject')
40+
->html('<html>Example email content</html>');
41+
}
42+
}

tests/MailboxRouteCollectionTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use Zend\Mail\Message as TestMail;
66
use BeyondCode\Mailbox\InboundEmail;
77
use BeyondCode\Mailbox\MailboxRoute;
8-
use PHPUnit\Framework\TestCase;
98
use BeyondCode\Mailbox\MailboxRouteCollection;
109

1110
class MailboxRouteCollectionTest extends TestCase

tests/MailboxRouteTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Zend\Mail\Message as TestMail;
66
use BeyondCode\Mailbox\InboundEmail;
7-
use PHPUnit\Framework\TestCase;
87
use BeyondCode\Mailbox\MailboxRoute;
98

109
class MailboxRouteTest extends TestCase

tests/TestCase.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace BeyondCode\Mailbox\Tests;
4+
5+
use BeyondCode\Mailbox\MailboxServiceProvider;
6+
7+
abstract class TestCase extends \Orchestra\Testbench\TestCase
8+
{
9+
10+
protected function getPackageProviders($app)
11+
{
12+
return [MailboxServiceProvider::class];
13+
}
14+
15+
protected function getEnvironmentSetUp($app)
16+
{
17+
include_once __DIR__.'/../database/migrations/create_mailbox_inbound_emails_table.php.stub';
18+
19+
(new \CreateMailboxInboundEmails())->up();
20+
}
21+
}

0 commit comments

Comments
 (0)