-
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
Allow factory state to be set #10
Changes from all commits
9ba8bdb
f46993a
c196249
46ec27d
14bd8c0
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,6 +2,7 @@ | |
|
||
namespace JoggApp\MailViewer; | ||
|
||
use Illuminate\Database\Eloquent\Factory as EloquentFactory; | ||
use Illuminate\Support\ServiceProvider; | ||
|
||
class MailViewerServiceProvider extends ServiceProvider | ||
|
@@ -15,6 +16,15 @@ public function boot() | |
$this->loadRoutesFrom(__DIR__ . '/../routes/web.php'); | ||
|
||
$this->loadViewsFrom(__DIR__ . '/../views', 'mailviewer'); | ||
|
||
if (app()->environment() === 'testing') { | ||
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. Curious to know what is the benefit of this container binding since it might effect user's own testing env 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. I had to do this to make sure the |
||
$this->app->singleton(EloquentFactory::class, function ($app) { | ||
$faker = $app->make(\Faker\Generator::class); | ||
$factories_path = __DIR__.'/../tests/Factories'; | ||
|
||
return EloquentFactory::construct($faker, $factories_path); | ||
}); | ||
} | ||
} | ||
|
||
public function register() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
use JoggApp\MailViewer\Tests\Stubs\Models\Test; | ||
|
||
/** @var \Illuminate\Database\Eloquent\Factory $factory */ | ||
|
||
$factory->define(Test::class, function () { | ||
return [ | ||
'is_awesome' => 'no' | ||
]; | ||
}); | ||
|
||
$factory->state(Test::class, 'is-awesome', [ | ||
'is_awesome' => 'yes' | ||
]); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace JoggApp\MailViewer\Tests\Stubs\Mail; | ||
|
||
use Illuminate\Bus\Queueable; | ||
use Illuminate\Mail\Mailable; | ||
use Illuminate\Queue\SerializesModels; | ||
use JoggApp\MailViewer\Tests\Stubs\Models\Test; | ||
|
||
class TestEmailWithState extends Mailable | ||
{ | ||
use Queueable, SerializesModels; | ||
|
||
public $object; | ||
|
||
/** | ||
* Create a new message instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct(Test $object) | ||
{ | ||
$this->object = $object; | ||
} | ||
|
||
/** | ||
* Build the message. | ||
* | ||
* @return $this | ||
*/ | ||
public function build() | ||
{ | ||
return $this->view('mailviewer::stubs.emailtestview_withstate'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace JoggApp\MailViewer\Tests\Stubs\Models; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Test extends Model | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
The test email view | ||
|
||
Is awesome: {{ $object->is_awesome }} |
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.
Curious to know why
make()
overcreate()
😄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 first reason was the tests, the create function will try to save the Test model in the database as it's a Illuminate model. I can make it some general model that doesn't save to the database to make the test pass too. The other reason is that you probably don't want to save the records to the database when you are just viewing the email via the
/mails
page, especially on a production environment ;) I actually didn't realise this was happening until I was editing this code.