I'm rewriting my plugin and trying to use patterns like factory and service locator like here: https://github.com/schlessera/wcbtn-2018-api (factory, plugin).
I have set up my testing, loaded everything using composer (I'm autoloading my classes using classmap), and when I run tests, I can confirm that the unit test is passing through the code properly.
For instance, I want to check the hooks defined in register
method that looks like this:
/**
* Register the plugin with the WordPress system.
*
* @throws Exception\InvalidService If a service is not valid.
*/
public function register() : void {
add_action( 'plugins_loaded', [ $this, 'register_services' ] );
add_action( 'init', [ $this, 'register_assets_handler' ] );
}
I tried adding the print_r
inside this method and I can see some output when I run
./vendor/bin/phpunit -c phpunit.xml.dist --no-coverage --colors=always"
My test looks like this:
<?php
/**
* Class Plugin tests
*
* @package My_Plugin\Tests\Unit\Src
*/
use Brain\Monkey\Actions;
use My_Plugin\Tests\Init_Test_Case;
use My_Plugin\Core\Plugin_Factory;
use My_Plugin\Core\Plugin;
/**
* Class that tests the Main plugin functionality.
*/
class Plugin_Factory_Test extends Init_Test_Case {
public function setUp() {
parent::setUp();
Plugin_Factory::create()->register();
}
public function test_plugin_execution() {
$this->assertTrue( has_action( 'plugins_loaded', 'My_Plugin\Core\Plugin->register_services()' ) );
$this->assertTrue( has_action( 'init', 'My_Plugin\Core\Plugin->register_assets_handler()' ) );
}
}
When the plugin is loaded the Plugin_Factory::create()->register();
method is called, so this is what I've put in my tests setUp
method.
As I've said, this works, as print_r
will show stuff in my terminal.
But I get
1) Plugin_Factory_Test::test_plugin_execution
Failed asserting that false is true.
/Users/denis/vagrant-local/www/personal/project/public_html/wp-content/plugins/my-plugin/tests/unit/src/test-plugin-factory.php:31
I even tried with
Actions\expectAdded( 'plugins_loaded' );
Actions\expectAdded( 'init' );
But that throws the following error:
There was 1 error:
1) Plugin_Factory_Test::test_plugin_execution
Mockery\Exception\InvalidCountException: Method add_action_plugins_loaded(<Any Arguments>) from Mockery_0 should be called
at least 1 times but called 0 times.
/Users/denis/vagrant-local/www/personal/project/public_html/wp-content/plugins/my-plugin/vendor/mockery/mockery/library/Mockery/CountValidator/AtLeast.php:47
/Users/denis/vagrant-local/www/personal/project/public_html/wp-content/plugins/my-plugin/vendor/mockery/mockery/library/Mockery/Expectation.php:310
/Users/denis/vagrant-local/www/personal/project/public_html/wp-content/plugins/my-plugin/vendor/mockery/mockery/library/Mockery/ExpectationDirector.php:123
/Users/denis/vagrant-local/www/personal/project/public_html/wp-content/plugins/my-plugin/vendor/mockery/mockery/library/Mockery/Container.php:303
/Users/denis/vagrant-local/www/personal/project/public_html/wp-content/plugins/my-plugin/vendor/mockery/mockery/library/Mockery/Container.php:288
/Users/denis/vagrant-local/www/personal/project/public_html/wp-content/plugins/my-plugin/vendor/mockery/mockery/library/Mockery.php:204
/Users/denis/vagrant-local/www/personal/project/public_html/wp-content/plugins/my-plugin/vendor/brain/monkey/inc/api.php:38
/Users/denis/vagrant-local/www/personal/project/public_html/wp-content/plugins/my-plugin/tests/init-setup.php:28
Any idea why this doesn't want to work?
Before refactoring (wasn't using factory and other design patterns besides dependency injection), this worked (I had one issue opened before with rest_pre_serve_request
iirc).