Launchpad
Launchpad
Launchpad
  • Introduction
  • General
    • Installation
    • Creating a subscriber
    • Contributing
  • Tutorials
    • Beginner documentation
      • Beginner handbook
        • Creating subscribers
        • Wiring subscribers
        • Installing local environment
        • Adding front-end
        • Building the plugin
      • Plugin life circle
        • Activation
        • Deactivation
        • Uninstall
    • Starting with the framework
    • Migrating to Launchpad
    • Notions
      • Framework concepts
        • Inversion of control
        • Subscribers
        • Dispatcher
      • Good practices
        • Hooks
          • Preventing magic constants
          • Decouple features
          • Sanitize filters output
      • Testing
        • Organize tests
  • CLI
    • Commands
    • Creating a command
  • Testing
    • Unit test
    • Fixtures
    • Integration test
  • Container
    • Architecture
    • Parameters
    • Providers
    • Auto wiring
    • Manual wiring
    • Activation/Deactivation
    • Inflectors
  • Modules
    • Definition
    • Listing
      • Action Scheduler
      • BerlinDB
      • Bus
      • Options
      • Renderer
      • Logger
      • Uninstaller
      • Filesystem
      • Front-end
      • Hook extractor
    • Creating a module
Powered by GitBook
On this page
  • Creating the class
  • Mocking classes and functions
  • Mock a class
  • Mock a function
  1. Testing

Unit test

First we we will how to create a simple testing class testing a method.

For that first we will have to create a file from the name of the method inside the tests/Unit/inc folder and following the namespace from the class the method belongs to.

Example if we want to test the method my_method from the class Launchpad\Engine\MyNamespace\MyClass , we will create the file myMethod.php in the folder tests/Unit/inc/Engine/MyNamespace/MyClass.

Creating the class

Inside that class we will have then to add the following content:

  • The namespace from your test that follows the path from your class in our example it is Launchpad\Tests\Unit\inc\Engine\MyNamespace\MyClass.

  • The definition from the class with the following name Test_ followed by the name of the method, for your example will be Test_MyMethod.

  • That class should be extending Launchpad\Tests\Unit\TestCase.

  • Finally that class should contain a public method starting by test and that describe the usage from the test in your case it will be testReturnAsExpected .

namespace Launchpad\Tests\Unit\inc\Engine\MyNamespace\MyClass;

use Launchpad\Tests\Unit\TestCase;

class Test_MyMethod extends TestCase {

   public function testReturnAsExpected() {

   }

}

Mocking classes and functions

Now that we know how to create a class we will now learn how to deal with a class using other or with a class using some external function from WordPress for example.

Let’s imagine the class we want to test has the following content:

namespace Launchpad\Engine\MyNamespace\MyClass;

class {
  protected $dependency;

  public function __construct(\MyDependency $dependency) {
    $this->dependency = $dependency;
  }
 
  public function my_method() {
   $id = $this->dependency->method(12);
   $post = get_post($id);
  }
}

We will in this case to mock two things:

  • The class MyDependency that our class use to use its interface without testing its content.

  • The method get_post that my_method use to prevent use from re-implementing the method.

Mock a class

In this boilerplate we use Mockery to mock classes.

To mock a class with Mockery we use the Mockery::mock method this way:

$mock = Mockery::mock(MyDependency::class);

Once we got the mock object we can then set expectation this way:

$mock->expects()->method(12)->andReturn(45);

Mock a function

To mock method in Rocker launcher we are using Brain Monkey.

Brain Monkey allows us to mock function with an interface close to Mockery.

To mock a function with Brain Monkey we can use the expects function this way:

use Brain\Monkey\Functions;

Functions\expect('get_post')->with(48)->once()->andReturn(false);
PreviousCreating a commandNextFixtures

Last updated 1 year ago

For more information on how Mockery work you can check .

For more information on how Brain Monkey work you can check .

their documentation
their documentation