Example code for a series of Torque article showing how to modify REST API requests to content routes to improve search, using an object-oriented approach.
This Plugin Is For Educational Purposes This Plugin Does Not Work See the "Recommend Next Steps" section below for recommendations on what to do to finish this plugin.
This plugin demonstrates the beginning of the process of using PHPUnit for isolated unit testing of PHP code for WordPress. It will also illustrates -- in a future revision -- how to use PHPUnit and the WordPress test suite to create integration tests.
This plugin also demonstrates using Composer for dependency management, as well as using Composer as a task runner. The tasks run by composer will include code sniffing with PHPCS and automatic conforming to PSR-1 and PSR-2 for PHP code formatting, but with tabs instead of spaces. Always tabs.
This plugin is not done. You should finish it to learn these concepts.
This code is for a series of articles on Torque Magazine by Josh Pollock on advanced object-oriented PHP for WordPres developers.
- Part One: Customizing The WordPress REST API To Improve Search
- Part Two: Future Post Writing Testable Code That Interacts With The WordPress Plugins API and REST API
- This Commit is the plugin as of part two.
- The code in this commit basically works to modify the REST API results, but we don't have tests to prove it.
- Part Three: Future Post Writing Unit Tests
- This Commit is the plugin as of part three.
- The code in this commit works to modify the REST API results, and creates as much test coverage we can get with isolated unit tests.
- Part Four: Future Post Setting Up Integration Tests In Docker)
- This Commit is the plugin as of part four.
- In this commit the Docker-based integration testing environment is added. The integration tests, which will be explained in part five are almost complete in this commit, but do not pass.
- Part Five: Future Post Writing Integration Tests
- This Commit is the plugin as of part five.
- The tests, that are explained in part five are complete in this commit, and do pass.
- Part Six: Future Post Test Automation With Travis
- This Commit is the plugin as of part six.
- The tests, that are explained in parts three and five are now run using the environment created in part 5 for local testing and development automatically using Travis CI.
Requires git and Composer
git clone git@github.com:caldera-learn/rest-api-search.git
cd rest-api-search
composer install
A local development environment is included, and provided. It is used for integration tests. Requires Composer, Docker and Docker Compose.
- Install Local Environment And WordPress "Unit" Test Suite
composer wp-install
You should know have WordPress at http://localhost:8888/
- (re)Start Server: Once server is installed, you can start it again
composer wp-start
Follow the steps above to create local development environment, then you can use the commands listed in the next section.
Run these commands from the plugin's root directory.
- Run All Tests and Code Sniffs and Fixes
composer tests
- Run Unit Tests
composer unit-tests
- Run WordPress Integration Tests
composer wp-tests
- Fix All Code Formatting
composer formatting
- Abstract out all WordPress specific logic to methods that are required by an interface
- Mock all classes that required creation of an interface in previous step
- Write a unit test for each method of each class that isn't mocked.
- Complete integration tests for WordPress interactions.
- Finish the
Hooks
class so all of this works. - Add coverage reports and achieve full test coverage.
- Integration tests (next revision and planned Torque post)