In the previous article, we introduced how to set up the word press test product series in the advanced object-oriented programming (OOP) series developed by PHP word press for integration testing. I have previously written unit tests that demonstrate how to run in an isolated environment. The test does not rely on WordPress at all, so it can be run on the system’s PHP. The integration test involves the interaction between the plug-in and word press, so a complete word press is required. Demonstrates how to set up these tests using VV or docker. In the sample plug-in, we set up integration tests to run with docker. Although it is not easy to set vagrant and VV, I think the initial settings of docker are almost the same. Compared with vagrant, it is much easier to create a WordPress environment suitable for local development from scratch and perform automated tests on PHP and JavaScript as part of the continuous integration and deployment system. The previous article listed a file required to create a docker file. I think vagrant can perform similar tasks, but I don’t know the type of server configuration script system required to perform this task without an able, puppet or docker. The
I don’t want to learn all that. Instead, I want to enter a command, composer WP tests, which runs integration tests with composer WP install to navigate to the plug-ins I have and install the environment. Setup is complete. It is time to discuss how to create these tests. Specifically, let’s take a look at the difference we are happy to assume when writing integration tests that correspond to unit tests. In my article on flip hypothesis unit testing, I talked about some methods of testing based on the assumption that WordPress works properly. Based on this assumption, we write tests that rely on simulation data. Through these assumptions, the test can prove that the public API of the code is consistent, and the core function (the operation performed by the input) is working. Side effects – assuming that the connection with WordPress is correct, the output is consistent, and the output is consistent, the correct effect will be generated for WordPress. The unified test assumes the opposite. We assume that the code runs in isolation and test its effect without isolation. The
If you are creating a sink, create a unit test to check that the faucet is turned on and off, generate hot or cold water as directed, and then create an integration test to ensure that the sink is filled with water, not poured directly. Sprinkle on the ground or part of it flows into the water tank, and part of it leaks from the pipeline prematurely. Test create test what? The code we have created so far affects WordPress in two ways. Add filters to import other wp\u query and rest API results. Therefore, we must test whether the results of wp_query and rest API are modified and affected as expected, add the effect of the code in the way we want, and whether the results of wp_query and rest API will be affected under the correct circumstances. There shouldn’t be times. The
In the first test group to add and remove filter tests, we tried to prove that the code could add and remove hooks appropriately. The filter wpquery class has addfilter() and remove filter() methods. Addfilter () can handle these interactions by testing whether WordPress reports that a filter has been added. You can also test whether a filter in WordPress is reported to have been removed by removefilter (). We have
In other tests, you will see what happens when you add a filter. The WordPress core has coverage hook tests. Enough to override this effect. The
To test whether to add a hook, instantiate the class, add a filter using the class, and then add a filter using phphunit to ensure that the result of has\u filter() is the same as expected. To test whether the hook can be deleted, instantiate the class, use the class to add and delete filters, and use phphunit to check the possible has\u filter() results when adding filters. Deleted: these two test certificates will affect word press. Before testing the impact on WordPress, test whether the filter has unintentional side effects. We are sometimes testing the system for modifying Posts returned by wp_query. We need to make sure that our system does not always modify the posts returned by wp_query. The
Our system is designed to modify wp_query when used on a specific rest API path. Test the path later. Now, add a filter, insert a post in the database, and verify that wp\u query returns all posts. This is required by default. We will check whether this operation is still performed when adding a filter. In this test, the post factory in the WordPress test suite will be used to generate the post. In the previous torque post, the class that implements the factory pattern is the class that creates other class objects. The
A good use of the factory pattern is to generate test data. Creating a single standard method for testing data types (in this case, WordPress posts) can reduce code duplication and make test data credible. You can also create tests more easily. The post generated by postfactory is the same as the query result of our test. The factory will generate the expected results compared with the test. You do not need to test all parts of the results post. The test of WordPress core involves wp\u post. Wp\u query result test we have not reached the point where the wp\u query result is not simulated data. We want to test and prove that the whole system is reliable, because according to the standard, wp\u post object arrays of the correct size are always output. The
You can change what happens inside in the future. Now it’s almost time for that report. Don’t worry. In doing so, new code requires new unit tests. But no new integration tests are required. In fact, when implementing other systems (such as elasticsearch or searchwp) to perform search, it is necessary to create a requirement that these integration tests must pass without modification. All of these functions are encapsulated in the getposts () method. Therefore, the two tests that need to handle this method are: one is to prove whether the array is returned, and the other is to check whether there are valid wp\u posts in the array. The
It’s easy to test whether getposts () returns an array. When passing the getposts () result, create a test to check whether the result of the is_array () function is true. We are proving that it is an array: the test involves the type of result, that is, the result of array type. The following tests will describe the results: The array includes wp\u post. To do this, use getposts() to get the post array, repeat the results, and claim that each post is a wp\u post. Rest API path tests all these tests will produce the expected results when wp_query says it is in the correct context.
Certification. That is a good thing, but from this point of view, the result does not prove to be correct. It is time to test the rest API response, which is designed to modify the code. The
We previously discussed torque’s rest API endpoint testing. Custom endpoints are described in the documentation. The following test describes the modified default endpoint. But the methods we tested were similar. We will not issue the actual HTTP request. The acceptance test begins. We will use the simulated wp_rest_server, which is the same as that used in the core test of word press. Trust the assumption that WordPress simulates wp\u rest\u server to accurately reproduce the behavior of WordPress rest API. This assumption can be made because of the test suite of word press core. If a problem is found in the WordPress test that handles the WordPress rest API, you must open the problem in the core trac and submit a patch. The
Two tests are required. Tests the conditional logic of a system that must return true in this context. The second test will prove that the results are what our system must produce. Here is the first test. Forward the request and make sure shouldfilter() returns true. This proves that the system knows to filter the results. Now, let’s prove whether the content of the result set is correct. This is very similar to the test of wp_query results. We are checking that the rest API is used in a natural way to display the correct word press posts. We are only testing the effectiveness of our system. This test generates a simulation request and starts in the same way as the last test. This repeats the results and compares them with the output of the getposts () method. It is identical to the method used below. The same results must be generated. If there is a problem with the code, this test compares the expected results with the wp_error object or other result sets, so the test fails. One advantage of this pattern is that if the getposts () method changes, the test target data will also change. This test does not check for specific data. It can be verified in other tests. Ensure that the two different methods of accessing the same data are synchronized. It looks like a small bright spot. This is not because we have written or identified a test before the test, which contains most of the system functions we do not control and safely declares a few people. The last test will be the ultimate test of our system. Final results. But it is no different from other exams. Suppose everyone proves a little thing, and all the other tests pass. The test is about safe families. If a test fails, all assumptions are untrustworthy. The next article in this series will show you how to set up Travis Ci to test all pull requests generated by plug-ins referencing the GitHub repository in multiple environments. This test automation combined with git flow workflow requires that all future changes to the code pass all tests. We call this test automation continuous integration, or CI for short. My thinking unit for the family, my strategy for integration testing, and the use of different environments for each type of testing all rely on some questionable assumptions. For example, wp_mock and mockery have very perfect simulation frameworks, which can provide reliable data, and can test the integration with WordPress without a WordPress site. I use PHP to separate unit tests
Because I think teaching and understanding how to use unit is a pioneer that can be used with other tools, I use this method. As you have just done, phpunit with the WordPress test suite is a practical example of other tools. Thanks to wordhat, many other tools such as behat, a PHP action based testing framework that can be used with WordPress, need phpunit. This series and other local testing environments are used for integration testing. In this article, we have seen what assumptions we should make. We hope to see the advantages and disadvantages of various methods when evaluating the most suitable workflow among these test tools.