 good morning everyone Thank you for turning up for the session like How are you doing? How is the Vienna con going on? Great, okay We're gonna talk about prophesies your PHP unit test Yeah, before we starting I would like to know how many of you guys are working with the PHP unit test Oh Great, I think you guys have a good experience. So it's very we can find out the session very easily Not gonna talk about This is great or that is bad. I mean about the you know, what is PHP? prophesies any idea What is prophecy call? Yeah, we'll talk about that before that My name is Rakesh James. I work for a small company in India. Yeah, I've been Introduced the prophecy by one of my mentor hits his right there who has in green glass in the head His name is Phenafroxma. He is Official migration maintainer as well as I have a couple of migration maintenance friends also here and While I'm working with the migration issue queues in the Baltimore He has actually introduced me about writing a test about in triple issue queue for Lock process plug-in in migration lock process plug-in how to I got a very difficult time to because it's When I'm looking at the other Classes like writing a test against the classes. There will be a chance of a lot of dependency injections and Lot of services will be included So we need to have a take care of that first then only you can write a test about the particular class. Correct Am I right or wrong? So when you're writing a test against a class Yeah so That's what I want to talk about It's a very basic session how we can do a prophecy Mocking framework with the your class So these are the things we're gonna talk about in terms of prophecy We gonna find out what is a prophecy and what is dumbest in terms of prophecy it stirs more spice and promises and predictions Any doubt start raise your hands or just ask the questions Yeah, we can move forward. It's very simple straightforward We are not taking very advanced level. It's because of it's easy once you start using it's you don't need to Be worried or so one or more do it you get to start using it So what is prophecy prophecy is very straightforward PHP mocking framework and it's actually Developed and created by our side like it's for a PHP spec and He lives in London. Yeah, then he He modified for can be used with the PHP unit I Don't know whether it's ever said is here or not never met him to anyway huge thanks to him but Want to say a prophecy is very simple you can you don't need to worry about the future behavior of the object So it's easy to you use it in your PHP unit test and it's describe your future of the objects object behavior So since this is a example are you able to read it? from the screen, okay we we're gonna write the test against a class called user rating calculator and In that class we are actually calling the method increase user a rating in that we are calling the interface user interface and In that user interface there is a tool methods are defined one is get rating Another one is set rating so in that Set rating you Having a parameter also arguments Yeah this is Below is below block. It's in the blue color. It's a prophecy testing for how you can prophesies the above classes and interface The first line of the blue line user profit profit is create create prophecies for you then You can say you are directly calling that dummy to the method and say you can return back this particular value then Next one is you predicting it Even it's this argument is past this has to be get called So when you're writing a test the third block it says if you put it in your test class you're gonna Create the new object of the class then You're gonna directly calling that method and say reveal the dummy Then that's why it's a test work So gonna check out what is dummy what is done in detail That's one before that. We need to find out how to install this one. You need a more than 5.3 PHP installed I think hope all of you guys how using PHP start using PHP 7 now How many of you start PHP 7 now? Yes, pretty good now and Composer Anyone anyone doesn't have any awareness about come closer. What is it? Everybody knows come closer. What it is Okay Compose you okay Composer is you Composer help is to add you the dependency injections I mean not dependency injection dependent is from the outside of the world like where it is present somewhere else in the package managers so I Will tell you in one example Interpolate you use address module in triple seven right address module address module so we have address module in triple eight, but Commerce guys actually create the address modules the triple eight one using the outside address libraries, so any part of the Part of the side world you can get the package manager It's not from only from the triple so you can add the packages libraries from outside of triple So other PHP it's there is a concept called moving away from the island So we are trying to talk each other with the other PHP communities So combustor is one one of the best way of we can achieve that To our Drupal So Composer is more than you can need to understand it's more over like you can add dependencies so you are the compressor.json file for your module and you write a Composer installed it's Installing all the other modules whatever you need it Anybody understand what is vendor directory in Drupal 8? vendor directory So you it's a vendor directory comes from the symphony components place in the other place. It's not from the Drupal It's from symphony, right? So all of the libraries we getting from symphony It's using Composer actually. Composer update. Yesterday Dries was talking about the auto-option in the future It's mostly gonna work with the Composer Okay, no problem. You have a Composer.json file you need to get Composer installed. Add the few lines required now and PHP probably see what is a version you need to the Composer.json file Then you can run this command Composer installed prefer Distribution so it will automatically get installed only one requirement more than 5.3 PHP has to be 5.3 more than So how to use it So first we need to create a profit for it Once you create profit you can create a profit you can prophecy from do that with that profit Then we you can say which class you need to be extended or which interface you need to be marked or extended from that in that prophecy But in Drupal is already included. So you don't need to check anything. You can directly Say that extra dollar secured this prophecy has to be get revealed So I'll explain this what is revealed and what they would Okay, first thing is what is dumb is The the variable you're getting it. It's actually prophesies from the user interface This is our code So this is the user. This is called dummy Actually, it's profit and prophesying this interfacing So in In dummies, you don't need to worry about it's always return nulls You don't need to worry about it's like what it does or is it throw an exception? It won't go into throw an exception and it doesn't have any logic at all. Just You creating you gonna you gonna mock like you gonna create an object dummy object for a particular class or particular interface That's what all about dummies that variable This user is the variable. It's user doesn't know what it interface does or anything Create an object of the user interface Yeah, then stops Stubs is object double All of you I think he knows what is doubles in PHP unit It's it's it's a it's a dummy object. Actually, it's reflects like the original class You guys mocking the object, right? So that's you created double of that one You're getting original image like the same object class that is called doubles But stops is we're different from mocks because some of the time you when you having the mocking object You need to really care about what all the Things included in that class what all the dependency injection is happened in that that class and what all the interface You are using in that class and what all the services you are using in that class So if you go into the user entity, you can see that if you want to do it There is a lot of interfaces that you're calling and you're a lot of services You are calling into that particular class and if you're writing about Test against that particular user class you need to worry about all this But in prophecy gives an option to just stop you stubs and You only create a dummy object and think about only this Method has to be called on this class. So that is what dummy does dummies that does this Creating an object of user interface user User object and getting it will return to so you say You have a great great great rating method Man, it always return to value 2 That's what it's called stuffs Anyone have any questions till now dummies and stuffs? Okay so That is stuffs and it won't it won't check about the exception behavior of your object It it it won't check about what how the object has to be real object is behaving it it is not caring about it so you can see You can see the you created the prophecy dummy called user and prophet prophesies the user interface Then you are saying user you calling this method sorry this method and Saying that it always return to So it's it doesn't care about what is that functionality does or that method is when it's Defining it's What it does, but it always does you are saying I have a dummy I have I mock this object and I am using that directly using this method and when you using a method Method you will always return. It's always written to The value will be written to That is what is called stuffs stuffs actually promise Promise promise means you can see the will return We call will return to that means you promise that when you calling this get rating method Or when you calling this particular method you promise that this will return only the particular Particular value in simple ways like So when you call this particular Method you say this has to be returned this particular and say we promise it this will return only this to value called to that is called promise promise or It's a logical blocks And there is a lot of different types of promises that exist One is called return promises then return argument promises and throwing promises and callback promises What is first one is return promises? So this will use this this is how you need to use you need to use will return one so you say when you calling this particular method this will return only one and There is there is you can return a particular argument and you can this will return arguments and Which is the mother for the which method you need to argument you need to return and Throw promise it will help you to throw it when you call that particular method It will help you to throw the exception so if if I return he If I return a throw exception here The Venice user get rating is called it will run exception That is called That is called throw exceptions same way callback promises So if you want to have a when the particular method is called calling you need to write your own logic for that particular like For that method you can you can use will callback promise and Callback promise in that you need to give you the function function name as an argument So it will get that method will be getting called So prophecy help you to Overwrite the methods if in the interface and use your own logic for that your own test so When you having a writing a test for a such a big classes you You will be get mad about like about the what all the interface what all the? What you call what what all the servicing dependency injection services are using for that But because which is not relevant to you for your test actually you know this has to become and this class has to be behave like this for that behavior only you're writing the test so Professor will allow us to promise In that particular method what all the things you need to be returned So this is called different types of promises. It will there is a four types of one is Return promises and one is written argument promises Then we moving forward to mocks All of you will have a mock mocking object rate in the PHP unit it is it's very close to the steps but it's it's always care about your Behavior of your actual method functionality actual method behavior. So Here we using the promise Set rating is for should be called so Actually, how this test will work in this you actually Creating a prophet and when the prophet will allow you to prophesize the user interface Then you are saying that when you calling the method get rating it has to return to and you are telling that is a promise you did then Then you say telling that when this Set rating is for Then it will be get called And when you come here you can see that you Mocking that you you creating an object from that and you calling this functionality then you say user reveal user reveal means that is called this prophecy and When it's called it is happening here actually your user reveal you you call this prophecy The prophecy come here, then you're going to user settings then Here you're gonna call the user get ratings that means you're gonna call this one here so here you will get it to and you're passing to from here and This entire user settings ratings Functionality actually gets for if it's this get for then you'll be called this functionality So that's how it's work That's our test works when you're doing the test So this is called predictions You say it's a settings are For then it should be cold This is what called predictions in prophecy I'm talking about all the prophecy times. What are the things how we doing it? So this means in this set ratings you'll get an argument called for Then only it should be called So you talking about the behavior of particular method and you're concerned about that and telling that it has to be get called If if there is a five comes it won't get called Instead of four So that means you are taking care of the you're gonna throw the exception you're gonna taking care of the How we should the method behave that is called predictions Yeah, and when the be when you talking about the behavior, it's called mocking the object So if the if the prediction is failed It's throws an exception We have a couple of different types of prediction you can use You can use should be called prediction When this is happening we can we can write set ratings for then should be called and if Should not be called this functionality and you can say should be called some times when it has to be called and You can say call back predictions also. I think everybody silent. I don't know whether you guys are understanding But I'm sure that's when after this you're going to start calling it. It will definitely help you So there are four types of Predictions are there can be should be called should not be called Should be called times when it has to be called as then Should return a call back. So you can say if it is coming for You can put a should call back one separate functionality Then you can actually overwrite that particular method or functionality So it's help you lot of leverage and a lot of freedom to write your own test according to the requirement so When actual where it help us When you have a lot of dependency and interface has to be mocked in a particular class There is a lot of things you need to take care of before When you while you're writing the particular test for that particular PHP class so Prophecy will allow you to easily do that overcome that Very simple way Then it spies so before Before going to that I will explain once again this code, please so while you mocking in the PHP unit, I mean, it's it's fine for me It's I'm not it's almost saying whether you which or you want to use it you can use it But what I'm telling when I try to use it But I found a lot of things I need to take care of while you mocking the class real class No, no, no, it's not like that You can you can use I just return the same way you need to understand so so what I'm telling You can use prophecy or you can use mock but prophecy gives you more leverage to overwrite The certain functionality you don't need to you don't want it in your test actually so you need to take while you're mocking a class have a Five interface is already using five services using five dependency injections. Maybe yeah So those time you need to really take care of it all those first Then you need to those interface you what all the things you need to be okay, but here what you did Just say the interface is there revealed it and you say this has to be written So it's clearly written here. You're you're very easy to do this one No, it's it's always care about the mocks. It always care of the exceptions and behaviors of the object So while we having dummies dummies really don't care about so this is a Class so we gonna We gonna do one second. So here you're calling the Predictions and here is the promises. Have you able to understand this code? Any questions of what this code so once again I explain you we have a interface We have a user interface and We are having two mothers in that That interface is called in other class Inside the method of other class So I we gonna write the test for the user rating class your user rating calculator class so We gonna while we're doing this before that we know that there is interface is called so we gonna create a prophecy for that because While you're writing test You need to know what all the things happen this when the user interface user called So we gonna prophesize that so when this is called This has to be happen like this So we say that we prophesize the user interface and say that if there is a method in this method calls will return this first get rating will return to and If it is the second set rating method getting F argument for then only it will get called so While you're writing the test for Cal I mean a new user rating calculator class You go you create an object and then you call the mothered Inside the class that accept the user interface as a pair of arguments with the one separate argument so in that user interface we already mocked before I mean like prophesized before as three lines in the top You getting it all this prophecy as a user reveal you just revealing it there and say you getting everything there and This will go here then you say gate rating then gate rating always return to and Once you return to it's a two plus two is four if it is for this Set rating will be called. I mean set rating will be set So this is a straightforward example for this and one last thing I want to say it's a prophecy can spy Spy What prophecy help us to do when you creating a dummy it will create it will log all the records what you're doing with your dummy So whenever you want to use this dummy you can use it So you can check whether it is called or not You The particular function is called or not Yes, it's okay. Just keep it like that while you start writing it. I'm sure that's very easy to understand So you you need to know some of the areas whether this particular functionality is called or not So you trying to mock the object or you're trying to create a unit test and you say whether it is called before You're writing thousand and hundreds of lines Then then you need to find out whether it's called before this functionality called or not So it's easy to check should have been called Or not So then you can write from there. So you easy to spy it out It's it is also there in PHP other mockery also So this is an example any questions now Yeah, so why you you don't need to do it right you just you calling it in a same line everything So you have a instead there is another method also is in interface two methods are there in interface So you're revealing and their interface there So you took you took him out this one instead of reveal You want to use get rating? It won't work. So you need to you need to reveal the entire interface. You are dummy then only it will work Yeah, no, you need to You need to reveal it first then use you get the Mockery you are dummy with the the certain functionality. What are the things you return prophecy or prediction in that? You prophesied your prophecy. You have to be revealed first. Then only they are able to use it Yeah, okay any other questions This all about the predictions and prophecy I think it's you can start using it here is the github. Yeah Yeah, please So it's up to your choice right like so it's a what is your requirement? How you want to test it? It's you can call whenever you want it. I think yes Adam we can call another callback and in that callback function. We can use the other yeah But you cannot explicitly call that function in the class, but you can you will be getting the Other dummies if you're using say different dummies you can use it Because dummies always get full in their lifetime dummies has a full record So you you're telling me you created dummy called user and you you get another dummy called user one Yes. Yeah, you put another call back there. Yeah, you can straightly call here itself You can straight directly call here Well, you you want to use it there. That's fine. You can work. Yeah, you can you can use it. It will work. Yeah Any other questions? Okay. Thank you. That's it. I Want I want to do special thanks to my mentor. He introduced me one triple called Baltimore then I also have colleague also Help me to understand prophecy Thank you, it's easy to use You can use it for your PHP unit test. Thank you