Skip to content

Testing

To test our router endpoints we are going to use Pest PHP. Pest is a testing framework which makes PHP testing a breeze. Besides, the creator of Pest is Portuguese and studied at the same university as me which is a bonus πŸŒŸπŸ˜‚

However, if you have a preference for some other testing framework, the same principles apply

Testing dependencies

To allow us mocking classes and function we will be using Mockery

composer require mockery/mockery --dev

Testing structure

We are going to write unit and integration tests. Our code structure would look like the following:

my-plugin
β”‚   (...)
β”‚
└───src
β”‚       (...)
β”‚
└───tests
    β”‚   bootstrap.php   # Loads WordPress for integration tests
    β”‚   Helpers.php     # (optional) Helper functions
    β”‚   Pest.php        # Pest configuration file
    β”‚
    └───Integration
    β”‚   β”‚    PostsApiTest.php
    β”‚
    └───Unit
        β”‚    PostsApiTest.php