phpunit is a simple tool to test your PHP code but how you can start from
scratch with PHP unit so in the browser let's type PHP unit search for the
Google and you will get the website for PHP unit that's great but we are
interested in this github repository of Sebastian Bergman okay so let's open
this github repository and you can take a look on this website which is very
descriptive not so cool but good to start let me show you how you can
actually start so here we have this PHP unit github repository and here if you
go you can see we have the installation part you can install by W gate or PHP
unit like these kind of things but alternatively you may use composer to
download and for that we need to go to getting started yeah here to download
via composer that will be easy when you start with composer and if you don't
know about the composer it's just a package tool or dependency manager which
allow you to download packages for PHP related things I don't need to copy this
we can just write it but before that what we can do we will create a folder
so mkdir and I will call it PHP unit test okay so this is good and let's go
to PHP unit test and here we will say composer then require PHP unit slash PHP
unit and yes that has to be a def dependency because when you are
developing when you are on localhost then only you need to test so hit enter
what it will do it will download PHP unit package on a new folder which will
be created by this command called vendor folder so let's wait for this to
complete so now you can see HP unit is installed so let's go to this PHP unit
test folder we have created and yes we have three files so why not open this
with VX code and now if we have three files so let's look at this composite
JSON file it's simple and it just saying that okay require a dev dependency which
is PHP unit 7.1 and in this vendor folder yeah we have lots of things but
the interesting folder in which we are interested is bin folder and here is the
executable PHP unit so how we run this so let's open the integrated terminal of
vs code and here I will type let's try to type PHP unit it's a special unit is
not found if you want to run PHP unit directly then you need to install that
PHP unit globally but it's not required because in the level if you are using
level already give you PHP unit so it's not a good idea to have PHP unit
globally but how we can run PHP unit so you need to actually go to vendor slash
bin slash PHP unit and that should be rendered ok so if I hit enter yes now
you can see we are getting lots and lots of options but we want to run something
it's not running it's just giving PHP unit related commands or its related
information you can see lots of information we have but we want to
create something like I have some function I want to check it for that we
need to create a new file for our PHP unit which is called PHP unit dot X M
and because it's an XML file we need to start with XML and let's close this XML
also so this is good and slash close then version which version of XML we are
going to use which is one point not or after that we need to say encoding and
encoding is utf-8 simple things then we will say PHP unit and this is
going to start the XML part so here let's close this PHP unit okay and in
this tag we need to define from where this PHP unit get starts so for that we
need to say boot strap that means the starting point not the CSS bootstrap
styling at the starting point so bootstrap is actually inside vendor we
have auto load dot PHP file so this is if you go to vendor folder you will get
this auto load file which is just for initializing purpose yeah we have this
and remember one thing that this has to be like this that's why it's not
highlighting okay so XML is good and inside this PHP unit tag we need to
define something there are two tags we need to define which first is test suits
and remember this has to be with F and test suits inside this because this is
the plural we need only one singular test suit not with F not test suits so
make sure we have this test suit and inside this we need to define the
directory where we are going to put our tests so directory and let's make it
like this directory and directory name is tests we will create this we don't
have yet and let's name this one as let's say
unit test okay simple so let's create a new folder called tests and let's create
another folder inside tests folder not here inside tests folder called unit
tests and this is this part is just going to say to PHP unit that okay let's
start from tests folder whatever you get if inside this tests folder have some
kind of tests so go look at that and if you find something just make that test
run so what if I now go to this terminal integrated terminal here and clear it
and let's once more run vendor bin PHP unit let's see the result hmm it says no
tests executed and you can see the difference between last one and this one
but let's define colors and we are just going to say okay show me some colors
when you are using this PHP unit and if I now go you can see this time there's
no test if with colors one thing more is verbose verbose is true and what this
mean if I now say you can see we have some other related informations with
this test and this test giving some more informations related to test and one
last thing which is stock on failure as the name suggests whenever some tests
filled it stopped executing other tests so that you will
have a good idea that okay our tests is not completed it breaks we need to fix
it so our PHP unit dot XML file is completely done so this is good but you
know every time I write to vendor slash bin slash PHP unit
I don't like this actually so I need some kind of shortcut and we don't have
any kind of test it says no test executed so we will create test in this
units unit folder and we will see what the package or extension need for vias
code to make these tests flawless without writing these things and these
things in the next episode so don't forget to subscribe and hit the bell I
can to get the notification instantly whenever I upload any video and follow
with films on Facebook Twitter Instagram LinkedIn and you can find the next
videos link in the description and also the whole playlist so we will meet in
the next episode till then good bye