 Good morning. I hope everybody's doing all right So we're gonna start but before starting just to get an idea of Everybody in the room. Who's done unit testing before? Okay, good amount of you and Who here is looking for? unit testing with PHP What about JavaScript? Okay, cool because we're gonna cover both of them today as well. So Before we start We'd like you to just go to this URL. Did you tweet it or? You should Yeah, yeah Just go to this URL it'll send you to the GitHub repository Where all the code for today is going to be so all that you need to do is clone the repo locally on Your computer or just you can download the zip file And yeah, if anybody has any issues during anything just raise your hand will one of the TAs or will come and help you out Does everyone I have the website open so kind of go to the next slide because I mean at the food room I have no idea if you can read it. There's the URL throughout the course. Yes. Yeah I Hope that you read the requirements part of I have no idea if you got an email or on the course description So you can do PHP or JavaScript and you need PHP locally And you need some kind of an editor ID whatever you want to edit some text possible And you need Composer because we use this to set up to orchestrate our dependencies like the testing framework and other libraries And for JavaScript, you need note which Yeah, yeah You need note which brings you MPM, but if you prefer to use yarn, yeah, this is up to you And if you clone through repository, then yeah, you can decide for a PHP and or JavaScript And this is what you need to get all the dependencies up and running And it would be great to just briefly erase your hand if you're done with that throughout What we're talking like that you see that yeah when we're starting with the first exercise But actually at least three fourth of you have set it up. Okay, then Okay Okay, so let's get back to the actual introduction about testing Yeah Hello, what is testing? Well When people talk about testing Many confused unit testing with testing testing. It's a very broad concept It is about everything that is Ensured that is made to ensure that the product is right So there is no defect in the product But also everything that is made to make sure that we are Building the right product so that this the requirements of the product is Exactly what the product does just to make a funny example This will be a test Because one of the two picture is the requirement so one client has that to do the This thing and the other is the final product now In There are two different there are five difference in the picture because It seems that we did a test will look at we spent time looking at the two picture And we found out that there are five different so the requirements are not met So this is a test Even if it does not involve code But it's a test because we ensure that their requirements are equal to the final product Yeah, then and if you know what it has to say we can think about why you should concern yourself with testing So why do we want what should we test? There are a couple reasons so No matter what you do next one You the software that you produce contains defects no matter how good you are how much time you spent Concepting and writing code and testing a set of reviewing code their defects and they obviously hide So if you know they are there you would get something do something about it If you have defects, these will cost failures like the product does not behave like you want it to be Depending on what you do this could cost money and even kill people I mean not with a weapons plugin maybe but in general It's if you test you get more confidence in your code and also I Testing itself does not improve quality It tells you about the current quality of your codes And you know where are issues that you have to solve and then you improve the code quality And also if you do testing right it takes a whole amount of time to set things up to understand things But if you do this right and if you get started then this will accelerate accelerate your software development process One real example does anyone know who this what this is or what this was Yeah, exactly this was a rocket You can slowly step through so this is a rocket and the test some software taken from the previous rocket Untested Everything else has been tested component by component integrative tests and so on this was great But there was differences between the two rockets and that resulted in some floating points issue There was some bug in the code which yeah produced a trigger the failure And then there was an exception handler, which is great But that was for whatever reason disabled which not that good And then about 40 seconds or so I think Yeah, the rockets destroyed itself and they yeah, that's worked out. So successfully All right, so we're gonna talk about testable code as well The idea with Whenever you start with unit testing one of the common things that you might be wondering or Struggle with is the idea of What what can I test? What what are the things that are gonna like you might feel like oh my code's not testable But there's always something that you can test you can test for return values of a function the output or other side effects and Those are really what Testing is about testing isn't really about Figuring out necessarily is my code working right now. It's That well depends how you do it, but we're not gonna get into that but what really you're you're trying to figure out is You're trying to be good for like I like to talk about future Carl so you want to be nice to future you and be oh I did this change and I broke something But I don't remember what I was thinking back then well the tests are there to help you prevent those situations and Catch these errors that you might have not intentionally done and that would break your code or co-op cause crashes or things like that and That's really what testing is about. It's about Making sure that the quality of your of your code stays consistent and that's what testable code is about if All code is testable in some way or form Obviously some of it is better to do than others But at the end of the day, there's always something for you to test and to make sure that in the future You don't break it and and cause yourself Headaches and problems trying to fix it and figure out what you were thinking back then the test is there to I'm kind of like documentation Who here writes documentation? Yeah, so that's Is it up to date your documentation Yeah, so that's kind of the idea also with documentation documentation isn't there for right now right now like you're writing the code And you know exactly what you're doing the documentation is there for like future you to be like what was I thinking back then? And testing is a bit the same way. It's a way to document your top process and catch mistakes As well, so that's kind of the context with unit testing Okay, so we had testing in general and now the switch to unit testing So before we actually start with what unit testing is we want to arrange it in the greater picture, so We start with actually where unit testing is So there is just the next there is an overview here So this is just one way to design your software development process It's a nice overview because you have different stages in the development Which does not mean that you have to go through the stages from top left to bottom and cannot return. This is wrong, of course And unit testing is here And you see unit testing concerns itself with the design of the unit of the modules of the components and You actually test the implementation And you can build on top of unit tests integration tests You can build pretty slim integration tests and only test actually parts where the integration is concerned because you had Unit tests before and ensure that every unit by itself is working as a truth So when you have integration Interactions between units you only have to test that in the next level If you only take one thing from today this slide should be it So one one more unit testing is dynamic testing of individual units in isolation And I will step through these three concepts one after the other so Dynamic testing what does it mean? Dynamic testing means that we don't read our code like a static analysis tool We actually run the code When we have defects they trigger payers We want to find the defects we want to debug we want to fix the box and then we improved our software quality Unit testing is about validation So this actually means what does if I mentioned before you can step on through and so this is just the definition You want to make sure that this the requirements that you have so that your implementation actually matches what you should implement and also unit testing is white box testing this means that You have to know the code because you test the finest detail level of code So you have to have the code and not just know what the function does So you really have to read through the code line by line and then write a test for that so One simple example if this is a function we have php and JavaScript code and you see here It is not obvious what language this is So if this is a function that we want to test then of course we have some kind of expectation you remember the two images if we Want to test if something is returning correctly or behaving correctly and of course we have to know what The expected correct result should be so this simple example this function returns a string 42 And this is what we can expect if we call the function. We want to have the return value string 42 cool, so Let's talk about defining units of code so Really what you're trying to do Who here comes from I love this analogy But who here comes from a bit of an engineering background like electrical Who's done like kind of the breadboards where you like put a circuit on one hand and Like test currents so unit testing to me is a lot like that What you're trying to figure out is like your code at the smallest individual levels so You're trying to figure out okay if I put this these variables in what do I expect to come out at the other end and So things that can be like at the atomic level are like words if statements Logical by any sort of logical condition, so are you trying to figure out? Okay? Well if this is true I would expect this to happen and will this will this happen and Those are kind of the reasonable Units or what you're trying to do with unit testing. You're really trying to figure out in the perfect Isolation how your code is going to behave at the smallest possible level So you're always at the at the function level or or things like that. It's so this is a good example That this is a good example Logic here, we're just trying to say okay. Well if a number is greater than zero. We want to multiply it By two but otherwise you want to divide it by two and a Unit here is is really one fraction of this function. So you want to test Whether if I send a number greater than zero it's going to go a certain way and if you send a number Less than zero will it behave a different way and that's really what the units are about Yeah, the problem with this is the third concept that we mentioned like the isolation part So you can test logic here the problem is if you have either the first if true or false You also have the other mathematical expression being included. This is also logic So you cannot test only the condition in the if or only the computation in the first return or the second return So this leads us to not having a logic as a reasonable unit So could also be a function or a class So this is still up to you if you decide what function one method is my unit or the whole components the class The object is my method. This is up to you. This is just fine Yeah, we say unit testing is dynamic testing, but it's also testing in isolation So what isolation mean isolation means that? When we When a test phase so unit testing is about fine Bugs or find the facts in software, but when a test fail because by show this Show themselves as failures when a test fail we need to know Which is the reason because if a test fail, but we don't know why then we don't know how What we need to fix so? Yeah, look took this sample then if this this test You will get confident with the sun syntax if you are not already We are testing a lot of things So basically this is work. Let's go. They are familiar with that, but we are testing that A taxonomy exist a taxonomies, which is our end of string we are testing that this taxonomy is your article and we are Also doing some global manipulation now. Let's imagine that This test fail It's not that we can really understand if the reason of fail But also the problem is that we are using work as code. So Yeah, the problem with the test is that we are using work as console if the test fail It's because of our code or because of word press it could be about in workers. So we don't really know if the The test is Fading because there is a bug in the press because there is a bug in the test because in the back in our phone So that's in isolation means that when we write code we try to run only our code So when the test fail, we know the reason why it fails. So Yeah, it's It's important when we write the code that we write good code that is Easily to settle in isolation Okay, now that we know What is testing why we should do it? Of course, we want to know how to do this right like what to test how to write a test So what makes the test good? There's a great article by Eric Elliott which highlights five questions that every unit component or module test should answer The five questions are what is it that you're testing then what should it do? Like if you're testing a component that that might have different methods So what exactly is the thing that you're testing here? And then you of course have this expectation like does it return a value or does it output something or does it have some other side effect like? Manipulate some global variable or so and then of course if we run the code We have the actual results like okay. This is what I expect, but this is what I got And then the fifth thing is how can I reproduce this? So we said before testing or a test is some sort of documentation Which is only good if you know not what happens, but also how you can reproduce this and for unit test this is simple because We have two templates. This is a PSP template So we have the what is spread over the what class and then the what method And then we have what should it do like here is in the name. So my Whatever object when I call this function. It should do this and that And here we have the expected value if we get some value We have the actual value and then we have some research and so because they should have some relationship They should be the same. They should equal They should be one thing should be contained in the other or whatever and now I'm just yeah I control this here. This is the JavaScript equivalent before we have phd units. This is using just The fifth question or the answer to the first question. How can you reproduce this? This is actually in the actual right side. So however You created the actual results and maybe also all the lines before if you needed to set up things This is how you reproduce this so you call one function This is the unit that you want to test and maybe you have to do some preparation. All of this is how you can reproduce the test all right Who did set up all the things that they think they should have or maybe who would it not who has still problems setting things up Like composer install can anyone else Problems in yeah Gary Okay, so we're just gonna fix make sure everybody's on but we're gonna start with with with the exercises so The way we have it structured is a bit kind of a game. So we're gonna go with level one You're gonna be given broken code that that you're gonna have to fix so the goal here is to Really familiarize yourself with the testing frameworks, whether you use whether you want to learn about JavaScript with jest or or PHP with PHP unit and You want to be able to interpret to figure out what is the testing output telling you about these errors and You want to debug and fix that code And this is important. So the first exercise you only read test code You don't write test code and you don't change something in the test the tests are correct But the failure is because the actual production code. There's some issue with it Okay, so you will write test code later from access has to one But this is only reading the test reading the output and then trying to fix the actual collection code Yeah, and really To understand what I was talking about earlier, which is thinking about your future self or What somebody else I didn't even Talk about that, but you might also be required to fix code that somebody else wrote and Sometimes it can be hard to understand what somebody was doing. That's why documentation is important But also that's why testing is important because the test will tell you what the person was expecting to happen and You can make sure that this happens again or you can question whether this should have been the case in the first place Is everybody Ready to go I see I may still Helping but if you want to do the The first exercise with PHP you can run this command and This is the command to run for JavaScript and You'll find the tests here Yeah, yeah, just a second No, no, that's fine. That's my bad Yeah, that's shit Shit Okay, there's the code that you have to run and this is also what you should be able to see in the readme of the repository So maybe have this open. Yeah, there's a readme also in the repository. I will tell you How to run both these tests also with documentation references to documentation Which we also have included in some of the test files, but the readme is hopefully helpful So what you should do if what you should see Is everybody ready for us to go forward or is anybody not ready for us Negative we'll go the opposite. Okay We'll wait a bit But what you should be seeing and we have slides to show this in a bit But right now you should just be seeing a lot of red Because the idea here is that we've given you code that actually is written tests that are written and The idea here is to for you to To be able to read what the testing framework is telling you about that code So that you can go and fix it Yes, so again, this is to be seen at the readme. So we can move on now I guess so this is where you can find the The test code for the PHP exercise So this is what you should be Yeah, sure. I made this mess actually. Yeah, so you can blame me for everything So this is what you're gonna see With the PHP code if you run this the command that we saw just now and We're just gonna step through the first test and and see what's what's happening here. So The goal of the test was to Figure out Wander so what we're doing with this with this code is that we want to add a special error code So who's done custom errors on the login page? so the idea here is that we're trying to add this custom error code on For the login page and make sure that it it's the filter works and as the code as as needed so You can so the first step is we have an empty array that we pass to our filter and What we're saying is that we want to assert that In the array there is the workshop Error code after So what we did Okay, so that's the current code. Yeah, okay and so this is our error code right now and You might notice that it's we added a typo so It shouldn't have been workshops it should have been workshop and If you add this and you remove the s you'll see that now One of your tests is passing again. Yeah, now we have also a JavaScript example For JavaScript because we have JavaScript classes JavaScript waters. We have sometimes two or three files So, okay, can you go one slide back? I took an example from Just just arrow up What once more so from this file? I took one test as an example and Not a second. So this is what we can see in the terminal as the problem Maybe you're able to spot it here, but let's have a look at the test. So the test Takes Something it comes from wherever and then we just pick up some property And then we want that property to be that string and this fails because this is how the actual code looks like And yeah, there's a typo. So it's not Lauren Ipsum. It's Lauren Ipsum If you fix that then this test should not fail any longer Yeah, and now it's up to you so head over to exercise one for PHP or exercise one for JavaScript and I'm I think we should give you 15 maybe 20 minutes or 50 minutes Yeah, yeah if you have problems or questions or questions comments, whatever we threw out just ask for TAs or us so And have fun with the first exercise. I should also mention those instructions inside the exercise file at the top as well We put a lot of documentation there. So if you have Still questions after that you can wait by the TA or at us If you have any questions at any time you can just raise your hand and we'll come and see Yes Yeah, but just just as a note we we only have these three Expansion cords unfortunately for for power So if you if your laptop's already low or you want to charge it you should do it now in case somebody else later needs it Oh because Gary just mentioned it, it's a failure on my part. The goal here is to fix the production code, not to fix the test. So the error is really in the production code. And that should be, in general, what you should expect from testing, like you should expect that the test is the one that's right and what you wrote is wrong. So I just wanted to clarify that because that was actually a really good question. . . . . . minutes left. I just wanted to see who's done, if anybody's done so far with the exercises. It's still glittery at point. And the function name. Glittery at point. You can put this in the structure. Yeah. In workshop function. Ah. Yeah. I have to do that. Yeah, either this or you can press play. Then, um, flag. Like, that you don't care about pasting on. But I would just make this. The uppercut value is. On the one. And here. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Um, you. I don't think that we have time to do this. The problem is actually. That's not a type. This is specific to the function. So in common words. We use explode on a string. And if you have an empty string. Explode will return an array. With the empty string inside. Which is wrong. So in our. If you pass an empty string. The result would be one word. Which is the empty string. Which is not what we want. So in the fraction code. Make this edge case. Like you test if the string is empty. Then you directly return zero. And if you have anything inside the string. You can just explode and count the words. So it's really a more complex. Because it's the last one. So we already have just. You didn't really think through the edge cases. Of this case. Or it could also be some of the party function. Yeah. Yeah. Yeah. Really? Yeah. Okay. So that's the way. We go. Okay. Okay. Okay. Okay. Okay. Okay. No. Okay. Okay. That was. Yeah. Yeah. Yeah. Yeah. Okay. So generally, if you minister to the state as well, that would be very much true. So we're out of time for this first exercise, but before we proceed I just wanted to talk a bit about what, again, what the goal was here and ask if anybody asked questions as well. But really the goal here was really to make you familiar with the testing frameworks that we're going to use throughout the rest of this workshop and to understand what that testing framework is telling you about tests that are not working so that you can fix, you can figure out what broke it and how to fix it. So before we move on, I wanted to see if anybody had questions about this first exercise that maybe the TAs didn't answer or that you wanted asked so that everybody could hear. Please also know that you can just come and find us today or tomorrow also, like you don't have to ask questions here and then there's no chance this is really wrong. So we're here. Yeah, we're all on Twitter. They're all written at the bottom as well. So even after work camp you're up is done. You can always, we're always around for questions. So without further ado, we're going to move on to the next exercise. So now that we're a bit more familiar with PHP unit or just what we want to do now is familiarize yourself with what we call assertions. So assertions is just a term that is used in testing to say I expect this. And what you want to do here is we're going to give you partially written tests where some of the assertions are missing. So what you want to be, no, keep it back a bit, just a bit more, but what you want to really understand here is you want to be able to look at the code that you have and figure out what am I trying to, when we were talking earlier about testable code, you want to figure out what am I trying to test here? What are the outcomes out of this code that I want to make sure do not break in the future because that's what the goal of an assertion is, is to make sure, for example, that if a method returns true or false, that I expect this to be true. And that's what the assertion means. So there's various different types of assertions. So you can test whether true or false are simple ones, whether it's null, but you can even go more complicated things like whether an array contains a specific key or things like that. So the goal here is to familiarize yourself with all these different types of assertions and to create the ones that are missing. The documentation also. Yeah, so we mentioned, so with PHP, we link to assertions. There's also links to documentation inside the exert exercises as well. So now you can move on. So to run your test similar as before, you just want to run with the test suite exercise two or just with exercise two. And we're just going to walk through the first examples. So here we are testing that we want to make sure that our plugin removes the ability to update, removes the capability of updating plugins from a user. And we're missing an assertion here. So what are we expecting here if you move on to the next slide? So if you look at the code here, we're calling unset twice. So we're obviously in the first example we were looking at install plugins. Now we're looking at update plugins. So what are we expecting after that is that if you move on? So what we're looking here, and that's what I was talking about about the keys, what we're making sure here is that we want the update plugins key in our array to be gone. That's what tells WordPress that you can update a plugin that the user has that capability. So for our code to work correctly, that key needs to be gone from the array. So to test that, we are calling the assert array not has key, which is a bit of a mouthful, but that's testing frameworks for you. But what this does is that it checks the caps array that we get back from our method, and it ensures that that caps array does not have the update plugins key. And this is just one way to write the test. So you could also assert false and then check is set or something else. Yeah, it's one way to do this. Now we have the JavaScript part. And again, I just picked one file. So in the access as two folder, there are three or four files. I picked the array test for now. This is the current test. So we call map objects to property with data, which is string. We pass the property name, which is children. And then we expect that the actual thing, so the return value of our function call, is to be what? So let's have a look at the code. We have a function that takes any argument, and if this happens to be an array, it tries to reduce or map any items in the array that are objects and have a specific property to the value of that property. So in our case, we don't pass the first test. The first condition, because if we pass the string that we had before, this is not an array. So this should return data, the first argument, just as we passed it. So this would be the complete test. So we call the function with something that is not an array. So we expect that to be returned as is. And now it's up to you. Exercise two. Yeah, so we'll stop at half past 11. So you have about 20, a bit more than 20 minutes to work on this. So we're going to explode the string in the space that we directed. That won't explode in just half the space, I think. So we'll put one on top of that, and we'll sort of do it like this. Yeah, that first. That way, then we'll pass the string. We have to do that in space. And then in between the two. Yeah, that's it. If you trim it there, then it's in that way. Yeah, you got it. Okay, so apparently you can't work with it. That should return the one. What, in this case, needs the function toy. It's basically an if. Empty text. Return zero. If not, return counter explode. Like that. So in this case, we're just trying to make the test pass. We'll get there. That's not what the test should be. But it would be enough to make the test pass in space. Yeah. There's a typo in the first exercise in the first problem. It says every day and then in the message it says every minute. So just be aware that it's every day for just the message. And it's not. So what is the actual. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Let me know. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. There's going to be a break after this exercise. So don't worry about that. If you want to go get some water or go to bathroom or things like that. Just a quick show of hands. Who's done with this exercise? Okay. Does anybody want more than five more minutes before to finish this? Okay. Then there's still five more minutes. And then after that we're going to move on to the next exercise. We'll just present it. And then we're going to give you more time to do that exercise as well so that you can go to the bathroom, take your break, come back and work on it as well. Yeah. Yeah. Exactly. Yeah. There's a second test similar to this one. So we want to check that. Yeah. Yeah. That has to be for the end of the build. So it's similar to the exercise. The idea with this test is for the end of the build. So you go to the exercise. Yeah. It's very similar to that one. We'll check the end of the effort. We'll check that there's interval. So that's the end of the build. So it's a lot of work. Yeah. Yeah. So it's really meant to be so that you can see a bit what you're missing from the build. Yeah. Yeah. Yeah. Yeah. I'm not sure why I'm not in the picture. So I'm going to use this one. I'm going to use the number 3. Yeah, and then there's the other one. And then watch it. It's like just white. It's not like white. It's a very strange one. I don't know. I mean, white. I don't know. You could still look up and move over. Yeah. There are two words. It's not a game. There's one in the room. You try to think about the edge case. So if something here for six words there's a very low probability it's going to be great for seven words. But if something works for one word It might still look good for two words, because it's just the third internal proper rate, which is the terms of first element. It's a difference between one element and two. It's a very fundamental one in terms of working. More important than the difference between six and seven, because once you know that multiple words will be returned, then it will probably work for all the numbers until the next three electricates. Okay, so we're going to quickly wrap up this exercise and present the next one. And then after that we're going to give you a much larger amount of time for the next exercise so that you can get up, stretch your legs, go to the bathroom, and whatever you need. So just to go back to recap of it, what this exercise was about, the idea here was to take what we had in the first exercise and make you realize what assertions do, but as well be able to look at code and understand what needs to be tested. So what are we trying to validate? So if we take, for example, the first exercise after with the every minute, was it every minute or every day? Well, whichever it was. The two missing assertions here was to check one that we had the interval key inside the array and then that the value was correct because in this situation one, two things can happen that can break around this. One, somebody could make a typo around the array key. And second, you need that value to stay consistent. So you, every day is always this amount of seconds and this is never going to change. So if somebody, yeah, until physics tells us otherwise, that's not going to change unless you have at least seconds. But that's another problem entirely. But that's the idea here is to make sure that this is okay. So the goal here was really to look at your code to realize that one, you needed that key to be there and two, that this key needed that specific value. So that was the idea with this exercise. So we're going to move on to exercise three. So now we introduce dependencies. So what we had before in our code is we had one unit and we tested one aspect of the unit. Maybe it had multiple aspects or maybe you only have one. And now we have dependencies. So we have to make sure that we take these dependencies because we only want to test our unit in isolation. That was the second and third concept of human testing. So we want to define dummy pieces of code like functions or objects that we can control. So we still have to make these dependencies available because otherwise PHP parser will fail or whatever. But we want to make them do what we want them to do, which is not what the actual thing does, but something that makes sense for our test. So we have to take care of two things. We want no errors because our code references things like functions or classes that are not there. So we have to define them somewhere so that our language setup is as it should be. And we want all other units, that our unit that we want to test the subject under tests, that it uses to be fake. So the learning objectives here are, they are mocking tools. Depending on what testing framework you're using, you can mock already with your framework or you want or have to use an additional library. Also, you have to understand what you have to mock and how to mock things. Like if you have control of something, it's up to you to make it do what you want to do. And sometimes this is completely not what the actual code looks like, but it's the simplest thing that makes sense for your test. And also, of course, you have to create these mocks now. So we still have incomplete test code for you so that you see what we set up. There is maybe an assertion, maybe not. Maybe the assertion is incomplete. Maybe we also created parts of mocking something and you have to complete that. Running the test is just like before. So now we have to exercise three. And here's your PHP example. Yeah, this is something that you will find in exercise three. So you see here something new, which is this monkey function when and just retire. So this is a tool in PHP that allows you to mock the function of WordPress because maybe you see that in the test you were seeing in exercise one and two there were no WordPress function. It was all about PHP. There was no WordPress function. But now in exercise three, which will deal with pretty much real world code in WordPress, you will see, of course, WordPress function. And the problem with this is that we are not loading WordPress. So if WordPress is not loaded and we are going to use WordPress function, of course, PHP unit will complain and define the function and will do a parallel error. So we need a way to define this function to do what we want to do, as Thorsten said before. So monkey, brain monkey is a tool in PHP that allows you to do that. So basically this is one example of the code you will find, the production code you find. So you will find get user data that is a WordPress function. You will find get user team member page, which is a custom function that makes use of WordPress code. And you also find WP delete post. This function are not defined and WordPress is not loaded. So we need a way to fake them. So to tell our PHP framework, so PHP unit, that what this function does. Yeah, again, in this test, we see that we are telling via monkey function when this function is called, then we just return true. And then we assert, as we saw before, that calling the delete member page with 42 will return 1. But if we go back and if get user data return 0, return 1, sorry, then this will pass because this will not mention. And then we have this function that is defined. No, it's not defined. So the test will play. What we need to do is, we have a client with us. So basically what we need to do is to tell the task that when this function is called, we should return no. At that point, if this function just return no, then this assertion will be true because if we go back to the production code, when this function return no, this condition is met and so return 1 is met. So this is about mocking. Mocking is about faking the function that is not defined to make the condition that we want so the test pass. So we are simulating the behavior of WordPress or it could be another third party library which is not included in our code and we make the test pass. This way we are only testing our code and not WordPress code, not their party code. We have the same for JavaScript and Dustin will introduce to that. So for JavaScript I chose the index test.js file. The test, the incomplete test which I picked looks like this. So we started always before with describe and test. So we take something from the global scope that we have mocked for you already so you don't have to take care about that. It's been included in the test file at the top. We pick a function then we want to make sure that this thing, the register bug type, has been called with something because this is what happens when we include this, the index file. So it directly runs. There's no function to execute if you include this file then something happens directly. So what we need to do here is because if you go to the actual code, so we have our blocks, we have, this is group work code, we have two blocks, Lauren Ipsum and a progress block and they live in different files and we import them into here. This is a dependency. We don't want them to be real because maybe things will change over time which makes our test break which is not directly related to them. Tests should only break if the unit you're testing changes. In the production code we take this register bug type function and then we pass an array of the two blocks and then we call register bug type with the name and the settings property of the two blocks. So if you go back to the, so what is missing here is this shouldn't have been called with two things. I just test for one block here. So I'm mocking one block. This here is mocking with jest itself. So we don't need an additional library here. So what this means is if in our production code we find somewhere an import of this piece of path, ipath and then we just run a function. And this function will return a plain object with two properties, name and settings. And I made them up. Unfortunately you cannot pass in variables because this is how jest is defined. So I cannot declare a constant name whatever and use it here and also use it there. For this example I have to duplicate the hard coded values. So if you now I'm going to the complete example I see that the name of the lorem is lorem-ipsum and that the settings is an object with the lorem, key and ipsum value. So this is exactly what I expect this register block type to be called with. This does not mean that it's only being called one time with this set of data and it's also not ensuring that this is the first time it's being called with. It just ensures this function has been called at least once with this set of arguments. And now we have... So it's 1140. We're going to keep going until... 1215? 1215. 1215. So you can work on the exercise now if you want. You can go for a break, go to the bathroom and why not. As well. Maybe be here at 1210. Yeah, 1210 we'll start making a recap. So be back by then. Thank you. So... So... I know. No. No. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yes, sure. But it should not look like this. So the terminal dies. There are ten pieces. If you have done this completely wrong, okay? Anything wrong with the function that is working on? I have a question. You can also skip the ten points. But let's try to... That is not going to find my work. So if we try to... So if this one fails. This one should at least get us to the other end. But we will still die. Just a minute or two. And it won't give us any more. And... But still... Yeah. And do you maybe have... No, that's really... No, I do. I use... So now you have found out. So... So obviously the last one... No, you have died. Yeah, but I already had... Oh, I see. Okay. Okay. So I can try to skip... Four and... I will get... I mean, if you can, if you have downloaded it, just try that. I would not download it if... I mean... It should work on... It works. Okay. Yeah, we will definitely... Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Okay. Okay. Yeah. Yeah. Yeah. Yeah. Yeah. No, I didn't say that. Yeah. Okay. Yeah. Yeah. So, everything is fine here, so the goal here is to make sure that you understand what you look for at the end of the day. And what you like to do is make the training tests for the... And not like the whole test. Instead, they've got, like, each other's choice. Yeah. But it's pretty simple. Well, okay. It sounds like... So that's the final finding. Like, the most final... It's more like, once you have a test, it's what you do. So, like, when we go together, we do, like, the first one. So, what I'm saying in there is that... So, I have a description of the movement. So, what we're trying to do is that we're making sure that we are having everything correct. So, right now, what we're trying to do is that we get every day... So, we get a jump, and then we're ready to go. And we want to make sure that we have everything inside that. And then, yeah, we want to make sure that inside that, we're ready to go. So, what we're trying to do is... Yeah, we're trying to make sure that... Right. And then, this is meant to be very simple to do, like, exercise. But this is a good question. Yeah, but as the idea is to show you that, the idea now is to make it very similar, so that you can relate to the two exercise politics. So, this is an example of what we wanted. So, we're doing that to make sure that we have everything inside our... Yeah, yeah, absolutely. So, we are... Yeah, it's a similar one. So, the idea is to make it as simple as that, but we want to make sure that it's all done the best we can. So, the idea is to make it as simple as that, and what you can try to realize is that what we need to do is not only set up the exercise, but we can also set up the exercise. We can also do it as simple as that. But then, that's... So, what we need to do is not only set up the exercise, but we can also set up the exercise. Yeah. So, we also need to make sure that we have a lot of work done. So, that goes for the two exercises that were in the same angle, because we need this, and we need that. And that was it. That was the other one that we were checking. Now, this was the example that I went over as well. So, it's the same idea, but we want to make sure that we can move that up a little bit. So, it's the same idea. You can hear this. And then, here, we have a more complicated exercise. The goal of the third one is really to make it more comfortable. Now, the goal is to help us create even a certain energy that's on the floor. The goal works. There's no energy. There's no energy. Now, what you might need to check is to make sure that the light goes on the floor. So, that was the first one. Now, what I would like to do is to do about this. When I have a complete exercise, and I get an air pump, I will be able to understand what the goal of the fourth one is to understand what I look at. What needs to be the next level is I said, maybe you could have a new item, like the, and you could take a slice of it, and accept it as, like, a little more similar to this. And then you could pick it up, even if we were really more advanced, like, you could actually have a line of pages, and a whole bunch of stuff. But really, here, what I'm coming to, is to tell you what the test is, and what the testing is, and what you'll have to determine is, like, especially with the, what are my students doing checking? Right? Because that's what I'm searching for, which is checks, and it would really be a good thing to open it up, and see what's going on. So, like, you can even see on that, like, like, the next one is, yeah, the difference between the two, but the thing is, with three, it's very important, but I guess from a learning standpoint, it would be a lot of fun. Yeah, but yeah, I was like, I feel like there's a difference between that and that, and then you, because you're solid, so, yeah, yeah, because I will post a solution for somebody again, so you'll still be able to commit, you'll be able to put everything in. Thank you. I think we've got a lot of, yeah, and the first, yeah, so, yeah, yeah, okay, so, yeah, so, yeah, yeah, so, yeah, yeah, yeah, yeah, yeah, yeah, yeah, yeah, yeah, yeah, yeah, yeah, yeah, yeah, which means this condition was going to be negative, so we're not going to be able to do that. So, next thing you need to do is come up with this. And then we do a little step by step. If we want to get this done, which means the condition needs to be, I think, used to be, because when you change the argument, it's going to go back into here. It has to do with page nine. Change the argument game. It's going to go back into here. Yeah, well, yeah. Okay, now, comment. Okay. In this case, we're going to change the argument game. We're going to go back to the final story. We're going to see page nine. Okay, so this is going to get used to the argument game. However, it's going to go back to page nine. See the next slide. Okay. So, what we're going to do here is mock this. You want to mock that new member page so that this is not going to be used for. So, you get to use a new member page when you mock that new member page. Because you're testing once it's done, you're testing the new member page. We're going to leave the new member page everything that goes to code. We're going to run some external code. It's going to mock so that you can test this in one search. So, yeah, you're going to mock this user data, but you also want to mock the other extended code in this one. However, you can pay attention there. This is how it includes the namespace. So, you can do fully qualified namespace. You always need to use fully qualified namespace. One word. And you turn there. If you want to hit, you want to hit this button. This button is so that it returns one. So, you're going to mock the first one, skip that condition, and the second one, and make the initial input. So, you're going to check the condition, and you're going to do something that matches it. It will go back. So, this condition. So, you want this to be true. So, you need to return false. And then return the decade should be one. Because that's specifically called... What happens if I have to mock this one? So, I want to leave this part away, and I want to mock more. So, this will be a true. Yeah, you mock only what is immediate, immediate external to the function. Otherwise, when you don't mock this one, but mock the existing one, when the test plays, you cannot be sure whether the bug is in this code or in this code. So, what happens for instance, this part, so I will mock just separately? Yeah, if you want to test this method, then you will mock to get use of it, and then you get... Yeah, basically, the files are being loaded. So, this is one of the mock strips for the configuration of the mock. So, if you go to... Yes, if you open this one, there's boot script. So, testing it to be... If you look into this file, that's probably part of the ultimate. If you go... If you go into Composite of Jason's file... Yeah, yeah. Then it might be one of the other problems that you might be able to test. That's the reason for it. Yeah, that's the reason for it. Yeah. I like... Yeah, I could... Where are you from? I've been this file. I've been this file. I've been this file. Where is this one? Yeah, so... No, this is for the student. It's set up on the slide. It's on the slide. Yeah, it starts with N. More truly. I know I did this last year. I know, but I think... Maybe we need more... Yeah. We need help. Yeah. Yeah. Yeah. No, no, no. You're not on your mind. Yeah. Yeah. Is anybody done with the current exercise? Okay, so we're gonna... We're probably just gonna continue with this exercise because there's only 25 minutes left, so we might just go at the end, go over the last one briefly, but we'll post solutions over the weekend that you can refer to. But for now, we'll just keep going with this exercise if you're all still working on it. Yeah. The testing? Of course, of course. Yeah. Yeah. I mean, it's... I'm... I write a lot, I read more about it, and how to get... It's... It's not... It's... It's a habit. It's easy, so just decide on it. Yeah. Yeah. Yeah. Yeah. That's kind of like... Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah, yeah, exactly. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. No, I'm just wondering around. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Yeah. Okay, that's fine. But everybody's sitting down. Oh, no. Yeah, everyone's sitting down. Yeah, well, she's not sitting down. But yeah, if you look into that, that's kind of like... No, she's not sitting down. Yes, it does. Wait. This is called the policy, because it doesn't matter if that's a policy. Okay, so if you... Yeah, that's the policy. Yeah, that's the policy. Okay, so... Yeah, so... I can just do the problem. You want to bring that to the... Yeah. All right. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. Thank you. All right. We're going to have to slowly wrap this up because it's lunchtime. So, yay for that. So, we didn't get to the last exercise, but we're going to talk about it briefly in a second. But I just want to go a bit over what the goal of this exercise is, because this one, going from exercise two to exercise three, is a very big leap because we're asking a lot out of you in terms of understanding what mocking is, what the mocking tools are, what you should create mocks for and all that stuff. But mocking is almost... it's one of the cornerstone of unit testing because with unit testing, the goal is really to test your code. It's not to test somebody else's code. And the only way to get around this necessity of using other people's code but not really is mocks. And mocks are there really to abstract that away and let you be, okay, well, I don't really care what WordPress does, but I expect that if WordPress returns, for example, if you get user data and it returns to be a user, well, this is what I expect my code to do with this user that WordPress returns to me. The point of unit testing is not to figure out if WordPress is working or not, it's to figure out if what I'm doing is working. So mocking is really important for that. We'll just talk briefly, we'll just leave this slide and then move on to the questions or do we want to go to the example? No, okay. Yeah, so what the last example would have, the last exercise would have been is basically putting it all together. So we would have given you an empty test with nothing. And you would have had to figure out what am I trying to do. So we still give you a hint. You know, the test name, you would have a description, but the goal here is to put it all together to figure out, okay, what do I need to mock? What do I need to assert to validate my outcome of this test? So that's what you would have done in exercise four. We'll post some solution. We can step through the PHP example while you're going really fast. But if you just go back just to the group. So we're trying to simulate going here to return null. So what do we need to do to get there? We need to first assert what we want at the end. So at the end, we want to know that if we call getTeamPage with no user ID we will get null back. But to do that we need the getOption function. So if we had gone back you don't have to do it. But if you had gone back we called getOption. So we need to simulate that. So that's where the mocking comes in. So we need to mock that because we're not using WordPress. Even if you were using WordPress you would have to simulate a database and put that data in. You don't want to do that. You're just trying to simulate what would happen if I get an option. And getOption returns zero with the constant and I think that's the entire test. And then because we return zero it will return null. Basically in the size 4 file you will find all these empty tests that allow you to test the whole file. So you will find a null 10, 12 test. So you have a null to exercise Atom and try to find the way to test that. In the coming week we will post on the repository the solution for all the things. So we will fix all the exercise. So if you want to do Atom you will have a way to see if you have stuck somewhere the reason and why the tests were failed. So you have also worked for Atom. I think that we have more five minutes or something. So this is one expectation or one hope that I have or that we have. I hope this is true or true ish. Do you have any questions that you think we should cover now? Maybe Do you have any suggestions for a course or whatever to dig deeper into the issue? Testing in general, testing for one language or in my case PHP. I expect tons of PHP courses to exist but actually I don't know one specifically. Sorry Do you? I think there should be video courses out there. There should be tutorials out there but I don't know someone or some course specifically that I can recommend. Thank you. If you like to learn by boot instead of courses that's nice and quite simple to get started with and it's about a lot of this PHP framework and there is a book which is called Testing with Laravel and it's by Jeffrey Wei it's the author and he has a very big introduction regarding testing for PHP in general and he will also introduce Mockery and all the things that we saw so in my opinion that's a very good place to start with unit tests for PHP. Any other question? I was just looking it up there is a book I haven't read it but it's called Grumpy Pub Programmers Building Test Driven there's a basically it's called Grumpy Learning.com and it's a writer that's written a lot about testing in general and specifically with PHP if you want to do more Laravel too there's a Adam Watan has like a video course called Test Driven Laravel as well so if you want to do that that's also a possibility but in general I'm writing a bit more about testing I have code bases as well that you can take a look at if you have questions but part of the problem or the technical aspect with these courses is that there is also a background in terms of programming that is required as well so it's kind of tricky for the learning material especially for WordPress specific learning material. Do you have another question? I was also thinking about that we maybe could tweet out other code bases like simple plugins that are fully tested or at least that there are some unit tests JavaScript and PHP and also I think you can tweet out some references to other articles somewhere like on your blog for example or on others for example. On my GitHub repo there's a plugin called Passwords Evolved and that is fully unit tested so if you want to look what it looks like but it's also object oriented just keep that in mind as well so next question why a static double colon and not self or this arrow yeah static or self in this case makes no difference because the thing is we are using assert2 and all the other assertmitted we are meted in the class test case the base class of unit test if you go to see that class all this meted are all static so it's a good move to use static to call static meteds with the double column and not the arrow this is the code of PHP unit it's a static function so you call it static you can call it dynamically because PHP will not complain about that it's good headed to use static function as static fact I use this I use this but if you look at the PHP unit documentation I think it's still all this so it's really a question of preference and readability more than anything else yeah pretty much you can also use the function if you want to function fire just registers all the assertions as global functions so if you do not want to have this or static you can do that as well you just have to make sure that in your bootstrap you include this function fire from PHP this does not really work in mockery because among this function there is a function which is called any like any and this function any conflicts with the function of any which is called any and so this is why we use 9 space by the way to avoid this conflict any other questions and just a thing the brain monkey library that we use today it's a library that I wrote it's open source it's on github so if you have questions or you find problems don't hesitate to write an issue on github I try to answer as soon as possible so there seems to be a linda but some video yeah, yeah so just to repeat for the video there's also linda.com for video courses for PHP but they're not WordPress specific right? no, so I'm personally not aware of any course or book on testing I think there is one by no the code there is a there is a test about unit testing in general and also unit testing with brain monkey so you can write a book yeah, I mean if I do it will probably be a video course more than a book but yeah but like I mentioned personally if you have any questions I write a lot and if you have any questions about testing or things like that you can reach me on twitter if it's a really good question and I feel like I should write about it I will definitely write about it because I think testing is great and something that developers should do more just another question right so what I'm wondering right now I'm using a very pure definition of unit testing where it has to be completely in isolation and the assumption is that work as works is expected as long as we just like PHP for example we don't test PHP but in practice there could still be instances where work as is not work as expected there might be mistakes in the code or the implementation which means our tests are going to pass but our application won't work anymore so this is our first scenario which we recommend instead of mocking every work as functionality to instead use something like an in-memory database and use actual work as functions well work as is a framework this is some project which should be tested individually so if this is not working anymore there should be the failing tests so you should not be concerned about your unit testing whatever product you should not be concerned with unit testing or actually testing on whatever level another project or library or so it's also true that when Tosman showed you the V-Model about software development unit testing is only a part of testing there is also accept time testing integration testing and end-to-end testing when you write an application or a plugin you have all the right and maybe you should really write not only unit testing unit testing is about and helps you during development and during the factory but it's not the only way to write testing integration testing so when you actually load work as and you do the test it's absolutely valid and everyone should do it would be better if everyone would do that so unit testing you mode but for the application or for the plugin you want to make sure that it works it's absolutely right to write also integration testing or even end-to-end testing or browser testing to test that specific class that's added there even because WordPress code might change so you need to change your code accordingly and your mocks accordingly this is something that happens in the real world so unit testing don't make your code shouldn't save to just expand a bit on that I personally I think we the I think unit testing in general is the better starting point for testing because one it requires this might seem complicated to you to set up but I can guarantee you that the other types of testing are even harder to set up because for example if you do integration would be what you were talking about where you want to see how your code behaves with WordPress itself well that requires a database it requires you to clean up the database after each test and things like that which makes things a lot more complicated here with unit tests the beauty of it is that it's all your stuff you're testing your stuff you don't have to worry too much about what WordPress is doing but obviously it can feel a bit scary to just rely on what you think WordPress is going to do versus what it actually will do really that's kind of the point here and if you look at the Plung and I mentioned earlier password evolve it's still very basic but I added acceptance testing so I think acceptance testing versus integration is a better next step because with acceptance testing you're really testing the behavior of WordPress as a whole so you're like navigating true WordPress to do things you're doing the browser testing you're testing how your plugin is going to behave or your team is going to behave inside WordPress and that's what as a whole not only in the browser but also inside WordPress so I think that's a better next step if you want but it also makes things a lot more complicated because now you need you need a database you need WordPress to be installed you need your plugin to be installed you might need data inside and all that stuff browser simulator and things like that so it's really a step of all it's still a step beyond but it's a useful step for sure definitely if you're a plugin shop or something like that you will want that as well but they serve two different purposes and the thing with unit testing is unit tests are the only level or the only kind of test that if you write the test correctly the absolute truth if you test some integration of two pieces and both are broken the integration might work like if you have some stupid example where you calculate something and you have a plus function and it does not add the two the two values but also adds one more for whatever reason and you have a minus function it subtracts one more if you just test add for three minus seven whatever the result is correct because there are two two defects that are masking each other if you test each unit individually you know for whatever reason you call minus or plus it's totally wrong from the first place which means that like I said before unit testing is the base for integration tests or if you can do the integration tests on top of unit tests where you only consider the actual interaction between units so we're now past the 50 minutes before lunch no questions here thanks a lot for showing up this was a huge fun to us to think hopefully for you too if you have questions come find us here or tweet or wherever thank you