Laravel API Development & Vue JS SPA [COURSE]
Lección 9 de 10 · Laravel API Development & Vue JS SPA – e09 – Basic Validation · Educación
Laravel API Development & Vue JS SPA – e09 – Basic Validation
[Music] [Applause] [Music] [Applause]
[Music] welcome to level API development with view Jas single page application from scratch the following episode is going to be an
excerpt from the full course now available on udemy if you are interested in developing robust laravel api's with a front-end built on view and tailed when CSS then this is the course for you
we go into great detail talking about things like authentication testing tailwind CSS view j/s levo PHP unit and so much more so I hope you'll join me for the full course go ahead and click
on the link in the description to get sent to the udemy page where you can purchase the full course I hope you enjoy this episode now that we have our basic test working for adding a new
contact to our application let's take it a step further so let's pull open our features test and then open the contacts test one more time and let's go ahead and add some more fields of course name
is not the only thing that we need to store so let's go ahead and add a couple of other fields so at the very minimum a contact is going to need a name they're going to need an email just add a test
email com they are also going to need a birthday so we'll say birthday column is going to be equal to 0-5 14 1988 and I'm going to show you some cool stuff on how to do
dates specifically how to turn them into Carbon instances pretty cool stuff finally they're going to need a company that they're going to be associated with this is just simply going to be a string
of a company we're just using this to have a couple of fields that we can play with so if we ran this of course what else do we expect but why don't we fetch that contact instead so let's say
contact go ahead and fetch me that first one that is in the database now there is only one in the database so of course this is going to be the same one that we just added because remember that refresh
database gives us a clean database after every single test so we can change this from contact all to just simply be and then let's go ahead and make some further assertions
let's assert equals test name if we were to get the contacts name also we expect test at email comm if we get their email so this is basically asserting that we received all of the appropriate data
back from our database let's say birthday and finally ABC string is gonna be the company name so we'll change that to company all right let's go ahead and run our test with these changes and then
we get the first error assert count meaning I couldn't assert the count because it is not countable of course when you run count first what returns back is not a collection
anymore so we can actually safely remove this altogether because if this passes we actually don't need this line anymore so let's get rid of it altogether let's run our test again okay we failed
asserting that no matches the expected test and this is for a couple of different reasons it's at this line right here of course there is no email field in our database and we are also
not saving this into our database so what do we do that now let's start with the migration so let's go to the create contacts table and we're going to need a new string for email and we know we're
going to need a new string for birthday and then we're gonna need a new string for company then inside our controller contacts controller we're also going to have to do something very similar so
name email and then birthday and company okay let's run our test now and we're back to green great so we are taking care of adding each of these fields and mapping them to the appropriate place
but we're still not doing any validation at all as a matter of fact if we were to actually send this in with only test name it should fail so why don't we make a test to prove that that does not work
yet so let's say a name is required now we're going to copy a lot of this set up here and then we're gonna do a refactor to show you how to clean this test up a little bit so if we were to post and
simply omit the name what do I expect here I expect it to be a validation error for this post so let's go ahead and save this response and then we can assert on that response so let's say
response assert session has errors for the name because we expect there to be a validation error so what else can we test well we can also test that no-contact got added to our database we
could do that that's simply saying this assert count of zero inside our contacts table when we fetch all of the records does that make sense so if we were to post this and we get a validation error
well there should not be anything inside our database fair enough all right so let's go ahead and run this test so phpunit – – filter and then we'll just filter this to this
one test now scrolling back up reveals that we have an integrity constraint violation basically we got all the way through and try to actually insert this record in our database but of course our
database said wait a minute that name field is required and you didn't pass in any data for name so of course we know this is because we're not doing any sort of validation so let's
take care of that now let's go into our controller and let's go ahead and start validating our data so let's say data is equal to request validate and then validate of course is going to take an
array this array is going to take the shape of each of the fields that we're trying to validate and they're each gonna have a set of rules so we can actually copy this array right
here and we'll modify it so instead of request all of that let's just get rid of this altogether and just simply put empty strings for now and instead of passing in a right to here we'll pass in
data data of course is coming from this that we're saving right here so the request validate returns an array of data that has been validated so that's pretty cool all right so let's go back
here run our test again and we should get the exact same error because yes we are validating but we haven't told laravel that the name is required so let's do that now so the name is
required let's run our test again and now it says the given data is invalid we're getting the validation exception however the reason why the test is not passing is because we have exception
handling turned off yet we are kind of depending on it so why don't we remove this top line here and run our test again and sure enough we are at green awesome all right let's do another one
for email now so the email is required so our new test email is required very basic setup as this except that this time we will have a name but no email we'll just say test name okay let's run
this test instead PHP unit – – filter and sure enough we failed asserting that there was a key for error of course we're not doing much in terms of the email being required let's go ahead and
hit required on that let's go back here and run our test again and we get fail asserting that the name is missing we must have made a mistake here of course we forgot to change from name to email
email is the one that we're actually trying to validate so let's run that test again and sure enough it's back to green let's run all of the tests and all of the tests are also passing so I was
just starting to see this is starting to get a little bit repetitive so why don't we take a couple of minutes to refactor this code to be less repetitive
Now that we have the basic test working, let’s explore how to perform validation to our project.
Buy the full course at https://www.udemy.com/laravel-api-development-vue-js-spa-from-scratch/
In this episode, we cover:
Adding other fields to contact model
Writing validation tests
About the course
Learn how to develop a robust API with Laravel and a Single-Page Application in Vue JS from Scratch
What you’ll learn
– RESTful API Development with Laravel
– Vue JS Single Page Application Methodology
– Front-End Design Using Tailwind CSS
– Implementing Search Functionality Using Laravel Scout
– Build a Complete SPA from Scratch