در حال بارگذاری ...

Introducing PEST Framework0:01

So, my name is Nuno Maduro. PASST is a PHP testing framework with a lot of simplicity. And it was crafted, kind of, to bring the joy of testing to PHP. It gives you a ton of cool features. Hello and welcome. My name is Luke Downing. I am a contributor and maintainer for PASST, an elegant testing framework built for PHP with a focus on functional style and simplicity. PASST was created by Nuno Maduro, who also works for Laravel,

Choosing PingCRM Demo App1:30

that makes PASST a dream to work with in day-to-day life. Obviously, if I'm going to be demoing PASST, I'll need an application to write tests for. And I thought there was no better option than PingCRM, the demo application for InertiaJS. I chose Ping for many reasons, one of which is its gorgeous code base. But I also realized that Jeffrey's already talked about Ping in his InertiaJS series, so many of you will already be familiar with this code. However, if you've not seen that series yet, don't worry, I'll take you through the code nice and slowly so you can follow along.

Reviewing Existing PHPUnit Tests1:59

However, if you've not seen that series yet, don't worry, I'll take you through the code nice and slowly so you can follow along. If we take a brief look at the existing PingCRM code base, you'll note that we already have some tests in place, and these tests use phpunit, the default test runner that ships with Laravel. You can see in this one, for example, we have four tests that will be executed when run. And in fact, if we jump into our terminal and run php artisan test, you'll see that there are a total of nine tests that exist in this code base already. Let's go ahead and install Passport in the project.

Installing PEST in Laravel2:29

you'll see that there are a total of nine tests that exist in this code base already. Let's go ahead and install PASST in the project. And if we go to the installation instructions in the documentation, there are some really clear instructions to follow for getting PASST installed. So I'll run composer require. Now we're using Laravel, so there's actually a Laravel plugin we can install on top of PASST that will make our lives even easier. We'll go ahead and do that. And now we have this artisan command that we can actually execute, which will initialize PASST in our project.

And now we have this artisan command that we can actually execute, which will initialize PASST in our project. So we'll run that as well. You'll note that it's created this PASST.php file, and this is used to tell PASST how to run, perhaps extend PASST for our project's needs. We'll take a look at this file in a later episode in the series. For now, let's run php artisan test again. And you'll see we still have nine passing tests. Now we've made no changes to the code, so how are these tests passing?

And you'll see we still have nine passing tests. Now we've made no changes to the code, so how are these tests passing? They're written in phpUnit after all. Well, PASST is actually built on top of phpUnit, which means that any tests that are written in phpUnit in your project will still work when you install PASST. You don't need to manually convert them, you don't need to make any changes. It will all just work. And that's really nice because it means if you want to install PASST and give it a try in an existing application

Converting a Test to PEST3:47

And that's really nice because it means if you want to install phpUnit and give it a try in an existing application where you have hundreds, maybe thousands of phpUnit tests, you can do that without a problem. It will all run just fine. However, for the sake of showing you the difference between the two testing frameworks, let's take a look at this example test, which ships with pretty much every layer of our project. And you can see it has a little bit of a pointless test in here.

which ships with pretty much every layer of our project. And you can see it has a little bit of a pointless test in here. We assert that true is true. But by converting this test, you'll see the difference between the two files after making the conversion. So first things first, we don't need namespaces in PASST.php. You can keep this there if you want, but we really don't need it, so I'm going to strip it out so we have even less to think about when building a new PASST test file. We also don't use classes in PASST, so we can remove this use here.

when building a new PASST test file. We also don't use classes in PASST, so we can remove this use here and we can move the entire class declaration out of here. And what we're left with is basically a single test method. Now obviously we can't call this, it would need to be in a class to work, so we need some way to declare this in a different syntax. And we can use one of PASST's test functions for this. So that's either test or it. I'm going to use test, but feel free to use whichever one works for you. So I'll say testExample.

I'm going to use test, but feel free to use whichever one works for you. So I'll say testExample. This is the description of the test and you can pass pretty much any valid string to this first parameter. And the second parameter is going to be a closure, which contains our testing logic. Now because this is built on top of phpUnit, pretty much any valid phpUnit test will work just fine inside this closure. So I'll copy the contents of this testExample and paste it into our PASST test.

So I'll copy the contents of this test example and paste it into our PASST test. And finally I'll remove the public function entirely. If we jump back into the terminal and run php artisan test again, you'll see we have nine passing tests as we did before. And here is our newly converted PEST PHP test. You can see how clean PEST is. We've removed pretty much all of the boilerplate and the only thing you have to be concerned with is your test logic. So that's the very basics of getting PEST installed in your Laravel project.

Planning Real Application Tests5:51

and the only thing you have to be concerned with is your test logic. So that's the very basics of getting PEST installed in your Laravel project. Why don't we go ahead now and write some tests that actually make sure PingCRM works.

Installing PestConverting Tests from PHPUnit

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟