---
title: 'php unit testing tutorial for beginners [ Test Driven Development ] Full guide'
source: 'https://youtube.com/watch?v=CmlTXDRji0A'
video_id: 'CmlTXDRji0A'
date: 2026-06-17
duration_sec: 0
---

# php unit testing tutorial for beginners [ Test Driven Development ] Full guide

> Source: [php unit testing tutorial for beginners ( Test Driven Development ) Full guide](https://youtube.com/watch?v=CmlTXDRji0A)

## Summary

This 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.

### Key Points

- **Introduction to Unit Testing** [0:00] — 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.
- **Setting Up PHPUnit** [1:14] — 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.
- **Configuring PHPUnit with phpunit.xml** [5:22] — A `phpunit.xml` configuration file is created to define test suites, bootstrap file (`vendor/autoload.php`), and options like `colors=

### Conclusion

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.

## Transcript

welcome guys in this video we're going
to see unit testing so what exactly unit
testing is and how it works
so let's see is the definition of it by
definition 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 so basically we check the
small portion of code if that particular
code is doing what it's supposed to do
so we check that and that is the way how
we you put the unit testing on the
application now we are going to see test
driven development in this
video and this test driven development
basically tells us that how
you create
a test and how you uh create the
application according to that test so
that you get the idea how things should
work and
it is easy to debug in case when you
want to debug it now you will be able to
implement unit testing once you complete
this video uh and understand all the
concepts so you will be able to
implement it in your own application so
let's get started and see how we can
make unit testing work
[Music]
to start with i'm going to create a
brand new folder on the desktop and i'm
going to give it a name
of
unit test
i'm going to open the terminal now in
here
and make it a little bigger here so i
say ls list item here i have desktop so
i can go there so i simply say here
desktop and slash
on desktop i have this unit test server
unit
and then test
simply and you can see that i'm there
now so if i say ls here you can see
there's no items here so i simply say
here
clear now i'm going to install a package
and for installing that package we need
to make sure we have composer installer
on our system so if i say here composer
and hit return i see these commands
and if you don't see these commands
while running composer that means
composer is not installed in your system
so you need to install composer
and for that the links are in the
description for windows as well as for
mac os
now as i have the composer install i can
install the php unit package
that package is going to uh help us work
for the testing so i'm going to clear
this screen now
and
the package we're going to use is php
unit you simply can put your php unit
and the google and the first thing you
will see in the php unit it's a very
good and
advanced package for
testing new php so i may click there
and
again here in the documentation
english
now we have here installing php unit so
we can simply go here now we're going to
use the
uh composer which is given here this
command we need to run you can also
use the curl if you want to you can
check this
link this link is in the description so
if you want to try that if you're trying
to install using composer that's a lot
easier so we are going to do that way so
we are already in the folder so i'm just
going to copy paste the command and the
version is 9.5 that we're going to use
in this
uh course so i'm going to hit return now
and then it's going to install for us
it's installing the files
open the folder in the desktop i have
this php unit so i'm just going to open
there
and here we have the
project
so currently it is installing it
completed in the install if i go inside
the folder i get this vendor folder that
is installed by composer there is
composer.json there is composer.log
so these three
things we have here the same in the
rbs code
so
we need to
write tests here so first of all in the
main unit test directory i want to
create
a directory and that directory going to
be the tests directory so let's do that
so you can click here
and i say tests
now there can be
tests for feature tests for units
so we are going to cover unit so i'm
going to create a folder inside here
so here it's going to be tests unit and
inside this unit we're going to write
our tests
now if you uh brought
or have worked with larval you might
notice that there they have by default
tests with the
directory and their units and they
feature
uh two for the directories but here we
are going to work with the unit testings
of the vhs creating unit here i'm also
going to create another directory in the
unit test directory which is going to be
app so we're going to create the model
you know or any other php files in here
so this is going to be the main file
model directory and here in the
composer.json i'm going to
add the autoload so that it autoloads
the files
classes automatically so i'm simply
going to say here
auto load
[Music]
and here we are going to
pause three values and it is going to be
psr
4. and it's an object and here we are
going to give the directory to app
as we have app directory
and
we pass app
all right so this is the directory we
created here
so we can save this and this is going to
deal with the auto load now for
working with this we need to run a
command that's going to be
composer
dump
autoload
[Music]
dash oh
and
i made a mistake here
and you can see that it's it's
generating optimized autoload files so
here i'm generating optimize upload
files
so yeah this has 10 1000 plus classes
that is all happening because of the
vendor folder there they have a few
packages and those packages have classes
so it is optimizing and generating
those for us so here i simply gonna say
clear
the screen as we have php unit installed
we can run phpnet command and that's
command is
simply we say
dot slash and then we're going to say
vendor
[Music]
and after that we want to say bin
and then php unit and when you run this
command you're going to get all the
options all the
configuration options from the php
test in a test so here you can check
all the options and details about those
and you need to define a few of these
options to work with
[Music]
the tests so uh
for example we need to define the where
we have the
dash we verbose so output as much as
information possible for that we need
this or we can say dash bootstrap a php
script that is included before the tests
run
so we also have prepend um php script
that is included as early as possible
there are so many options we have
options for debug we have option for
repeat so it runs the tests repeatedly
so this is something that we need to
provide as a configuration
now what i'm going to do i'm going to
create a file and you're going to use
few of these options
to tell
our application so whenever we run the
command it takes these parameters the
parameters we provide in that
file
and pick from there before running any
tests so for that what we need to do we
need to go here in the directory and
here in the main unit test directory i'm
going to create another file and this is
going to be phpunit
dot xml so this is going to be the file
and here we need to say first of all
first thing first we need to paste here
the xml version information and after
that we i'm going to create a tag it is
going to be php unit
and then we need to close this tag as
well
now inside this tag we need to tell that
what
we want to do what exactly
uh going to the directory where we want
to run the tests so we created the
directory tests here so we want to
mention our directory there so here what
i say and say here tests
suits
all right
this is going to be another tag
just like this
now inside this test suit i'm going to
pass single suit so it's going to be
test
suit
now here we can give it a name so i'm
going to give it the name of
let's call it php
you can give any names and say here php
test suit
simply and here inside this we want to
run uh tell the directory for the test
so here i say the another tag it's gonna
be directory
and then i'm gonna close this directory
just like that now here inside it we
just
mentioned the name of the directory so
it's tests in our case so i say tests
now this is uh almost done but few more
things we can add here so let's add that
so in php unit we need to pass these
configuration settings as well so we're
currently just saying tests but we
haven't passed any configuration
settings so the main settings
for this
video of ours we need is first is
bootstrap
[Music]
so if you remember the bootstrap we saw
here a php script that is included
before and runs the test and what i'm
going to do i want to load all the
classes so here render
slash overload
dot php
and this is inside vendor
which is
autoloading so if i go here
and you can see that that file is there
so we're just calling that file nothing
more if you want to run any other file
you can use the configuration from here
and you can adjust this file
now we already added this now what i
want to do i want to add a few more
parameters so you just pass me a
parameters here so i say the output we
want to see it should be colorful so
it's going to be easy to read
so i'm going to call here colors
true
[Music]
and then we want verbals
and i want it to be true as well
now the another thing is stop
so stop
on
failure
so let's say we're going to write a few
tests and each php going to run each
test one by one so if any test fails we
want to stop the script there and it
should inform us that that particular
test failed
so for that we need to say here for also
true so i'm going to say true
[Music]
stop on failure true
all right
so this is all what we need to make our
php
uh
unit command work so if i run here
clear
now we created this file and it's going
to pick every setting from here
if you don't provide this file
in that case you will have to
provide all the
uh these commands after your
main command so here like i just ran
this command here and dot
dash vendor bin php unit so if you want
to run
let's say version or something something
like that you will be putting after at
the end here
or here let's say here
like this so we don't want to do this
with that's why we created this file and
then if you notice here we have colors
true so we are getting here a
mustard color and we're getting the
information about our tests on running
we are using 9.5 version so that is uh
showing up and provide running all this
command so this is the command that will
be running
to make uh
to test now currently when i ran this
command it says no test tests executed
thing is we gave it the directory tests
and if we go in tests we have only a
folder no test inside it
so to have some tests we need to
create tests here and those tests going
to be the php classes so here in unit
folder i'm going to create a sample test
so i'm going to call it sample
and this has a sample test
it should have the test at the end
sample test so i'm going to create this
class now here we are going to say php
and we want to say here class
[Music]
and sample
test same as the name of the
file
and i forgot to add the php here it
should be dot php
and here sample test
now this simple test
it's going to be a class but it's going
to extend php unit so here we're going
to call class we're going to say here
use and we're going to say php
unit
backslash
framework
and test case
all right so this is what it is going to
extend so here's the extents
test case
all right great so here we have our
first sample test so if i run this
command now it should know as we created
the sample test clause we can run
the command again so i'm going to run
this vendor in php unit and this time we
get no tests found in class sample test
now the thing is if you notice
previously we had no tests executed this
time it recognized that we
have a sample test class
but this time it is looking for tests so
it says no tests found in class so we
we don't have anything here currently
that's we're getting the error so we
need to
add our test now so uh here we have the
assertion zero in a moment being able to
see what assertions are and how works so
first and we need to define a test here
now let's write our first test so i'm
going to uh right here a function it's
going to be public
function i'm going to say this
now whenever you write tests you
start with tests so he says test and
then whatever you want to do so here i
say true
returns
true
all right simple so just true returns
true
and this is going to be our test now
when we run the command it's going to
look for okay there is a test written in
the function name initially so this is a
test so let's see if i go here and i run
this command again this time you say
this test did not perform any assertion
so we have tests
which with the status of risk 1
and it doesn't have any assertion
assertion is still 0. so what exactly is
this
assertion
so the thing is when you
write a function
you have a function for testing
something
you check that what exactly
a function should return so let's say
you were writing this test to check some
particular function
or feature
and you were expecting some outcome from
there so if you want to compare that
you will be using assertions so let's
see the example first so assertion is
something like this so i say here
dollar this
and then i say assert
and then i simply say true
[Music]
now this is going to check
if whatever value we pass inside this
function
is true or not so
if i pass here
true
and i'm going to save this
and then i run this command again so i'm
going to clear the screen first and then
run command again
you see here
one test and one assertion and this
green color means our test passed so it
says okay so there is no issue
so this test was expecting
a true in return
so we got the true we got a successful
test
now let's say
for example
you might have some functionality here
so let's say if
1
equals one which is true
uh
then
you say here let's say here i create a
variable dollar
output
just to show you the
uh example here
like how it might work in your code so
this is a dollar output and this folds
by default but if one is equal to one
you are setting it to true
and then you're passing this
to here so pause here
go back there run this test again we get
test passed now if i say one is
is equal to 2 which is not true and so
this is not going to be true and this is
going to pass their failed false so here
i run this it says here field asserting
that false is true so this search true
is expecting a our function
so
let's say you have an application where
you are inserting
this test
you will call its code here and you will
use this assertion if you are expecting
true from that code so this code i'm
expecting true
then i'm using a search true
now we have so many options when it
comes to assert if we go to php unit
documentation
here
you will see the assertions so we have
here
so many types of assertions you can see
a certain account
where we are checking the count we're
dealing with that we're going to see few
of these we have a cert
array has key so if our a it has
particular key so you might return some
data where you will check in some key so
for now just just get this idea that you
will be using few of these assertions
and this assert true expect true
from the above functionality and if it
is uh true then this test is going to be
passed
so that's how this works so let's see
another example just to clear the basics
here so i'm going to save this and i'm
going to create another test so here i'm
going to say public
function
and this is going to be check
if
has
key
so i'm just
writing a function this function returns
nothing so i pass
return void
and here inside is a dollar this
and i'm going to call the uh
assertion this time so i go to the
website as i showed you before i'm just
going to copy this
assert
has
key the same one we need to use here and
here i'm going to pause
that okay we need to check so pausing
here
let's check if the array has an age key
and then i want to pause here an array
so here i create an array
so here dollar
array
roger maybe
and then the h all right
so we check here age
23 okay now we are we have an array now
we can pause this
user array here dollar user array and
save this now i just need to check
and if i semicolon
so it is going to check if it has the
key and if it has key this test should
pass so let's go let's go to the
terminal here i'm going to clear the
screen again and i'm going to run this
test
okay we got uh
and that is happening because of the
first test so if you uh check here we
have failure test one
and uh
so it
basically tests one assertion one
failure one so it ran the test and if
you remember in the configuration we
said stop on failure true
so it failed here it didn't go to the
next test it just stopped here
so let's correct that what we need to do
we need to make this test pause so we
can go to next test so i simply turn
this to one so one equals to one it
becomes true and this test is going to
be passed now so we'll go here run the
test again
and this time it runs now it's in one
test
and i made a mistake i made a
uh this check if has key but we i had to
forgot to do a test here so we need to
pause here test
check
and this is something you need to be
careful about too i
save this
and here this time it should work
so let's see let's go back
around again we have test two
and two assertions now if i go back
change this to two
just to see if it stops on the first
test so we go here
it stops again failure test one
assertion one failure one so it stops it
doesn't go to the second test it just
stops there so to work
we need to have the
uh
evolve test true to go on the next test
so that's how we configured it so you
get the idea here so we have the key
present in our array and it is checking
assert array has key and uh if it is
having the key so it is returning and
the test true and successful so if i
change here h12 or something something
else
and now if i try this so we get failure
test two assertion true failure one so
one test failed one passed and this one
one is the one failed so we have
if you go and see the details details
are given right here that failure
assertion that an array has the key of h
12. so you get the idea that where
exactly is the mistake so here
one more thing to add here yeah b pass
test so this becomes a test so we you
know php unit sees that as a test
but what if you don't want to pass test
there is an option so if i say here
check if has key and still i want it to
be test so in that case i will have to
put here
a comment
like this
and then i say add
test so if you pause this
and this again will be considered as
test so if i go back here
let's clear the screen and run the test
you see two tests two assertions now if
i go back here and i remove this comment
and then i try again
you can see that one test one assertion
so yeah this is something another thing
that you can use if you want to
and if you want your tests to be written
like this or you don't want to add the
tests here
keyword in the function name so you can
use this way okay so we saw the sample
test now let's work with some
functionality now we want to do test
driven
development
so in this test driven development we
are going to first going to create a
test file and then we are going to
create the model file to see how it
works so let's get started in unit uh
folder here where we have sample tests
i'm going to create another for a file
i'm going to we're going to call it
employee
so it's going to be employee
[Music]
test
dot php
so this is going to be the
another test file and for your tests you
will might create more further
classes just the same way as i have the
sample test here we can copy the code
from here so i simply
copy everything and paste in here
now we need to change first of all the
class name it's going to be employed
[Music]
all right now the functions we are going
to define ourselves so i'm going to
remove those functions here so we have
another test class ready so i'm going to
close the sample test i'm going to close
this xml and composer
and here now first
i'm going to create a function and
that's function is going to do it's
going to get the
employee details so
let's say we want a function to get the
employee name so here it's a
public
function
and i'm going to call it test
get
employee
and then name
so we are getting the employee name
if the function is uh not returning
anything we can pass void
now if you don't want to pass boy that's
up to you
i'm going to create a dollar
employee
and it is going to be from employee
class we don't have that class so we're
going to create it so i'm simply going
to say here
new
and it's going to be
app
models
employee
okay this is going to be the class
currently we don't have it we just have
app folder so we will be creating it and
here once you have it you can say
dollar
employee
and then you say
set
name
and we pass a name so i'm passing roger
here so here we are saying that in this
class we have a function set name and we
are setting name using that function
as simple as that and at last we are
going to check for dollar
days
and this time we are going to use
another insertion so assert
and it's not true it is equals so equals
so it is going to check if the both
values are equals
so we need to get dollar employee
and as we are setting a name in a
function
inside this class we can get the name as
well so we say here get name
it's another function and we want to
check
whatever this function returns is it
equal to
roger
so if these two values are equal this
test will pass so that's how this test
will work
so all right so if i
uh go now and run the command here
and
we get error we get class employees not
found so we need to create that class so
i go here and here we need to create a
directory first of all it's going to be
modeled
and inside there i need to create a
class
with the name of employee
php
simple class
[Music]
all right save this class go back and
clear the screen run the test again
and this time we have okay still not
i need to provide the namespace sorry
about that
[Music]
namespace and it's going to be
uh the app
[Music]
okay it should be fine now until it's
done
so we get this time a different letter
so here we were getting the class not
found this time it says call to
undefined function set name
why set name because
in the test when this test ran
it first checked class second check the
set name and at last it will check
the get name so here it gave the error
because it didn't see this function in
this class so let's create this function
so here i say function
to set
[Music]
employee name
so i say here public
function
[Music]
set name
and here we're gonna get a name
and what i want to do i want to set it
to a variable so we can use it anywhere
so i say here
i create a product and
believe
it's going to be employee
all right and here as we are setting we
can call this variable inside this class
so i say dollar base
employee
and this is equal to dollar name so
whatever value is coming in name put
inside this dollar
employee so
once it is inside here we can call it in
any function so we can save this now if
i run this now we are again going to get
together as we do not have get name
so you can see that we get the error
forget name so let's create a get name
method there so simply create here
function
to get
employee
name
and it's going to be public
function
get me
now here we don't need to pass anything
because we are just going to return the
value
so here we can say return
and we can as we are setting the name
here we can return this so we say dollar
this
and we say employee
all right so this is the name we are
returning
now
so here if you see first we call the set
name it it's added the name it
added the name here in the variable and
then we're running the get name here so
it's going to get the name automatically
so this time as we
are doing both the steps it should give
us a successful test so let's see so we
go here run the test we get test three
assertions three
so it
ran the test on both files and simple an
employee test and it gave us the results
and our test successfully passed in this
time
now let's see another example now we
want
we're already working with the name
let's do something
more with this employee so first we get
the age and then prefer their data so
what we need to do simply
create a public function
and test
get employee
each
all right function returns nothing now
we pass void here
and here we're gonna use this
uh dollar employee again because we want
to
set ph this time so dollar
employee
and then i say h
[Music]
i'm sorry we're gonna say set
h and here we're going to pass the
number so i'm going to pause here
the number then error is 7 of this
and here we can say insert
equals and insert equals we could say
dollar
employee
get
h so we're setting age and we're getting
it so i said 23 here
all right so if i run this it should
give us error because we don't have
these functions we have this class that
error will not come anymore so i run the
here and you can see call to undefined
function
set h so let's create this function so i
go back there and here i say
function
to
public function
set h
just like we did in the name we are
going to set an age here so i said all
of these
and employee
h
going to be equal to dollar
h so we don't have dollar h right now so
let's put it there
i'm gonna put it here we don't have this
variable either so let's save this too
so we go here
protected
you can give it as public as well if you
want to but
here
i'm giving it protected so that we use
uh using getter get method and set
method
so here we are setting the age now let's
get the age so here i say public
function
and this is going to be get
age
we don't need to pass anything in here
and i did not add the comment which is
very good practice if you add a comment
so
function
to
get
employee
each
and then get set here so let's create
that
now and get each we just want to return
this so simply do that
so say here
return
and save this so
if i go to test we are doing the same
thing we are passing 23 then we are
checking if it is 23. so let's see if
everything works fine so here let's
first clear the screen
and then run the test so we get four
tests four assertions
and we have okay sign so that our tests
are clear and working fine now we have
the test to get the
name and age of employee we can also
have again
tests to get both values
so for that so simply say here public
function and at the moment it is going
to make sense why i'm doing more tests
and why and how this is going to effect
so let's add this as a test
and this time we're going to
get
employee
[Music]
name
and h
all right
function will be void again
and
we just want to test it so it's not
going to return anything so we can
work with that so here i simply gonna
say
copy and paste employee again
just like that and here dollar employee
and this time we are going to call a
function
set name
and h
and here i'm going to pass two values
first roger
second 23
and here we're going to say assert
equals again
[Music]
and here it's going to be dollar
employee
[Music]
get
name
and
h
all right
and here we want to compare two
uh to an array and it is going to be
uh
for
roger
and the age going to be
23.
all right
so here we're getting these values and
comparing with this array of these two
values and here we are getting the value
so currently obviously it is going to
give us the error
because we don't have the set
name as well as we don't have to get
name so let's add that so we go here
so you're simply going to
add a function
[Music]
function
to
set
name and
each of
employee
all right
public
function
and we say here set
name
and
age
[Music]
all right so we are getting two values
here so simply say
file name
dollar each
and here we're gonna say dollar days
employee name dollar name
download this
employee
age
dollar each
all right so regarding the both
properties here
and here i have employee and i called
here employee name so it's
it's to be corrected it's going to be
employee
and employee okay so both are going to
set now let's get
if i run obviously we're gonna get the
error for get name and h so let's add
that one
so for that i say here function
to
get
name and h
[Music]
all right so here we don't need to pass
any values
i simply gonna say another
phase
now
uh
here we are if you remember in the test
we are comparing it with an array so we
need to make sure that whatever this
returns it should be an array of with
these two values to make it uh a pause
test
so here when i go
what i can say i can simply say here
return an array
and they're going to be first valid all
the days
and it's going to be employee
and the second will get all of these
and employee
name
all right so we're getting
the both values now let's see what
happens in the tests
so i go here clear the screen and run
the test
and we get i think i made a mistake so
here uh i made an
employee in name which is not there so
we're gonna say employee so we go back
there and
run the test again
and you can see that we get the five
tests five assertions
so we are getting uh
what are we looking for so here we
are successfully uh doing it we are
running the
assertion each
single assertion on each function but we
can run more than one assertions so
let's see what i'm talking about here so
if i go to the sample test that we
checked before here we have our as key
i'm just going to copy this and i'm
going to use the same here in this
function so i'm going to pass here
uh so first we're going to insert equal
so if the values are equal
after that what i'm going to check i'm
going to check that out so
now here uh we are returning
uh two values so we can
uh check if there is value so if i say
here one
so this array has
two other zero index and one index and
that means key zero so it's going to be
like this it's doing that way so that is
what is returning it's not showing zero
one there because it is
indexed or it's not
it's simply passing the values not the
keys so here uh we're gonna check if we
have key one
inside this get name so we're just gonna
copy this
pass
here save this
go back i'm going to clear the screen
first
and i'm going to run the test now you
see here that we have five tests and six
assertions
and we got successful uh on this one so
if i say here
uh check if the key three is present we
know that we have only two values coming
in this array so we don't have three so
it's gonna fail so if i go here
it fails it says fail to asserting that
array has the key three because then
area has green one zero and one so you
get the idea we have multiple assertions
we can pass in a
test let me show you one more thing now
when we go here in our employee test
class we have the function test and
get the employee name then age the name
and age
so if you notice we are calling the
employee model each time
and here
it is also used here it is also used
so what we can do we can
take this model out and call it whenever
we
want it because there is a method that
is
available in pgp unit that runs
before every test
so let me show you how it works so
basically we are going to define a
method and it's going to be
public
function
and it is called setup
so this setup method is going to run
each time
and it returns nothing so it's void
and it return um basically going to run
so these test runs this function is
going to call before this test runs then
before this test so if you have 10 tests
it's going to run each time
so we are instantiating our class every
time so what i'm going to do i'm going
to put
this here
and i want to create the employee
whenever
we
set up so what i do simply i'm going to
copy this i'm going to paste it here
all right nothing changed but the thing
is now if i take this employee outside
we can access it in each function so if
i say here
that protected
dollar
employee
and here it is protected
and here now i call it this
dot employee so you get the point at
this time
we are i forgot semicolon here
so
employee now this time we can use it in
every function so all we need to change
instead of dollar employee we need to
call this employee so let's copy this
i'm going to remove this from here
and i'm going to paste it here
all right so base employee and then set
name
so
now each time it is going to re-uh
run on each test so it is going to be
renewed every time so it is not going to
be
something that your employee did a
change here and
chain data
passed to the next function it's not
going to do that because it's
we are setting it each time
so it is going to be renewed
instance of employee class so here we
got this employee
i'm going to pause the same way in other
functions
all right so we have this employee
on every function now and we are
instantiating our class here
so let's do this let's run the test and
if everything is right we should get all
the tests passed so here we have five
tests six assertions everything is
working just as expected now let's see
the example of a collection how you work
with collection if you're not aware of
collection if you
heard laravel and laravel we have
collections and uh
we can store data in
collection using the larval
now in our case we are not using larval
we are using php so i'm going to create
a collection class and then we are going
to put the data in there and that
that data
array going to become an object of
collection so let's see how it works
first of all we want to create another
class in our unit test because we want
to have another test and that's going to
deal with only the collections so here i
see here
another class
collection
test dot php
all right so this is the class it's
going to be now i'm going to copy the
code from
sample
test
i'm going to paste here
change the name to collection
and make sure name spellings are cracked
collection
test
and we are going to remove
the methods here and we're going to add
our own
now the same way i'm going to create
another model so here
uh
in directory in this
app folder for us
so let's do that i say here
support
and then i'm going to call
a file inside it which is going to be
collection
just two ranges this way
and here i'm going to create a class so
it's going to be collection
php
php namespace
and it's going to be app
and then support
all right and it's class
all right i changed the name of this
folder to capitalize so yes should be
bigger
so support collection so let's see how
we can get uh
and work with more assertion methods
using collections here
the collection class and here i'm going
to write the first
uh test for it and collection test so
that's right here so it's going to be
public
function
and i say
uh
let's
do this and this one we're not going to
use the test we're going to use the
comment so that we can see how we can
use that as well so here we say check
and we want to check
if data
present
and this array is going to check now we
want it to run as tests so we want to
add the comment add test
and here what we want to do i want to
call the collection class so it's going
to be dollar collect
you can see
here what i want to do i want to say
dollar collect
and i want to call the function which is
going to
be
get data
and paste here a certain empty so it's
just going to check for this particular
uh
if we have this method and if it is
returning empty or not so it is going to
work on array so let's go there in
collection so we need to
create this method get data otherwise
we're going to get errors apparently if
i run
this we get
the error so we don't have get data so
let's say here
so you're probably so here what we want
to do we want to
get download this
and here we can
get the items
and we want to
have the items it's other item now
currently we
don't have the
data setting anywhere so what we can do
here is either we can have by default or
we can create a
set data the way we did before so
previously we used get data and send
data so in this case um i want to try
something different so what i do i'm
just going to pause here
an array and that is going to be the
data so i'm going to say here 14
23 56 so these are the values in this
collection we are passing and here in
collection
as we have and currently no data we can
call the
uh constructor so if you
know the concept here when you
instantiate a class a constructor runs
automatically so we can create a
constructor here and that constructor is
going to assign the values for us so
here first i'm going to assign this the
dollar items so here i say
protected
dollar items
and that's going to be the variable
now here we want to run a constructor so
it's going to be public
function underscore underscore
constru
rocked
all right so we have a constructor here
and this constructor is getting an item
and this is the item that we want to set
like this so we're gonna
just gonna paste here instead there
so
if you see here we go to
collect here we are passing an array
this array will be passed here and it's
going to be assigned to item
items here and it will be accessible so
in get data function what we can do we
can have a local variable so i say
dollar data it is going to be an empty
array then i can say here
for each
and we can access to these items now so
simply copy this from here
paste here and then i say it has
dollar item
and then we can put this inside data
it's just like that dollar
item
all right and here we return
builder data
so you can see that the get data will
return
value an array of
array of these numbers so it should
uh it is not empty in that case so it
should fail this test so let's see let's
go here
i clear the screen run the test
so it says failure asserting that the
array is empty because array is not
empty we have three values we are
passing so what i do i remove these
three values this time
and
save it
go back
now you can see that has passed
because we are checking
here if empty so we are passing empty
and that makes this test to pass
so this is that is uh the
working with the empty and assert empty
now here we
uh get the data like for example here we
pause the array
and if you see dollar collect here
so let's me reward it back
and if i copy this and bar dump here
you can see
this time when i run we get an object
where we have
are inside it
with the three values so
that's the reason when we
were really here we were looping through
that object and uh
making it an array because here a
certain empty checks for for an array if
that array has value or not so here we
are passing an array with the values
so okay if you have noticed uh in the
sample test.php the
class name is not correct it can create
a header so we need to correct it it's
just simple
test
also in employee test if i go to the
class name it should match always so it
should
have a capital t
so make sure
you have it other than days i think
rest all is good but
yeah it should match
if you like the content of this channel
and you want me to create more videos
like this
please support me on patreon you can
also subscribe this channel like this
video and share with others thank you
for watching
[Music]
