Pest - An Elegant PHP Testing Framework

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part of my premium course so if you liked this video be sure to check out useful laravel packages calm okay let's take a look at passed by Nuno Maduro past is an elegant PHP testing framework with a focus on simplicity it removes all distractions from your PHP unit tests allowing you to focus on what's important its framework agnostic and it works with any PHP project but in this video we'll take a look at using it within there well so in a nutshell if we go into the docs you can use past to write your PHP unit tests like this sorry wrong page like this so you can see we can use this closure syntax and we can use either the test method or the it method and these are interchangeable you just use whatever makes sense grammatically so I have a sample app here typical blog implementation where we see our blog posts here we can create a post and you can only create a post if you're logged in and you can view a single post I also have tests backing up this functionality called post tests and see these test runs so if you're on PHP unit there we go or we can run PHP artisan tests so let's go ahead and take a look at how to convert these tests into passed tests so let's start with the installation so it says we need PHP unit version 9 and condition version 5 so we have to upgrade that in our composer JSON because we are not at those versions as of yet probably in the next version they're well they should be those versions of PHP in it should be 9.0 and collision should be 5.0 okay save that let's run composer update okay it's done let's continue let's go ahead and require pest okay and it's as if you're using laravel we have to use this command to install it okay so you see added these two files which we'll take a look at later so now we have this new pest CLI command so if we run then they're being pests then it will run our tests and as you can see it still runs our PHP unit tests just fine so it makes migrating to pests pretty painless so you probably want to Elias this so I have an alias for pests and we can just run pests like this cool so let's go ahead and convert one test over and we'll see how that looks so I believe there is also a new pest PHP arson command let's find out if it does so pest yeah so pest test and then we will create a new test PHP artisan pest test let's call it posts pest test okay and as you see it created a new feature test and you can create unit tests as well but we'll just stick to future tests in this video and there it is and you can see an example test here and let's go ahead and create this fake endpoint so we can get this test to pass so it's called post posts pests so if you go to a ruts file let's just make a dummy route here called posts oops posts test and we can just return anything here and this should pass now so if I run pests this is gonna run everything including our new pest test and it fails because I named it wrong so hosts pests and it's run that again and now you can see it passing right here cool so let's go ahead and start converting our tests over so the first test in our normal test is this one right here so the main page loads correctly so let me just grab this and we can replace this one I guess and just paste it in and I'm gonna comment this line out for now this has to do with the setup method which we'll take a look at in a second and let's just rename our tests what was it called the main page loads correctly so now let's make it grammatically correct and say it loads the main page correctly so this is gonna fail because our main page requires the database so we need the refresh database straight so let's run a test and see you fail there we go and let's go ahead and add that trait so we would include the trait in a similar way so we can just grab this and put it right here but to make use of the actual trait instead of using use refresh database we have to use a method here called uses and then we can do refresh database class and now they should work so let's give this another run and see if it passes cool so if you look at the docs you can actually abstract this away to the past file here which was created earlier so let's go to the docs and it's going to believe it's in the underlying test case and as you see here you can use it in this file and it will work in all your tests so let's do this in the past file let's get rid of this and it's make sure to include the use statements up here and now we can get rid of it in our past up here so it makes it cleaner see if this still works and it does cool so now let's take a look at the setup method so back to our normal tests you'll see I have a setup method here it's not really doing anything because I didn't make use of it in my tests but I just want to show you how to use it in pest so I am assigning this instance variable called hay to the value of artisan and I'm asserting it down here just to show that this setup method runs before every test so let's do the same for pest so if you look at the docks and setup and teardown we can make use of this before each method and this is pretty much the equivalent of setup in PHP unit so again the closure syntax back to our past tests let's put it up here and it's a yes hey equals artisan and now we can uncomment this and this should still pass because this method will run before each of our tests and you can do the same for what's it called after each as well if you want a teardown yeah but we'll just stick to before each for now and it see if this works and it does cool so let's convert another test over and let's take a look at helper functions so I'm going to convert this test down here and this one is responsible for checking if logged in users can see the create post button so you can only see this button if you're logged in and we are so you can see it here so let's go ahead and recreate this test in pests so I'm gonna grab this and paste it down here and close it out and let's name it the same let's remove the underscores okay and this is an instance when the test method makes more sense so it reads better so test loading users can see create a post button and I forgot to remove this one underscore and let's make it a bit more grammatically correct and put the word in there so it reads much nicer okay so let's just grab the test we can basically just copy and paste it and it's paste that in cool and it's run our test and this should pass class user not found so yeah we stopped I import this cool let's try again and it does work so if you look at the docs under the helpers section so custom helpers so it says passed auto loads the tests helpers method which was created when we installed best right here so we can add any global helpers in here and our tests can call them you can also just define it in the test locally so if you wanted a helper function in here you can just do function my helper or whatever and then you can just call it from your tests or like I said you can put it in that helpers method or helpers file and if you look at the helpers there is an example one for acting as and if you look at this test method you look at the docs it just says the test function allows you to get access to the current test case so it's just referring to the actual acting as method so what that does is it allows us to use the acting s method in our tests without calling this and again it makes our code a bit cleaner and more minimal so if I remove that this should still work let's run past again it still works so if you want we can get rid of this call to get as well if we make a helper method for that so again same thing that's just define a method here for get so function get and it's a string we need the URL and it's just to say return test get URL and now they should still work but now we've cleaned up our code again cool so if you look at the D Docs for higher order tests we can also write our tests chaining on methods like this so let's see how we would do that so back to our pass the test let's do it for this one here since it's very severely simple so we would grab this but instead of a closure we would just chain on these methods and let me just say chain here so we have a different name and from here we would just chain on methods so get but if you look at the docs it says you can't use their Val helpers like route so we have to manually do that I will just do get the home page and we can start asserting right away I'll start successful ok and we can assert C posts ok let's see if this works should be green and it is oh yeah something else I forgot to show you is any PHP unit assertions can be written like this with the disk keyword or again we don't need that for PHP unit assertions so similar to how I just showed you we can get rid of that and that should still work oops cool so I'm going to go ahead and just recreate the rest of these tests as it's pretty much just copying and pasting and changing the name of them so I've gone ahead and recreated the rest of our tests and pests and I've also added a global helper for assert database halves so we can get rid of the this keyword as well so same process here and if I run my tests everything is looking good and passing one more thing I want to show you that's essential to my workflow when I'm testing is the ability to run single tests wherever my cursor is so for that in vs code I make use of a package by caleb porzel called better PHP unit so if you look at my extensions here you'll see I have it installed somewhere right here and there's one for sublime two called sublime PHP unit so if we go into our normal tests and if we go into one of the tests and run our key binding it will just run that single test so I usually do this when I'm writing tests as I don't want to run my entire test suite because that can take too long so I was digging into the issues to see if anyone converted this over to pests and someone actually made a fork of Caleb's package called better pest so we'll just search for that in your extensions and you can see it here and I have it installed already and it's created by Miguel and it's pretty much the same thing as better PHP unit but it works for pests so if we go here oops sorry back to our past tests we can do the same thing so put a cursor here and I'll have a key binding just run it from the command palette but you'll see that it does work and hopefully this is eventually merged with better PHP unit so I can just use the key bindings that I already have so if you take a look at our final test here you'll see that we got rid of all the setup and our test feels lighter and easier to read so if you want an even better experience when writing your tests be sure to check out pests
Info
Channel: Andre Madarang
Views: 12,153
Rating: undefined out of 5
Keywords: pest php, php pest, pest laravel, laravel pest, laravel testing, testing laravel, pest testing, pest nuno maduro, elegant php test, andre madarang, drehimself
Id: vp0jP5rMvR4
Channel Id: undefined
Length: 14min 37sec (877 seconds)
Published: Fri May 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.