Foundry Fixtures for Inheritance
… Over in the terminal, run:
symfony console make:factory
One nifty feature of Foundry is that you can create factories individually,
or you can just go with "all" and it will make factories for all
entities that don't yet have one. Select "all" and…
Foundry: Fixtures You'll Love
…finishes, run
git status
to see that the recipe enabled a bundle and also created one config file... which
we won't need to look at.
In short, Foundry helps us create entity objects. It's... almost easier just to
see it in action. First…
Foundry Tricks
… We now have many Question objects and they represent
a rich set of unique data. This is why I love Foundry.
If you click into one of the questions, you can see that the slug is unique...
but was generated in a way that is…
Foundry: Fixture Model Factories
…has extra
features for test assertions!
The bundle was created by Kevin Bond - a long time
Symfony contributor and friend of mine who's been creating some really excellent
libraries lately. Foundry is Canadian for fun!
Let's get to work! Scroll down to the…
Foundry: Always Pass a Factory Instance to a Relation
…manual. Put our callback... back... and return
an array. There actually is a way in Foundry, to say:
please give me a random Question where some field matches some value.
But... in our case, we would need to say WHERE askedAt IS NOT NULL... which…
Pagination & Foundry Fixtures
…fixtures!
For this, I like to use Foundry along with DoctrineFixturesBundle. So, run
composer require foundry orm-fixtures --dev
to install both as dev dependencies. Once that finishes, run
php bin/console make:factory
If you haven't used Foundry before, for each entity, you…
Alien Tech for Fixtures: Foundry & Faker
…status
to see what the recipes did: it enabled a bundle and added a config file.
That config works well out of the box, so no need to look at it.
With Foundry, every entity can have a factory class.
To get these going run:
…
Setting Relations in Foundry
OK, we have a couple of parts and a few starships, but to fill our testing
data fleet: I want a lot more. This is a job perfectly suited for our good
friend: Foundry. Remove the manual code, then anywhere, say:
StarshipPartFactory::createMany(100):
And…
Many To Many with Foundry
… Normally,
this would trigger an angry error. But it will work!
Foundry sees that we have an addDroid() method, and it calls that instead,
one-by-one for each Droid.
Time to see this in action! Find your terminal and run:
symfony console doctrine:fixtures…
Relations in Foundry
…which is a really cool
method.
With this setup, when we call createMany(), Foundry will first call getDefaults(),
grab that array, add question to it, and then will ultimately try to create the
Answer using all of those values.
The QuestionFactory::random() method does what…
Handling ManyToMany in Foundry
Now that we've seen how we can relate Tag objects and Question objects, let's
use Foundry to properly create some fresh Tag fixture data. Start by generating
the Tag factory
symfony console make:factory
And... we want to generate the one for Tag…
Doing Crazy things with Foundry & Fixtures
…end up with extra questions that we don't really want.
By the way, we're about to see some really cool, really advanced stuff in
Foundry. But at the end, I'm also going to show a simpler solution to creating
the objects we…
Pagination
Foundry helped us add 20 ships. That makes our app look more realistic.
But on production, we might have thousands of starships. This page would be
gigantic and unusable. It'd probably also take a long time to load, time during
which we are likely…
Setting the Relation
…the properties. Then make sure this saves with $manager->persist($part), and
finally, $manager->flush():
Foundry usually calls persist() and flush() for us. But since we're in manual
mode, we need to do it ourselves.
We have a Starship and a StarshipPart, but they…
Doctrine Tests
…a real database and real entities for this!
Over in the terminal, install Zenstruck Foundry to help us manage our test
database and fixtures:
symfony composer require --dev zenstruck/foundry
Back in our IDE, in the tutorial directory, copy the Entity folder
into our bundle…
Orphan Removal
…Try reloading the fixtures:
No errors, and for the first time, we see that proxy object I've been mentioning.
Remember: when you create an object via Foundry, it hands you back your shiny new
object, but it's bundled up inside another object called…
Symfony, Doctrine Relations & Warp Drive Basics
…it's all about the objects!
Handling relationships in fixtures with Foundry
The OneToMany "inverse" side of a relation
Filtering a collection of related objects with Criteria
orphanRemoval (not as heartless as it sounds!)
Querying with Joins
Solving the N+1 problem
ManyToMany Relations
ManyToMany…
API Platform 3 Part 2: Security for your Treasures
…imaginable... and spin up a nifty test suite along the way:
Disabling documentation on production
Different types of API authentication
Logging in via Ajax & sessions
Creating an API Token system with "scopes"
Securing your API resources
Bootstrapping tests with zenstruck/browser & zenstruck/foundry!
How to…
State Processors: Hashing the User Password
…Now, we need to hash that password before the User
is saved to the database. Like we showed when working with Foundry, hashing a
password is simple: grab the UserPasswordHasherInterface service then call a
method on it:
But to pull this off, we need a…
Allow Admin Users to Edit any Treasure
…a public function testAdminCanPatchToEditTreasure().
Then create an admin user with UserFactory::createOne() passing roles set to
ROLE_ADMIN:
That'll work fine. But if we need to create a lot of admin users in our tests,
we can add a shortcut to Foundry. Open UserFactory…
x
178