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

Allow factory state to be set #10

Merged
merged 5 commits into from
Oct 4, 2018
Merged

Allow factory state to be set #10

merged 5 commits into from
Oct 4, 2018

Conversation

kielabokkie
Copy link
Contributor

This PR allows you to pass factory states via the config like below:

'mailables' => [
    UserVerified::class => [
        ['class' => User::class, 'states' => ['verified']]
    ]
]

@kielabokkie
Copy link
Contributor Author

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


/** @var \Illuminate\Database\Eloquent\Factory $factory */

$factory->define(Test::class, function () {
Copy link
Member

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 😄

$factoryStates = [];

if (is_array($dependency)) {
// Check if the dependency array contains factory states
Copy link
Member

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 😄


if (is_array($dependency)) {
// Check if the dependency array contains factory states
if (in_array('states', array_keys($dependency)) === true) {
Copy link
Member

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 🙂

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

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

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() 😄

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 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.

@@ -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) {
Copy link
Member

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 🙂

@@ -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"
Copy link
Member

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

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

Copy link
Contributor Author

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.

@introwit
Copy link
Member

introwit commented Oct 4, 2018

@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 😄

@kielabokkie
Copy link
Contributor Author

@introwit I've updated the PR based on your feedback. Let me know if there is anything else I can do.

@introwit
Copy link
Member

introwit commented Oct 4, 2018

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 v2.0.3 😄 Great work Wouter ❤️

@introwit introwit merged commit 6ff5c5b into JoggApp:master Oct 4, 2018
@kielabokkie
Copy link
Contributor Author

@introwit Yes of course, that's not a problem at all. Thank you for the awesome package 👍

@introwit
Copy link
Member

introwit commented Oct 4, 2018

@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 👌

@kielabokkie
Copy link
Contributor Author

@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.

@introwit
Copy link
Member

introwit commented Oct 4, 2018

@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 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants