Skip to content

Instantly share code, notes, and snippets.

@oxyc
Last active April 25, 2022 18:28
Show Gist options
  • Save oxyc/1efee855a45d0e7dfc98160865ede834 to your computer and use it in GitHub Desktop.
Save oxyc/1efee855a45d0e7dfc98160865ede834 to your computer and use it in GitHub Desktop.
{
"phpVersion": "8.0",
"plugins": [
".",
"../woocommerce"
],
"config": {
"WP_TESTS_DOMAIN": "localhost:8080",
"WP_TESTS_EMAIL": "test@test.org",
"WP_TESTS_TITLE": "M3 Customers",
"WP_PHP_BINARY": "php"
}
}

Testing

npm -g i @wordpress/env
wp-env start
wp-env run phpunit 'phpunit -c /var/www/html/wp-content/plugins/<plugin>/phpunit.xml'
<?php
// actually in tests/bootstrap.php
/**
* PHPUnit bootstrap file
*/
// Composer autoloader must be loaded before WP_PHPUNIT__DIR will be available
require_once dirname(__DIR__) . '/vendor/autoload.php';
// Give access to tests_add_filter() function.
require_once getenv('WP_PHPUNIT__DIR') . '/includes/functions.php';
tests_add_filter('muplugins_loaded', function() {
require dirname(__DIR__, 2) . '/woocommerce/woocommerce.php';
require dirname(__DIR__) . '/plugin.php';
} );
// Start up the WP testing environment.
require getenv('WP_PHPUNIT__DIR') . '/includes/bootstrap.php';
{
"name": "...",
"type": "wordpress-plugin",
"authors": [],
"keywords": [
"wordpress"
],
"require": {},
"require-dev": {
"squizlabs/php_codesniffer": "^2.5.1",
"phpunit/phpunit": "^9.5",
"wp-phpunit/wp-phpunit": "^5.9",
"yoast/phpunit-polyfills": "^1.0"
},
"autoload": {
"psr-4": {
"M3\\Customers\\": "src/"
}
},
"scripts": {
"test": [
"phpunit"
]
},
"archive" : {
"exclude": [
".gitignore"
]
}
}
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="unit">
<directory prefix="test-" suffix=".php">./tests/</directory>
</testsuite>
</testsuites>
<php>
<env name="WP_PHPUNIT__TESTS_CONFIG" value="tests/wp-config.php" />
</php>
</phpunit>
<?php
// actually in tests/test-rest.php
use M3\Customers\Models\Customer;
class SampleTest extends WP_UnitTestCase
{
protected function doFixtureRequest(): WP_REST_Response
{
$fixture = file_get_contents(__DIR__ . '/fixtures/create.json');
$request = new WP_REST_Request('POST', '/m3/v1/customers');
$request->set_body($fixture);
$request->set_header('Content-Type', 'application/json');
return rest_do_request($request);
}
/**
* A single example test.
*/
function test_sample(): void
{
wp_get_current_user()->add_role('administrator');
$response = $this->doFixtureRequest();
$this->assertEquals($response->get_status(), 200);
$this->assertEquals($response->get_data(), (object) [
'updated' => 0,
'created' => 1,
'trashed' => 0,
]);
$response = $this->doFixtureRequest();
$this->assertEquals($response->get_data(), (object) [
'updated' => 1,
'created' => 0,
'trashed' => 0,
]);
$user = get_user_by('email', 'email@email.com');
$this->assertNotFalse($user);
$this->assertEquals($user->first_name, 'billing.Firstname');
$this->assertEquals(get_user_meta($user->ID, Customer::CUSTOMER_ID, true), 1234567);
$customer = new WC_Customer($user->ID);
$this->assertEquals($customer->get_shipping_address(), 'shipping.Address');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment