Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mpociot committed Jan 21, 2019
1 parent 05019d7 commit 1f90a5b
Show file tree
Hide file tree
Showing 10 changed files with 124 additions and 15 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
},
"require-dev": {
"phpunit/phpunit": "^7.0",
"orchestra/testbench": "3.7.*",
"zendframework/zend-mail": "^2.10"
},
"autoload": {
Expand Down
26 changes: 26 additions & 0 deletions database/migrations/create_mailbox_inbound_emails_table.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMailboxInboundEmails extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('mailbox_inbound_emails', function (Blueprint $table) {
$table->increments('id');
$table->nullableTimestamps();
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::dropIfExists('mailbox_inbound_emails');
}
}
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,7 @@
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<php>
<env name="DB_CONNECTION" value="testing"/>
</php>
</phpunit>
20 changes: 10 additions & 10 deletions src/Drivers/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@

namespace BeyondCode\Mailbox\Drivers;

use BeyondCode\Mailbox\Facades\Mailbox;
use BeyondCode\Mailbox\InboundEmail;
use Illuminate\Log\Logger;
use BeyondCode\Mailbox\Facades\Mailbox;
use Illuminate\Log\Events\MessageLogged;

class Log
{
public function __construct(Logger $logger)
{
$logger->listen(function ($log) {

});
}

public function processInboundEmail(InboundEmail $email)
public function processLog(MessageLogged $log)
{
Mailbox::callMailboxes($email);
$email = new InboundEmail([
'message' => $log->message
]);

if ($email->isValid()) {
Mailbox::callMailboxes($email);
}
}
}
5 changes: 5 additions & 0 deletions src/InboundEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,9 @@ public function forward($recipients)
'text' => $this->text()
]);
}

public function isValid(): bool
{
return $this->from() !== '' && ($this->text() !== '' || $this->html() !== '');
}
}
19 changes: 16 additions & 3 deletions src/MailboxServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace BeyondCode\Mailbox;

use BeyondCode\Mailbox\Drivers\Log;
use Illuminate\Log\Logger;
use ZBateson\MailMimeParser\Message;
use Illuminate\Support\ServiceProvider;
use Illuminate\Log\Events\MessageLogged;

class MailboxServiceProvider extends ServiceProvider
{
Expand All @@ -11,10 +15,10 @@ class MailboxServiceProvider extends ServiceProvider
*/
public function boot()
{
if ($this->app->runningInConsole()) {
if (! class_exists('CreateMailboxInboundEmails')) {
$this->publishes([
__DIR__.'/../config/mailbox.php' => config_path('mailbox.php'),
], 'config');
__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'),
], 'migrations');
}
}

Expand All @@ -28,5 +32,14 @@ public function register()
$this->app->singleton('mailbox', function () {
return new MailboxRouter($this->app);
});

if (config('mail.driver') === 'log') {
$this->registerLogDriver();
}
}

protected function registerLogDriver()
{
$this->app['events']->listen(MessageLogged::class, [new Log, 'processLog']);
}
}
42 changes: 42 additions & 0 deletions tests/Drivers/LogTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace BeyondCode\Mailbox\Tests\Drivers;

use BeyondCode\Mailbox\Facades\Mailbox;
use BeyondCode\Mailbox\InboundEmail;
use BeyondCode\Mailbox\Tests\TestCase;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;

class LogTest extends TestCase
{

protected function getPackageProviders($app)
{
$app['config']['mail.driver'] = 'log';

return parent::getPackageProviders($app);
}

/** @test */
public function it_catches_logged_mails()
{
Mailbox::from('{name}@beyondco.de', function(InboundEmail $email, $name) {
$this->assertSame($name, 'example');
$this->assertSame($email->from(), '[email protected]');
$this->assertSame($email->subject(), 'This is a subject');
});

Mail::to('[email protected]')->send(new TestMail);
}
}

class TestMail extends Mailable
{
public function build()
{
$this->from('[email protected]')
->subject('This is a subject')
->html('<html>Example email content</html>');
}
}
1 change: 0 additions & 1 deletion tests/MailboxRouteCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Zend\Mail\Message as TestMail;
use BeyondCode\Mailbox\InboundEmail;
use BeyondCode\Mailbox\MailboxRoute;
use PHPUnit\Framework\TestCase;
use BeyondCode\Mailbox\MailboxRouteCollection;

class MailboxRouteCollectionTest extends TestCase
Expand Down
1 change: 0 additions & 1 deletion tests/MailboxRouteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Zend\Mail\Message as TestMail;
use BeyondCode\Mailbox\InboundEmail;
use PHPUnit\Framework\TestCase;
use BeyondCode\Mailbox\MailboxRoute;

class MailboxRouteTest extends TestCase
Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace BeyondCode\Mailbox\Tests;

use BeyondCode\Mailbox\MailboxServiceProvider;

abstract class TestCase extends \Orchestra\Testbench\TestCase
{

protected function getPackageProviders($app)
{
return [MailboxServiceProvider::class];
}

protected function getEnvironmentSetUp($app)
{
include_once __DIR__.'/../database/migrations/create_mailbox_inbound_emails_table.php.stub';

(new \CreateMailboxInboundEmails())->up();
}
}

0 comments on commit 1f90a5b

Please sign in to comment.