Running the Example6:26 with Alena Holligan
When learning something new, it's a good idea to make sure you can get their example working before you move on to anything more complicated. This will also help us understand a little more how PHPUnit works.
While learning something new, it's a good idea to make sure that you can get 0:00 the example working before you move on to anything more complicated. 0:04 This will also help us understand a little more how PHPUnit works. 0:08 After installing PHPUnit with Composer, 0:12 the next thing we see is an example of a Composer file. 0:15 Since we're going to be testing an actual class, 0:19 PHPUnit needs to know where that class is located. 0:22 We can use Composer's autoload feature for this. 0:27 We'll copy this autoload and then we'll go into Visual Studio. 0:30 Open composer.json, And add the autoload. 0:34 When using the classmap, 0:39 it allows us to give an array of folders to look for classes. 0:42 Anytime we update our composer.json file, we should run, composer update. 0:46 This is giving us an error because the src folder does not yet exist. 0:55 Let's go back to the documentation. 1:01 Our next step is the code for the actual class. 1:03 This is in src/Email.php. 1:07 So we'll copy this code. 1:10 We can create a new directory for src, And 1:14 a new file, Email.php. 1:19 And we can paste our code. 1:24 Now let's try composer update again. 1:26 Great, our autoload files have been generated. 1:32 Let's go back to the documentation. 1:35 Now we're ready to copy the test code. 1:38 Tests/EmailTest.php, Copy this code, 1:41 And then in our tests folder, we'll create a new file 1:48 named EmailTest.php, and we'll copy our code. 1:52 Let's close that for right now. 1:59 So we can see this a little bit more. 2:00 This class has three tests with three different test methods. 2:03 I've added a link in the notes to the documentation for 2:08 all the different options of test methods. 2:11 The first, testCanBeCreatedFromValidEmailAddress, 2:14 asserts that the result of submitting a valid email address to 2:19 the fromString method returns an instance of the Email::class. 2:24 The second testCannotBeCreatedFromAnInvalidEmailAdd- 2:29 ress is an exception. 2:35 Instead of assert this time, we are specifying what we expect. 2:37 First we say that we are expecting an exception, and 2:42 the type of exception is an InvalidArgumentException. 2:46 Then we call the code we expect to throw an exception, 2:49 passing an invalid email to the fromString method. 2:54 The third and final test can be used as a string. 2:58 This is an assertion once again. 3:03 This time asserting that we can use the resulting object 3:06 of a valid email address as a string. 3:10 Let's run the PHPUnit command from the documentation. 3:13 We see three dots because all three tests have passed. 3:27 The first thing that we should notice from this command is that it starts with a ./. 3:31 This means to start in the current directory. 3:37 We can either use that or not. 3:40 The next thing to note is the bootstrap flag. 3:42 This is followed by the vendor/autoload.php file. 3:48 This is the file that we want to run before any of our tests, 3:53 just like bootstrapping an application. 3:57 If you install PHPUnit using Composer like we did, 3:59 it will default to using the Composer autoload files. 4:02 So we don't actually need that part either. 4:07 Finally, note that there is no .php extension when calling our test case. 4:09 Again, this will work with or without the .php. 4:16 We could use vendor/bin/phpunit and 4:22 then Tests/EmailTest.php. 4:27 We can also just specify the test folder. 4:32 By default, it will run all the files in that directory that end with test.php. 4:38 This includes EmailTest.php and FirstTest.php. 4:45 We now see five tests with five assertions and one failure. 4:51 Let's go back to our documentation. 4:56 The final section on this getting started guide is TestDox. 4:59 This gives us some more verbose documentation for 5:04 the behavior of our tests. 5:07 Before the test directory or the file, we can add the flag, --testdox. 5:09 Let's go back to Visual Studio. 5:15 Before tests, we'll add our --testdox flag. 5:18 We can see a lot more details. 5:26 We can see in our email class a detailed list of the tests that passed. 5:29 testdox uses the name of our test to create the output. 5:35 So it's important to make sure that the name of the function clearly 5:40 describes what each test does. 5:45 Also notice that testdox leaves off test from the end of our class names. 5:48 So EmailTest.php is just email. 5:54 It also leaves it off the beginning of our function names. 5:58 So you can write your test functions with Test in front or Test annotated. 6:02 We also see, in our first test, that our second assertion failed, 6:08 but our third assertion passed. 6:13 Before we go any further with our testing, let's take a look at some basic 6:16 configuration that will make PHPUnit easier to use and nicer to look at 6:21
You need to sign up for Treehouse in order to download course files.Sign up