WEBVTT

NOTE Created by CaptionSync from Automatic Sync Technologies www.automaticsync.com

00:00:01.106 --> 00:00:05.946 align:middle
When you make a pull request to Symfony,
you almost always need at least one test.

00:00:06.476 --> 00:00:07.256 align:middle
And... yea...

00:00:07.806 --> 00:00:11.146 align:middle
we definitely need a test
for our new TargetPathHelper.

00:00:11.886 --> 00:00:14.346 align:middle
But, before we start writing it...

00:00:14.706 --> 00:00:19.836 align:middle
shouldn't we first figure out
how to run Symfony's tests?

00:00:19.876 --> 00:00:20.546 align:middle
Great idea!

00:00:21.046 --> 00:00:24.026 align:middle
And I'm happy to report that it's quite easy.

00:00:25.406 --> 00:00:26.826 align:middle
Look in the symfony/ directory.

00:00:28.406 --> 00:00:35.316 align:middle
It has a composer.json file that describes
all of the libraries that Symfony itself needs

00:00:35.316 --> 00:00:39.276 align:middle
in order to work and in order to run its tests.

00:00:40.526 --> 00:00:45.416 align:middle
Move over to your terminal and run: composer
update There's one important difference

00:00:45.416 --> 00:00:49.556 align:middle
between a reusable library like
Symfony and a normal application:

00:00:50.346 --> 00:00:54.666 align:middle
Symfony does not have a composer.lock file!

00:00:55.386 --> 00:01:02.226 align:middle
We commit the composer.json file to
Symfony, but we do not commit composer.lock.

00:01:03.016 --> 00:01:06.106 align:middle
Why not? Well, there's just no point.

00:01:06.816 --> 00:01:12.836 align:middle
Individual apps that require Symfony will
lock Symfony at some version in their app.

00:01:13.556 --> 00:01:16.396 align:middle
But, when we're working on Symfony itself,

00:01:16.396 --> 00:01:20.526 align:middle
we usually want the latest
version of all of its dependencies.

00:01:21.236 --> 00:01:25.466 align:middle
So before you run your tests,
make sure to run composer update.

00:01:26.256 --> 00:01:29.516 align:middle
Running composer install isn't good enough,

00:01:29.516 --> 00:01:33.046 align:middle
because there could already
be a composer.lock file

00:01:33.046 --> 00:01:36.036 align:middle
from an earlier time you ran composer install.

00:01:37.316 --> 00:01:40.286 align:middle
Running update makes sure
you have the latest stuff

00:01:40.556 --> 00:01:43.196 align:middle
for whatever branch of Symfony
you're currently on.

00:01:45.176 --> 00:01:51.626 align:middle
Perfect! Now we do have a composer.lock file.

00:01:51.626 --> 00:01:55.956 align:middle
Ok, we're ready to run the tests!

00:01:55.956 --> 00:01:56.406 align:middle
Do it with: .

00:01:56.406 --> 00:01:58.646 align:middle
/phpunit Um...

00:01:58.856 --> 00:01:59.326 align:middle
that's it!

00:01:59.876 --> 00:02:04.326 align:middle
This is a wrapper around PHPUnit:
it downloads some dependencies

00:02:04.326 --> 00:02:06.116 align:middle
to a different directory, then...

00:02:06.316 --> 00:02:07.866 align:middle
starts running the tests!

00:02:09.306 --> 00:02:09.976 align:middle
And... yea...

00:02:10.366 --> 00:02:12.966 align:middle
there are a lot of tests.

00:02:13.636 --> 00:02:16.046 align:middle
I'm going to stop these by pressing Ctrl+C.

00:02:16.776 --> 00:02:20.976 align:middle
To be honest, I never run
the full test suite locally.

00:02:21.366 --> 00:02:22.836 align:middle
You just don't need to!

00:02:23.856 --> 00:02:28.876 align:middle
As you'll see in a few minutes, Symfony
has a robust continuous integration setup:

00:02:30.186 --> 00:02:34.436 align:middle
when you make a pull request, Symfony's
test suite is run automatically.

00:02:35.136 --> 00:02:40.016 align:middle
Thanks to that, locally, I usually
just run the tests I'm working on.

00:02:41.016 --> 00:02:43.076 align:middle
Let's test everything in SecurityBundle: .

00:02:43.076 --> 00:02:49.396 align:middle
/phpunit src/Symfony/Bundle/SecurityBundle
This time...

00:02:49.396 --> 00:02:51.696 align:middle
if you didn't fast forward like me...

00:02:52.156 --> 00:02:55.216 align:middle
you'd see that these tests
only take a minute or two.

00:02:56.476 --> 00:03:02.506 align:middle
There are a few "skipped" tests: that's
probably not something you need to worry about.

00:03:03.196 --> 00:03:07.396 align:middle
Some tests require a special PHP
extension or some other service

00:03:07.396 --> 00:03:09.386 align:middle
that your local computer might not have.

00:03:10.116 --> 00:03:12.086 align:middle
So, those tests are skipped.

00:03:12.716 --> 00:03:13.826 align:middle
No big deal.

00:03:15.036 --> 00:03:19.186 align:middle
Now that the tests are running,
it's time to add our own!

00:03:19.986 --> 00:03:22.906 align:middle
I'll double-click to get
back into SecurityBundle.

00:03:24.086 --> 00:03:29.916 align:middle
Because we want to test TargetPathHelper,
the test should live in tests/Security.

00:03:31.146 --> 00:03:34.946 align:middle
Create a new PHP class called
TargetPathHelperTest.

00:03:37.506 --> 00:03:40.676 align:middle
Make this extend the normal
TestCase from PHPUnit.

00:03:42.676 --> 00:03:46.496 align:middle
Then add public function testSavePath().

00:03:46.736 --> 00:03:49.776 align:middle
For the body of the test...

00:03:49.956 --> 00:03:52.046 align:middle
yea... I'm going to cheat.

00:03:52.576 --> 00:03:57.436 align:middle
This isn't a testing tutorial, so I'll
paste in some code I already prepared.

00:03:58.286 --> 00:04:03.946 align:middle
Oh, and I need to auto-complete a few
things to get the missing use statements,

00:04:04.276 --> 00:04:08.456 align:middle
like FirewallMap from SecurityBundle,
and a few other ones.

00:04:15.856 --> 00:04:19.326 align:middle
Our TargetPathHelper class
doesn't really do much:

00:04:19.856 --> 00:04:23.466 align:middle
it pushes most of the work back
to the methods from the trait.

00:04:25.106 --> 00:04:30.896 align:middle
So, this test basically creates a bunch
of mocks, creates a FirewallConfig

00:04:30.896 --> 00:04:37.736 align:middle
that returns a firewall name of, um,
firewall_name, and then we ultimately make sure

00:04:37.736 --> 00:04:43.576 align:middle
that this special key is set on the
session to the URL we passed to savePath().

00:04:44.946 --> 00:04:49.766 align:middle
If you're interested in understanding this
test better, you can totally look into it more.

00:04:50.256 --> 00:04:56.756 align:middle
But, the beautiful part is that creating
a unit test for Symfony is no different

00:04:56.756 --> 00:05:02.306 align:middle
than creating a unit test for an
application: there's no framework code here.

00:05:03.306 --> 00:05:05.506 align:middle
Let's go run this one test directly: .

00:05:05.506 --> 00:05:17.066 align:middle
/phpunit src/Symfony/Bundle/SecurityBundle/Tests
/Security/TargetPathHelperTest.php The last step

00:05:17.206 --> 00:05:23.006 align:middle
is to register our new class as a
service and enable it to be autowired.

00:05:23.816 --> 00:05:24.566 align:middle
Let's get to it!

