WordPress advanced OOP Part 2: automatic test settings for WordPress rest API plug-ins

In the last post, we showed how to use the WordPress hook (aka the WordPress plug-in API) to modify the WordPress content endpoint during rest API requests. The actual example changes the default query to improve the search. Now, we need to test the isolated code in the WordPress plug-in API. This article assumes that the plugins API works in a documented manner. But this is not a safe assumption, so you must be prepared in case it doesn’t work as planned. This post is actually about how to control the problem. Note the word press plug-in can and cannot be set for acceptance test and unit test. This way, you can take this distinction into account, learn how to organize your code, and start showing the tools and techniques for writing these two types of tests. The
Set up the WordPress plug-in for unit and integration testing wp-cli. Use unit testing to create WordPress plug-ins. I don’t want to spend too much time setting up and configuring tests. Most of the code can be compiled using wp-cli. First, let’s use this command to create a plug-in. This will create a plug-in called \
Read the following post in torque to learn how to set up: The method of testing e-mail does not follow the word press send word press coding standard. The method of using composer to implement code standards in word press development. In order to improve word press development, the reason and method of using automatic class loading and namespace wp-cli have made two major changes. First, in order to comply with psr2 instead of word press encoding standard, we changed phpcs rule set to use tabs instead of spaces. The
Previously, inline parameters were used in code listeners. Now, because the command line options are too complex, the phpcs XML configuration file is used. Male
Male
Male
Male labeled psr2 replaces blank. Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
Male
There is a root namespace and all PHP classes in the male SRC directory. The following is the composer that integrates all this. JSON. Another new fact about test runs in Git hooks is that I added cghooks composer git hooks. This package is a composer plug-in that allows you to connect to git events and run composer scripts on them. We used the pre commit hook, which runs before writing the GIT commit. Since the script must exit successfully to complete the submission, the setup will perform a series of standard required pre submission steps. The
In my case, I only added code formats, not tests. This prevents code format issues from being pushed to uupstream. However, you can continue to push incomplete operations until all tests are modified. Not all developers want or need to install tests to contribute.
You do not want to use it in community open source projects. Preparing unit tests actually requires some refactoring before you start testing. But I want to pause and write a test first. Let’s create a very basic test that doesn’t involve code. I want to add this type of test first to ensure that the plug-in test settings are correct. I just claim that facts are facts. Effectively, phpunit works in this context. The
Male assertnotequals ($excluded, $actual);
}The
}When creating unit tests, our goal is to test our systems with as few dependencies as possible. No dependence is the impossible ideal we pursue. We will believe that the phphunit test works normally. As long as a test passes, its relevance can be trusted. The
You may notice that the test is extending classes in the same namespace called testcase. It is just an empty class used to extend phpunit \framework \testcase. The suggested file structure for phpunit testing is one test class per class. Therefore, there will be multiple classes in the test directory. I’m sure one day we’ll want to share code between these tests. It is helpful to extend one class per test class. <? PHP
Namespace calderlearn\restsearch\tests\unit;
\/\/PHP unit test case.
To prevent \/ \/ two classes with the same name in the scope, you must specify an alias.
Use phphunit\framework\testcase as the frameworktestcase.
\/**The
*Class test cases
*The
*Default test case for all unit tests
*@package calderalearn\restsearch\tests\unit
*\/The
The abstract class testcase extends frameworktestcase.
The{
\/\/Put the shared code for all tests here later.
}Test start time after the test is passed, the test suite setting will be completed. That's amazing. Now you can start writing tests directly, or wait for the next post to cover code refactoring for testability and more test writing. The
GitHub can find plug-ins for creating and testing this sample code. To reach the full scope of application, more tests are required. Free forklift, carefully understand through experiments. Before we finish, let's simply return to the wp-cli generated auto generated test and installation scripts. WordPress' test product family will not run with composer, as with the rest of the tests. In the WordPress ecosystem, these test product families are usually used for unit testing and integration testing. I don't like this pattern because I like to treat them as two completely different test suites and use composer to write it all. The reasons will be discussed in the next two posts, but if you are used to using them together, I want to point out that.

Author:

Leave a Reply

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