-
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
Conversation
I have a few Mailables that need my model to be in a specific state. It makes the mailables configuration array a bit less clean but more flexible. Let me know what you think @introwit |
database/factories/TestFactory.php
Outdated
|
||
/** @var \Illuminate\Database\Eloquent\Factory $factory */ | ||
|
||
$factory->define(Test::class, function () { |
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.
I think we can move this file to the tests
directory 😄
src/MailViewer.php
Outdated
$factoryStates = []; | ||
|
||
if (is_array($dependency)) { | ||
// Check if the dependency array contains factory states |
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.
I feel we can remove this comment since the code is readable enough 😄
src/MailViewer.php
Outdated
|
||
if (is_array($dependency)) { | ||
// Check if the dependency array contains factory states | ||
if (in_array('states', array_keys($dependency)) === true) { |
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.
We can remove that === true
since in_array
returns a boolean iirc 🙂
src/MailViewer.php
Outdated
if (is_array($dependency)) { | ||
// Check if the dependency array contains factory states | ||
if (in_array('states', array_keys($dependency)) === true) { | ||
// Set the states and actual dependency class |
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.
I guess we can remove this comment too 😄
if (is_string($dependency) && class_exists($dependency)) { | ||
if (isset($eloquentFactory[$dependency])) { | ||
$args[] = factory($dependency)->create(); | ||
$args[] = factory($dependency)->states($factoryStates)->make(); |
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()
over create()
😄
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.
src/MailViewer.php
Outdated
@@ -70,6 +81,14 @@ public static function prepareMails(array $mailables): array | |||
$givenParameters = []; | |||
|
|||
foreach ($dependencies as $dependency) { | |||
if (is_array($dependency)) { | |||
// Check if the dependency array contains factory states | |||
if (in_array('states', array_keys($dependency)) === true) { |
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.
Same as above for the comment and === true
🙂
src/MailViewer.php
Outdated
@@ -83,8 +102,7 @@ public static function prepareMails(array $mailables): array | |||
|
|||
if ($constructorParameters !== $givenParameters) { | |||
throw new Exception( | |||
"The arguments passed for {$mailable} in the config/mailviewer.php file do not match with the constructor | |||
params of the {$mailable} class or the constructor params of the {$mailable} class aren't typehinted" | |||
"The arguments passed for {$mailable} in the config/mailviewer.php file do not match with the constructor params of the {$mailable} class or the constructor params of the {$mailable} class aren't typehinted" |
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.
Can you revert this change so that we adhere to PSR2's guideline for length of the line being 120 chars max. Also, we won't need to horizontally scroll when interacting with this file on GitHub if done 😄
@@ -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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
I had to do this to make sure the TestFactory.php
class would be loaded when running the unit tests. Laravel sets the default environment to testing
when running unit tests so shouldn't cause a problem for anyone's testing env. See the code on Laravel's repo.
@kielabokkie Thank you so much for the contribution Wouter ❤️ 🙂 Just some minor stuff and few questions out of curiosity, rest looks great 👌 Also, a small request can you update the comment in the config file to document the new behaviour & feature if possible? Thank you again 😄 |
@introwit I've updated the PR based on your feedback. Let me know if there is anything else I can do. |
Thank you so much @kielabokkie 🙂 Is it okay if I tag you in future conversations related to this feature, if any comes up? I will merge this and tag a new minor release |
@introwit Yes of course, that's not a problem at all. Thank you for the awesome package 👍 |
@kielabokkie thank you for the contribution Wouter 🙂 Have tagged the new release v2.1.0 and added credits to you in the changelog & release. Great work 👌 |
@introwit I just realised an issue with the environment check in the service provider. All the unit tests that use factories on my project are now failing because it's only loading the factory that is part of this package. I'll have a quick look to see how I can fix this. |
@kielabokkie if it takes too long just send a PR that removes that test factory & that binding, since it's just needed for the test anyways 🙂 |
This PR allows you to pass factory states via the config like below: