SymfonyCon Madrid 2014

Comparando Prophecy con PHPUnit para crear mocks

Sarah Khalil  · 

Presentación

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

hello everyone I'm sorry I'm here to talk about a little bit about milk and no phpunit prophecy just about me a quick words i'm basically scrub my style at since your lives and i work for them for now three years and today it's very special

because it's the first time i talk about tests in front of a lot of people like that so today hopefully we are going to learn new things i have a quick question who uses phpunit whoa okay and what about PHP spec okay with prophecy okay nice okay so first

I'm going to just make a quick turn about the terminology about object dublin's just a few words you have to know because i'm going to use them a lot then we are going through PHP units implementation of the mocking library then prophecy and finally

we are going to see the differences between the two philosophies ok so the terminology first you have to know about dummies what is the me it's basically an object that has a type hint that's it you are going to use it in your code so that's it

what is a stub this time we need to configure the way a method behave in in a code that you are testing okay so first you have your dummy you have your object with the special type hint and you have the method that's basically is called whenever you want

and finally we have the milk and this time we are expecting things most of the time we expect that a method is called one two three or 0 times and that's it for the terminology with all of these words I guess you can understand what I'm going to tell

you so first we are going to talk about phpunit so PHP units is a library authored by sebastian bagman you should know because it's every time you know when we launch the test the last stable version is fourth dot 3 dot 5 and you can install it through

composer and actually it's the only way now actually we expect so some fundamentals around the the library when you test with PHP units you need to extend this class phpunit framework test case it allows you to use all the method like assert something

if you may know the method that you need to know with PHP units basically get mock okay with this one you can do anything so generation it generates an object all method returns null okay and with that you can describe the expected behavior of your objects

here is an example to get a dummy so you call the method get mock with the name space you need and you get to dummy so on the objects you need if you want to do a stub this time you are first creating your dummy then call the method method and pass the name

of your method and with the method will you will expect a certain response object a value and finally if you want to mock you add this method expects within its the expectation in here I put this one so I expect that the method dispatch is called once when

I launch my original code okay I'm going to go through a real life example and basically I'm going to do the same thing with a prophecy so you can do the the difference yeah so I'm going to show you a very difficult application it's composed

by two pages so here you can see that there is a link to connect with github and when you click on it it's basically going to github ask you from for some credentials and when you go back you are authenticated okay who knows exactly how our stance of how

it works exactly with the tub okay great we don't care actually to test them why I'm saying that it's great because lots of you don't know how to eat work so but it's okay we are going to test it to do that with symphony i'm using the

prio team tkt the token to authenticate the user but it's okay i'm going to show you some code but i don't expect you to understand all of this okay so you can see that there is a lot of use it implements some interfaces so we expect that there

is a lot of dependencies then you can see that in the constructs method we have 3d balances so an HTTP client because we will have to call github we have a router basically it's the root off from symphony and we have the log on to face the client ID a

client secret is just a string so we don't care as we won't call github in our test it's fair but ok and we have a big method here could create token there is some some cut in here but we don't really know what's going on so we are going

[ ... ]

Nota: se han omitido las otras 2.164 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.