PHP code example of gin0115 / wpunit-helpers

1. Go to this page and download the library: Download gin0115/wpunit-helpers 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/ */

    

gin0115 / wpunit-helpers example snippets


$box = Meta_Box_Inspector::initialise()->find('my_meta_box_key');
$this->assertInstanceOf(Meta_Box_Entity::class, $box);
$this->assertEquals('My Title', $box->title);

$page = Menu_Page_Inspector::initialise()->find_parent_page('parent_page_slug');
$this->assertInstanceOf(Menu_Page_Entity::class, $page);
$this->assertEquals('My Settings', $page->menu_title);

$post_meta = Meta_Data_Inspector::initialise()->find_post_meta('post', 'my_key');
$this->assertInstanceOf(Meta_Data_Entity::class, $post_meta);
$this->assertEquals('This is my meta field', $post_meta->description);

WP_Dependencies::install_remote_plugin_from_zip(
    'https://the-url.tc/woocommerce.zip', 'path/to/test_wp/root/'
);
WP_Dependencies::activate_plugin('woocommerce/woocommerce.php');

//  Access protected & privates properties.
Objects::get_property($instnace, 'property');
// Set protected or private properties.
Objects::set_property($instnace, 'property', 'new value');
// Invoke private or protected method.
Objects::invoke_method($instance, 'method', ['the', 'args']);
 
// iterable_map_with allows array_map to be done with access to the key and as many other
// values you wish to pass.
$result = Utils::iterable_map_with( 

    function($key, $value, $spacer){
        return $key . $spacer . $value;
    }, 
    ['key1'=>'value1', 'key2' => 'value2'],
    ' -|- '

); 
var_dump($result); // ['key1 -|- value1', 'key2 -|- value2']


$wpdb = new Logable_WPDB();

// Set a return value.
$wpdb->then_return = 1;

// Mock an insert 
$result = $wpdb->insert('table_name', ['col1'=>'value1'], ['%s']);

// Get back the set return value
var_dump($result);

// Access the useage log
$log = $wpdb->usage_log;
var_dump($log);
/**
 * ['insert' => 
 *   [0] => [
 *     'table' => 'table_name',
 *     'data' => ['col1'=>'value1'],
 *     'format' => ['%s'],
 *   ]
 * ]
 */