Laravel Tinker for Testing

Video Activity
Join over 3 million cybersecurity professionals advancing their career
Sign up with
or

Already have an account? Sign In »

Time
9 hours 41 minutes
Difficulty
Intermediate
CEU/CPE
10
Video Transcription
00:00
Hi. Welcome back to module for this is less than 4.7 layer volt. Tinker for testing. Now, what is level? Tinker? It is a python program and it's used to test Larible models
00:13
in this lesson. We're gonna test our models. We're gonna test the relationship to ensure that we have them set up correctly. Now, when we tested these relationships is going to return an array of objects that fulfill the relationship
00:28
and an array in any programming language is just a collection of models or objects.
00:36
Now Larible is running queries behind the scenes when we implement these relationships. So when we take a model and we ask for all of the related models,
00:48
what level does behind the scenes is actually builds the query
00:51
that would create that relationship and then executes it and then returns you the results in other models that it again builds
01:00
for you. Now, if you weren't using a framework, there'd be a lot more work entailed in this. You would have to,
01:07
uh, embed queries and create a way for your objects to create those queries.
01:12
But with level and tinker, we will be able to test our models pretty easily with very minimal amount of code.
01:23
I will come back. Let's go ahead and get started. So I'm gonna open a shell session into my Bagram box. So fragment Ssh!
01:30
And then we need to go ahead and go into our shared APP
01:34
directory and go to our first file. We're gonna move file by file, testing the relationships, making sure that they all work.
01:42
Once we have that shell session open, we want to see the into our root directory of where these all those APP files are.
01:49
And then we don't want to run the command. PHP are Titian, Tinker,
01:55
and that's gonna open the Tinker program
01:57
and we will start running commands. So the first thing we're gonna run his building Colon, Colon, find one. So that's gonna find the building with I d one.
02:09
So that is not saying, find one or anyone. It's fine, specifically the building with I d one.
02:15
So that returns the test result I had type in.
02:20
Now the cool thing here is you can go further and let's say Okay, once I have building one, I want to know the name of that. For some reason, I could just do this
02:30
test building
02:31
and further break it down.
02:35
So we want to test the relationship, which is rooms
02:39
and the rooms. Definition is right here. Pretty short, pretty sweet. So rooms and that is related to this building has many
02:50
AP Room.
02:52
Okay,
02:53
so we'll run that. I get the rooms back
02:57
now. The difference here is that I'm getting a collection or an array, so they're just happens to be only one room
03:04
so again and Ray with just one room in it.
03:07
So if I try to, for example, just go straight to the
03:13
name of the room, I can't get in air.
03:15
But if I do this number, I say first
03:19
and then I say name,
03:23
then I would get Thea the name.
03:25
It doesn't even mind.
03:28
We're not going too far into this or just doing enough so that we can easily see the database so that when we create a model, we can go and then say, OK, give me the rooms of this model and put in this test data.
03:42
Okay, so that completes the building.
03:45
Let's go ahead and open the course.
03:46
So again we're gonna do is typing course. Colin Colin to find one.
03:53
Andi
03:54
Arrow
03:57
room.
03:59
Okay, we got a room back. So that's working
04:01
grades.
04:04
All right, so those are the grays that are entered for that course.
04:09
So that's working. Let's go to the next one, raid.
04:14
So again, I'm gonna do grade. Colin. Colin, find one.
04:18
All right, let's test the grade relationships. What user does this? Does this great belong to
04:26
that user? Okay,
04:28
course.
04:31
Yep, that's all looking good.
04:34
Move on to the next one, Which is the room. So room
04:40
corn, find one.
04:43
This is the one who gave me an idea of two and not one.
04:47
So there we go.
04:49
So arrow courses.
04:54
All right,
04:55
on, then building. What building does the room belong to?
05:00
All right,
05:00
that's working. Next we go into the user file
05:04
again. User going corn, Find one.
05:10
All right. That's the user one. Yep. On let's get his grades.
05:15
There are his grades and user courses.
05:19
See if that works
05:21
and let's go into the next one.
05:27
So this will be
05:30
use, of course. So, user course.
05:33
Find one.
05:36
All right. And then let's find his user.
05:41
That works.
05:43
Let's find his course
05:45
that worked. And let's find his
05:47
user type
05:51
and we got no back. That doesn't appear to be correct.
05:57
Let's go and take a look at what we have going on in user type
06:03
courses.
06:10
Right? It's going to the migration file
06:13
Database migrations,
06:15
user type.
06:20
Okay,
06:21
Go into the
06:24
user courses.
06:28
Okay, so here is the The problem is I put type underscore. I d
06:32
I should have put user underscore type I d.
06:39
When we go in and change that
06:42
and then I will need to change. This is Well,
06:46
the reason that didn't work is because levels making assumptions on how I named things. And if I accidentally deviate from what level expects than the framework
06:58
makes the wrong assumption. And I don't get the right answer back.
07:02
So now with that change, when exit,
07:05
I'm going to a PHP partition.
07:10
My great fresh.
07:14
Actually, we need exit. Uh,
07:16
now that'll work. PHP are Titian,
07:19
my great fresh.
07:29
Okay, so I need to change this unique key.
07:36
Run the migration again.
07:46
Okay, I'm back. After running that migration, I had to go back and insert some dummy data because as we know, the migration rebuilds everything. So I lost all my test at that is part. The reason we're going with the database seating is because once we have the seeding process set up,
08:03
if we re migrate, it won't matter. It will. It will receive the database for us.
08:09
So let's go ahead and go into tinker again
08:18
on we were typing
08:20
user course.
08:22
Klein one
08:31
gotta capitalize. The u
08:39
see what we got here. I hope I didn't say that.
08:43
Here we go.
08:45
Bring back open my shell and it looks like I have an idea of three.
08:50
So
08:54
my relationships were
09:01
I see, user
09:07
and use your type.
09:16
And last but not least course.
09:22
Okay,
09:24
we go back into that folder and see that was the last one we had in the APP folder. Of course, user type was the last one was going and open that
09:33
then user type colon colon. Find one,
09:37
all right. And he has a user course relationship used, of course,
09:43
is
09:46
and that's working.
09:48
So all of our relationships are working. There was one problem in which I accidentally left out the user out of the type. I had a column that was called Type I D or type underscore. I d to be specific and it should been called user underscore type underscore i D. And that is because when we were setting up these relationships,
10:07
Laurel is assuming that based on the model name and the table name that certain Colin names
10:13
specifically associated with the i d are going and have a specific name based on the table in the model and when they do not,
10:22
um, you can either tell level that you've used another name
10:26
or levels not gonna create the correct relationship or is gonna look
10:31
create a query that won't get the results you're looking for.
10:33
So let's go ahead and head over to the summary,
10:39
and that brings us to less than 4.7 layer. Vel tinkers Summary. What do we do with this lesson? We use Larible Tinker to test our model relationships. We discussed why and when to use Tinker again. Tinker is used to test the models and the relationships that you've set up in a terrible application,
10:58
and we would just want to verify that they're working so that when we go to set up our
11:01
database seeding process will be confident that that process will function correctly because to add a bunch of test records to our database will be will have to be certain that the relationships are working correctly so that we can very quickly generate a bunch of fake data. Now, with that said,
11:20
let's see the database in the next couple of lessons
11:24
hope to see you there. Thank you.
Up Next
Intermediate SQL

This free course introduces the student to intermediate concepts found in the implementation and application of Structured Query Language (SQL) within professional business environments.

Instructed By