 at 1 o'clock when he's here, very soon. Our next speaker coming up has been building the WordPress site and projects since 2007. Currently we're satadestors.com. You can support that. The Walt Disney Company has a tech lead there and he's got a lot of knowledge with this but that's not correct. And today he's gonna be speaking about unit testing of WordPress plugins. So that's gonna be a great technical talk here. But it's just an introduction he said. So please take a warm welcome to the bedside. Hello everyone, good afternoon. It's also coming another week. We will work in three weeks to do our best here. So my name is Vincent, I'm an agent. You can find me with my guest, her name on Twitter, GitHub, or please go forward. So that's one of the benefits of this So I will be using a lot of codes to do these projects and get out the page and see the code that will find out what it is. We'll be talking about what the unit testing is and it's all getting tested. Some tooling and how to start to all to scaffold and use plugins that can run a unit test and also be showing you how to write your first unit test. For the introduction, we're gonna do a unit test before using new stuff here. But if you've never written a unit test for a WordPress plugin, probably it's gonna be beneficial for you when you're done with it. The one PHP unit is a feedback framework for running between a unit test. It's a bit hard to set it up correctly for testing WordPress plugins. So let's go for the unit test itself. Software testing method and verifies the individual units of the code mostly it's function or checks if those units work as expected. And our good goal here is to ensure that the behavior of our function stays consistent and after you write your test, there will be another code that runs your code and checks if the result is correct. You can have a function that has two numbers and returns the sum of those two numbers. So you can write another code that runs that function with different arguments. You can pass two and two and verify your result is four. You can pass minus two and two and verify your result is zero. And that way you ensure that your function is working correctly. And also with that test, you will have a safety net. So you can easily change your code and be more confident that you didn't break out something. You run the test and you will see that your function. It allows you to test in a more granular level than just by testing the UI in the browser. So let's say you go back in and you did some changes and you test the first easiest thing is just going to the UI and click on the button and see if you see some screen changes and you see the sum expected to resolve. But you cannot test everything or just the point that we test things. There might be some function that Parade will be executed and et cetera. So when you do the testing, you will be able to run every piece of your code and make sure it works up with the code. So you will have a better quality. You can see it in, you can see those unit tests and code bases of some of the known WordPress plugins like Jetpack or Yoast SEO. So if you go to their code bases, you can see those all unit tests. That's one of the methods they do. One of the things they do is to ensure that their plugin works with different versions of PHP with different version of WordPress. So they test it automatically using the unit tests and other automated tests. You already know from my page notes that they're back to you guys for the unit tests. So you can easily change your code and break something if you're on your test sheet and see if you can do it faster. So if something goes wrong, tests show you what went wrong, which function, which line, what was expected, what instead the function returned or what happened instead of the expected behavior. So it rolls into the web cluster. And the test also can provide some documentation. So you can read tests and see which, how the sound part of the code or some functionality in the code behaves, how it's expected to behave. That way you can learn the, and also tests can be used to design your code. Writing any code, you can start writing a test for it. You can start the test by, I want to have a class with this name and define a frequency instance of this class and run this method of the instance of the class. I expect to have that in some sort of way. You turn it on value or the, and also you can define like the, I want to have this method with that many arguments. If I send this argument, I expect to have some test and then run the test, of course fails because there's no class, there's not a method. Then you can start fixing those issues by creating the actual code. So this is called test-drawn development and this is one of the methods you need to launch an company's practice. So we show all these benefits. So what do you need to do to start doing this? In the right unit test, you will need the unit space operating system, can be Linux or Laplace. You need each view for us. You will need ICO or IATV. You will need each unit testing, library, WordPress test suite which is a library or additional library or WordPress that allows you to write additional, gives you additional functionality to each unit. WPCLI is optional because you can write the code yourself or you can scaffold in general because you don't, you cannot write the unit test. It's sometimes the presenters who talk about unit testing, they recommend install all these tools one by one each beginning, you can go to the website, install all the instructions and install each of them. This can be very overwhelming for someone here and for someone who recently started development and configuring these tools correctly takes some time to do. So I would command for starting, it's varying, vagrant, vagrants, the wrong form. So it said open source, vagrant configuration. You can just go to the website, learn more, this is the recommended government environment for the WordPress core government to go to make work and see it and so it comes with all those tools. So it's a mutual machine that means you can run it in Windows. So that's not a problem, Linux or Unix-based system would be running as a virtual machine in your system and you still can run it. It comes with Ubuntu, WordPress develop version, the stable, the stable version of WordPress, WPC-LI, so to create all the tools you will need to start running with this will be available. And just for running with this, you can run your own local environment or your website inside WVB. You can migrate, I would recommend you if you want to advance. So I would recommend to migrate to it from things like Mac to that. So this gives you more flexibility. It's a little bit manual, there will be a little bit of manual configuration but the configuration is a YAML file. It's very easy, documentation is really good. You can run your local environment for all your websites in it. Let's see how we can scaffold the plugin and the unit tests using WPC-LI. So WPC-LI is available inside the WVB already installed. You can just, as a sage into the migrant box, go to the default WordPress installed with WVB, call it off to the default WordPress, it's the latest stable version of WordPress. And you can run it, command WPC-LI, plugin and zero plugin state. And it will generate an empty plugin by the plugin's folder. And you can see the folder and see the content of it. A bunch of files, some, most of them are configurations, some, you can see some PHP files, but most important ones are the installed WVB tests, Sage, script, PHP unit, XML file, which holds the configuration information about the estimate. And the bootstrap PHP, which bootstraps your testing environment and includes the plugin's code. PHP unit.xml file, part of this is that bootstrap file, which is pointing to that bootstrap PHP file, you see. So that serves like an all-out PHP of composers, so it will load your file. And the most important part is this directory part. So for the test report, it will, here you define where your tests are. So tests are inside the tests directory and all the tests start, test files start with test dash graphics and ends with the PHP something. So that means when you run PHP unit, it will search for files that start with test dash and end with dot PHP inside the tests, all the test runs to test. So you do not have to change this one. It's already created by WVCLI. And the next thing is bootstrap dot PHP. So as I mentioned, it bootstraps WordPress testing environment then it loads your plugin inside the test that's in the library. So you see it's trying to find the WV test there, the library we go and if it finds it, then it cannot find it, then it tries to find it in the tmp folder. If it cannot find it there too, then it fails with another message. If it finds it, then it goes to the tmp folder. If it finds it, then it goes to the tmp folder and it finds the file. The file will function to dot PHP inside the test suite directory and it loads your plugin and runs the bootstrap PHP file inside the WordPress. So you don't have to download the test suite to it. It's already inside WVCLI. It's already moving down the link with you when it's first read right now. So for your questions, I'll probably use your answers to your questions in the slides. So you don't have to change this file to this already, configure the other thing. When you run the PHP unit, according to the configuration, it finds this file, loads it and the next thing is... So the quick information about those environment variables if you run, if you try to echo those variables inside WV, you will see where they are. So this is where the first test directory variable is inside this folder in the background box. You can go there and see how the WordPress test suite looks like, what kind of bugs are there. And the next same thing is that when you go there, you can see it inside the file, but it's inside the WordPress test suite. And this variable defines where the WordPress core is for the WordPress core that you use for your unit tests. So you can test your plugin with different versions of WordPress. So in this case, the WordPress core is the latest development version of WordPress. So when you run this test, if you run the tests for this plugin, now it will use the latest development version of WordPress inside your review box for the run of the tests. So the third file that we mentioned that isn't important is installed WVTests.sh for Shell script. This script downloads the required WordPress version and installs WordPress test suite. It defines those environment variables, creates a database for running duration tests, and you don't have to run it inside VVVP. So if you don't use VVVP, if you have your build server or other server or another environment system, VVVP environment or map environment environment, or even Docker, you can use this script to create a testing environment for the running for your code. So what's inside this? This is what it does, and you can use it with hostname, download the version of WordPress, create a database for it, and produce this environment using that version of WordPress. This is very important script, and you can use it for running tests in your build server. So when you saw yesterday, this is the script that must be used inside those TransCI, GitLab CI, everything now, you can run each beginning. So you need to step forward in your plugin, just go to your plugins folder, and inside your review, and you just type each beginning. It returns green, okay, and one test and one search. What this came from? That's this one, test-sample.pg. It was generated by WPCLI, also it's just a sample test for you to see how it works. You can keep it, you can delete it, but that's how your test files will look like. So as you see, it's a class called sample test, and it's extending a WPC unit test case class. This WPC test case class is part of WordPress's testing suite, and it extends the core test case class of each beginning. So it gets functionality from each beginning, and it adds some additional functionality needed for testing WordPress tests inside it. So some of those functionality is like you can are very useful for integration tests. For example, you can quickly generate new costs, new taxonomy to some WordPress related item. For factories, you can do WordPress tests. What's text? Test is methods inside this upgrade, and those methods must be named to the practice test. Each unit considers all the public methods of class that spread this test to the test, and it runs there when you click it. But you don't have to name them as starting with test, that is if you don't want to start the name of the function with test, you can add test wide in the dog block and it will consider that function as a test. So, and it runs that's another method of the test assertions. So you probably don't just test the inside this method, there is assert true, true, assert true, this is very dummy assertion, it's just true, that true is equal to true. So that's why if you're getting green on KD result, true is always equal to true. But, when you read test, you will have other assertions and so assertions just check if it checks the result of some code and makes sure it matches the expected result. So, HPMN comes with a lot of different assertions some of them or some examples are assert calls, assert equals assert no, assert contains, assert starting with, assert string starts with, assert stops string ends with, etc. So you can see the code is on HPMN's website. Sometimes the code is showing that it's initializing empty grade and it starts writing our first unit test. So, to start writing unit test we need the code to start. We need some code to test the code, right? If we can't go for a TVD like start writing unit test before writing this code but we are not doing it for this presentation then we will get it hard for the let's say you have a code it's a very simple class called calculator which has two methods add and subtract that has two numbers inside your finding folder you have this class you should of course be required inside your main HP file file which will be executed by your friends to start writing unit tests are located it expects that tests are located inside the test folder so you should create your HP file or test inside the test folder or change your HP unit configuration part so you can always change a configuration file but we decided not to change it you can always so now configuration must be in the tests folder and you can start writing you can just copy and test it you can also a test file and just rename it or just type all this code example code here so what you need to name the class you can name with anything I prefer to start the name with test to show that this class is the test class and because it starts with text it means that I don't have to use the dock block but I prefer to name it cd dock blocks this allows you to just come and click on this code let's see what it's doing it's initializing the category to a class it's creating an object of the class and then write them in a searcher so we are sending it to that method and asserting the return value it will be 4 so once you have this code you can run with your phpunit and that is expected the return is created all tests test now you should see that there are two tests and two assertions one is that dummy assertion the other two is the second one is we just wrote it checks if that method you pass 2 and 2 to the that method is returning 4 we start with 4 we want to test see how the slide line of the test changes expected value to 5 and 2 plus 2 is not 5 of course if you run it again now it shows where it failed test test failed and it failed it's showing the return value of the code you wrote and it also shows you why