I’m developing a plugin that has a class which uses methods of
$site. Basically, the plugin provides an API endpoint that updates pages based on input data. I want to test whether pages are updated correctly. How would I do that?
I’ve set up PHPUnit, which is also used by Kirby. I know how to write tests for a specific class. However, in my case, that class relies on Kirby methods and global functions. I need to load those somehow.
I’m aware of the concept of mocking, but I want to avoid it in this case because I want to test the behavior of the Kirby methods as well. What I want in the end is:
- The test gets some input data
- That data is processed by the plugin
- The page is updated with the processed data
- The updated page data is tested for expected values
What would be the best approach to do this?
I discovered that I can do:
include __DIR__ . '/../../../../kirby/bootstrap.php';
…in my test case and it would correctly load Kirby.
It appears possible that I can create the necessary
site folders in the plugin folder. Then, the test script includes a Kirby bootstrap that uses the plugin’s
site instead of the project’s and then runs the test. Basically, you run the plugin’s tests using the surrounding Kirby installation of a project. This way, I can include my plugin as a submodule in my project and run tests directly.
If I run a test without changing the roots, Kirby correctly sets up the whole site and I can test the project content. However, I want to use the test content. When I change the
content root to the plugin’s
0 even though there are actual pages.
Could that be because the site content is located inside the
project/site/plugins/myplugin/tests/content instead of
Edit: I simply had messed up the folder path.
@hdodov i created a demo for how to test a complete kirby installation with routes, pagemodel and its plugins. any feedback would be welcome: https://github.com/bnomei/kirby3-unittestkit