Pest 2 - Summer of Spice

Nuno Maduro

New features of Pest 2

Transcript

00:00:00 hello larican I'm super excited to be here today for those who don't know me my name is Nunu Maduro and as haran said I am the creator of multiple open source packages on the PHP ecosystem I'm also laravel core team member in the creator of one of the most popular testing Frameworks in the world past PHP in today it's all about past we are going to see what's coming new for past this summer now before we jump into that um for those who know what past is is an elegant PHP testing framework with the

00:00:39 focus on Simplicity and it is damn simple and once we actually release it past V1 in Spring 2021 we already managed to convince more than 50 percent of the laravel developers that passed should be their default testing framework so let's see what some of this past match week is all about I'm going to move here into my Sublime Text the best editor in the world by the way and my terminal now what you can see with this rewind text is pretty much a fresh lot of yellow application which as you may know today

00:01:16 ships with PHP unit by default now when you look at the PHP in the test you see that we have a namespace a bunch of uses a class that extends something let's write the comment and then a public function to actually see your first test now also with PHP unit which is a standard in the industry for a quite a while now we have this output on the right which is PHP need 10 by Sebastian around time the configuration a bunch of stuff that we don't really care now I want to show you something different

00:01:48 which is past PHP so the only thing I'm going to do here is that I'm going to clear my terminal and I'm going to type composer require past PHP slash past now I wasn't unsure about the internet connection so I already have run this command at home but once you run this command to install pass in your project you can immediately type vendor bin passed and you immediately see this output so there's two things you can understand from past already first of all the output is the output is gorgeous that's the first thing but also the fact

00:02:21 that past is a progressive testing framework meaning that it just works on top here for existing PHP unit charts you don't need to change a thing now besides the output which is gorgeous we also have a different testing API that you might want to use so here on the left on my Sublime Text I have this thing called it a class based API which comes with PHP unit and we have something different with past so I'm going to migrate this test to pass PHP just in a couple of seconds so what I'm going to do here is copy the content of

00:02:54 my test and I'm going to remove all of this boilerplates so no public functions trades classes users extends nothing and I'm going to Simply type it has a welcome page within this uh closure which I'm going to just type right here I'm going to paste the content of my test just like that and now if I run past again on my terminal you see that we have exactly the same output so with 50 of the content that we saw just before we have exactly the same output and if you think that you are losing any kind of features

00:03:29 by using this new testing API that's not true so everything you love on PHP unit things such as for example skipping this particular test everything is available on the test on the past testing API as well now besides the the output on the terminal and this new testing API we also offer you something really cool called expectation API so let me show that to you as well I'm going to move here this time to this unit test call it the example test and as you can see we had a past test in the PE Community test in

00:04:03 the same test suite and everything just works but on this particular PHP unit test I'm using something from PHP unit call it the assertion API and the first thing I'm going to do here is just actually migrate this to because I actually need to scroll to see the test so I don't like that so I'm going to just type chest basic and migrate this PHP uni test to past PHP so just apply the same rules just copy paste the content inside run past and hopefully we get to see the same output as before which we do now this particular test is

00:04:36 using something I like to call the assertion API in the session API is something you probably know it's just about calling this assert and then tell PHP init what you are about to expect and this just really works on top of past however we have something that we believe is better than this so there is a couple of problems we have identified with this API the first one is that even though I'm performing assertions on the same value I have to provide the value three times here here and also on the

00:05:06 bottom other thing we didn't like is the fact that we cannot change these assertions which is kind of a bummer and also the fact that some of these assertions are just two verbose for example a search string contains string and then we have like these two parameters I don't know what comes first so just to complicate it let me show you something different so what I'm going to do here is type expect and I'm going to provide the value just once and then I'm going to just chain stuff I'm going to type well

00:05:35 I expect the value to be string just like that if I expect the value not to be an integer I'm going to just type that not to be int so every time you want to negate an expectation with past you just basically prefix that thing with the not keyword how nice is that in addition for stuff that basically looks like this a search string contains string we just make it a little bit more simple so expect value to contain Lara Khan so this effectively is exactly the same thing as we had before but using this

00:06:10 minimal API call it expectation API which ships with past PHP and for me this feels like laravel it's simple it's minimal and I can do the best feature in the world which is just DD the given value just like that so all this stuff just really works out of the box and it feels like laravel to me now do we just have saw a little bit of the G stuff past V1 so gorgeous terminal output but also a gorgeous testing API with a minimal expectation API but this leads us a little bit to past V2 which was out spring this year and we've

00:06:48 passed me two I'm proud to announce that we have just reached 7 million downloads and this Mark right here yeah [Applause] thank you and this Mark right here is from people who are opting out from PHP you need to choose past instead so we are very proud of this result right here in addition it was the very first release that we started to use the plugin system of past so past PHP have this plug-in system internally where you can have the community or even ourselves developing this optional features that

00:07:24 people may require through composer and we had very popular plugins some of you are probably already using them in laravel itself parallel testing profiling test coverage but also the very famous architectural plugin now today it's not about past V1 or past V2 today it's about everything coming new this summer so we are bringing past 2.9 which will be out this summer and the ACT prepared five new features for you that are going to blow your mind okay those five new features I believe will make you think that this should be

00:08:00 past V3 instead however because we just released it past V2 I cannot simply make another major version so we are going to get all this stuff simply by typing composer update anyway that being said let's just get started and the first feature we are bringing this summer is Snapshot testing now snapshot testing is this concept that is not very used in PHP but it's kind of the most valuable feature you can have to test long and static strings and what I mean by long and starting strings I mean things such as a full laravel

00:08:37 response and I'm going to show you an example I'm going to move here back to my Sublime Text and I'm going to open this file call it example test now we are going together write a particular test for this about us page so you have this about this page in your company which you probably displays this email call it careerslada.com you want to show people how they can join your company and if a boss tells you that you should write a test for it you probably would do something like this you would type

00:09:06 something like it has an about page you would visit that given URL and then probably you would type just below a cert C and then probably the given email which is this one okay so I'm going to just do that I'm Gonna Save it and run it with past and probably things are still working as expected which they are now the problem with this particular type of testing is that they are testing only one thing so if I were to go to this about as played view which is right here and I'm going to remove the CSS for example just to

00:09:43 just remove the CSS from this blade View now the issue is that our production is completely broken however our test Suite is still working as expected and that's because the email is indeed still there now this is the moment where your boss comes to you and just says well I just give you two weeks to write tests however your production is broken your test week is passing so something is wrong and I can tell you that snapshot testing can be really useful on this type of situation so let's just migrate

00:10:13 that test to snapshot testing I'm going to move back into my Sublime Text again and I'm going to re-add the CSS on this particular page now when I go to my example test instead of typing this which is only testing one thing I'm going to test the entire thing with snapshot testing I'm going to I'm going to type expect response to match snapshot just like that now the very first time I'm going to run this particular test my test will be incomplete so bear with me for a second what will happen is that past PHP behind

00:10:47 the scenes will take this snap this response in store in the physical file in the snapshot and that snapshot is under dot past snapshots example test so here is our physical infile string representation of the response the entire thing just right here okay now the second time I'm going to run this particular test past PHP will compare the new response with the previous with the previous snapshot meaning that if I were to go to the blade view again which is right here and I'm going to remove the CSS for example and I'm going

00:11:24 to run past again and now things to past we are able to detect that there was a change in our response which was the missing CSS just with one line you are testing the full response coming from laravel even better if I were to re-add the CSS and now I'm going to remove the SEO just like that SEO gone now visually when I remove the SEO our page looks okay right however Google search will just hate the fact that it's no SEO so I'm going to run pass now and it will see that pass will detect that the SEO

00:11:58 is missing on top so with one line you write in testing you are testing the entire thing how nice is that one question that you may have is that how about when I actually need to change the HTML or the content of the page well let's assume for a second that you want to change this this careers laravel.com to something like like even better join laravel.com okay so if I run past you will see that password starts to complain because indeed the email is different however something I can do is just instruct past that he needs to

00:12:33 update the snapshot so I'm going to just type dash dash update snapshots I run past the first test will be incomplete the snapshot was updated behind the scenes and then I can just run it again and now the new response will be comparable with the new updated snapshot so I still want to show you another example regarding snapshot testing so what I'm going to do here is that I'm going to change his extension of user test to something like usertest.php so it gets executed now this particular user test is very simple

00:13:07 the first thing is doing is before each test is creating a user using the laravelas Factory and then just below I'm testing the two array method of my model some of you may think why why are you even testing that thing well it's kind of interesting but every time you return something from an API that contains a user model behind the scenes laravel will use these two array methods meaning that everything being returned on the two array method will potentially be seen by an external user meaning the

00:13:41 following what we can do here is test actually that content we can type okay this user to array give me that then fetch me only the keys of the two array representation of my model and just ensure that those keys match the snapshot avoiding this way regressions obviously the first time I'm going to run this the task will be incomplete of course however behind the scenes we have we now have this snapshot with all the keys of my two array user meaning the following I'm going to go to my user test now to my user model excuse me

00:14:17 which is right here and I'm going to do something really funny I'm going to just comment the password from the hidden property now if you know what will happen now is that past PHP will just tell me about this regression because the password field was now added uh to the response it doesn't match the previous stored snapshot so this was snapshot testing coming for past PHP this summer let's move forward um and the next feature I'm about to talk to you is probably the most requested feature on past since day one and if

00:14:55 you're a past OG you Pro you know exactly what I'm talking about these scrap locks yeah this feature is finally coming for past and if you haven't worked with any kind of functional testing before you know how useful these scrap locks can be to organize your API so I'm going to show you an example for those who are not familiar with this concept of these square blocks I'm going to move here to my user test again which is right here and when this user test you saw that we are testing these two array method

00:15:26 however just below I have two more tests now these two particular tests are testing my threads relation of my user model so each user can have multiple threads and we are testing that relation in this particular two tests the first test is very simple it's just testing the count of those threads so I'm going to create two threads Associated that with a user and then expect that count of threads to be two and then just below I have this threads owner which is basically doing the same thing so again creating two threads and

00:15:59 just below expect that those two threads match the given user now something interesting about this particular two tests is the fact that they both share the same setup right so typically something you would do to kind of refactoring this particular two lines is add this on the top on the b4h on top which is right here however if you add it here will be potentially affecting other tests on this particular file because the touring method will now have two threads so your goal here is pretty much having a B for each specifically

00:16:36 for this in this test so something you can do is use describe blocks and to do it it's very simple you just type describe and you provide the name threads you provide the function and within this function just copy paste the content of this two tests just like that okay I'm gonna make it this little bit more pretty bam run past and now we get to see this new output a little bit here which tells you that threads threads owner so we see the word threads duplicated and that's because we have the threads word on the describe but

00:17:09 also on the test description so I can remove it from here and make it a little bit more pretty just like that okay and obviously everything is still working as expected and we can use describe blocks to organize our test Suite A little bit better but back to the topic of topic of the before each because we were trying to refactor this two particular threads so something we can do is apply a B for each just for this describe block simply by having the B for each within the describe block so just like that before each provide the

00:17:40 short closure within this short closure just copy paste this line which creates these two threads just refactor this a little bit just like that run past and we get to see exactly the same output as before but now we have refactored our test a little bit with this B for each now in addition of utilizing hooks just for your described block you can also use some methods in a more fancy way so here for example I have this particular test which I would like for example to Mark as a to-do and then this one just below

00:18:14 which I would like to as well to Mark as a to-do now if I do this test will mark this two particular tests as a to-do but it's kind of a bummer that I have to do it after each test on the same describe block so something you can do it just basically chain to do at the end of your describe and now every test within the describe block will be marked as to do so this is the scrap locks coming for past PHP this summer thank you [Applause] now the third thing I would like to talk about today is about architectural

00:18:55 clicking because we are actually bringing some improvements to this architectural plugin this was one of the most popular plugins we have built however we knew from the beginning that we were the beginning of something really huge now before showing you what's coming new to this architectural plugin let me just show to you some of the potential of this plugin so I'm going to get back into my Sublime Text and I'm going to just change the extension of this architecturaltest dot PHP now to demo to you a little bit what

00:19:26 are existing architectural plugin is about I'm going to just show you to you two particular tests that I really really like the first one is to detect DDS on my code so here I'm going to open this app HTTP controllers user controller and I get to see right here in the bottom that we have this particular DD dumping all the contents off my users now if I learn past past is passing meaning that it's not actually reaching this DD line so something really useful will be having our test Suite detecting things like this or

00:20:00 something you can do just go back to your user to your architectural test and type something like this test globals expect DD and dump oops sorry just close it up one sec okay okay we're back to the game something we can do I was saying that we can go to architectural testing and type something like this um test globals expect DD and dump not to be used okay it's great it looks great I know so if I type now vendor been passed with just three lines we are now ensuring that no DDS will be ever shipped to production

00:21:05 again so architectural testing with just three lines detecting all these things and I can go now to my user user controller just remove the sdd run past again and now we have our test Suite passing so three lines and you no longer have DDS in production ever in addition something we also had on this architectural plugin is allow you to define the separation of concerns of your application I don't know about you but when I'm developing a lot of value applications I always have this repository layer so I have this

00:21:39 repositories folder which encapsulates any interaction with my latavel model so typically if I would expect anything to interact at all with my label models needs to be on this particular layer so if you are a little bit like me and you prefer to enforce these rules something you can do is go back to architectural testing and type something like test models expect everything under app models namespace to only be used in app repositories okay so three lines and we are ensuring effectively that models can

00:22:19 only and only be used in our repositories of course it's failing because at the moment I'm using my repositories on the user controller layer which I shouldn't so I can just remove any reference to it and instead use our repositories layer just like that okay by injecting via ioc container the user repository and returning all the users over there run past and now it's passing as expected again so again three lines and you are ensuring that you don't see thank you [Applause] so this was pretty much the gist of

00:22:55 architectural plugin the existing one we are making some cool additions to this because I really like this kind of stuff so let me show you some of those additions the first one is regarding strict types now if you're not familiar with strict types it's something that I really like to use because looking at this app value objects last which is very simple we have this string typing now did you know that if I were to pass a Boolean yeah a Boolean right here I will actually have the number one string yeah that's just PHP doing his

00:23:29 thing and it's happening because you are not using Street types mean following if I were to go to the top and if I were to declare for example uh three types equals to one the only accepted type here will be a string not booleans floats integers nothing like that so something really cool would be enforce this stuff forever so something you can type is just go back to architectural test and type something like test streak types expect everything under the app namespace to use strict types again three lines euclido

00:24:09 terminal run past and things to pass now we are able to detect that everything under the rehab name space should use three types I'm gonna go there add the three types here to the address classroom past and now it's passing as expected so again three lines and you effectively ensure that three types are used forever also something really cool and I really like this one is validation regarding naming I don't know about you but for me every controller should have the suffix controller period so if I go to these

00:24:40 controllers folder which is right here I obviously have my abstract class which contains uh all the stuff that I need and I have my controllers extending this particular abstract class I have my user controller which is okay is using the suffix word controller however there is always this guy who'd never done laravel in his life and He commands with crazy names like this like thread response handle none the thing like what are you trying to do right so what I want to avoid is stuff like this to happen so something we can do just go

00:25:14 back to architectural testing and type something like this test controllers expect everything under the app HTTP controller's namespace to have supix controller bam just like that again three lines dude you just type it once and you are set up for life it's complaining about the thread response handle nonda non having the controller suffix I can simply go there rename this particular class just to thread controller I just remember that PHP star is sponsoring and I was pitching about Sublime Text oh my God

00:25:56 anyways it just renamed this to thread controller anyways you just rename this thread controller now past it Jesus passing again as expected yeah so three lines and you'll be sure that naming wise you can have your application just working in the way you want so in be consistent now we also have other stuff coming to the architectural clicking read-only validation visibility object type extends and interfaces and have a single example to demo to you all of these ones so again back to this value

00:26:28 objects example which we have this particular class and something I like to ensure especially about value objects is that they remain immutable over the runtime and something that PHP 8.2 bring to us which is very useful is the possibility of typing read-only after the class and we get to see this object a mutable forever so would be cool again to enforce this kind of stuff what we can do just go back to architectural testing and type something like test value objects for example like make this to the top here

00:27:01 test value objects expect everything under the app value objects namespace uh uh to be read only just like that three lines past will start to complain class address is not read only we just add it oops declare we just add it like this read-only and now hopefully pass will just pass as expected which it is three lines again and your classes are read only forever we also are adding validation to visibility so I don't know if you're a fan of final classes or not [Music] but something we could do here is just

00:27:41 basically type to be final and this will effectively validate that everything under that namespace is final but if you want a very cool feature you can simply type this thing which is not to be final okay let's go yeah so three lines well actually it's one line and you no longer have final classes forever I think it's just the best feature of today by the way and moving forward you can validate the type of stuff within a specific namespace you can type 2B classes for example so everything under that

00:28:16 namespace must be a class otherwise it would fail you can do 2b interfaces to be nums everything is just right there you can also type something really cool which is to extend nothing this one is really cool because you can basically tell pass that everything under that namespace should not extend any particular class so final uh not final a very simple simple PHP objects and well on the controllers we actually is a good example we could type something like this to extend the controller Base Class

00:28:46 so remember we have this Base Class that we want every control to extend well you can basically enforce that as well simply by typing to extend and then the controller class and obviously why not also to interfaces you can type something like to implement nothing if you wish those objects to not Implement any interface and our test Suite is working as expected and this is everything coming new for the architectural plugin this summer thank you [Applause] the next feature I'm about to talk to you this is the fourth one

00:29:20 I think we are the very first testing framework in the world to do it you're probably familiar with the term code coverage and as you may know code coverage is this quality metric that allows you to see which percentage of your code is being tested and we have that on past and we also Port that to laravel as well now today we are bringing something new to the table which I think you will really like and I'm talking about type coverage now type coverage you probably guess it is about to see which percentage of your

00:29:54 code is typed Let Me Show an example I'm going to move here into my Sublime Text and show you first not type coverage but regular code coverage and the only thing you have to do is just go here to your terminal type clear and type rendering past dash dash coverage and we already have this thing on past and it pretty much allows you to see all the typical all the code coverage in your terminal so no HTML files no HTML stuff no just right there on the terminal right it's just gorgeous now this is

00:30:27 code coverage and it's pretty cool I'm using it literally all the time now we are introducing something new today call it type coverage and it's very simple just clear a terminal instead of typing coverage you type tied coverage just like that you run with past and now you have the percentage of your code which uses types and as you can see we're actually having a very good result which is 96 of our code is fully typed meaning also that we recently shipped some changes on the skeleton that fools types of skeletons

00:31:01 so also helps there a little bit but we also can see that some of our code is not typed so let's just fix that so you can see the process I'm going to go here to the top and I get to see that I have this app models threads with 67 percent of type coverage the return type on the line 20 it's not typed so let's just go there I'm going to visit this thread model and I'm going to see that indeed we have our user not having a return type so I can just add it run past and now hopefully we are going to jump from 96 of type coverage

00:31:34 to 97 of type coverage okay in addition I see also that this process throughout video doesn't have Types on the perimeter type line 22 but also the return type on the line 31. so let's address that issue I'm going to go to this process thread video and indeed double check that this property is not typed I'm going to just do it and also the return type on the line 31 which is right here I can add the type here as well remove these comments because they are just useless and now run past and we get

00:32:08 to see that we just jumped from 96 to 100 of type safety now even better than this you probably want to ensure that your code remains typed forever so guess what besides this something you can do just go to the top and type minimum of 100 the first time we run it obviously the test Suite will still pass as expected because our code is fully typed however if I were to remove this string here right here which is a parameter type and run past past PHP will start to complain because the given code coverage

00:32:44 is below the expected which is 100 so effectively if you add this to a CI you have a project typed forever so this is type coverage coming for past PHP this summer as well thank you thank you all right so so far we have seen snapshot testing these scrap locks architectural improvements but also the new type coverage plugin for me all these features would have done a great task D3 release however we are going to get this stuff for free just by typing composer update this is my first slot icon so I really wanted to make this one

00:33:28 special yeah so there is one more thing um just by having passed itself and also with the help of laravel we are already convinced 50 of the laravel developers that passed should be their default test and Freight framework so I've put a lot of work into past and obviously passed I think it's a great product but with the help of laravel on the skeleton but also starter kits we have we already get this great adoption now with this approach I felt that we were forgetting a little bit this huge percentage of PHP unit users that have

00:34:07 this massive test Suites and they just wish to have something to quickly shift their projects to past PHP so today I'm proud to announce the drift plugin which as you can guess just like laravel shift but for past PHP so I'm gonna demo this plugin to you but it really works like magic let me show you I'm going to move back into my Sublime Text again make my terminal a little bit smaller and I'm going to move from this app demo to this drift demo okay and this Rift demo is a very simple a lot available

00:34:46 application which uh built on top of jet stream so it ships with PHP news by default and if I go there as well on the terminal and if I run vendor Bean past PHP unit you will see about 43 tests running as expected on this jet stream application now this is a full fleshed out PHP in the test Suite so you can see that classes extends like this 100 Imports on Top This trade this public function so everything written on PHP unit you can even see the searching API here right now wouldn't be cool to have something to

00:35:21 really ship this thing in like in milliseconds and that's where we have built today and I'm going to show that to you what I'm going to do here is just basically um clear my terminal and I'm going to type Bender bin passed but instead of typing just past I'm going to just simply use this option call it um a second call it drift okay so just like that now the first time I'm going to run drift on this project will obviously install past on top but then just below we get to see the output which is 22 files got migrated to past

00:35:55 PHP now the big moment if I simply refresh my Sublime Text we get to see this full test Suite migrated to past PHP in a couple of seconds we are using the testing API we are even using the expectation API full migrated to pass PHP from the assertion API even better we are doing stuff such as expect not to be neural which is the great keyword ever so everything full migrated to past PHP all files as you can see here using past PHP the new testing API everything we like about past in the couple of

00:36:27 milliseconds so time is not the excuse anymore thanks to this plugin okay now the vegan View is that what happens if I run this with past and now we have a full test Suite passing as expected with past PHP so drift begin coming for past this summer thank you thank you [Applause] all right this was my presentation today if you liked it please go to past php.com we have the most beautiful website in the world with Mike and Echo and we also have this beautiful documentation so if you are getting started with past or if you want to

00:37:08 learn how to write tests please go to passphp.com and uh learn from it this is past 2.9 it's coming out this summer but I really wanted to make this one special so everything is coming out today thank you thank you thank you thank you so my name is Nunu I'm a lot of el core team members if you wish to chat with me just follow me along thank you so much for having me Lara Khan and see you all next time [Applause]


Highlights

00:00:21 ๐ŸŽ™ Nuno Maduro introduces himself as the creator of open-source packages in the PHP ecosystem and a Laravel core team member.
00:01:43 ๐Ÿ–ฅ Nuno demonstrates the simplicity and elegance of Pest PHP testing framework compared to PHPUnit.
00:04:22 ๐Ÿงช Nuno showcases the migration process from PHPUnit to Pest PHP and the similar output achieved.
00:06:56 ๐Ÿ” Nuno explains the expectation API in Pest PHP, providing a more concise and readable way to write assertions.
00:09:24 ๐Ÿ’ฅ Nuno announces the release of Pest 2, which reached 7 million downloads and introduced a plugin system for additional features.
00:11:40 ๐ŸŒŸ Nuno reveals five new features coming in Pest 2.9, including snapshot testing for testing long and static strings.
00:14:52 ๐Ÿ—‚ Nuno introduces scrap blocks, a highly requested feature for organizing API testing in Pest PHP.

Key Insights

    ๐ŸŽ™ Nuno Maduro is a highly respected figure in the PHP ecosystem, being the creator of multiple open-source packages and a Laravel core team member.
    ๐Ÿ–ฅ Pest PHP provides an elegant and simple testing framework that has gained popularity among Laravel developers, with over 50% adoption rate.
    ๐Ÿงช The migration from PHPUnit to Pest PHP is seamless, with Pest PHP offering a beautiful output and a minimal API for writing tests.
    ๐Ÿ” The expectation API in Pest PHP allows for more readable and concise assertions, making it easier to write and understand tests.
    ๐Ÿ’ฅ Pest 2 has achieved significant success with 7 million downloads and introduced a plugin system for additional features.
    ๐ŸŒŸ Snapshot testing in Pest 2.9 is a valuable feature for testing long and static strings, ensuring that no regressions occur.
    ๐Ÿ—‚ Scrap blocks, a highly requested feature, will be introduced in Pest 2.9, allowing for better organization and readability of API tests.

(Generated by our friendly AI bots)