diff --git a/src/MailViewer.php b/src/MailViewer.php index 54d3c3f..500bc25 100644 --- a/src/MailViewer.php +++ b/src/MailViewer.php @@ -4,6 +4,7 @@ use Exception; use Illuminate\Database\Eloquent\Factory as EloquentFactory; +use Illuminate\Support\Facades\DB; use ReflectionClass; class MailViewer @@ -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) { @@ -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); } @@ -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"); diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index af56529..90303af 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -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; @@ -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]; @@ -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)); } } diff --git a/tests/Factories/TestFactory.php b/tests/Database/Factories/TestFactory.php similarity index 100% rename from tests/Factories/TestFactory.php rename to tests/Database/Factories/TestFactory.php diff --git a/tests/Database/Migrations/2018_10_10_131000_add_test_table.php b/tests/Database/Migrations/2018_10_10_131000_add_test_table.php new file mode 100644 index 0000000..0b7a53b --- /dev/null +++ b/tests/Database/Migrations/2018_10_10_131000_add_test_table.php @@ -0,0 +1,30 @@ +boolean('is_awesome'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('tests'); + } +} diff --git a/tests/Stubs/Models/Test.php b/tests/Stubs/Models/Test.php index 7a6c33d..3b4bd56 100644 --- a/tests/Stubs/Models/Test.php +++ b/tests/Stubs/Models/Test.php @@ -6,4 +6,5 @@ class Test extends Model { + public $timestamps = false; }