Skip to content

Commit

Permalink
Merge pull request #12 from kielabokkie/master
Browse files Browse the repository at this point in the history
Use database transactions
  • Loading branch information
introwit authored Oct 11, 2018
2 parents 9843f46 + 75566c4 commit cbab6a2
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/MailViewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Database\Eloquent\Factory as EloquentFactory;
use Illuminate\Support\Facades\DB;
use ReflectionClass;

class MailViewer
Expand Down Expand Up @@ -33,6 +34,8 @@ public static function all()

public static function find(string $mail)
{
DB::beginTransaction();

$eloquentFactory = app(EloquentFactory::class);

foreach (config('mailviewer.mailables', []) as $mailable => $dependencies) {
Expand All @@ -53,7 +56,7 @@ public static function find(string $mail)

if (is_string($dependency) && class_exists($dependency)) {
if (isset($eloquentFactory[$dependency])) {
$args[] = factory($dependency)->states($factoryStates)->make();
$args[] = factory($dependency)->states($factoryStates)->create();
} else {
$args[] = app($dependency);
}
Expand All @@ -64,6 +67,8 @@ public static function find(string $mail)

return new $mailable(...$args);
}

DB::rollBack();
}

throw new Exception("No mailable called {$mail} is registered in config/mailviewer.php file");
Expand Down
27 changes: 21 additions & 6 deletions tests/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace JoggApp\MailViewer\Tests;

use Illuminate\Database\Eloquent\Factory as EloquentFactory;
use JoggApp\MailViewer\MailViewerServiceProvider;
use JoggApp\MailViewer\Tests\Stubs\Mail\TestEmailForMailViewer;
use JoggApp\MailViewer\Tests\Stubs\Mail\TestEmailWithDependencies;
Expand All @@ -12,6 +11,20 @@

class BaseTestCase extends TestCase
{
public function setUp()
{
parent::setUp();

$this->withFactories(__DIR__ . '/Database/Factories');

$this->loadMigrationsFrom([
'--database' => 'testing',
'--path' => __DIR__ . '/Database/Migrations'
]);

$this->artisan('migrate', ['--database' => 'testing']);
}

protected function getPackageProviders($app)
{
return [MailViewerServiceProvider::class];
Expand Down Expand Up @@ -44,11 +57,13 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('mailviewer.allowed_environments', ['local', 'staging', 'testing']);
$app['config']->set('mailviewer.middlewares', []);

$app->singleton(EloquentFactory::class, function ($app) {
$faker = $app->make(\Faker\Generator::class);
$factories_path = __DIR__ . '/Factories';
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);

return EloquentFactory::construct($faker, $factories_path);
});
$app['config']->set('app.debug', env('APP_DEBUG', true));
}
}
File renamed without changes.
30 changes: 30 additions & 0 deletions tests/Database/Migrations/2018_10_10_131000_add_test_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

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

class AddTestTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tests', function (Blueprint $table) {
$table->boolean('is_awesome');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('tests');
}
}
1 change: 1 addition & 0 deletions tests/Stubs/Models/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@

class Test extends Model
{
public $timestamps = false;
}

0 comments on commit cbab6a2

Please sign in to comment.