A
Anonymous
Guest
Hi All,
I am building a simple WordPress plugin and I am looking to add unit tests using PHPUnit, I have the following class code:
And this is my test file:
However I am always getting the error
when I run
What is the best way of mocking the add_action calls purely within PHPUnit without having to rely on a third party solution?
Any help with this would be greatly appreciated.
I am building a simple WordPress plugin and I am looking to add unit tests using PHPUnit, I have the following class code:
Code:
<?php
namespace App;
class MyPlugin {
public function __construct()
{
add_action('admin_enqueue_scripts', [$this, 'enqueueAdminScripts']);
add_action('admin_menu', [$this, 'createAdminMenu']);
add_action('admin_init', [$this, 'settings']);
}
public function enqueueAdminScripts()
{
// Code here
}
public function createAdminMenu()
{
// Code here
}
public function settings()
{
// Code here
}
}
And this is my test file:
Code:
<?php
use App\MyPlugin;
use PHPUnit\Framework\TestCase;
class MyPlugin extends TestCase {
protected $myPlugin;
protected function setUp(): void
{
$this->myPlugin = new MyPlugin();
}
public function testRegister()
{
$this->assertInstanceOf('\App\MyPlugin', $this->myPlugin);
}
}
However I am always getting the error
Code:
Error: Call to undefined function App\add_action()
Code:
vendor/bin/phpunit
What is the best way of mocking the add_action calls purely within PHPUnit without having to rely on a third party solution?
Any help with this would be greatly appreciated.