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

Fabricator class #2936

Merged
merged 25 commits into from
May 18, 2020
Merged

Fabricator class #2936

merged 25 commits into from
May 18, 2020

Conversation

MGatner
Copy link
Member

@MGatner MGatner commented May 5, 2020

Description
This is something I keep needing and finally set out to write a module for it, then realized it might be appropriate for the core framework.

Fabricator is a test utility used to create faked data based on Model definitions. Similar to Laravel's "factories" it creates instances of $model->returnType with random data generated by Faker.

Checklist:

  • Add create() method for DB inserts and "faux" inserts
  • Write unit tests
  • Update User Guide
  • Securely signed commits
  • Component(s) with PHPdocs
  • Conforms to style guide

@MGatner
Copy link
Member Author

MGatner commented May 5, 2020

@lonnieezell et al, still a fair amount of work to do but please take a look at the concept and weigh in on if you consider this something for the core framework.

@lonnieezell
Copy link
Member

@MGatner yes, I would love to have something like this. I've wanted it numerous times. :)

Only catch - it should be able to work for those who don't want to use Composer, unfortunately. Maybe a super simple class that spits out some semi-random stuff for a handful of common elements if Faker isn't present?

Another thought is to make it a Project instead. I don't have a problem with those requiring Composer.

@MGatner
Copy link
Member Author

MGatner commented May 5, 2020

I understand the need to support non-Composer installations, but just to push back a little:

  1. Faker would be a require-dev specification, and we already have a few that aren’t supported by downloads. This would be the first class that requires a Composer package outside of framework tests but it also is very specifically only for testing. I don’t feel the same need to support download-only unit tests as I do for projects, especially since the developer will already need to be downloading content (like PHPUnit).

  2. It wouldn’t be hard to add a non-Composer version that always returns (say) a random string of characters. However the unit tests for this class and the code would suggest to a developer that certain formatters would return valid types (like “email”) which would then fail with our non-Composer version. I could see that being confusing.

@lonnieezell
Copy link
Member

That's a fair point that testing already requires composer. That's convincing enough for me. :)

@MGatner MGatner changed the title WIP Fabricator class Fabricator class May 8, 2020
@MGatner MGatner requested a review from lonnieezell May 8, 2020 20:12
@MGatner
Copy link
Member Author

MGatner commented May 8, 2020

I think this is ready! @lonnieezell take a look if you would. And anyone else who sees this, I'd appreciate feedback/suggestions/critiques. A lot harder to change things once they are released so let's make this good up front.

Copy link
Member

@lonnieezell lonnieezell left a comment

Choose a reason for hiding this comment

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

All in all - I really like it. I was a little concerned at first about having to provide the formatters in each class. I could see that being a giant PITA to maintain consistency. but I think your solution of the fake() method in the model works well.

user_guide_src/source/testing/fabricator.rst Outdated Show resolved Hide resolved
system/Test/Fabricator.php Outdated Show resolved Hide resolved
system/Test/Fabricator.php Outdated Show resolved Hide resolved
system/Test/Fabricator.php Outdated Show resolved Hide resolved
@MGatner
Copy link
Member Author

MGatner commented May 16, 2020

Thanks! Re: formatters, I wanted to be sure there was a way people could run distinct tests without modifying models. I will probably always use extended models in Tests\Support with the fake() method like I described in the doc, but the option is there.

Looking over your comments now.

@MGatner
Copy link
Member Author

MGatner commented May 16, 2020

Requested changes implemented!

@MGatner MGatner requested a review from lonnieezell May 16, 2020 16:27
@lonnieezell
Copy link
Member

Looks great to me. I'm assuming nothing else you want to do to it? If so - feel free to merge!

@MGatner MGatner merged commit b5e1941 into codeigniter4:develop May 18, 2020
@MGatner MGatner deleted the fabricator branch November 28, 2020 18:45
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.

3 participants