Introducing Architecture Testing0:00
Another really cool feature about PEST version 2 is architecture testing. And what Nuno means with this, there is a new architecture ARC plugin, which is included into PEST already, so you don't have to install it. And with this plugin, we can make some assertions about the architecture of our application. Let's take a look at one example here. Like here already, we're testing the models. Let's see what this says. May be used to guarantee that certain classes are restricted to specific functional classes. For example, you may ensure your models are streamlined and solely depend on the Lumi database namespace, and not, for instance, dispatching or queuing jobs or events.
and solely depend on the Lumi database namespace, and not, for instance, dispatching or queuing jobs or events. So this means with this rule, we can make sure that no other classes are being used inside our app models, just to make sure that we keep our models clean. If you want it like this, you have to decide for yourself. I sometimes like to do a little bit more in my models, so don't have an issue with that. But I really like how with this new API, we can expect specific namespace to only be used, some other stuff. And this is what I really like about.
Planning Debug Statement Test1:09
we can expect specific namespace to only be used, some other stuff. And this is what I really like about. Let's take a look at some other examples. So this one is which I really like, which is immediately useful and which I already put in all my applications. We want to make sure that some global functions like dd or dump method are not being used at all in our application because we only use them for debugging and we don't want to see them in our production code. So I would say let's create this test and do this inside our application.
Creating ArchitectureTest1:35
and we don't want to see them in our production code. So I would say let's create this test and do this inside our application because also within our application, we don't want to leave any debugging statements. So let's create now a new test and we're going to call it ArchitectureTest. All right, and the first thing that we want to test, it finds missing debug statements. All right, we are acting and asserting here kind of at the same time. We expect that some specific global methods like dd, dump,
All right, we are acting and asserting here kind of at the same time. We expect that some specific global methods like dd, dump, and I also like to include ray sparsity product. What do we expect we need here? We don't want to see any of those inside our code. And not to be used. I think that's how it was done. Let's check it here again. Not to be used. All right, let's try this out.
Verifying dd/dump Detection2:28
Not to be used. All right, let's try this out. And this is passing just to make sure it's really working. Let's add a debugging statement somewhere like this one and let's add a dump and die method here. Just something. Let's run the test again and now it's fit. Expecting dd not to be used inside my controller, but we have used it, which comes from our architecture test. So I really like how my tests are telling me now.
but we have used it, which comes from our architecture test. So I really like how my tests are telling me now that we missed one of those debugging statements. Let's get rid of this. Let's run it again and now it's passing. So it's a very simple test here for application, but it's a really interesting one because yeah, sometimes let's be honest, this is going to happen that we forget those inside our code when we deploy our code.
this is going to happen that we forget those inside our code when we deploy our code. But yeah, we don't want to do that at all. So from here on, it depends on your personal needs and your references, your requirements, and you can be very creative in creating those architecture rules for your application. Just to show you one more example, which I like to include in my application, here is another one.
Banning Validator Facade3:34
which I like to include in my application, here is another one. So I want to make sure that the Validator facade is not being used. It does not use Validator facade like this one. And the reason for this is that I like to use the request validation method where you have a kind of request in your controller like this one, and then on the request, you have the validate method, which I like to prefer, which I prefer in my controllers.
and then on the request, you have to validate method, which I like to prefer, which I prefer in my controllers. So I don't want to use the Validator facade there. So what I can do now, I can expect that the Validator class, the one for the facade, this one here, not to be used. All right, let's run this. And this is failing. Let's check why, because the facade is being used inside my PageCourseController.
Let's check why, because the facade is being used inside my PageCourseController. Let's take a look at this one. And yeah, I've added it here just as an example. Let's get rid of it. And it's still not working because it's also used in PageCourseDetailsController. Are we using it here? No, but I believe, oh yeah, I still have this import here. And now I believe it should pass.
No, but I believe, oh yeah, I still have this import here. And now I believe it should pass. No, it's not. Let's check again. Oh, yeah, so there are a few controllers here by this namespace, Fortify, which comes with chat stream. And yeah, this is not code that I created. So I'm fine if it is used there. So I can add here ignoring and then the namespace. And now I believe it should pass and it does, which is cool.
So I can add here ignoring and then the namespace. And now I believe it should pass and it does, which is cool. So again, make sure to think about what are the things that you like to use. Nuno also got some more examples on the architecture testing page on the past documentation. So check this out as well. But I think for now, I'm fine with those two, and especially this one already brings a lot of benefits to application just by adding this one test.
Wrap-Up and Next Steps5:37
and especially this one already brings a lot of benefits to application just by adding this one test. So if you haven't checked out past version two, please give it a look. Upgrade, as you've seen, it's not that difficult, but it provides a lot of new features for your application. With that said, have fun testing.
