[0:00] welcome to another develop and design [0:02] tutorial in this video we will explore [0:05] how to create a unit test in laravel [0:07] using PHP unit first we need to check [0:10] that we have the PHP unit package [0:12] installed by opening up the Project's [0:14] composer. Json file the package name we [0:17] should see is phpunit phpunit now we [0:20] know this is installed I am going to [0:22] open up the customer model class to [0:24] write a function for our unit test this [0:27] is just a simple function called full [0:29] name which returns a string [0:30] concatenating the customer's first name [0:32] and last name to create our unit test we [0:36] need to open our terminal or command [0:38] prompt then navigate to our laravel [0:41] project directory Now to create the [0:44] customer unit test file itself we need [0:47] to type the command PHP artisan make [0:50] colon test customer test-- unit then hit [0:55] enter now the command has run [0:57] successfully we should now see the file [1:00] path to our unit test displayed in the [1:02] terminal just a quick breakdown of the [1:04] command itself PHP artisan make colon [1:07] test creates the test file customer test [1:10] in Pascal case names the test file for [1:13] the customer class's unit tests only the [1:15] customer class's unit tests will go in [1:17] this file-- unit specifies that the [1:21] tests are unit tests and will place the [1:23] file under the unit test directory now [1:26] if we navigate to the test directory in [1:28] our code editor then into the unit [1:30] directory we should see our customer [1:32] test file let's click on it to open it [1:35] we can tell the test is a PHP unit test [1:38] as the customer test class extends the [1:40] test case class from the PHP unit [1:42] library now to write the test we'll [1:44] first add a description in the doc block [1:46] above the test function explaining what [1:48] it [1:49] covers let's put Test the full name [1:52] method of the customer model Returns the [1:54] correct [1:56] format it's good to put clear [1:58] descriptions as it helps other [2:00] developers understand the tests now [2:02] let's give the test function a name [2:04] let's call it Test full name Returns the [2:06] correct [2:08] format now let's delete everything that [2:11] was pre-generated inside the example [2:13] function then begin writing the test by [2:16] creating a variable called customer [2:19] which will be an instance of the [2:21] customer model [2:23] class we need to give the customer a [2:25] first name and last name to test the [2:28] function I'll assign the first name John [2:31] and the last name Smith to the customer [2:34] for unit tests we do not use model [2:37] Factory calls instead we instantiate the [2:40] class using the new [2:42] operator now let's add a call to a [2:45] method called assert [2:47] equals this method takes two variables [2:50] and asserts whether they are equal or [2:52] not if they are equal then the unit test [2:55] has passed if they are not equal then it [2:58] has failed [3:01] for this method's first parameter we [3:03] want to put the string value we expect [3:05] the customer full name method to return [3:08] which is John [3:10] Smith the second parameter we want to [3:12] pass is the full name method call from [3:14] our customer variable as this should [3:17] concatenate the customer's first name [3:19] and last name to equal John [3:21] Smith now to run the test we want to [3:24] open up the terminal and ensure we are [3:26] in our laravel project [3:28] directory then type the command PHP [3:31] Artisan test and hit enter Then in the [3:34] terminal we can see that our unit test [3:36] has passed successfully the command we [3:39] just ran on its own runs all the [3:41] tests if we have lots of tests it could [3:44] take a while to run them all if we just [3:46] want to see the outcome of one test file [3:48] then we can run the command with a [3:50] filter let's try the filter command in [3:52] the [3:54] terminal type PHP Artisan test-- filter [3:58] equals customer test [4:00] where customer test is the name of the [4:02] test then hit enter to run it and there [4:07] we have the customer test which has run [4:09] successfully if you have any questions [4:11] about this then please leave a comment [4:13] below and I will get back to you as soon [4:15] as I [4:16] can if you found this video useful then [4:19] please give it a like and hit the [4:20] Subscribe button