WEBVTT NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com 00:00:01.046 --> 00:00:05.946 align:middle In the load() method of the fixture class, we can create as much dummy data as we want. 00:00:06.356 --> 00:00:07.186 align:middle Right now... 00:00:07.366 --> 00:00:09.876 align:middle we're creating exactly one Question... 00:00:10.136 --> 00:00:13.916 align:middle which isn't making for a very realistic experience. 00:00:14.626 --> 00:00:16.686 align:middle If we created more questions... 00:00:16.736 --> 00:00:21.916 align:middle and especially in the future when we will have multiple database tables that relate 00:00:21.916 --> 00:00:25.146 align:middle to each other, this class would start to get ugly. 00:00:25.516 --> 00:00:27.266 align:middle It's... already kind of ugly. 00:00:28.066 --> 00:00:30.306 align:middle No, we deserve better! 00:00:30.986 --> 00:00:34.076 align:middle Let's use a super fun new library instead. 00:00:34.496 --> 00:00:39.286 align:middle Google for "Zenstruck Foundry" and find its GitHub Page. 00:00:40.076 --> 00:00:45.636 align:middle Foundry is all about creating Doctrine entity objects in an easy, repeatable way. 00:00:46.286 --> 00:00:50.766 align:middle It's perfect for fixtures as well as for functional tests where you want 00:00:50.766 --> 00:00:54.066 align:middle to seed your database with data at the start of each test. 00:00:54.686 --> 00:00:57.476 align:middle It even has extra features for test assertions! 00:00:58.106 --> 00:01:03.186 align:middle The bundle was created by Kevin Bond - a long time Symfony contributor and friend 00:01:03.186 --> 00:01:07.266 align:middle of mine who's been creating some really excellent libraries lately. 00:01:07.836 --> 00:01:10.106 align:middle Foundry is Canadian for fun! 00:01:11.156 --> 00:01:11.866 align:middle Let's get to work! 00:01:12.336 --> 00:01:13.806 align:middle Scroll down to the installation, 00:01:14.036 --> 00:01:18.446 align:middle copy the composer require line, find your terminal and paste. 00:01:18.616 --> 00:01:24.026 align:middle The -- dev is here because we only need to load dummy data in the dev & test environments. 00:01:24.956 --> 00:01:27.596 align:middle While that's running, head back to the docs. 00:01:28.666 --> 00:01:30.806 align:middle Let me show you what this bundle is all about. 00:01:31.356 --> 00:01:34.746 align:middle Suppose you have entities like Category or Post. 00:01:35.766 --> 00:01:42.106 align:middle The idea is that, for each entity, we will generate a corresponding model factory. 00:01:42.726 --> 00:01:48.346 align:middle So, a Post entity will have a PostFactory class, which will look something like this. 00:01:49.156 --> 00:01:53.946 align:middle Once we have that, we can configure some default data for the entity class and then... 00:01:54.216 --> 00:01:55.766 align:middle start creating objects! 00:01:56.716 --> 00:02:02.786 align:middle I know I explained that quickly, but that's because we're going to see this in action. 00:02:03.286 --> 00:02:04.106 align:middle Back at the terminal... 00:02:04.726 --> 00:02:06.126 align:middle let's wait for this to finish. 00:02:06.696 --> 00:02:09.686 align:middle I'm actually recording at my parents' house... 00:02:09.686 --> 00:02:13.536 align:middle where the Internet is barely a step up from dial-up. 00:02:14.566 --> 00:02:21.306 align:middle After an edited break where I ate a sandwich and watched Moana, this finally finishes. 00:02:22.126 --> 00:02:25.506 align:middle Let's generate one of those fancy-looking model factories for Question. 00:02:26.196 --> 00:02:33.646 align:middle To do that, run: symfony console make:factory I also could have run bin/console make:factory... 00:02:33.816 --> 00:02:37.856 align:middle because this command doesn't need the database environment variables... 00:02:38.366 --> 00:02:42.536 align:middle but it's easier to get in the habit of always using symfony console. 00:02:43.256 --> 00:02:45.616 align:middle Select Question from the list and... 00:02:46.036 --> 00:02:51.386 align:middle done! Go check out the new class src/Factory/QuestionFactory.php. 00:02:52.266 --> 00:02:56.436 align:middle The only method that we need to worry about right now is getDefaults(). 00:02:57.166 --> 00:03:03.666 align:middle The idea is that we'll return an array of all of the data needed to create a Question. 00:03:04.526 --> 00:03:09.796 align:middle For example, we can set a name key to our dummy question name - "Missing pants". 00:03:10.626 --> 00:03:12.126 align:middle This works a bit like Twig. 00:03:12.666 --> 00:03:18.576 align:middle When Foundry sees the name key, it will call the setName() method on Question. 00:03:19.526 --> 00:03:25.066 align:middle Internally, this uses Symfony's property-access component, which I'm mentioning, 00:03:25.206 --> 00:03:29.296 align:middle because it also supports passing data through the constructor if you need that. 00:03:30.296 --> 00:03:36.716 align:middle Copy the rest of the dummy code from our fixture class, delete it... 00:03:36.956 --> 00:03:39.256 align:middle and delete everything actually. 00:03:41.756 --> 00:03:44.396 align:middle Back in QuestionFactory, paste! 00:03:45.266 --> 00:03:48.446 align:middle But we need to convert all of this into array keys. 00:03:48.896 --> 00:03:51.096 align:middle As exciting as this is... 00:03:51.236 --> 00:03:53.566 align:middle I'll... type really fast. 00:03:57.856 --> 00:03:59.576 align:middle And.... done! 00:04:00.066 --> 00:04:01.636 align:middle Phew... Ok! 00:04:02.056 --> 00:04:05.706 align:middle We now have a simple array of "default" values that are enough 00:04:05.736 --> 00:04:07.876 align:middle to create a valid Question object. 00:04:08.766 --> 00:04:11.176 align:middle Our QuestionFactory is ready! 00:04:11.906 --> 00:04:13.916 align:middle Let's use it in AppFixtures. 00:04:14.466 --> 00:04:18.996 align:middle How? First, say QuestionFactory::new(). 00:04:19.936 --> 00:04:23.766 align:middle That will give us a new instance of the QuestionFactory. 00:04:24.306 --> 00:04:28.526 align:middle Now ->create() to create a single Question. 00:04:29.326 --> 00:04:35.506 align:middle Done! Ok, it's still not interesting - it will create just one Question... 00:04:35.766 --> 00:04:36.886 align:middle but let's try it! 00:04:37.466 --> 00:04:44.946 align:middle Re-run the fixtures: symfony console doctrine:fixtures:load Answer yes and... 00:04:45.366 --> 00:04:46.176 align:middle no errors! 00:04:46.796 --> 00:04:50.366 align:middle Head over to the browser, refresh and... 00:04:50.846 --> 00:04:53.056 align:middle oh! Zero questions! 00:04:53.776 --> 00:04:57.646 align:middle Ah, my one question is probably unpublished. 00:04:58.006 --> 00:05:00.756 align:middle Load the fixtures again: Refresh and... 00:05:00.906 --> 00:05:01.736 align:middle there it is! 00:05:02.576 --> 00:05:06.656 align:middle At this point, you might be wondering: why is this better? 00:05:07.226 --> 00:05:08.706 align:middle Valid question. 00:05:09.526 --> 00:05:15.406 align:middle It's better because we've only just started to scratch the service of what Foundry can do. 00:05:16.196 --> 00:05:18.716 align:middle Want to create 20 questions instead of just one? 00:05:19.256 --> 00:05:22.746 align:middle Change create() to createMany(20). 00:05:23.566 --> 00:05:24.046 align:middle That's it. 00:05:24.956 --> 00:05:29.336 align:middle Reload the fixtures again: Then go check out the homepage. 00:05:30.826 --> 00:05:36.306 align:middle Hello 20 questions created with one line of very readable code. 00:05:37.236 --> 00:05:39.806 align:middle But wait there's more Foundry goodness! 00:05:40.336 --> 00:05:44.476 align:middle Foundry comes with built-in support for a library called faker: 00:05:45.126 --> 00:05:48.176 align:middle and handy tool for creating truly fake data. 00:05:49.106 --> 00:05:51.236 align:middle Let's improve the quality of our fake data 00:05:51.596 --> 00:05:54.996 align:middle and see a few other cool things that Foundry can do next.