WordPress advanced OOP Part 3: unit testing of WordPress rest API plug-ins

This post is part of a series of posts that extend the WordPress rest API using advanced object-oriented PHP. Demonstrates how to use three filters in the first post to modify the pattern of all post type paths. Using an object-oriented approach is more complex and requires more work than using a functional programming style or using some object-oriented code, but does not follow the solid principles as closely as in this post. The code we processed uses three word press filters and interacts with multiple word press APIs. The goal of this post is to explain why the code is more complex, but not too complex, and well maintained and extensible, based on these established best practices. The
Most of this series is about automated testing. Writing testable code helps achieve these goals. In this process, you will gain the ability to use test scope, continuous integration, and continuous deployment tools. The previous post introduced the word press plug-in settings for unit and integration testing. This post will rewrite the code of the first post to isolate and test in word press. Then, create several unit tests to prove this. This test does not cover the entire system that requires integration testing. We will discuss this in the next installment of this series. The
In a post on testability reprocessing, wp\u query showed a class that can prevent querying the word press database. Instead, the class provides its own wp\u post array. This pattern of replacing wp\u query with an alternative API is particularly helpful in solving the search and scalability problems of word press. Male post_title= \
$mockpost[$i]-> filter = \
}The
\/\/Returns a simulated array of simulated posts.
Returns $mockposts.
}The
\/\/Something is always returned even if it is not changed.
Return $postsornull;
}The
}The
}In the last article, I made it clear that classes are not ready for unit testing. There are four problems with this approach. The
Determines whether to run the callback filter on the filter, and provides a method to delete the new result acquisition filter. We will be guided by the principle of single responsibility and divide this category in four ways. In this way, business logic can be separated from the integration with WordPress. These business logic will be used to execute unit tests, and integration tests can be added later. This allows you to create a test simulation. These four concerns are the public APIs of classes. The public API is defined in the PHP interface. This allows format hints and return format declarations to be used for this interface. In this way, the way the class that implements the interface works is not important to other classes that depend on the interface. The
As the core part of solid principle, this method is not helpful to build test simulation objects. Conceptually, this means that a system that replaces wp\u query results during rest API requests is no longer built. Instead, we are building a system to replace wp\u query with a reference implementation in the rest API request. This still meets our original requirements. In addition, additional implementations can be added more easily in the future. The following is an interface where each concern has a method
Erwpquery:: callback(). The test can be trusted as long as other methods in the class do what they should. Mocking allows you to create and trust tests in this way. Interfaces and abstract classes add complexity. But the end result is that it is almost impossible to write a failed system implementation because of filter wpquery. This is why the complexity of solid OOP PHP is worth mentioning. You can trust that you can build systems that perform similar tasks and apply them when applying patterns, rather than writing code to perform a task. Next step: ensure completion. This is the basic matter for unit testing the isolated word press code in word press. This is based on the principle that one sentence should have multiple abbreviations and solved by ridicule. To enable this article to isolate tests, we want to show how to repackage the code and how to write these tests. We combine all the code examples and the last example in this article into one plug-in. Place the GIT tag at the end of this article. It is recommended that you branch the plug-in and add the entire test scope to practice what you have learned. I need some repackaging. To remove the WordPress plug-in API, we configured the mock object for testing. Because we don’t use it to do many things, it’s OK. In fact, for more complex situations such as adding these attached classes, it is important to mock the plug-in API. Tools such as 10up\/wp-mock will be very helpful. In the following article, you will begin testing with a low degree of isolation. To handle the interaction with the WordPress plug-in API, we will use the WordPress test product family to create integration tests.

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *