-
Notifications
You must be signed in to change notification settings - Fork 22
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
Changes from all commits
7ad96e0
c26935a
fd1eb6d
d2ca779
75566c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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([ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need a dummy table? Just curious 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I switched it back from |
||
'--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)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out of curiosity, any specific reason for setting this config? 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} | ||
} |
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'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,4 +6,5 @@ | |
|
||
class Test extends Model | ||
{ | ||
public $timestamps = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this? I think I might be missing something 🙈 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
} |
There was a problem hiding this comment.
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? 🤔There was a problem hiding this comment.
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.