1 00:00:00,000 --> 00:00:04,781 [MUSIC] 2 00:00:04,781 --> 00:00:09,087 When you start learning about testing, you very quickly start hearing about 3 00:00:09,087 --> 00:00:12,280 a practice known as test-driven development, or TDD. 4 00:00:13,290 --> 00:00:16,250 It's the practice of writing a test for 5 00:00:16,250 --> 00:00:21,450 a small module or unit of code as if it worked already. 6 00:00:21,450 --> 00:00:25,330 Then write the code until that test passes. 7 00:00:25,330 --> 00:00:30,070 Repeat until you have tests for every unit of code that you've written. 8 00:00:30,070 --> 00:00:35,170 What TDD does is force you to think about what your code looks like 9 00:00:35,170 --> 00:00:37,010 at the unit level. 10 00:00:37,010 --> 00:00:41,130 This doesn't mean that these modules will always be used in the exact 11 00:00:41,130 --> 00:00:42,600 way that you planned. 12 00:00:42,600 --> 00:00:44,760 It's like building with Lego. 13 00:00:44,760 --> 00:00:48,630 Unit testing verifies that each piece is compatible so 14 00:00:48,630 --> 00:00:52,890 that your application can use those pieces when needed. 15 00:00:52,890 --> 00:00:55,760 Let's go build some units from a test driven approach. 16 00:00:57,150 --> 00:01:02,020 We're going to write a simple English to Pig Latin translator. 17 00:01:02,020 --> 00:01:06,480 Pig Latin is a game in which English words are altered 18 00:01:06,480 --> 00:01:09,150 usually by moving the first consonant or 19 00:01:09,150 --> 00:01:15,440 consonant cluster of a word to the end of the word and then adding a y. 20 00:01:15,440 --> 00:01:19,390 So treehouse would become eehousetray. 21 00:01:19,390 --> 00:01:23,850 Let's write the code for this translator in a test driven approach. 22 00:01:23,850 --> 00:01:27,540 In test driven development, we write our test first and 23 00:01:27,540 --> 00:01:31,150 then write the code until our test passes. 24 00:01:31,150 --> 00:01:33,651 Let's add a new file in tests. 25 00:01:33,651 --> 00:01:38,618 We'll name this PigLatinTest.php. 26 00:01:40,696 --> 00:01:45,408 We're going to use PHP unit framework, 27 00:01:48,480 --> 00:01:50,979 TestCase. 28 00:01:50,979 --> 00:01:55,867 And then we'll add the class 29 00:01:55,867 --> 00:02:01,790 PigLatinTest extends TestCase. 30 00:02:01,790 --> 00:02:05,470 We'll add a .clock with the annotation test. 31 00:02:07,730 --> 00:02:09,611 And then we're ready to add our first function. 32 00:02:12,480 --> 00:02:17,417 The main functionality of our converter is to take a single starting 33 00:02:17,417 --> 00:02:20,460 constant word and change it to pig Latin. 34 00:02:20,460 --> 00:02:30,008 So convertSingleStartingConsonantWordToPigLa- 35 00:02:30,008 --> 00:02:30,971 tin. 36 00:02:34,590 --> 00:02:40,380 Now let's walk through how we expect to use our PigLatin converter. 37 00:02:40,380 --> 00:02:45,160 We'll have an English word that we want to convert, and we'll use test. 38 00:02:46,880 --> 00:02:50,540 Then we'll need our expected result. 39 00:02:50,540 --> 00:02:52,300 And this will be esttay. 40 00:02:54,040 --> 00:02:56,941 Next we create a new pigLatin object. 41 00:03:02,921 --> 00:03:11,310 Then we're going to get the result of calling pigLatin->convert. 42 00:03:12,840 --> 00:03:13,939 And passing the word. 43 00:03:16,920 --> 00:03:19,920 Now we're ready for our assertion. 44 00:03:19,920 --> 00:03:21,712 Instead of assertTrue, 45 00:03:21,712 --> 00:03:26,331 we want to make sure that the result equals our expected result. 46 00:03:26,331 --> 00:03:30,312 So we use $this->assertEquals 47 00:03:30,312 --> 00:03:36,480 the $expectedResult and the actual result. 48 00:03:36,480 --> 00:03:40,899 And the third parameter is optional, but allows us to add a custom message. 49 00:03:43,089 --> 00:03:48,731 PigLatin conversion did not work correctly. 50 00:03:51,690 --> 00:03:55,716 Now that we have our first test, we're ready to run our code. 51 00:03:55,716 --> 00:04:04,489 Vendor/bin/phpunit tests/PigLatinTest.php. 52 00:04:04,489 --> 00:04:06,296 Let's fix line 9. 53 00:04:12,695 --> 00:04:18,350 First, it fails because there is not a Pig Latin class. 54 00:04:18,350 --> 00:04:21,452 Let's add a PigLatin class to our source directory. 55 00:04:29,504 --> 00:04:34,930 Class, PigLatin, now let's run our test again. 56 00:04:37,440 --> 00:04:41,510 It's giving us the same error even though we have a class file. 57 00:04:42,510 --> 00:04:45,810 That's because of the way we are autoloading. 58 00:04:45,810 --> 00:04:52,040 If we look in vendor, composer, and our autoload_classmap, 59 00:04:52,040 --> 00:04:55,610 we can see that our email is being included. 60 00:04:57,250 --> 00:05:01,320 But nowhere in here is it adding our Pig Latin. 61 00:05:04,650 --> 00:05:09,290 We need to run Composer update again to grab our Pig Latin file. 62 00:05:16,150 --> 00:05:21,010 You only have to do this when you add files you need in your classmap. 63 00:05:22,170 --> 00:05:23,980 If you're using proper name spacing for 64 00:05:23,980 --> 00:05:27,260 your project, this is a different way of telling Composer where to look for 65 00:05:27,260 --> 00:05:31,490 class files, and you don't need to update Composer each time. 66 00:05:31,490 --> 00:05:37,141 We could also require the specific class we need directly in our test files. 67 00:05:45,047 --> 00:05:48,699 We could require once, 68 00:05:52,642 --> 00:05:57,830 Source, PigLatin. 69 00:05:57,830 --> 00:06:02,020 But we already have our auto-load working, so let's comment this out. 70 00:06:02,020 --> 00:06:03,170 Now let's run our test again. 71 00:06:05,080 --> 00:06:09,660 This time, it tells us that we do not have a convert method. 72 00:06:09,660 --> 00:06:11,143 So let's add that. 73 00:06:19,900 --> 00:06:24,974 The function convert should accept a word, And 74 00:06:24,974 --> 00:06:28,960 then for now we're just going to return the word. 75 00:06:31,040 --> 00:06:32,309 Now let's run the test again. 76 00:06:34,560 --> 00:06:38,055 This time we get a normal failure message, 77 00:06:38,055 --> 00:06:42,920 we were expecting esttay, but instead, we just got test.