PHP code example of inpsyde / wp-tests-starter

1. Go to this page and download the library: Download inpsyde/wp-tests-starter 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/ */

    

inpsyde / wp-tests-starter example snippets


public function testPersistBook(): void {

    $book = new Book('The Da Vinci Code', 'Dan Brown', '2003');
    $testee = new BookRepository($GLOBALS['wpdb']);
    $testee->persist($book); // maps book to WP_Post object and post meta

    self::assertGreaterThan(0, $book->id());

    $wpPost = get_post($book->id());

    self::assertSame('The Da Vinci Code', $wpPost->post_title);
    self::assertSame('2003', get_post_meta($book->id(), '_publishing_year', true));
    self::assertSame('Dan Brown', get_post_meta($book->id(), '_author', true));
}



declare(strict_types=1);

namespace MyProject\Tests;

use Inpsyde\WpTestsStarter\WpTestsStarter;

$projectDir = dirname(__DIR__);

') // Databse credentials in URL format, set in phpunit.xml.dist
);

// Some configuration:
$starter
    // Install WP core as multisite
    ->testAsMultisite()
    // boostrap your plugin or module code
    ->addActivePlugin(static function()  {
        (new MyModule())->init();
    })
    // add filters early
    ->addFilter('my_app.modules', static function(array $modules): array {
        // whatever, it's just an example
        return  $modules;
    })
    //finally load WordPress
    ->bootstrap();


//either
$starter = new WpTestsStarter($baseDir, $dbUrl);
// or
$starter->useDbUrl($dbUrl);


define('DB_HOST', 'localhost:3306');
define('DB_USER', 'user');
define('DB_PASSWORD', 'password');
define('DB_NAME', 'test_db');
define('DB_CHARSET', 'utf8mb4');
define('DB_COLLATE', 'utf8_general_ci');
$GLOBALS['table_prefix'] = 'wp_tests_'

$starter
    ->useDbHost('localhost')
    ->useDbUser('user');
    // and so on
xml
<phpunit
    bootstrap="tests/bootstrap.dist.php"
>
    <php>
        <env name="WPTS_DB_URL" value="mysql://user:password@host/db_name?table_prefix=wp_test_"/>
    </php>
    
    <testsuites>
        <testsuite name="integration">
            <directory suffix="Test.php">./tests/integration</directory>
        </testsuite>
    </testsuites>
</phpunit>