Installing PHPUnit with Composer
Transcript
alright so now I think it's a good time to add some unit test we have already previously added the PHP unit xmldist if it's not the case if you haven't done it because it was optional in one of the video just create one and we will add some configuration for PHP unit but before to proceed to add some contents inside of the PHP unit dot x m l dot list let's install PSP Unit for composer so for doing so let's go to packages and there let's do PSP Unit we will install the latest version
which is a version ten point two point three for doing so just do composer require PSP unit for slash PSP unit and there we can go in our terminal in my case I can go to item on my I usually use item and there we can just paste composer require PSP unit for such PSP unit what we paste it and here we just have to wait and then dev yes actually we want to install it as a dev dependency so this is actually a new feature and it's really handy because for
for testing unit test we run them only locally we don't need phpunit to be on production on the production server so we say yes otherwise what I could have done I could have used the dev flag while I was installing phpunit there but here we just do yes oh so let's do it again yes I think I had a special character there before so this is why it didn't work and now you can see it's been installed it has been installed if I go to the in composer of the Jason if I go here I will see require dash dev
and I will see that PHP unit has been successfully installed now I can go to PHP unit dot xm dot dist the config file I was speaking to you earlier and I will update that file with some XM rules so if we go to Safari and here if I go to spy unit.net and let's see the documentation and there documentation I click here the latest version the one we have 10.2 and here I will go PSP unit
X m L that one the X m L configuration file and here I see all of the rules the possible ones and as you can see the documentation is giving me some examples of what to use so I suggest you to have a look to see all of the possibilities you can do and you can mention in the phpunit dot XML in our case
I will just use the basic configuration I usually use for my other projects or for work it's the one that most of the time always works you can also have a look at organizing tests so this section organizing test and here you will have the full configuration XM contents we can use again we always have to update this one and this one so that's the U I if you are just interested too so this one and we can now and that one that was for the other one and now what I will do
I will go back to PSP Storm or for you it might be Visual Studio Code or any other code editor and here I will have the header like what I had here you can see and then I will have the PSP unit XM so basically is this one however bootstrap for us we don't have any bootstrap file in the test in our test folder and actually the test folder we will have to add test inside at the moment we have nothing so what we use instead we use the vendor autoload to use
we need the vendor autoload to be able to have the autoload composer to to have the classes and the other files been included by automatically by composer autoload the same way as we currently do for our project in the SRC folder we include the autoload if you remember if we go to index dot PHP in the root directory you can see we have in the autoload and basically what the boostrap property does it will always include the file mentioned there so it could be either test for slash bootstrap
if we have some rules or some constants to be initiated but in our case it's only the autoload we need usually the test bootstrap file will include the vendor autoload but in our case we just need to directly include the vendor autoload like this and then I just use colors property to tree to have the test looking a little bit nicer the results of the test so when we will run phpunit it will just look a little bit easier to understand what error felt or not whatever felt or not
and um it to be like red or green and so on and here that's just uh what we need to mention and that's a version of our PSP unit you can see do they mention the same uh not exactly here but that one is the one I got when PSP unit auto generated my config file last time because my config file was a outdated one so it suggested me to convert to migrate to be compatible with PSP unit 10 and this is probably why PSP unit added this so we can leave it
or you can just use exactly how it's mentioned there so let's continue we have Cash Directory and this PSP unit dot cash and there let's continue we have test suite and then the test suite is um what we mention unit test and the photo will be test where we will have our test is actually that one if I zoom I can can I zoom a little bit yeah so that one and then if we continue we have source that's our project so it's basically SRC and what we should include is any files with the PHP dot PHP extension in s r C folder in s r C directory and then we just close the tag and finally we we have the closing PHP unit tag and I can again reuse what they gave me here and with formatting that one so I can I could either use this one or this one
the one given uh from there and the the format is a little bit nicer I will just uh oops yeah no it's uh I think it's 8 spaces or 6 but that's that's okay I usually attend to have 4 spaces but for some reason the format is like 6 spaces we can always automatically reformat the XML the XML syntax later on or what I could do even if I go to settings in phpstorm if I type XML if I check for formatting so it should be here formatting XML and I can apply some rules and here it should be hmm space before it's not exactly this one
it's not what we are looking for code style is probably so that one we just use the different one color skimmer go yeah code style is aid but here continuous negation I would do for as well because before it looks different not really let's see I'm not sure this one will do anything different but if I just select again I do code and if I click on we format file
select text oh yeah it looks a bit better now so you see we could reformat it a little bit our code and that's pretty much it so now if we go to the test folder that one in s R C test folder if it's not there for you if you haven't created already um we did in a in a previous video quite a while ago but feel free to create create that folder again because it wasn't I told you that it was optional but at this stage um we you you will need to have the test further in there
test with a s and this is what we mentioned here again it could be singular too but in our case if you follow exactly the instructions I gave to you it has to be plural like this and this is also what we what we have mentioned in our git if we look git ignore yeah um yeah sorry we don't have that file yet but usually when you are about to to ship your product and you don't want sometimes you want to exclude some files or directory you don't want them to be there in your project when you you know when you ship it usually so for a package library and when we install it from composer you don't want some photos to be there because it's just unnecessary uh such as the test photos so what you would have is a get tributes
and actually if we look on the internet gets a tributes excludes this we can go to meet with love that website I think it explains quite well the reason you see like you have get attributes and so you have to mention the files you don't want to be there with export ignore you mention exactly this way and so it's like the file name and then export ignore so in our case if we do if we create one and I will mention get ignore export and then I can duplicate that one and I will add a few and here I will have get ignore get attributes I will have tests I will have for instance um could be also the
a lot of them and we would also have PHP units that one we would also create that one PHP unit dot XML dot this and so on and this is basically what we don't want to be in the archive when you go to a Github a Github for instance I will go to one of mine a Github package repository you see we have this one and when we go to download we have here to download to the archive it should be excluded from there so here you see where what I mentioned is pretty much the same test there I have a Github directory so I mentioned the dot Github to be also excluded and then I mentioned the phpunit exactly like what we just did and so on and I can just copy the comment you see so this is the purpose of it
it's a good practice actually to do this so that's why I just wanted to quickly show it to you now it's up to you you don't have to do it but it's always better in the long term to keep a good practice and it will always be a winning decision in the long term so now let's continue and let's create our first test it has to be a photo first entity and that's for our entities the name here to be user test with PSP unit is really important to finish your class name so the full name with test
so not just user but user test and now let's write our first unit test and for doing so the first thing I will do is to mention the user test class as final indeed we don't want that user test to be extended by other classes usually when it supports testing when you have a test class with PHP unit it's a good practice to Mark your class as final with the final keyword inheritance is not something that common when you have a single testing class but now some could be inherited by others
in our case it's final here I will also mention declare strict types to one because we want to to mention to PHP that the types we mention so the return types the the types in the in the argument of each function has to be strict that means if the type is not the proper one PHP will give a fatal error instead of trying to cast that value to the one to the type mentioned so strict is just like how it's um how it says is um it has to be strict every type we mention when we declare types
now let's add our first testing method our first testing function to be public function test and if we look to user entity let's test this one so it will be the Get Sequential ID and the name will be test Sequential ID like this then which on type will be void and here in the body this is where we will have the exciting part so we have to assert the value but first we have to update the namespace here you see the namespace is entity and it's a convention usually to have first
the namespace of the main project so for us it would be pH seven API simple menu p A7 API simple menu exactly like this and this is what we had in our composer of Jason for the autoload you see that one uh usually that's a convention and then we would have test and finally here although normally in the with the namespace you don't have namespace in plural but for test usually to be with the s and finally to be entity because we are in the entity folder
and there we have to import our entity so it would be P H 7 API simple menu entity user and there we will do I will what I will do I will have a protected function it will be set up it's like a constructor for PSP unit so it will be public protected sorry protected function set up and that one is just it behaves like a constructor
if we we have to extend actually this one to test case from PSP unit so it's from there you can if you if your code editor doesn't import automatically just import PHP units backslash framework backslash test case and if we go inside of test case you will see it does have a setup you see and we reuse this and usually it's a good practice to call the parent setup so we just do parent like this setup and in inside of the setup method what we'll have we will have this
and here it will be user entity equals new user and here we create an alias actually to be as user entity like this an alias like that here I will create a private property it should be private the type will be user entity user entity like this and so we do I will create a new instance it should be user entity perfect and so here inside I do this user entity set sequential sequential ID and there I will just have an ID so what I will do I will just do
I will just do like um probably it should be 5 like here I can do sequential ID equals 5 or 55 and there it should be sequential ID and now we can do this assert to be assert same because assert same is actually a strict a strict comparison and it means it uses the 3 equals in PHP so you have the comparison where it's not strict so it's not a type comparison and the strict one will compare precisely the type and it's always better to use the strict comparison so we do here we do
Sequential ID so it'll be 55 so if you go in the signature you will see what we expect and what's actual and what's actual it'll be this user entity get Sequential ID and that's our first test so let's try that one for doing so I go to my terminal and there I will do PSP unit if you have it installed in my case I have it installed globally so if you do v you'd see I have the version 5 the version ten and if you don't you can do you can use the vendor so if you go to if it's not global if the installation you have is not global you can do vendor
bin for binary PSP units and then you can do actually that's it you you don't have to do anything else you can even do v you will see the version always v lowercase but otherwise you just have to run that that way and here we can see it didn't run anything actually which I'm not sure did we use our test use our test entity name space doesn't match entity hmm directory to test
the space doesn't match the BSA oh maybe that's why actually because it's in test let's see where it was entity user test oh that's for pH seven no that's fine actually um we we don't need the full folders there so we can keep as it is like this user entity let's oh let's just see why the test oh yes the test is actually running you see it's that one a certain one because if I change and I do 44 here it will fail I didn't even see it it's really quick and I think with the PSP unit 10 it's even faster so now it will fail you see
uh now and now we can see the file name but when it was valid when the test was passing we couldn't see the file name of user test so that's why I thought it it wasn't um it wasn't executing our test but it was so our test is passing and if you have PSP unit installed globally you can just do PSP unit if you have it if you have the PSP unit program globally in the global scope where you can call PHP unit um anywhere in your terminal but otherwise you used to the past vendor bin PHP unit and now we can create another one another test public function test and here what we will do it will be if I go back to entity user let me have a look we can do
we can go for the user you you ID so we do test user you you ID and now void again it returns void so nothing and here to be these user entity set user you ID and here we'll create a new variable it will be user you or maybe just just you ID and here we have the UIUID library from remzi like this you you ID before and so we will use that value there like this so we reset the uuiid a valid uuiid oh and I need to do to string here like what we did before in our user service we also had to do to string this is how the library works so it's it just needs like uuiid method since it would return the uuiid interface
we need to use the to string to change from the object of the class to a string if you are used to use the NPM UUI d javascript library for instance it works differently but remzy works that way and now we can do we can do the assertion so we do this and we will assert so it will be same again same assert and here it will be oh there we'll um yeah we will do U U I d and there we will do this gets
to be this this user entity get user your ID exactly like that and let's try this so let's go back to our terminal and you see our new test now we have two assertions and both of them works and now we can use unserialize for our test it will be even easier so if I do public function test on serialize because we can use all of the getters thanks to that one so on serialize we are testing that one and here you will see it will be a good one because I will be able to do this
user entity on serialize and here what I will do I will pass an array with it so first I will do hmm I will do like user data equals or maybe maybe expect expect user data so I will pass that one just there that's alright expect user data and here I will have everything we need so ID and we can test again the ID for instance it will be like 55 or maybe it can be a different number just five then we have user U U I d again we just do user U U I d and here we have
U U I d similar Ramsey user U I d so we have it already imported Ramsey u I d u I d u I d 4 and then I will do to string perfect then what else I will have first name so first name and then it will be for instance Pierre then I will have last name it will be Syria for instance then I will have email email then to be Pierre at PS seven dot me just an example we can just do like me at PS seven dot me then I will have phone
phone and then I will just add a dummy phone number and finally password password I will have chicken for instance oops yeah chicken and then created date well created date I will just take what I have in our database and because it's oh wait created it yeah it's that one
at least that way I know the date is is correct because it's a little bit too long you see for the format and that created date is what we add from where was it when we are in user dial and when we create the user set creation date you see is from there from that format from the format we store in the constant and so this is what we store in the database in the user's table and I just copy paste just a row from a user it was just easier I could also have done select
created date just like that from users where user your ID and to specific user your ID and you see I have it and it's the same so yeah that's pretty much it and now I unserialize the data and then we can do the assertions and I will have this assert same again assert same and it will be it would be this one first so it would be
I can do this way ID so we take that value ID and there we do this user entity get user alright it will be get sequential sequential ID now the second one it will be this assert same expect user data it should be user U I d
like that and then this user entity get user u I d perfect then the third one it will be assert same again assert same is from our test case and I mean from the PSP unit this case we extend and then here it's first name so I do expect user data so it takes Pierre from first name
perfect and here it will be this user entity get first name beautiful then I will have a search same expect user data it will be last name so it will take Surya and then it will be here this user entity get last name beautiful and here to be user assert same expect user data email and then we have this user entity get email then this assert same expect user data phone
this user entity and it should be get phone and finally for the password assert same expect user data here to be password and then this user entity gets password and finally the last one assert same expect user data it to be created date and then this user entity get creation date so let's test this
let's see how it will behave and I will do PSP unit and again you see it looks to be working I can even use the filter flag and I can do user it will be a from entity user user test and then to be test on serialize and you see that's the one and if I write something else for the for the method it won't work so we can mention the specific test if you just want to run one test with a filter class the name of the of the class then colon colon and then the name of the method like this so that works obviously if I change this like here first name if I change um Pierre to like Henry the test will fail you see because expected Henry
because a chance to Henry instead of the array index Pierre and so it received Pierre because indeed it was Pierre there so the gate first name was Pierre and here it was Henry so it failed and now we can even go further by doing oh yeah we didn't implement them yet and we don't have to actually but if you look in the database recently we also we have some new columns so we have session token
session token and last session time but at the moment we don't retrieve them we don't need them right now so it's it's pointless to test them because they haven't been implemented yet in our user entity so pretty much that's already a good test we have done for user test we can now start committing what we have done so you see it's already a nice a nice a nice little test in our tests folder entity user test let's commit this
let's make sure it's still working perfect all of our tests are running and now if I do get add P flag you see we have PSP unit that has been installed we have the PSP unit configuration that was from the the last just the video before this one and now we have the new files it's get attributes we have all that one let's add that one in our git ignore it's important because we don't want to commit that one so if we go to git ignore we will just add that one in dependency I will do like here it should be test and I will add that one
and I can also add phpunit. XML in case we have one with dot XML because for us is phpunit dot XML dot Dist which is a distribution file but if you have a phpunit dot XML Phpunit will take into account first the phpunit dot XML and in case it doesn't exist it will fall back to phpunit dot XML dot d so the dot this is like the distribution one and if you want to customize the configuration of your PSP unit settings you can create a PSP unit dot XML
but generally you wouldn't commit the PSP unit dot XML so it's nice to also mention that one here and we can then commit to this oops I made a typo yeah so you see we have phpunit.cash and if you look to that file automatically phpunit will generate that one where is it PHP unit dot cash is that one oh yeah it's a folder and you see it has this inside it's like serialized values anyways we can ignore this and we can also add the test folder the test folder where we have our unit test
and now we can do commit message add PHP unit test perfect and now we can just push

Ask anything
We've read this whole study set, so questions about it get the best answers.