7 - Partial Mocking in PHPUnit

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys in this tutorial we are trying to write tests for update balance we actually have a class the class name is finance service we actually use this class to call the update balance method in this update balance method we actually call another method calling saying uh naming it as a send request so in send requests we are sending the request using the guzzle to some third-party apis to update the balance so we have a user but the user balance is another service is another place which i don't know where is it i'm just sending requests to it to update the balance of that user so as you can see the update balance will call the send request and the send request call the guzzle but as you know we cannot really send any requests to any third-party apis in our test so what should we do for this we can mark the finance service but if we wanted my finance service then we will mark all of the methods but we have a solution for this i mean php unit has a solution for it and we can use partial marking so what is partial marking partial marking saying that if you want to mark part of your class you can use the partial marking but how this partial marking works okay so let's create our first test class i will call it finance test let me zoom in extends test case and here i will run the scenario so the first scenario is to call the update balance the method that we are actually calling to update the user balance so update balance should call send request and process response with a specific arguments this is the first scenario are humans the second scenario is that update balance should return the user object with newly updated balance now let's start with the first scenario so we say public function test update balance should call send request and process response with specific arguments okay now let's write our tests so to write our tests we need to mark the finance service so we say mockery mark finance service class and if we go to the finance service you can see in the constructor we are accepting two variables so and both of them are using some the type hint the first one is the gaza client the second one is the user model so we also need to pass them to the mockery so we say new client gaza client and new user okay cool and after mocking we need to call a method which the name of the method is make partial so this is mean that i want to mark part of the class so we say mark finance finance service okay now we have the my finance service just a little bit yeah now we need to basically write some expectation so i'm expecting to call this method send request with what with this very with this arguments let me go to the other limits with this argument so i will call with this the balance let's uh specify a balance here so we can use it everywhere i will say the balance would be thousand cents okay balance once it should get called once and return and return self so what does it mean return itself return itself may retain the object itself return this mock object itself and in the class in the finance service class you can see the send request is retaining itself is returning these objects so after that we can call the process response and we should do the same thing in here the second expectation that we need to write is basically the process response so in the process response we actually want to call the processor with any argument so with any x once and return from mean array and return and add it because we are expecting an array from this response as you can see we are returning yes we are expecting the balance so balance let's say the process response will return for me the same balance that i'm sending to it in this case the purchase response acting as that third party api and so the balance is balance now i would like to run the test but before running it i need to call the method the method is update limits i think no no not update limit update balance sorry so update balance if i run the test now okay ah sorry i need to pass the balance to this update balance okay no assertion and the reason is because of the tier down so we need to basically define the expectation here mockery get container yes add get translation count close okay so we run the test again as you can see we have one test and two assertions okay cool so we marked send requests we mark the process response and we return the expectation and we call update balance and we actually just get i mean we get this test passed so now we want to write one more test exactly as the same but in this test we want to basically return something return the user object with the updated balance so test update balance should return should return updated user object so now if i define a response here and if i assert json so i'm expecting to have a balance balance and comparing to this response and i should get the content from it why because in the final service i'm returning json response so when i'm saying get content it will return for me a json string so if i run the test again oh sorry it is array we need to deco encode this using yeah json encode and now yeah we have two test file assertions okay cool and now as you can see we have two tests we have wrote two tests the first test is for actually defining the expectation that we are expecting our test to return for us and the second test is actually for retaining the balance of that user from the user object so i mean the purpose of this tutorial was to uh explaining how we can use the partial marking and when we can use it and how we can implement it in our test i hope you like this tutorial and i will see you in the next one
Info
Channel: mohammad kaab
Views: 1,091
Rating: undefined out of 5
Keywords: PHPunit, Partial Mocking, Mocking
Id: o4lfvVC9IC4
Channel Id: undefined
Length: 9min 14sec (554 seconds)
Published: Sun Sep 06 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.