What is Unit Testing?
45sClear definition of unit testing in simple terms, perfect for beginners seeking foundational knowledge.
▶ Play ClipThis video provides a beginner-friendly introduction to unit testing in PHP, covering core concepts and practical implementation using PHPUnit. It explains unit testing and test-driven development (TDD), then walks through installing PHPUnit, writing tests, and using assertions. The tutorial also demonstrates TDD by creating an Employee model and tests, and introduces collection testing.
Unit testing is a software testing method where individual units of source code (e.g., a single function or method) are tested to determine if they are fit for use. The video will cover test-driven development (TDD), where tests are written before the application code.
A new folder 'unit test' is created, and PHPUnit is installed via Composer using the command `composer require phpunit/phpunit --dev`. The video also covers creating the directory structure: `tests/unit` for unit tests and `app` for application classes.
A `phpunit.xml` configuration file is created to define test suites, bootstrap file (`vendor/autoload.php`), and options like `colors=
By the end of the video, viewers should be able to implement unit testing in their own PHP applications using PHPUnit, following a test-driven development approach where tests drive the creation of application code.
"The title accurately reflects the content: a beginner PHPUnit tutorial that covers test-driven development fundamentals."
What is the definition of unit testing according to the video?
Unit testing is a software testing method by which individual units of source code—sets of one or more computer program modules together—are tested to determine whether they are fit for use.
0:08
What is Test Driven Development (TDD)?
TDD is a development approach where you first create a test and then create the application according to that test, so that you understand how things should work and it's easy to debug.
0:39
What command is used to install PHPUnit via Composer?
`composer require phpunit/phpunit --dev`
2:38
What is the naming convention for a test file in PHPUnit?
The test file should end with `Test.php`, for example `SampleTest.php`.
13:53
How do you define a test method in a PHPUnit test class?
A test method must be a public function with a name starting with `test`, or you can add the `@test` annotation in a comment above the function.
16:07
What is the purpose of the `assertTrue` assertion?
`assertTrue` checks that the value passed to it is true. If it is true, the test passes; otherwise, it fails.
17:33
What is the purpose of the `stopOnFailure` configuration in phpunit.xml?
When set to true, the test runner stops executing after the first test failure.
11:50
How does the `setUp()` method work in PHPUnit?
The `setUp()` method is called before each test method in the class. It's used to set up common objects or state needed for tests, reducing code duplication.
44:00
What does the `assertEmpty` assertion check?
`assertEmpty` checks whether the given value (e.g., an array) is empty. If empty, the test passes; otherwise, it fails.
53:42
How do you run PHPUnit tests from the command line?
By running `./vendor/bin/phpunit` from the project root directory.
12:30
Definition of Unit Testing
Provides a clear, formal definition of unit testing that sets the foundation for the entire tutorial.
0:08Introduction to Test Driven Development
Explains the TDD workflow (write test first, then application code) which is the core methodology demonstrated in the video.
0:39Using assertTrue Assertion
This is the first assertion used in the tutorial, demonstrating how to verify a condition in a test.
17:33Using setUp() to Reduce Duplication
The `setUp()` method is a key pattern for writing cleaner, maintainable tests.
44:00Testing Collections with Assertions
Shows how to test more complex data structures (like a Collection class) and introduces `assertEmpty`.
47:01[00:00] welcome guys in this video we're going
[00:01] to see unit testing so what exactly unit
[00:03] testing is and how it works
[00:06] so let's see is the definition of it by
[00:08] definition unit testing is a software
[00:10] testing method by which individual units
[00:13] of source code sets of one or more
[00:16] computer program modules together are
[00:19] tested to determine whether they are
[00:21] fit for use so basically we check the
[00:25] small portion of code if that particular
[00:28] code is doing what it's supposed to do
[00:31] so we check that and that is the way how
[00:33] we you put the unit testing on the
[00:36] application now we are going to see test
[00:39] driven development in this
[00:41] video and this test driven development
[00:43] basically tells us that how
[00:46] you create
[00:48] a test and how you uh create the
[00:50] application according to that test so
[00:52] that you get the idea how things should
[00:55] work and
[00:57] it is easy to debug in case when you
[00:59] want to debug it now you will be able to
[01:02] implement unit testing once you complete
[01:04] this video uh and understand all the
[01:07] concepts so you will be able to
[01:08] implement it in your own application so
[01:11] let's get started and see how we can
[01:13] make unit testing work
[01:14] [Music]
[01:20] to start with i'm going to create a
[01:22] brand new folder on the desktop and i'm
[01:24] going to give it a name
[01:26] of
[01:27] unit test
[01:29] i'm going to open the terminal now in
[01:32] here
[01:33] and make it a little bigger here so i
[01:34] say ls list item here i have desktop so
[01:38] i can go there so i simply say here
[01:40] desktop and slash
[01:43] on desktop i have this unit test server
[01:46] unit
[01:47] and then test
[01:49] simply and you can see that i'm there
[01:51] now so if i say ls here you can see
[01:53] there's no items here so i simply say
[01:56] here
[01:56] clear now i'm going to install a package
[02:00] and for installing that package we need
[02:02] to make sure we have composer installer
[02:04] on our system so if i say here composer
[02:08] and hit return i see these commands
[02:12] and if you don't see these commands
[02:14] while running composer that means
[02:16] composer is not installed in your system
[02:18] so you need to install composer
[02:20] and for that the links are in the
[02:22] description for windows as well as for
[02:23] mac os
[02:25] now as i have the composer install i can
[02:27] install the php unit package
[02:30] that package is going to uh help us work
[02:34] for the testing so i'm going to clear
[02:35] this screen now
[02:37] and
[02:38] the package we're going to use is php
[02:40] unit you simply can put your php unit
[02:44] and the google and the first thing you
[02:46] will see in the php unit it's a very
[02:48] good and
[02:50] advanced package for
[02:52] testing new php so i may click there
[02:56] and
[02:58] again here in the documentation
[03:01] english
[03:02] now we have here installing php unit so
[03:05] we can simply go here now we're going to
[03:08] use the
[03:09] uh composer which is given here this
[03:12] command we need to run you can also
[03:15] use the curl if you want to you can
[03:18] check this
[03:19] link this link is in the description so
[03:21] if you want to try that if you're trying
[03:23] to install using composer that's a lot
[03:25] easier so we are going to do that way so
[03:28] we are already in the folder so i'm just
[03:30] going to copy paste the command and the
[03:32] version is 9.5 that we're going to use
[03:35] in this
[03:37] uh course so i'm going to hit return now
[03:39] and then it's going to install for us
[03:41] it's installing the files
[03:43] open the folder in the desktop i have
[03:46] this php unit so i'm just going to open
[03:48] there
[03:49] and here we have the
[03:52] project
[03:54] so currently it is installing it
[03:56] completed in the install if i go inside
[03:59] the folder i get this vendor folder that
[04:02] is installed by composer there is
[04:05] composer.json there is composer.log
[04:08] so these three
[04:10] things we have here the same in the
[04:13] rbs code
[04:15] so
[04:16] we need to
[04:17] write tests here so first of all in the
[04:21] main unit test directory i want to
[04:23] create
[04:24] a directory and that directory going to
[04:26] be the tests directory so let's do that
[04:30] so you can click here
[04:32] and i say tests
[04:35] now there can be
[04:37] tests for feature tests for units
[04:41] so we are going to cover unit so i'm
[04:43] going to create a folder inside here
[04:46] so here it's going to be tests unit and
[04:50] inside this unit we're going to write
[04:52] our tests
[04:54] now if you uh brought
[04:56] or have worked with larval you might
[04:59] notice that there they have by default
[05:02] tests with the
[05:04] directory and their units and they
[05:06] feature
[05:07] uh two for the directories but here we
[05:10] are going to work with the unit testings
[05:11] of the vhs creating unit here i'm also
[05:14] going to create another directory in the
[05:16] unit test directory which is going to be
[05:18] app so we're going to create the model
[05:21] you know or any other php files in here
[05:24] so this is going to be the main file
[05:26] model directory and here in the
[05:30] composer.json i'm going to
[05:32] add the autoload so that it autoloads
[05:35] the files
[05:36] classes automatically so i'm simply
[05:38] going to say here
[05:40] auto load
[05:41] [Music]
[05:43] and here we are going to
[05:46] pause three values and it is going to be
[05:49] psr
[05:50] 4. and it's an object and here we are
[05:54] going to give the directory to app
[05:57] as we have app directory
[06:01] and
[06:02] we pass app
[06:04] all right so this is the directory we
[06:06] created here
[06:07] so we can save this and this is going to
[06:10] deal with the auto load now for
[06:13] working with this we need to run a
[06:14] command that's going to be
[06:16] composer
[06:19] dump
[06:21] autoload
[06:22] [Music]
[06:24] dash oh
[06:27] and
[06:28] i made a mistake here
[06:30] and you can see that it's it's
[06:32] generating optimized autoload files so
[06:36] here i'm generating optimize upload
[06:37] files
[06:38] so yeah this has 10 1000 plus classes
[06:42] that is all happening because of the
[06:45] vendor folder there they have a few
[06:48] packages and those packages have classes
[06:50] so it is optimizing and generating
[06:52] those for us so here i simply gonna say
[06:55] clear
[06:56] the screen as we have php unit installed
[07:00] we can run phpnet command and that's
[07:02] command is
[07:04] simply we say
[07:06] dot slash and then we're going to say
[07:08] vendor
[07:09] [Music]
[07:10] and after that we want to say bin
[07:13] and then php unit and when you run this
[07:16] command you're going to get all the
[07:18] options all the
[07:20] configuration options from the php
[07:24] test in a test so here you can check
[07:28] all the options and details about those
[07:30] and you need to define a few of these
[07:33] options to work with
[07:34] [Music]
[07:36] the tests so uh
[07:38] for example we need to define the where
[07:41] we have the
[07:42] dash we verbose so output as much as
[07:45] information possible for that we need
[07:47] this or we can say dash bootstrap a php
[07:51] script that is included before the tests
[07:54] run
[07:55] so we also have prepend um php script
[07:58] that is included as early as possible
[08:01] there are so many options we have
[08:03] options for debug we have option for
[08:04] repeat so it runs the tests repeatedly
[08:08] so this is something that we need to
[08:10] provide as a configuration
[08:12] now what i'm going to do i'm going to
[08:14] create a file and you're going to use
[08:16] few of these options
[08:19] to tell
[08:20] our application so whenever we run the
[08:22] command it takes these parameters the
[08:25] parameters we provide in that
[08:28] file
[08:29] and pick from there before running any
[08:31] tests so for that what we need to do we
[08:33] need to go here in the directory and
[08:36] here in the main unit test directory i'm
[08:38] going to create another file and this is
[08:41] going to be phpunit
[08:43] dot xml so this is going to be the file
[08:47] and here we need to say first of all
[08:49] first thing first we need to paste here
[08:51] the xml version information and after
[08:54] that we i'm going to create a tag it is
[08:56] going to be php unit
[08:59] and then we need to close this tag as
[09:01] well
[09:04] now inside this tag we need to tell that
[09:08] what
[09:09] we want to do what exactly
[09:11] uh going to the directory where we want
[09:13] to run the tests so we created the
[09:15] directory tests here so we want to
[09:17] mention our directory there so here what
[09:20] i say and say here tests
[09:23] suits
[09:24] all right
[09:25] this is going to be another tag
[09:28] just like this
[09:31] now inside this test suit i'm going to
[09:33] pass single suit so it's going to be
[09:36] test
[09:39] suit
[09:40] now here we can give it a name so i'm
[09:43] going to give it the name of
[09:45] let's call it php
[09:48] you can give any names and say here php
[09:50] test suit
[09:51] simply and here inside this we want to
[09:55] run uh tell the directory for the test
[09:58] so here i say the another tag it's gonna
[10:00] be directory
[10:02] and then i'm gonna close this directory
[10:06] just like that now here inside it we
[10:10] just
[10:11] mentioned the name of the directory so
[10:12] it's tests in our case so i say tests
[10:16] now this is uh almost done but few more
[10:20] things we can add here so let's add that
[10:23] so in php unit we need to pass these
[10:25] configuration settings as well so we're
[10:28] currently just saying tests but we
[10:30] haven't passed any configuration
[10:31] settings so the main settings
[10:34] for this
[10:36] video of ours we need is first is
[10:39] bootstrap
[10:42] [Music]
[10:43] so if you remember the bootstrap we saw
[10:46] here a php script that is included
[10:49] before and runs the test and what i'm
[10:51] going to do i want to load all the
[10:54] classes so here render
[10:58] slash overload
[11:00] dot php
[11:01] and this is inside vendor
[11:04] which is
[11:05] autoloading so if i go here
[11:07] and you can see that that file is there
[11:10] so we're just calling that file nothing
[11:11] more if you want to run any other file
[11:13] you can use the configuration from here
[11:16] and you can adjust this file
[11:18] now we already added this now what i
[11:21] want to do i want to add a few more
[11:22] parameters so you just pass me a
[11:24] parameters here so i say the output we
[11:28] want to see it should be colorful so
[11:30] it's going to be easy to read
[11:31] so i'm going to call here colors
[11:34] true
[11:35] [Music]
[11:37] and then we want verbals
[11:43] and i want it to be true as well
[11:46] now the another thing is stop
[11:50] so stop
[11:53] on
[11:57] failure
[11:58] so let's say we're going to write a few
[12:01] tests and each php going to run each
[12:04] test one by one so if any test fails we
[12:07] want to stop the script there and it
[12:09] should inform us that that particular
[12:11] test failed
[12:13] so for that we need to say here for also
[12:15] true so i'm going to say true
[12:17] [Music]
[12:19] stop on failure true
[12:21] all right
[12:22] so this is all what we need to make our
[12:28] php
[12:29] uh
[12:30] unit command work so if i run here
[12:33] clear
[12:36] now we created this file and it's going
[12:38] to pick every setting from here
[12:41] if you don't provide this file
[12:43] in that case you will have to
[12:45] provide all the
[12:47] uh these commands after your
[12:51] main command so here like i just ran
[12:53] this command here and dot
[12:55] dash vendor bin php unit so if you want
[12:58] to run
[12:59] let's say version or something something
[13:01] like that you will be putting after at
[13:04] the end here
[13:05] or here let's say here
[13:08] like this so we don't want to do this
[13:10] with that's why we created this file and
[13:13] then if you notice here we have colors
[13:15] true so we are getting here a
[13:18] mustard color and we're getting the
[13:20] information about our tests on running
[13:22] we are using 9.5 version so that is uh
[13:25] showing up and provide running all this
[13:27] command so this is the command that will
[13:29] be running
[13:30] to make uh
[13:32] to test now currently when i ran this
[13:34] command it says no test tests executed
[13:38] thing is we gave it the directory tests
[13:40] and if we go in tests we have only a
[13:42] folder no test inside it
[13:45] so to have some tests we need to
[13:48] create tests here and those tests going
[13:50] to be the php classes so here in unit
[13:53] folder i'm going to create a sample test
[13:56] so i'm going to call it sample
[13:58] and this has a sample test
[14:03] it should have the test at the end
[14:05] sample test so i'm going to create this
[14:07] class now here we are going to say php
[14:11] and we want to say here class
[14:13] [Music]
[14:14] and sample
[14:16] test same as the name of the
[14:20] file
[14:21] and i forgot to add the php here it
[14:23] should be dot php
[14:27] and here sample test
[14:29] now this simple test
[14:32] it's going to be a class but it's going
[14:34] to extend php unit so here we're going
[14:37] to call class we're going to say here
[14:40] use and we're going to say php
[14:43] unit
[14:44] backslash
[14:46] framework
[14:50] and test case
[14:53] all right so this is what it is going to
[14:55] extend so here's the extents
[15:00] test case
[15:02] all right great so here we have our
[15:05] first sample test so if i run this
[15:07] command now it should know as we created
[15:10] the sample test clause we can run
[15:13] the command again so i'm going to run
[15:15] this vendor in php unit and this time we
[15:18] get no tests found in class sample test
[15:22] now the thing is if you notice
[15:24] previously we had no tests executed this
[15:27] time it recognized that we
[15:29] have a sample test class
[15:31] but this time it is looking for tests so
[15:34] it says no tests found in class so we
[15:38] we don't have anything here currently
[15:40] that's we're getting the error so we
[15:42] need to
[15:43] add our test now so uh here we have the
[15:47] assertion zero in a moment being able to
[15:49] see what assertions are and how works so
[15:52] first and we need to define a test here
[15:54] now let's write our first test so i'm
[15:56] going to uh right here a function it's
[15:59] going to be public
[16:04] function i'm going to say this
[16:07] now whenever you write tests you
[16:10] start with tests so he says test and
[16:13] then whatever you want to do so here i
[16:14] say true
[16:16] returns
[16:20] true
[16:23] all right simple so just true returns
[16:26] true
[16:27] and this is going to be our test now
[16:29] when we run the command it's going to
[16:31] look for okay there is a test written in
[16:33] the function name initially so this is a
[16:36] test so let's see if i go here and i run
[16:39] this command again this time you say
[16:41] this test did not perform any assertion
[16:44] so we have tests
[16:47] which with the status of risk 1
[16:49] and it doesn't have any assertion
[16:51] assertion is still 0. so what exactly is
[16:54] this
[16:56] assertion
[16:57] so the thing is when you
[16:59] write a function
[17:01] you have a function for testing
[17:03] something
[17:04] you check that what exactly
[17:07] a function should return so let's say
[17:09] you were writing this test to check some
[17:11] particular function
[17:13] or feature
[17:14] and you were expecting some outcome from
[17:17] there so if you want to compare that
[17:21] you will be using assertions so let's
[17:23] see the example first so assertion is
[17:25] something like this so i say here
[17:27] dollar this
[17:30] and then i say assert
[17:33] and then i simply say true
[17:35] [Music]
[17:37] now this is going to check
[17:40] if whatever value we pass inside this
[17:43] function
[17:44] is true or not so
[17:46] if i pass here
[17:48] true
[17:49] and i'm going to save this
[17:52] and then i run this command again so i'm
[17:54] going to clear the screen first and then
[17:56] run command again
[17:57] you see here
[17:59] one test and one assertion and this
[18:02] green color means our test passed so it
[18:05] says okay so there is no issue
[18:07] so this test was expecting
[18:11] a true in return
[18:13] so we got the true we got a successful
[18:16] test
[18:17] now let's say
[18:20] for example
[18:22] you might have some functionality here
[18:24] so let's say if
[18:26] 1
[18:27] equals one which is true
[18:29] uh
[18:31] then
[18:33] you say here let's say here i create a
[18:35] variable dollar
[18:37] output
[18:39] just to show you the
[18:41] uh example here
[18:43] like how it might work in your code so
[18:46] this is a dollar output and this folds
[18:48] by default but if one is equal to one
[18:52] you are setting it to true
[18:54] and then you're passing this
[18:57] to here so pause here
[19:00] go back there run this test again we get
[19:05] test passed now if i say one is
[19:09] is equal to 2 which is not true and so
[19:12] this is not going to be true and this is
[19:14] going to pass their failed false so here
[19:17] i run this it says here field asserting
[19:20] that false is true so this search true
[19:25] is expecting a our function
[19:28] so
[19:30] let's say you have an application where
[19:31] you are inserting
[19:33] this test
[19:35] you will call its code here and you will
[19:38] use this assertion if you are expecting
[19:41] true from that code so this code i'm
[19:45] expecting true
[19:46] then i'm using a search true
[19:49] now we have so many options when it
[19:51] comes to assert if we go to php unit
[19:54] documentation
[19:56] here
[19:57] you will see the assertions so we have
[19:59] here
[20:00] so many types of assertions you can see
[20:02] a certain account
[20:04] where we are checking the count we're
[20:06] dealing with that we're going to see few
[20:08] of these we have a cert
[20:10] array has key so if our a it has
[20:12] particular key so you might return some
[20:14] data where you will check in some key so
[20:17] for now just just get this idea that you
[20:20] will be using few of these assertions
[20:23] and this assert true expect true
[20:26] from the above functionality and if it
[20:29] is uh true then this test is going to be
[20:32] passed
[20:32] so that's how this works so let's see
[20:35] another example just to clear the basics
[20:38] here so i'm going to save this and i'm
[20:40] going to create another test so here i'm
[20:43] going to say public
[20:45] function
[20:46] and this is going to be check
[20:49] if
[20:50] has
[20:52] key
[20:53] so i'm just
[20:55] writing a function this function returns
[20:58] nothing so i pass
[20:59] return void
[21:01] and here inside is a dollar this
[21:04] and i'm going to call the uh
[21:07] assertion this time so i go to the
[21:08] website as i showed you before i'm just
[21:10] going to copy this
[21:12] assert
[21:13] has
[21:14] key the same one we need to use here and
[21:17] here i'm going to pause
[21:19] that okay we need to check so pausing
[21:22] here
[21:23] let's check if the array has an age key
[21:26] and then i want to pause here an array
[21:28] so here i create an array
[21:31] so here dollar
[21:32] array
[21:37] roger maybe
[21:39] and then the h all right
[21:42] so we check here age
[21:45] 23 okay now we are we have an array now
[21:49] we can pause this
[21:50] user array here dollar user array and
[21:54] save this now i just need to check
[21:57] and if i semicolon
[21:59] so it is going to check if it has the
[22:02] key and if it has key this test should
[22:05] pass so let's go let's go to the
[22:07] terminal here i'm going to clear the
[22:09] screen again and i'm going to run this
[22:12] test
[22:14] okay we got uh
[22:16] and that is happening because of the
[22:18] first test so if you uh check here we
[22:20] have failure test one
[22:23] and uh
[22:25] so it
[22:26] basically tests one assertion one
[22:28] failure one so it ran the test and if
[22:31] you remember in the configuration we
[22:33] said stop on failure true
[22:36] so it failed here it didn't go to the
[22:38] next test it just stopped here
[22:40] so let's correct that what we need to do
[22:42] we need to make this test pause so we
[22:44] can go to next test so i simply turn
[22:47] this to one so one equals to one it
[22:50] becomes true and this test is going to
[22:52] be passed now so we'll go here run the
[22:55] test again
[22:57] and this time it runs now it's in one
[23:00] test
[23:01] and i made a mistake i made a
[23:04] uh this check if has key but we i had to
[23:08] forgot to do a test here so we need to
[23:10] pause here test
[23:12] check
[23:13] and this is something you need to be
[23:15] careful about too i
[23:17] save this
[23:18] and here this time it should work
[23:21] so let's see let's go back
[23:24] around again we have test two
[23:27] and two assertions now if i go back
[23:30] change this to two
[23:32] just to see if it stops on the first
[23:34] test so we go here
[23:37] it stops again failure test one
[23:40] assertion one failure one so it stops it
[23:43] doesn't go to the second test it just
[23:45] stops there so to work
[23:48] we need to have the
[23:50] uh
[23:51] evolve test true to go on the next test
[23:54] so that's how we configured it so you
[23:56] get the idea here so we have the key
[23:58] present in our array and it is checking
[24:01] assert array has key and uh if it is
[24:06] having the key so it is returning and
[24:08] the test true and successful so if i
[24:11] change here h12 or something something
[24:14] else
[24:15] and now if i try this so we get failure
[24:19] test two assertion true failure one so
[24:22] one test failed one passed and this one
[24:25] one is the one failed so we have
[24:27] if you go and see the details details
[24:29] are given right here that failure
[24:33] assertion that an array has the key of h
[24:36] 12. so you get the idea that where
[24:39] exactly is the mistake so here
[24:42] one more thing to add here yeah b pass
[24:45] test so this becomes a test so we you
[24:48] know php unit sees that as a test
[24:51] but what if you don't want to pass test
[24:53] there is an option so if i say here
[24:56] check if has key and still i want it to
[24:59] be test so in that case i will have to
[25:02] put here
[25:03] a comment
[25:04] like this
[25:05] and then i say add
[25:07] test so if you pause this
[25:10] and this again will be considered as
[25:12] test so if i go back here
[25:14] let's clear the screen and run the test
[25:17] you see two tests two assertions now if
[25:21] i go back here and i remove this comment
[25:25] and then i try again
[25:28] you can see that one test one assertion
[25:30] so yeah this is something another thing
[25:33] that you can use if you want to
[25:35] and if you want your tests to be written
[25:38] like this or you don't want to add the
[25:40] tests here
[25:41] keyword in the function name so you can
[25:43] use this way okay so we saw the sample
[25:46] test now let's work with some
[25:48] functionality now we want to do test
[25:51] driven
[25:52] development
[25:53] so in this test driven development we
[25:55] are going to first going to create a
[25:57] test file and then we are going to
[25:59] create the model file to see how it
[26:01] works so let's get started in unit uh
[26:04] folder here where we have sample tests
[26:07] i'm going to create another for a file
[26:09] i'm going to we're going to call it
[26:11] employee
[26:12] so it's going to be employee
[26:15] [Music]
[26:16] test
[26:18] dot php
[26:20] so this is going to be the
[26:21] another test file and for your tests you
[26:25] will might create more further
[26:28] classes just the same way as i have the
[26:30] sample test here we can copy the code
[26:32] from here so i simply
[26:33] copy everything and paste in here
[26:36] now we need to change first of all the
[26:39] class name it's going to be employed
[26:41] [Music]
[26:42] all right now the functions we are going
[26:44] to define ourselves so i'm going to
[26:46] remove those functions here so we have
[26:48] another test class ready so i'm going to
[26:50] close the sample test i'm going to close
[26:53] this xml and composer
[26:55] and here now first
[26:58] i'm going to create a function and
[27:01] that's function is going to do it's
[27:03] going to get the
[27:05] employee details so
[27:07] let's say we want a function to get the
[27:09] employee name so here it's a
[27:13] public
[27:15] function
[27:16] and i'm going to call it test
[27:19] get
[27:21] employee
[27:23] and then name
[27:24] so we are getting the employee name
[27:26] if the function is uh not returning
[27:29] anything we can pass void
[27:32] now if you don't want to pass boy that's
[27:34] up to you
[27:35] i'm going to create a dollar
[27:37] employee
[27:39] and it is going to be from employee
[27:41] class we don't have that class so we're
[27:43] going to create it so i'm simply going
[27:45] to say here
[27:46] new
[27:47] and it's going to be
[27:49] app
[27:51] models
[27:52] employee
[27:54] okay this is going to be the class
[27:56] currently we don't have it we just have
[27:58] app folder so we will be creating it and
[28:01] here once you have it you can say
[28:04] dollar
[28:05] employee
[28:07] and then you say
[28:09] set
[28:10] name
[28:12] and we pass a name so i'm passing roger
[28:14] here so here we are saying that in this
[28:18] class we have a function set name and we
[28:20] are setting name using that function
[28:23] as simple as that and at last we are
[28:25] going to check for dollar
[28:28] days
[28:29] and this time we are going to use
[28:30] another insertion so assert
[28:32] and it's not true it is equals so equals
[28:36] so it is going to check if the both
[28:38] values are equals
[28:40] so we need to get dollar employee
[28:45] and as we are setting a name in a
[28:48] function
[28:49] inside this class we can get the name as
[28:51] well so we say here get name
[28:54] it's another function and we want to
[28:56] check
[28:57] whatever this function returns is it
[28:59] equal to
[29:01] roger
[29:04] so if these two values are equal this
[29:06] test will pass so that's how this test
[29:09] will work
[29:10] so all right so if i
[29:13] uh go now and run the command here
[29:17] and
[29:18] we get error we get class employees not
[29:22] found so we need to create that class so
[29:24] i go here and here we need to create a
[29:27] directory first of all it's going to be
[29:30] modeled
[29:32] and inside there i need to create a
[29:34] class
[29:35] with the name of employee
[29:38] php
[29:41] simple class
[29:47] [Music]
[29:52] all right save this class go back and
[29:55] clear the screen run the test again
[29:58] and this time we have okay still not
[30:02] i need to provide the namespace sorry
[30:04] about that
[30:05] [Music]
[30:06] namespace and it's going to be
[30:09] uh the app
[30:11] [Music]
[30:13] okay it should be fine now until it's
[30:15] done
[30:16] so we get this time a different letter
[30:19] so here we were getting the class not
[30:21] found this time it says call to
[30:22] undefined function set name
[30:25] why set name because
[30:27] in the test when this test ran
[30:30] it first checked class second check the
[30:32] set name and at last it will check
[30:35] the get name so here it gave the error
[30:37] because it didn't see this function in
[30:39] this class so let's create this function
[30:42] so here i say function
[30:45] to set
[30:46] [Music]
[30:48] employee name
[30:51] so i say here public
[30:54] function
[30:55] [Music]
[30:56] set name
[30:57] and here we're gonna get a name
[31:00] and what i want to do i want to set it
[31:02] to a variable so we can use it anywhere
[31:04] so i say here
[31:07] i create a product and
[31:09] believe
[31:10] it's going to be employee
[31:14] all right and here as we are setting we
[31:16] can call this variable inside this class
[31:19] so i say dollar base
[31:21] employee
[31:24] and this is equal to dollar name so
[31:26] whatever value is coming in name put
[31:29] inside this dollar
[31:31] employee so
[31:32] once it is inside here we can call it in
[31:35] any function so we can save this now if
[31:37] i run this now we are again going to get
[31:40] together as we do not have get name
[31:43] so you can see that we get the error
[31:44] forget name so let's create a get name
[31:47] method there so simply create here
[31:50] function
[31:52] to get
[31:54] employee
[31:55] name
[31:57] and it's going to be public
[31:59] function
[32:01] get me
[32:03] now here we don't need to pass anything
[32:05] because we are just going to return the
[32:06] value
[32:07] so here we can say return
[32:10] and we can as we are setting the name
[32:12] here we can return this so we say dollar
[32:14] this
[32:15] and we say employee
[32:17] all right so this is the name we are
[32:19] returning
[32:20] now
[32:21] so here if you see first we call the set
[32:24] name it it's added the name it
[32:27] added the name here in the variable and
[32:29] then we're running the get name here so
[32:32] it's going to get the name automatically
[32:35] so this time as we
[32:37] are doing both the steps it should give
[32:39] us a successful test so let's see so we
[32:42] go here run the test we get test three
[32:46] assertions three
[32:48] so it
[32:49] ran the test on both files and simple an
[32:52] employee test and it gave us the results
[32:55] and our test successfully passed in this
[32:58] time
[32:59] now let's see another example now we
[33:01] want
[33:03] we're already working with the name
[33:05] let's do something
[33:07] more with this employee so first we get
[33:10] the age and then prefer their data so
[33:12] what we need to do simply
[33:14] create a public function
[33:17] and test
[33:22] get employee
[33:25] each
[33:26] all right function returns nothing now
[33:29] we pass void here
[33:31] and here we're gonna use this
[33:34] uh dollar employee again because we want
[33:37] to
[33:39] set ph this time so dollar
[33:42] employee
[33:44] and then i say h
[33:46] [Music]
[33:48] i'm sorry we're gonna say set
[33:50] h and here we're going to pass the
[33:53] number so i'm going to pause here
[33:55] the number then error is 7 of this
[33:59] and here we can say insert
[34:04] equals and insert equals we could say
[34:07] dollar
[34:08] employee
[34:11] get
[34:12] h so we're setting age and we're getting
[34:15] it so i said 23 here
[34:17] all right so if i run this it should
[34:20] give us error because we don't have
[34:21] these functions we have this class that
[34:24] error will not come anymore so i run the
[34:27] here and you can see call to undefined
[34:29] function
[34:30] set h so let's create this function so i
[34:32] go back there and here i say
[34:36] function
[34:37] to
[34:40] public function
[34:43] set h
[34:47] just like we did in the name we are
[34:50] going to set an age here so i said all
[34:51] of these
[34:53] and employee
[34:55] h
[34:56] going to be equal to dollar
[34:58] h so we don't have dollar h right now so
[35:01] let's put it there
[35:03] i'm gonna put it here we don't have this
[35:05] variable either so let's save this too
[35:08] so we go here
[35:10] protected
[35:13] you can give it as public as well if you
[35:16] want to but
[35:18] here
[35:18] i'm giving it protected so that we use
[35:22] uh using getter get method and set
[35:24] method
[35:25] so here we are setting the age now let's
[35:28] get the age so here i say public
[35:31] function
[35:32] and this is going to be get
[35:34] age
[35:36] we don't need to pass anything in here
[35:38] and i did not add the comment which is
[35:41] very good practice if you add a comment
[35:43] so
[35:44] function
[35:46] to
[35:47] get
[35:48] employee
[35:50] each
[35:52] and then get set here so let's create
[35:54] that
[35:55] now and get each we just want to return
[35:58] this so simply do that
[36:00] so say here
[36:01] return
[36:03] and save this so
[36:06] if i go to test we are doing the same
[36:08] thing we are passing 23 then we are
[36:10] checking if it is 23. so let's see if
[36:12] everything works fine so here let's
[36:15] first clear the screen
[36:17] and then run the test so we get four
[36:19] tests four assertions
[36:22] and we have okay sign so that our tests
[36:24] are clear and working fine now we have
[36:27] the test to get the
[36:30] name and age of employee we can also
[36:32] have again
[36:34] tests to get both values
[36:37] so for that so simply say here public
[36:42] function and at the moment it is going
[36:44] to make sense why i'm doing more tests
[36:47] and why and how this is going to effect
[36:50] so let's add this as a test
[36:53] and this time we're going to
[36:56] get
[36:58] employee
[37:01] [Music]
[37:02] name
[37:03] and h
[37:05] all right
[37:06] function will be void again
[37:08] and
[37:09] we just want to test it so it's not
[37:12] going to return anything so we can
[37:14] work with that so here i simply gonna
[37:16] say
[37:17] copy and paste employee again
[37:20] just like that and here dollar employee
[37:25] and this time we are going to call a
[37:27] function
[37:28] set name
[37:31] and h
[37:33] and here i'm going to pass two values
[37:36] first roger
[37:37] second 23
[37:39] and here we're going to say assert
[37:43] equals again
[37:45] [Music]
[37:46] and here it's going to be dollar
[37:48] employee
[37:50] [Music]
[37:52] get
[37:54] name
[37:55] and
[37:56] h
[37:57] all right
[37:58] and here we want to compare two
[38:01] uh to an array and it is going to be
[38:04] uh
[38:06] for
[38:08] roger
[38:09] and the age going to be
[38:10] 23.
[38:12] all right
[38:13] so here we're getting these values and
[38:15] comparing with this array of these two
[38:17] values and here we are getting the value
[38:20] so currently obviously it is going to
[38:22] give us the error
[38:23] because we don't have the set
[38:26] name as well as we don't have to get
[38:27] name so let's add that so we go here
[38:30] so you're simply going to
[38:34] add a function
[38:36] [Music]
[38:37] function
[38:39] to
[38:40] set
[38:42] name and
[38:44] each of
[38:45] employee
[38:48] all right
[38:50] public
[38:52] function
[38:55] and we say here set
[38:58] name
[38:59] and
[39:00] age
[39:03] [Music]
[39:08] all right so we are getting two values
[39:10] here so simply say
[39:12] file name
[39:13] dollar each
[39:15] and here we're gonna say dollar days
[39:25] employee name dollar name
[39:29] download this
[39:32] employee
[39:34] age
[39:37] dollar each
[39:39] all right so regarding the both
[39:41] properties here
[39:42] and here i have employee and i called
[39:46] here employee name so it's
[39:48] it's to be corrected it's going to be
[39:49] employee
[39:50] and employee okay so both are going to
[39:53] set now let's get
[39:55] if i run obviously we're gonna get the
[39:57] error for get name and h so let's add
[39:59] that one
[40:00] so for that i say here function
[40:05] to
[40:06] get
[40:07] name and h
[40:18] [Music]
[40:22] all right so here we don't need to pass
[40:24] any values
[40:26] i simply gonna say another
[40:29] phase
[40:30] now
[40:31] uh
[40:32] here we are if you remember in the test
[40:34] we are comparing it with an array so we
[40:37] need to make sure that whatever this
[40:39] returns it should be an array of with
[40:42] these two values to make it uh a pause
[40:46] test
[40:47] so here when i go
[40:49] what i can say i can simply say here
[40:52] return an array
[40:54] and they're going to be first valid all
[40:55] the days
[40:56] and it's going to be employee
[41:00] and the second will get all of these
[41:03] and employee
[41:06] name
[41:09] all right so we're getting
[41:11] the both values now let's see what
[41:13] happens in the tests
[41:15] so i go here clear the screen and run
[41:17] the test
[41:18] and we get i think i made a mistake so
[41:21] here uh i made an
[41:24] employee in name which is not there so
[41:26] we're gonna say employee so we go back
[41:29] there and
[41:31] run the test again
[41:33] and you can see that we get the five
[41:35] tests five assertions
[41:37] so we are getting uh
[41:40] what are we looking for so here we
[41:42] are successfully uh doing it we are
[41:45] running the
[41:47] assertion each
[41:49] single assertion on each function but we
[41:52] can run more than one assertions so
[41:54] let's see what i'm talking about here so
[41:56] if i go to the sample test that we
[42:00] checked before here we have our as key
[42:02] i'm just going to copy this and i'm
[42:04] going to use the same here in this
[42:07] function so i'm going to pass here
[42:09] uh so first we're going to insert equal
[42:12] so if the values are equal
[42:14] after that what i'm going to check i'm
[42:16] going to check that out so
[42:18] now here uh we are returning
[42:21] uh two values so we can
[42:24] uh check if there is value so if i say
[42:27] here one
[42:28] so this array has
[42:31] two other zero index and one index and
[42:33] that means key zero so it's going to be
[42:36] like this it's doing that way so that is
[42:38] what is returning it's not showing zero
[42:40] one there because it is
[42:42] indexed or it's not
[42:45] it's simply passing the values not the
[42:47] keys so here uh we're gonna check if we
[42:50] have key one
[42:52] inside this get name so we're just gonna
[42:54] copy this
[42:56] pass
[42:56] here save this
[42:59] go back i'm going to clear the screen
[43:01] first
[43:02] and i'm going to run the test now you
[43:04] see here that we have five tests and six
[43:06] assertions
[43:08] and we got successful uh on this one so
[43:11] if i say here
[43:13] uh check if the key three is present we
[43:15] know that we have only two values coming
[43:18] in this array so we don't have three so
[43:20] it's gonna fail so if i go here
[43:23] it fails it says fail to asserting that
[43:25] array has the key three because then
[43:28] area has green one zero and one so you
[43:30] get the idea we have multiple assertions
[43:32] we can pass in a
[43:34] test let me show you one more thing now
[43:36] when we go here in our employee test
[43:40] class we have the function test and
[43:43] get the employee name then age the name
[43:46] and age
[43:47] so if you notice we are calling the
[43:50] employee model each time
[43:52] and here
[43:54] it is also used here it is also used
[43:57] so what we can do we can
[44:00] take this model out and call it whenever
[44:03] we
[44:05] want it because there is a method that
[44:07] is
[44:08] available in pgp unit that runs
[44:12] before every test
[44:14] so let me show you how it works so
[44:16] basically we are going to define a
[44:18] method and it's going to be
[44:20] public
[44:21] function
[44:22] and it is called setup
[44:25] so this setup method is going to run
[44:28] each time
[44:30] and it returns nothing so it's void
[44:33] and it return um basically going to run
[44:36] so these test runs this function is
[44:38] going to call before this test runs then
[44:42] before this test so if you have 10 tests
[44:44] it's going to run each time
[44:46] so we are instantiating our class every
[44:50] time so what i'm going to do i'm going
[44:51] to put
[44:53] this here
[44:54] and i want to create the employee
[44:58] whenever
[44:59] we
[45:00] set up so what i do simply i'm going to
[45:02] copy this i'm going to paste it here
[45:05] all right nothing changed but the thing
[45:08] is now if i take this employee outside
[45:11] we can access it in each function so if
[45:13] i say here
[45:15] that protected
[45:18] dollar
[45:20] employee
[45:22] and here it is protected
[45:25] and here now i call it this
[45:29] dot employee so you get the point at
[45:31] this time
[45:33] we are i forgot semicolon here
[45:36] so
[45:38] employee now this time we can use it in
[45:41] every function so all we need to change
[45:43] instead of dollar employee we need to
[45:45] call this employee so let's copy this
[45:48] i'm going to remove this from here
[45:51] and i'm going to paste it here
[45:54] all right so base employee and then set
[45:56] name
[45:57] so
[45:59] now each time it is going to re-uh
[46:02] run on each test so it is going to be
[46:04] renewed every time so it is not going to
[46:06] be
[46:08] something that your employee did a
[46:10] change here and
[46:11] chain data
[46:12] passed to the next function it's not
[46:14] going to do that because it's
[46:16] we are setting it each time
[46:20] so it is going to be renewed
[46:22] instance of employee class so here we
[46:25] got this employee
[46:27] i'm going to pause the same way in other
[46:29] functions
[46:41] all right so we have this employee
[46:44] on every function now and we are
[46:47] instantiating our class here
[46:49] so let's do this let's run the test and
[46:51] if everything is right we should get all
[46:53] the tests passed so here we have five
[46:57] tests six assertions everything is
[46:59] working just as expected now let's see
[47:01] the example of a collection how you work
[47:04] with collection if you're not aware of
[47:07] collection if you
[47:09] heard laravel and laravel we have
[47:11] collections and uh
[47:13] we can store data in
[47:15] collection using the larval
[47:18] now in our case we are not using larval
[47:21] we are using php so i'm going to create
[47:24] a collection class and then we are going
[47:26] to put the data in there and that
[47:29] that data
[47:30] array going to become an object of
[47:32] collection so let's see how it works
[47:35] first of all we want to create another
[47:37] class in our unit test because we want
[47:40] to have another test and that's going to
[47:42] deal with only the collections so here i
[47:45] see here
[47:47] another class
[47:48] collection
[47:50] test dot php
[47:54] all right so this is the class it's
[47:56] going to be now i'm going to copy the
[47:58] code from
[47:59] sample
[48:00] test
[48:01] i'm going to paste here
[48:03] change the name to collection
[48:07] and make sure name spellings are cracked
[48:10] collection
[48:12] test
[48:13] and we are going to remove
[48:15] the methods here and we're going to add
[48:17] our own
[48:18] now the same way i'm going to create
[48:21] another model so here
[48:23] uh
[48:24] in directory in this
[48:26] app folder for us
[48:28] so let's do that i say here
[48:31] support
[48:32] and then i'm going to call
[48:34] a file inside it which is going to be
[48:36] collection
[48:39] just two ranges this way
[48:41] and here i'm going to create a class so
[48:43] it's going to be collection
[48:46] php
[48:49] php namespace
[48:52] and it's going to be app
[48:54] and then support
[48:57] all right and it's class
[49:00] all right i changed the name of this
[49:02] folder to capitalize so yes should be
[49:05] bigger
[49:06] so support collection so let's see how
[49:10] we can get uh
[49:12] and work with more assertion methods
[49:14] using collections here
[49:16] the collection class and here i'm going
[49:19] to write the first
[49:20] uh test for it and collection test so
[49:23] that's right here so it's going to be
[49:25] public
[49:26] function
[49:27] and i say
[49:28] uh
[49:31] let's
[49:31] do this and this one we're not going to
[49:33] use the test we're going to use the
[49:35] comment so that we can see how we can
[49:37] use that as well so here we say check
[49:40] and we want to check
[49:42] if data
[49:44] present
[49:46] and this array is going to check now we
[49:49] want it to run as tests so we want to
[49:51] add the comment add test
[49:54] and here what we want to do i want to
[49:56] call the collection class so it's going
[49:58] to be dollar collect
[50:01] you can see
[50:03] here what i want to do i want to say
[50:04] dollar collect
[50:06] and i want to call the function which is
[50:08] going to
[50:10] be
[50:10] get data
[50:12] and paste here a certain empty so it's
[50:15] just going to check for this particular
[50:18] uh
[50:19] if we have this method and if it is
[50:21] returning empty or not so it is going to
[50:24] work on array so let's go there in
[50:26] collection so we need to
[50:28] create this method get data otherwise
[50:30] we're going to get errors apparently if
[50:32] i run
[50:33] this we get
[50:34] the error so we don't have get data so
[50:37] let's say here
[50:39] so you're probably so here what we want
[50:41] to do we want to
[50:43] get download this
[50:45] and here we can
[50:48] get the items
[50:51] and we want to
[50:53] have the items it's other item now
[50:56] currently we
[50:58] don't have the
[51:00] data setting anywhere so what we can do
[51:02] here is either we can have by default or
[51:04] we can create a
[51:06] set data the way we did before so
[51:09] previously we used get data and send
[51:11] data so in this case um i want to try
[51:14] something different so what i do i'm
[51:15] just going to pause here
[51:17] an array and that is going to be the
[51:19] data so i'm going to say here 14
[51:22] 23 56 so these are the values in this
[51:26] collection we are passing and here in
[51:28] collection
[51:29] as we have and currently no data we can
[51:32] call the
[51:34] uh constructor so if you
[51:36] know the concept here when you
[51:38] instantiate a class a constructor runs
[51:41] automatically so we can create a
[51:43] constructor here and that constructor is
[51:46] going to assign the values for us so
[51:47] here first i'm going to assign this the
[51:50] dollar items so here i say
[51:53] protected
[51:54] dollar items
[51:56] and that's going to be the variable
[51:59] now here we want to run a constructor so
[52:01] it's going to be public
[52:04] function underscore underscore
[52:07] constru
[52:08] rocked
[52:11] all right so we have a constructor here
[52:14] and this constructor is getting an item
[52:16] and this is the item that we want to set
[52:21] like this so we're gonna
[52:23] just gonna paste here instead there
[52:27] so
[52:28] if you see here we go to
[52:30] collect here we are passing an array
[52:33] this array will be passed here and it's
[52:37] going to be assigned to item
[52:39] items here and it will be accessible so
[52:43] in get data function what we can do we
[52:45] can have a local variable so i say
[52:47] dollar data it is going to be an empty
[52:50] array then i can say here
[52:53] for each
[52:55] and we can access to these items now so
[52:58] simply copy this from here
[53:00] paste here and then i say it has
[53:03] dollar item
[53:06] and then we can put this inside data
[53:09] it's just like that dollar
[53:12] item
[53:13] all right and here we return
[53:16] builder data
[53:18] so you can see that the get data will
[53:21] return
[53:23] value an array of
[53:26] array of these numbers so it should
[53:29] uh it is not empty in that case so it
[53:32] should fail this test so let's see let's
[53:35] go here
[53:37] i clear the screen run the test
[53:40] so it says failure asserting that the
[53:42] array is empty because array is not
[53:44] empty we have three values we are
[53:46] passing so what i do i remove these
[53:48] three values this time
[53:50] and
[53:51] save it
[53:52] go back
[53:54] now you can see that has passed
[53:57] because we are checking
[53:59] here if empty so we are passing empty
[54:02] and that makes this test to pass
[54:06] so this is that is uh the
[54:09] working with the empty and assert empty
[54:12] now here we
[54:14] uh get the data like for example here we
[54:18] pause the array
[54:19] and if you see dollar collect here
[54:22] so let's me reward it back
[54:25] and if i copy this and bar dump here
[54:30] you can see
[54:33] this time when i run we get an object
[54:35] where we have
[54:37] are inside it
[54:38] with the three values so
[54:40] that's the reason when we
[54:42] were really here we were looping through
[54:46] that object and uh
[54:48] making it an array because here a
[54:52] certain empty checks for for an array if
[54:55] that array has value or not so here we
[54:57] are passing an array with the values
[54:59] so okay if you have noticed uh in the
[55:01] sample test.php the
[55:04] class name is not correct it can create
[55:07] a header so we need to correct it it's
[55:09] just simple
[55:11] test
[55:13] also in employee test if i go to the
[55:16] class name it should match always so it
[55:19] should
[55:20] have a capital t
[55:22] so make sure
[55:23] you have it other than days i think
[55:26] rest all is good but
[55:28] yeah it should match
[55:30] if you like the content of this channel
[55:31] and you want me to create more videos
[55:33] like this
[55:34] please support me on patreon you can
[55:37] also subscribe this channel like this
[55:38] video and share with others thank you
[55:41] for watching
[55:42] [Music]
⚡ Saved you time reading this? Transcribe any YouTube video for free — no signup needed.