PHP code example of pfaciana / brain-monkey-doubles

1. Go to this page and download the library: Download pfaciana/brain-monkey-doubles library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pfaciana / brain-monkey-doubles example snippets


// tests/Pest.php
use Brain\Monkey;
use BrainMonkey\Doubles;

pest()->beforeEach(function () {
    Doubles::setUp();   // installs the doubles first
    Monkey\setUp();
})->afterEach(function () {
    Monkey\tearDown();
    Doubles::tearDown();
});

use BrainMonkey\Functions;

// global beforeEach — silence WP notices and grant caps for every test
Functions\stub('_doing_it_wrong')->justReturn();
Functions\stub('current_user_can')->alias(fn() => true);

// one file's beforeEach — revoke caps for these tests only
Functions\stub('current_user_can')->alias(fn() => false);

it('asserts a call that already has a stub baseline', function () {
    // native Brain Monkey expect() would silently never fire over the stub above — but this does!
    Functions\expect('current_user_can')
        ->once()
        ->with('edit_posts')
        ->andReturn(true);

    expect(current_user_can('edit_posts'))->toBeTrue();
});

// greeting.php — your plugin
namespace Greeting;

function register(): void {
    add_filter('greeting', fn($name) => "Hello, $name");
}

// GreetingTest.php
use Brain\Monkey\Filters;

beforeEach(fn() => Greeting\register());

it('runs the filter callback', function () {
    expect(apply_filters('greeting', 'World'))->toBe('Hello, World');
});

it('applies the greeting filter', function () {
    Filters\expectApplied('greeting')->once()->with('World');

    apply_filters('greeting', 'World');
});

it('registers the greeting filter', function () {
    expect(has_filter('greeting'))->toBeTrue();
});

it('fires once per apply', function () {
    apply_filters('greeting', 'World');
    apply_filters('greeting', 'Cleveland!');

    expect(did_filter('greeting'))->toBe(2);
});