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

Dynamic Image Resizing0:41

If we take a look at the image address, which I've opened in this tab here, you'll note in the URL we have a bunch of different variables as the query string. So we've set this width, we've set this height, we've set a fit to crop, and that is how we receive this 60 by 60 square with our image cropped to the middle. Now, this is using a php library called Glide. It is an amazing library. And just to show you how it works, I'm going to pass 120 as the width, for example, and you can see that dynamically this image changes. You'll note we're grabbing this image from this little image root here, and I want to test that this image root is set up correctly.

Running Automated Tests1:19

You'll note we're grabbing this image from this little image root here, and I want to test that this image root is set up correctly. I know that we're obviously seeing the results in the browser, but we don't want to rely on manual testing in the browser each and every time we want to make sure this image root does actually work. So in the code base itself, I've already gone ahead and created a little test. It can show supported image formats and options. And if I jump into the terminal and run php artisan test, you'll see that we do have three passing tests. In other words, this is working correctly.

Test Setup With Fixtures1:48

you'll see that we do have three passing tests. In other words, this is working correctly. Just to walk you through what's taking place, I'm going to fakeStorage, and then I'm going to put a file into the fakeStorage disk, which is basically just this item from the fixtures directory. Now, in the fixtures directory, which is here, I have a JPEG, a PNG, and a WebP file. If you take a look at the data set that I've already created, and you should be familiar with this syntax from the last episode, you'll note that I'm testing against the example.PNG, then the example.JPEG, then the example.WebP.

Using Combined Datasets3:38

So what can we do to improve this test? Well, this is where I would reach for a combined data set. A combined data set is a Cartesian product of given data. If you've never heard of a Cartesian product before, it's basically every possible combination of two or more sets of data. So let's take a look at the different unique sets of data that we have available to us. We obviously have three different image types, so that's one set of data, and then we have a bunch of different arrays containing width, height, and fit. So what I want to do is split up my data set into two separate sets of data. I'll do this by using a secondWith method,

So what I want to do is split up my data set into two separate sets of data. I'll do this by using a second with method, and I'm going to pass that with method the data I'm interested in. So example.png, example.jpeg, and example.webp. So that's the first set of data, and once I've gone ahead and done that, I can remove all of that duplicate data from this data set here. The second data set is obviously going to be the options I want to pass, but you'll see that after separation, what we've actually done is reduced the amount of information required because we no longer need to duplicate these options over and over again for each type of image.

what we've actually done is reduced the amount of information required because we no longer need to duplicate these options over and over again for each type of image. So I'll remove the duplicate values, and we're left with just three different option sets in our test. The last thing I'll need to do is just wrap this in another array so that PEST is able to pass the properties correctly. If I jump back into the terminal at this point and I run php artisan test, you'll see we still have all of these test cases that we had before. Note that the description has changed slightly. We get this little forward slash,

Scaling Test Combinations5:55

And I guess you can imagine what's going to happen if we were to add another option set here where we might set these to zero, for example. Once I do that and I run php artisan test again, you'll see that we're running the exact same tests, but we now have three tests, one for each file format, where we have zero as the height and width. So combined data sets are incredibly powerful. When you have two different sets of data that you want to combine in order to create a large set of test data to run against specific tests in your TestCase.

in order to create a large set of test data to run against specific tests in your test case.

Combined DatasetsCartesian Products

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