Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use database transactions #12

Merged
merged 5 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how are the factories persisted to the db if we are not using DB::commit() after this? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The factories don't actually have to be persisted in the DB for the view to work. As long as they exist when return new $mailable(...$args); is called the view is fine. They are never committed so keeps the database clean of data that was created just for the purpose of rendering the email view.

} else {
$args[] = app($dependency);
}
Expand All @@ -66,6 +69,8 @@ public static function find(string $mail)
}
}

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([
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need a dummy table? Just curious 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I switched it back from make to create for the factory creation the table needs to exist for the test

'--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));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, any specific reason for setting this config? 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When there is an error somewhere in the code the tests that fail dump the generated error page (because of the assertSee). Without this setting it is just the standard error page, with this setting enabled you can check the dumped html for the actual error so just helps debugging.

}
}
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need this? I think I might be missing something 🙈

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because the Test model is persisted to the database for the tests I either had to add $table->timestamps(); to the migration or just remove the timestamps for the model. I decided to go for the last one as the timestamps are irrelevant for the test anyway 😉

}
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');
}
}