Deduplicating strings, adding Swagger, and workflows

Download MP3
Announcer:

This is the Laravel news podcast, your one stop podcast to find out about Laravel related news, tutorials, packages, and more. Here are your hosts, Jake Bennett and Michael Dyrynda.

Jake:

Hello. Hello. Good evening. Good afternoon, and good night. It is episode 221 of the Laravel News Podcast.

Jake:

Welcome back to the show. Michael, how's it going my friend?

Michael:

Oh, going well. I'm starting to we we're starting to get glimpses of us stealing back summer from your northern

Jake:

From the northern hemisphere. Yes. Good. Yep. And we are officially on our last non school night.

Jake:

Tomorrow is the first night, you know, school night where we actually have to get to bed before, you know, or at a reasonable time because Friday is the 1st day of school back for the kids. And so for long and fun summer break yeah. Why why

Michael:

go back to school on a Friday? That's crazy.

Jake:

I don't know. Right? I don't know. It's just like they get the one day in of, like I think it's just like, hey, everybody. Welcome back.

Jake:

And I think it's a very casual Friday. And then it's like, they're off for the weekend and then back Monday. Oh, yeah. No. No.

Jake:

No. It's not like a actual valuable day. It's more like a, you know, just like a transition day back into the flow of things. Just kinda like, hey. On Thursday, there's like a, back to school night, like, an open house sort of deal.

Jake:

Meet your teacher, run your schedule through, and then on Friday, they have the 1st day of school, and then they're off and then back on Monday. So yeah. Yeah. Should be good. But, mourning the loss of summer.

Jake:

Absolutely.

Michael:

I am I'm excited. We had, a couple of days this week that have been warm and the sun has been out. It's it's raining or it's meant to rain this afternoon and it rained overnight, but it's it's definitely trending in the right direction, which I'm excited for. Yeah. Because I'm sick of That's good.

Michael:

Sick of feeling cold.

Jake:

Yeah. I hear that, dude. I hear that. And, yeah. I'm not looking forward to getting cold again.

Jake:

I'm still got my pool open, and I'm hoping to keep it as open as long as I can. And, we're definitely trying to squeeze every drop out of summer here. So having lots fun and making lots of good memories. So I had a good summer. Well, hey, everybody.

Jake:

It is August 14, 2024. Our sponsor today is Honeybadger. Thanks so much, Honeybadger, for sponsoring the show. Honeybadger is error tracking and monitoring for your Laravel applications. We'll talk about them a little bit later in the show.

Jake:

But for now, let's jump into it. Releases. We've got Laravel 11.20. Let's talk about what we got here. 11.20 was released this week, which would have been as of August 8th, including a new collection method, a deduplicate string method, the ability to use enums with assertable JSON, and more.

Jake:

So Jason McCreery, Laravel's garbage man, as he likes to call himself, Jay Mac, contributed a collapse with keys method to the eliminate support collection class, also known as collect. You can just collect an array or something like that. So what this will do is it will collapse nested arrays while preserving their keys. And I'm assuming this is that they don't have duplicate keys. Right?

Jake:

So if you have a listing of arrays that you're collecting together and then you say collapse with keys, they'll preserve those keys, and flatten them down to just a single, array, which is great. We also have contributed by J Mac a deduplicate string hopper that replaces consecutive occurrences of a character within a it says character, but I think it means consecutive occurrences of a character within a string. So you could think of if you had a word oh, I'm trying to think. Illuminate, I l l u m I n a t e. If you said string, deduplicate, illuminate, it would find that there were 2 l's in a row, and it would eliminate one of those l's.

Jake:

If you had double spaces in a string, it would eliminate the double spaces and replace them with a single space. If you had a path that had slashes in it and you had more than one slash in a row, it would deduplicate those by removing one and leaving the other. So that's what the deduplicate string helper does. Thanks, J Mac, for contributing that one. We've also got custom view paths when making components.

Jake:

Andrew Brown contributed this. This is the ability to define a path flag when you need to create a view in a custom file location. So, if you're making if you're making a view or a component using the artisan command line tools, you'd say, PHP artisan make colon component. You give it a name. In this case, we'll call it Foo, and then you can say dash dash path and pass your custom path along with that command, which will then create the view in that custom file location.

Jake:

By default, it puts it into the resources views components folder, but this now allows you to pass your own location that you can create it in. We've also got Ihor Vercesky well well, Ver try. Yeah. Contributed to the mix file not found exception, and this will handle missing mix files. So this this really, provides a more precise error handling mechanism when this exception gets thrown.

Jake:

So if you're using I'm guessing this is Laravel Mix, it will handle the exception in the case that those mix files are not found. And previous to this, it was just sort of a you didn't really understand maybe what it was. You'd get this mix missing error, and you're like, what is that exactly? And this will now be for more verbose mixed file not found exception. Okay.

Jake:

Patrick O'Meara contributed the ability to use enums while using where methods with assertable JSON instances. So this is really cool. We've talked about these, assert JSON things a a few times. So in a response in a test, you could say assert JSON. And then into assert JSON, you can pass a closure, which then accepts assertable JSON.

Jake:

If you type hint that assertable JSON, you can use these fluent methods to actually make assertions on that JSON object. And so now what you can do is you can say dollar sign JSON where role is, you know, the first argument, comma, and then you can pass in an enum value. So user role, double colon, developer, instead of having to say user role, double colon, developer, arrow, value. If you have a backed if you have a backed enum, you'd have to say arrow value. This just, automatically will cast it to its string representation, its arrow value, and so you don't have to do that anymore.

Jake:

So just a nicety. There are some little, you know, stuff to make the developer experience just that little bit better. So Patrick O'Meara, thanks for contributing that one. Christophe Hrempel, I think that's how you say it, with a little German there, updated

Michael:

the Australian. Careful. Australian.

Jake:

Sorry. Yeah. Yeah. Hey. Hey.

Jake:

Easy. Hey. At least I didn't say Australian. You know, those break dancers, we'll have to talk about that later. Oh, boy.

Jake:

Oh, boy. Anyway, moving on. We we won't we won't leave a sidetrack to that. Christophe Humble, updated the make notification command to prompt for creating an optional markdown view when you don't provide any, initial input. So if you say PHP artisan make notification, you're gonna get prompts to select a markdown view for your notification class.

Jake:

So it's gonna say, what should the notification be named? Would you like to create a markdown view? Yes. What should the markdown view be named? It'll do that for you.

Jake:

But if you don't pass any of that initial input, that's what it does for you. It'll give you those prompts to say, hey. What exactly are you wanting to do here? So you don't have to remember all the flags. Really, really nice.

Jake:

I like that improvement a lot. Alright. We've got wow. We've got, like, 3 more here. Okay.

Jake:

Here we go. You ready? Yo. Stack contains and hidden stack contains methods to context stacks. Boy, it has been a while since we've talked about context, not that long, but this wasn't, this wasn't something that was created too long ago.

Jake:

Was this Tim McDonald that created contexts? Yeah.

Michael:

Yeah.

Jake:

Give me your if you can, Michael, I know I'm putting you on the spot. Give me your, like, 10 second elevator pitch for what exactly context is.

Michael:

Context is a way to be able to attach data to a request life cycle and and for that context to then follow all the way through the the request. And that and that will go into, I believe, into jobs, like, queue jobs that are dispatched as well, so you can trace the execution from request entry all the way through to wherever that request ultimately terminates.

Jake:

Very cool. So it's, like, almost binding it to the container, but only temporarily until that request is done, and then it tears it down, and it's it's lost forever at that point. But you can you can push items onto the context, so you can say context push, and then you can pass values in there. And then in this case, you can use these new values these new sorry, these new methods to see if the context contains a particular value. So you could say context, double colon, stack contains, and then you can pass in items to see if it contains those items.

Jake:

So you could say, you know, you can pass in 1 or you can pass in multiple. In addition to having the regular context push, you also have this idea of a hidden context. So you can push hidden values as well, and so you have a, a method that's paired up with this called hidden stack contains. So we have methods for both of these. Does the stack contain items and does the hidden stack contain items?

Jake:

And if it does, it will return true, and if it does, it will not it will return false as simple as that. Gunther de Brauer contributed assert not deleted, assert not failed, and assert not released assertions to the queue that you can use to assert the inverse of these methods. And so, if you had a job was assert deleted, failed, or released, you now have the not version of those, the inverse of those methods to assert that those things did not happen inside of your tests. And lastly, Caleb White contributed an update to the is fake method, making the visibility public. And so what we're talking about here is a facade.

Jake:

So in your tests, if you're using a bus or if you're using even, you know, mail fake, storage fake, all those things, what's what that's what that's doing is it's taking that facade reference and it's loading in a fake in the background in place of the real item that you would be loading in. And if you ever need to check to see if the, facade that you're referencing is the fake version of it, you can now use this bus is fake to determine if it is or if it isn't and then do some behavior based on that. That is a lot of stuff, n11.20. Thank you for writing that one up. Everyone's favorite human, mister Paul Redmond.

Jake:

Of course, you can find the entire change log on GitHub.

Michael:

Yeah. A lot of stuff in there. 22 things I'll note. There there was, you know, a lot that that we talked about there that Jake went through. There was 30 separate contributions into that version of of Laravel.

Michael:

So, you know, obviously, everything's still kicking along of and and lots of people still contributing. We also mentioned right at the top the, string deduplicate method. If you check out the Laravel News YouTube channel, Eric

Jake:

Ah, yes.

Michael:

Our fearless leader has also included a video talking about removing duplicates in a string, so be sure to check that out and subscribe to the YouTube channel if you have not already. But that is the only release. There was no release this week, I assume, because Taylor is vacationing with his family and so no release this week. And then Laracon US is coming up in a couple of weeks. And on that note, he tweeted today that there is plans to livestream Taylor's keynote at Laracon to the Laravel YouTube channel free and live, obviously, being a livestream.

Michael:

So that'll be on 20 6th, I think, of August. So that is the end of day keynote on day 1. So be sure to check it out. It'd be exciting to to be involved in Laracon if you are unable to attend in that small way. So check that out.

Michael:

There'll be links posted all over socials at that time. But subscribe to the Laravel YouTube channel. There's heaps of content there. Josh is doing a lot of stuff. Christophe is doing a lot of stuff as well.

Michael:

So good place.

Jake:

I'm guessing that means it's gonna be pretty important. It's gonna be live streamed.

Michael:

There is a lot of stuff a lot of stuff, Taylor, tease, you know, inertia things that are coming. There's a lot of new stuff coming to the framework, ambitious new projects. He he also tweeted earlier that, you know, the the team has been grinding harder than they've ever ground before over the last

Jake:

6 months.

Michael:

So it's exciting to see, what what that culminates to, obviously, at at Laracon US this year. So yeah. Check it out. Enjoy if you're gonna be there. I think we'll have an episode out.

Michael:

Or no, maybe we won't because you'll be you'll be in Dallas the time that we're next due to record. So maybe

Jake:

there won't

Michael:

be an episode that week.

Jake:

Yeah.

Michael:

But that's okay. Yeah.

Jake:

I could probably take my mic along. Yeah. Probably take my microphone along. See if I can

Michael:

record 1.

Jake:

We'll see.

Michael:

If you don't hear from us by then, make sure that you have a great time if you are going. And if like me, you have a bit of FOMO and, you know, be all over social media checking out what's happening.

Jake:

Maybe I'll just, take random videos of people and, like, get little, audio snippets of, like, what, you know, people think of particular things, and we could throw that together as a Laravel News episode.

Michael:

Yeah. I think that'd be cool.

Jake:

Maybe. Yeah. It would be fun. Okay, folks. We are moving on to some other news here.

Jake:

So one of the packages that is quite popular in the Laravel ecosystem right now is Laravel it's not Laravel. It's just Filament. It is Filament. You use it with Laravel, but it is just Filament. It's not Laravel Filament.

Jake:

So Filament has a couple different pieces to it. Right? It's got an admin panel. It's got a form package. It's got a table package, and they kind of all can be used in, you know, they they integrate together into, you know, to be whatever you kinda need.

Jake:

Instead of having to take all of it at once, you can kinda use, the different parts and pieces if you like. But there's this new plug in now for Filament PHP called Filament Gaze, and what this does is you know how, like, if you're in Notion or if you're in Google Docs or even if you're using Microsoft applications across your teams and you have multiple people in a Word doc or in an Excel doc together, you can see where they are. You can see where a person is or if they're viewing the same page as you. This attempts to give you something similar to that. So it allows you to display when other users are viewing the same resource as you are viewing.

Jake:

So it provides a really nice API to make a simple banner or even allow authorized users to take control of a locked page. Now when it says locked page, I don't know exactly what that means. I'm not sure Filament, if you're using the admin panel optimistically locks a resource if somebody is viewing it so that not 2 people can be modifying it at the same time. Don't know for sure. It sounds like that's sort of the behavior.

Jake:

I'm not sure if that's something you have to build in or if that's something that's built into Filament's admin panel by default, but it seems like you can now, allow admins or authorized users to then say, hey. I want to take control of this page. I'm going to control this or, you know, lock this for myself rather than another person who's currently viewing it. So here's some of the features. You can quickly display when other users are on the same resource.

Jake:

There's a configurable pull time to determine how often it should inform Filament if somebody is still viewing the page. You can lock a resource, you can allow lock overrides, and then you can conditionally hide the banner. So it's mentioned here in this, little tutorial, which was written up by Paul Redmond, that his favorite feature is the can take control method, which allows you to define custom logic for taking over a page that someone else has control over. And so using the code that's on this page that he's showing, the owning user can see that they have control of the page in which other users are on the page, and then the interface will allow you to say, nope. I want to take control of this, and it will then lock out those other users from making changes.

Jake:

So pretty cool. Really interesting. I've not used their admin panel a ton. I typically use Laravel Nova for that stuff, but I have used some of their other things, their actions, their forms. Those are really interesting.

Jake:

So this is a this is a good one. If you happen to be using Filament and you're up to date with the latest, Filament 3, I think, is what they're on right now. You should definitely check out this plug in Filament Gaze.

Michael:

Nice. We have spoken about the new, I guess, mini series of videos that Eric has been doing on our YouTube channel called the Laravel creator spotlight. And this one here, in this episode, Eric is joined by Simon Hamp, who is the creator of native PHP and talks about the project and the potential of what can be done with it and what's what's on the horizon. For those of you who don't know, native PHP is a tool that allows developers to build desktop applications using PHP and then distribute them without requiring PHP on the user's machine. So I know Marcel Passyote has done a lot of, you know, public stuff around using native PHP and and publishing apps.

Michael:

And he spoke at Laracon AU about it last year. Simon Hamp, the original creator of that and that, you know, the 2 of them have collaborated on it. So it's good to to get an overview of that. So it's a 30 minute episode. If you wanna check it out, we have links to it for you in the show notes.

Jake:

Very nice. Moving on to some of the packages here. The first one we have is adding a Swagger UI to your Laravel applications. The Laravel Swagger UI package makes it easy for your project's swagger, OpenAPI version 3 JSON or YAML file to be accessible in a Swagger user interface right inside of your Laravel application. So you just drop in your OpenAI file in a specific location, which is resources /swagger/openapidot JSON.

Jake:

This is configurable, of course. You could drop it into a different location, but then you navigate to /swagger locally in your project. And what this does is it displays, a user interface for all of your configured endpoints there. And so it automatically updates the user interface to use the current project's environment. So if you are in your local environment, it will show these with the base URL set to the Laravel project's base URL.

Jake:

It also allows you to configure OAuth 2, which can be injected into the Swagger UI with the packages configuration file to update those values as well. Of course, you can configure it to not have to be Swagger, but you could do it at a different location, too, and then you can, of course, control access to that location, to say, like, hey. Somebody should or should not be able to view this slash swagger URL, using a simple gate definition of view Swagger UI. So you can read more back of the package, get full installation instructions, view the source code on GitHub in the show notes. That's a pretty cool one.

Michael:

Very handy. Especially if you're building out those things and you want some way like, writing the docs is one thing, displaying them in a way that your app app consumers or your other developers can can actually get to it is another thing. So it's nice to have

Jake:

a Yes.

Michael:

A drop in application for that. Masker is a simple zero dependency input mask, package for JavaScript, plain JavaScript, Vue, Svelte, and Alpine JS. If you don't know what input masking is, say you wanted to mask a phone number format, you could use, some format with a data attribute, so data dash masker on your input class that specifies, for the US, plus 1 and then, pound pound pound space pound pound hyphen pound pound pound pound, and then give it a placeholder. And this allows you to kind of control how the phone number is actually displayed when you enter it. So when the user inputs the phone number, it follows that mask formatting in the provided format.

Michael:

It has integrations for popular JavaScript libraries and is easy to use on its own or integrate with any framework that you like. There are no dependencies and it is just 3 kilobytes gzip. There is a vanilla JS version, a Vue 2, 3 version, an Alpine JS and a Svelte integration. It works with native and custom inputs. There are custom tokens with modifiers, transform functions and hooks, the number mask mode for easy money formatting, and dynamic, reversed, and eager masks.

Michael:

If you want to know more about the package, we will have links to itself, its documentation, all in the show notes.

Jake:

That's exciting. I always love these libraries. I feel like they make inputting so much more obvious for users, for what's expected, and it sort of just helps them to stay between the rails, when it comes to what the format is that's expected. It sort of helps to just automatically format those things. For specific things like phone numbers, especially, it can be a little bit confusing.

Jake:

Like, do I have to put the dashes? Do I not? And these masking libraries, a lot of times, we'll just go ahead and put that in there for you. And if they're really good, you can also paste values, and they will handle that automatically as well. And so I'm sure this is one of the good ones, so definitely check that out.

Jake:

Hey. If you've not heard about Laravel Pulse, I believe this was released at Laracon AU last year. And so Laravel Pulse is a way to show it's an aggregate it's sort of like so, like, telescope Laravel Telescope was to inspect sort of individual requests or jobs or queues or things like that. Pulse is a little bit higher level view, so it's gonna show you here here are your slow database queries. Here are your slow routes.

Jake:

Here are, you know, any any number of things that you can think of. Like, here are, how many jobs you currently have queued, etcetera. Right? There's a there's a large list of of things that it can do out of the box. But what one of the other things you can do is you can create your own custom cards for Laravel Pulse as well to show whatever information you'd like to show.

Jake:

And so what we're talking about here is a new card called active sessions. So this will show your application's total number of sessions, including both web and API users. This is based on the pulse active session threshold value, and so it displays an interactive color coded indicator in the card, based on these values. So it'll show you, like, a little donut chart there showing you this is the percent of users that are logged in through the web. This is for the percent of users that are logged in through API, and then it'll give you a count, for those and, what driver is being used, in order to authenticate those.

Jake:

So web would be like, you know, you're using session driver, and API, you're using sanctum or whatever. So it will display the active web sessions, percentage of users, color coded passport support when using only one provider. And so to get started and install it really simple, we have the instructions here. It's just a simple composer require, and then you can configure it in the pulse dot PHP. Pretty simple.

Jake:

I was gonna say we've actually just talked about this today. There are certain applications that we prefer to do deployments on when there's less users on them. And so, you know, sometimes it's like with it when it's like a SPA or like a layer of a LiveWire situation, if you do it when there's a bunch of users on there and you have to do, like, FPM, you know, to restart FPM, things like that, you're just gonna drop some some connections, and so that's not ideal. And so we like to do that when we have a a lesser number of people on it, and so typically I will do that in the evening time, but sometimes it's like the end of shift is like 9 o'clock. But sometimes those people who are saying a little bit after, and so we were just saying it'd be really nice to be able to see how many users are currently logged in and have active sessions.

Jake:

And so this solves that problem. Exactly. So you just go to your slash pulse and go take a look at it. Yep. Oh, looks like everybody's out, or we've only got a couple users left in here.

Jake:

No problem. I'll just go ahead and deploy right now. I can think of a lot of other reasons why you'd want to use it, but that's that's one of them. So pretty cool. If you already have Pulse installed, this is a really easy one to add and seems like it'd be very helpful.

Michael:

Yeah. Having having that kind of high level aggregate insight data Yeah. In a post dashboard is really useful. And just as useful from our friends at Honeybadger is their take on insights, which is logging and performance monitoring that helps application developers gain deeper visibility into what's happening with their applications, and it goes beyond application monitoring and responding to exceptions and downtime. Insights will let you drill down into the details and step back to see patterns in your data, and you can get even higher clarity using BadgerQL.

Michael:

If you want higher clarity in production but don't have the time to earn a PhD in observability or you don't have a data scientist on staff, you can forget logs, metrics, and traces because Honeybadger Insights is built around structured events. And when you send your application logs and other events to Honeybadger, you'll unlock the power of Honeybadger's powerful new query language, BadgerQL. Using BadgerQL to ask any question about your data, convert any event into a metric, and chart your metrics on custom dashboard so you've got another place to look for how many active users you've got at any one time. You can do all of this on Honeybadger's free plan as part of their comprehensive monitoring suite, which includes error tracking, uptime monitoring, status pages, and more. And did you know that an error is really just a first class event in Honey Badger?

Michael:

In fact, you can use Insights and BadgerQL to explore all of your existing Honey Badger data in new ways, which is pretty pretty cool. You can give it a try today at honeybadger.i0.

Jake:

Very nice. Thank you, Honey Badger, for sponsoring the show. Always much appreciated. Well, we've got another package here, and this one is specifically around creating actions and workflows with this Laravel workflows package. So this is by Kalle Bouzidi, and it simplifies workflows in Laravel with clear action definitions and event tracking.

Jake:

So you can define really complex workflows and then break them up into their particular actions and then define events, that are gonna happen as well. So at a high level, this package defines as a large parent, a workflow class, and then that workflow class consists of 1 or more action classes. You can create those actions or workflows using the packages, provided command line, commands. So PHP artisan make workflow, PHP artisan make action. There's some examples in the read me, something like a validate cart items action, which what this is doing is looking to see, is there actually items in the cart?

Jake:

If the cart is empty, you're gonna throw an exception. Hey. The cart is empty. There's nothing for you to purchase here. Otherwise, we pass along some context that's going to get passed to the next item in the workflow, I believe.

Jake:

And in this case, we're saying context validated equals true. And so they're just setting a key on that context, which is then going to get passed to the next action in that pipeline. So then in the workflow, once you've defined an action like that, you can then combine those actions into that workflow, and, you can define before, main, and after actions. So you can say, before I do my main action, you should do these things. So in this in this instance, they're saying you should validate the cart cart items, and then you should calculate the total.

Jake:

Those are the before action items. Then you have the main action, which in this case is make payment, and and then you have an after action, which in this case is send an email. And then you can observe these different events. In this case, register observers when you get an order class. You have an order observer that's going to get fired, and then you can track these events inside of a, payment processed class.

Jake:

So, the other thing that's nice about this as well is it makes it really easy to test against. So you can have, you know, there's assertions about the results from the workflow as well as whether it was run successfully or whether it failed. So, you can learn more about the package. You can fill installation instructions, of course, on, in our show notes here. But seems like a really, really interesting workflow.

Jake:

Seems like it would work nice and, you know, especially if you're dealing with these complex sort of workflows where there's a lot of steps you need to do. It can be nice to have that all sort of packaged up into a workflow and then make it easy to test those things. So nice job.

Michael:

I like I like that the interaction context is just a plan. Right? Like, I've done this kind of thing before and trying to figure out, you know, how do you get a generic context object that you can kind of reuse in different places and what is common enough. Like, just throw an array through it. Sure.

Michael:

That and, like, an array is just an array and and and you don't have to worry about conforming to any specific, contracts or having methods exist there or things that don't always make sense in every context. So I like the simplicity of that. Sometimes sometimes you just need to sit back and and think, you know. Maybe the simple approach is the easy one. So

Jake:

good word,

Michael:

on that one, Khalil. The last package we have here is the a package that will help you block known spam IPs from your Laravel app with the abuse IP package. This is a package by Ra Rahul Alam and adds a layer of protection to your Laravel applications from known spam IPs. The package provides an artisan command to keep your IP list in sync with the aggregated AbuseIP DB block list and a middleware to check requests against that list. So the first thing you want to do is to, once you've installed the package, sync the blocklist file of IPs.

Michael:

And if you run your application on multiple servers, your storage driver will need to be centralized so that you can sync and store the IP list data on each server. And then you can use the package's abuseIP middleware, which will check requests against the block list to prevent IPs on that list from ever accessing your application. So it's a pretty useful one, especially if you this this happens from time to time. Like, you would just have these the bots that go out and they start scanning apps and and sending requests and doing all kinds of dismal and and annoying things. So being able to just block them at the application level is is a good start.

Michael:

As you scale up, as your things get bigger, you probably need to offload those things from your application. But when you're getting started, it's a good way to to kind of keep your application running by preventing known abuses from attacking your infrastructure. So thanks for that one, Raul.

Jake:

Absolutely. We've got one other one here, which is, written up again. Everyone's favorite human, mister Paul Redman. And this is a really cool one. Paul is a what do we call that?

Jake:

Like, he does source diving.

Michael:

Right?

Jake:

So he likes to dig through the framework and get into the messy bits of how things work. And so he goes through sort of how he came upon this discovery, and and what he's talking about here is how PHPUnit, when you create a new I think it's when you create a new feature test, it will inherit the test case class that is the framework's test case class. And so when it does that, it calls this method called set up the test environment. And so he talks through what exactly is that method doing. One of the things that it's doing is it sort of recursively goes over any of the traits that are on that test instance, and then it does some magic convention work that Laravel is known for.

Jake:

Right? It used to be that, like, we had conventions kind of all over the place, and we still do. Mhmm. But this is just sort of one of those magic conventions that if you know about, you can leverage it to make, your tests really, really nice to work with. And so the thing that he says is this.

Jake:

If you have a trait that is on a test method or sorry, on a test class, that trait, if it has something called setup and then the name of your class, like, if you had a class or trait, I'm sorry, called my useful helper. If you have a method on that thing called setup my useful helper and then tear down my useful helper, what this, method that we talked about at the top set up the test environment will do is when you're getting ready to start running your test before it runs your test, it'll run that setup method. And at the end of your test, it will automatically run that teardown method. Now why is that important? Well, if you put this trait, if you put a trait that you've defined on your test method, on your test class, excuse me, one of the things you have to do is you have to extend then the setup method, and then you have to call parent setup and you have to, you know, call that method on that trait.

Jake:

Well, if you're using this sort of magic method name, you don't have to do that anymore. Every time you include the trait, it will automatically run that setup code and it will automatically run that teardown code. So he talks about a recent trait that he created, that required some setup and teardown logic, And since he is using these magic method names and it's in a feature test, he doesn't have to actually extend that setup method. He doesn't have to call parent and then call his own method or remember to do that in every place where he's using it. He just puts the trait onto the class, and it works.

Jake:

And so, these are really, really cool little things that you can find. They're not really documented, except for when people like Paul Redmond decided to let us know about it. So Mhmm. Pretty cool tip here. And, if that's something that sounds interesting to you, of course, dig in a little bit more on this tutorial, and, you can take a look at some of the code that's getting, written in there as well so you can implement this on your own.

Michael:

Not not just in your test either. That that setup class name or setup trait name thing works with conventional traits anywhere else in the framework. Especially I know for sure in in Eloquent that if you have, like, a boot trait name method, then that will get run on your other classes. Yeah. So this kind of this pattern kind of exists in Laravel, and it's how you you know, how we how different things can be kind of scaffold out or initialized without relying on, the consuming class or a package user, for example, having to call that stuff.

Michael:

So if you have, like, a boot trait name method in an eloquent context, then it will get scaffolded automatically for you. So Oh,

Jake:

how cool is that? I did not know that. That's really neat.

Michael:

Take that one to the bank.

Jake:

Love it. Well, hey, folks. This one is episode 221. Thanks so much for hanging out with us again on the show. Hope you got something useful out of it, and if you did, we'd really appreciate it if you'd rate this up in your podcaster of choice.

Jake:

5 stars would be amazing and incredible. If you have any questions about this episode or for future episodes, hit us up on Twitter at Jacob Bennah, Michael Dorinda, or at Laravel News. And find show notes for this episode at podcast.larvaldashnews.com/221. Folks, in 2 weeks, I will be at at Laracon in Dallas at a hotel down there. Hit me up.

Jake:

I would love to hang out with you. Say hello, and I will, telegram Michael, and we will get on a call, and he can say hello too. He would love to, I'm sure. If you haven't gotten your Laracon yeah. Exactly.

Jake:

If you have not gotten your Laracon AU tickets yet, you should definitely do so as well. Tickets are still on sale. And, I have here there are some pretty amazing stuff you're not gonna wanna miss there. Taylor is going to be there well. So if you missed out on your opportunity to go to Laracon US, you waited too long to get those tickets, get some tickets to Laracon AU and go see him there and all the other great folks that are gonna be there, Michael included.

Jake:

Alright, everybody. Till next time. We'll see you. Bye.

Creators and Guests

Michael Dyrynda
Host
Michael Dyrynda
Dad. @laravelphp Artisan. @LaraconAU organiser. Co-host of @northsouthaudio, @laravelnews, @ripplesfm. Opinions are mine.
Deduplicating strings, adding Swagger, and workflows
Broadcast by