WEBVTT NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com 00:00:00.036 --> 00:00:04.506 align:middle Yo friends! 00:00:05.016 --> 00:00:10.346 align:middle Oh, SO glad you've made it for our phpspec tutorial! 00:00:10.786 --> 00:00:12.376 align:middle You will not regret it. 00:00:12.376 --> 00:00:16.616 align:middle Thing number one to know about phpspec is: it's... 00:00:16.746 --> 00:00:17.996 align:middle just... fun! 00:00:18.976 --> 00:00:21.656 align:middle Ok, but what is phpspec? 00:00:22.316 --> 00:00:24.146 align:middle It's a unit testing tool... 00:00:24.386 --> 00:00:26.406 align:middle exactly like phpunit. 00:00:26.786 --> 00:00:28.706 align:middle Wait... that's not totally right. 00:00:28.706 --> 00:00:31.426 align:middle If you watched our PHPUnit tutorial, 00:00:31.586 --> 00:00:36.746 align:middle then you know that PHPUnit is a perfectly fine tool for unit testing your code. 00:00:36.746 --> 00:00:41.026 align:middle So... why are we even talking about phpspec? 00:00:41.836 --> 00:00:47.916 align:middle Here's the truth: yes, phpspec is a tool for unit testing your code. 00:00:48.316 --> 00:00:51.246 align:middle But, that's not its main job. 00:00:52.176 --> 00:00:56.026 align:middle Nope, it's a tool for helping you design your code 00:00:56.476 --> 00:01:00.586 align:middle in a well-organized, meaningful and maintainable way. 00:01:01.316 --> 00:01:06.176 align:middle You probably already think about the design and user experience of your front-end. 00:01:06.756 --> 00:01:12.026 align:middle But, have you ever thought about the design and experience of your PHP classes? 00:01:12.646 --> 00:01:14.146 align:middle That's phpspec's job. 00:01:14.586 --> 00:01:20.196 align:middle And yes, as a nice by-product, you will get unit tests. 00:01:20.196 --> 00:01:26.856 align:middle And as a nicer by-product, you will also enjoy the process - coding with phpspec is fun. 00:01:27.276 --> 00:01:32.626 align:middle Oh, and later - we'll talk about how phpspec & PHPUnit fit together - 00:01:32.866 --> 00:01:35.596 align:middle like should we use both in the same app? 00:01:36.086 --> 00:01:37.546 align:middle Short answer: yes! 00:01:38.346 --> 00:01:39.416 align:middle Ok, let's go! 00:01:39.976 --> 00:01:45.656 align:middle Just like in our PHPUnit tutorial, we're going to design & build a dinosaur park - 00:01:46.116 --> 00:01:53.176 align:middle complete with T-Rex, stegosaurus, enclosures for our dinosaurs and, with any lucky, 00:01:53.266 --> 00:01:59.386 align:middle some security systems that - thanks to our tests - won't fail as soon as a storm rolls in 00:01:59.606 --> 00:02:02.036 align:middle or a developer leaves early for lunch. 00:02:03.216 --> 00:02:06.616 align:middle To make sure our dinosaurs don't once again rule the Earth, 00:02:07.166 --> 00:02:09.566 align:middle you should totally code along with me. 00:02:10.586 --> 00:02:12.316 align:middle Download the course code from this page. 00:02:13.146 --> 00:02:18.476 align:middle When you unzip it, you'll find a start/ directory with the same code that you see here. 00:02:19.146 --> 00:02:20.166 align:middle But... well... 00:02:20.576 --> 00:02:21.866 align:middle what we have here is... 00:02:22.116 --> 00:02:27.436 align:middle nothing! Just an empty project with a composer.json file that also... 00:02:27.646 --> 00:02:29.786 align:middle has nothing important inside. 00:02:30.476 --> 00:02:36.836 align:middle This tutorial directory does have a few files that we'll use later - so make sure you have it. 00:02:37.416 --> 00:02:44.276 align:middle We're starting with an empty project because phpspec is truly a framework-agnostic library. 00:02:44.776 --> 00:02:48.396 align:middle But don't worry - if you're a Symfony user, we'll build a structure 00:02:48.396 --> 00:02:50.776 align:middle that will be very familiar to you - 00:02:51.036 --> 00:02:54.336 align:middle with the same directories and namespaces as a Symfony app. 00:02:55.476 --> 00:03:01.826 align:middle To get phpspec installed, open a terminal, move into the project, close Facebook, and run: 00:03:02.216 --> 00:03:09.786 align:middle composer require phpspec/phpspec -- dev And.... 00:03:10.066 --> 00:03:19.036 align:middle ding! Just like with PHPUnit, installing phpspec means that you get a new, shiny executable! 00:03:20.046 --> 00:03:31.116 align:middle Run: ./vendor/bin/phpspec The phpspec executable really only has two commands: describe and run. 00:03:31.226 --> 00:03:34.146 align:middle And we'll talk about both of them very soon. 00:03:34.646 --> 00:03:39.246 align:middle But first, we need just a little bit of configuration to get things working. 00:03:40.076 --> 00:03:42.166 align:middle The first piece of configuration... 00:03:42.446 --> 00:03:46.266 align:middle has nothing to do with phpspec at all! 00:03:46.946 --> 00:03:50.296 align:middle Our app has no PHP classes yet. 00:03:50.676 --> 00:03:54.926 align:middle But when we add some, I want to put them in the src/ directory 00:03:55.026 --> 00:03:57.206 align:middle and prefix each namespace with App. 00:03:57.416 --> 00:04:00.996 align:middle That will be exactly like a Symfony project. 00:04:02.166 --> 00:04:03.396 align:middle Open composer.json. 00:04:04.106 --> 00:04:06.326 align:middle To make sure Composer's autoloader knows 00:04:06.326 --> 00:04:09.846 align:middle where our classes live, we need to add some config here. 00:04:10.476 --> 00:04:13.866 align:middle This is code that you normally get automatically when you start, 00:04:13.866 --> 00:04:16.496 align:middle for example, a new Symfony project. 00:04:16.786 --> 00:04:19.186 align:middle But I want to show how it's done by hand 00:04:19.376 --> 00:04:23.436 align:middle so that we can truly understand what's going on behind the scenes. 00:04:24.456 --> 00:04:31.666 align:middle Add autoload, then psr-4, then say that classes starting 00:04:31.666 --> 00:04:34.806 align:middle with App\\ will live in the src/ directory. 00:04:36.216 --> 00:04:39.846 align:middle To make Composer notice this change, find your terminal and run: 00:04:40.106 --> 00:04:44.656 align:middle composer dump-autoload Autoloading... 00:04:44.916 --> 00:04:52.246 align:middle done! Next, one of my favorite things about phpspec is that it generates code for you! 00:04:52.606 --> 00:04:58.086 align:middle But to do that, it also needs to know that our classes will live in the src/ directory 00:04:58.356 --> 00:05:00.956 align:middle and that each namespace will start with App. 00:05:00.956 --> 00:05:06.816 align:middle Unfortunately, phpspec can't automatically get all this info 00:05:06.816 --> 00:05:10.216 align:middle from composer.json, but it's no problem. 00:05:11.276 --> 00:05:15.076 align:middle Create a phpspec.yml file at the root of the project - 00:05:15.076 --> 00:05:18.766 align:middle phpspec automatically knows to look for this. 00:05:19.646 --> 00:05:22.596 align:middle Inside add suites then default. 00:05:23.846 --> 00:05:26.826 align:middle Like most testing tools, you can organize your tests 00:05:26.826 --> 00:05:30.446 align:middle into multiple groups, or "suites" if you want. 00:05:31.426 --> 00:05:35.456 align:middle In this tutorial, we'll stick to using the one, "default" suite. 00:05:36.376 --> 00:05:40.826 align:middle Under this, add namespace: App - because all of our classes will start 00:05:40.826 --> 00:05:45.506 align:middle with the App namespace - and psr4_prefix: App. 00:05:46.776 --> 00:05:52.166 align:middle Those two lines are enough to help phpspec know where to generate our files. 00:05:53.016 --> 00:05:55.656 align:middle And... team, we're ready to go! 00:05:56.576 --> 00:06:00.526 align:middle Next, let's create our first specification... 00:06:01.126 --> 00:06:02.586 align:middle ooOOOOooo. 00:06:03.076 --> 00:06:10.606 align:middle That's the file where we will describe how a single class should behave by writing examples. 00:06:11.276 --> 00:06:11.826 align:middle Woh.