Skip to content

Commit

Permalink
Add unit tests for orders/products
Browse files Browse the repository at this point in the history
  • Loading branch information
gdarko committed Apr 22, 2023
1 parent 40ce6ab commit 06b160e
Show file tree
Hide file tree
Showing 16 changed files with 1,043 additions and 172 deletions.
46 changes: 39 additions & 7 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,43 @@
assets/etc/defuse.txt
assets/etc/secret.txt
logs/debug.log
logs/error.log
# Composer
vendor
vendor/*
.idea
.idea/*
/composer.phar
composer.lock

# Editors
project.xml
project.properties
/nbproject/private/
.buildpath
.project
.settings*
.idea
.vscode
*.sublime-project
*.sublime-workspace
.sublimelinterrc

# Grunt
/node_modules/
none

# Sass
.sass-cache/

# OS X metadata
.DS_Store

# Windows junk
Thumbs.db

# Tests
/coverage
/tests/bin/tmp
phpcs.xml
phpunit.xml
.phpunit.result.cache

# DLM
assets/etc/defuse.txt
assets/etc/secret.txt
logs/debug.log
logs/error.log
71 changes: 0 additions & 71 deletions .travis.yml

This file was deleted.

15 changes: 4 additions & 11 deletions .phpcs.xml.dist → phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,25 @@
<arg name="extensions" value="php"/>
<arg name="parallel" value="8"/><!-- Enables parallel processing when available for faster results. -->

<!-- Rules: Check PHP version compatibility -->
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="5.6-"/>
<!-- https://github.com/PHPCompatibility/PHPCompatibilityWP -->
<rule ref="PHPCompatibilityWP"/>

<!-- Rules: WordPress Coding Standards -->
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<config name="minimum_supported_wp_version" value="4.6"/>
<config name="minimum_supported_wp_version" value="4.7"/>
<rule ref="WordPress"/>
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<!-- Value: replace the function, class, and variable prefixes used. Separate multiple prefixes with a comma. -->
<property name="prefixes" type="array" value="my-plugin"/>
<property name="prefixes" type="array" value="digital-license-manager"/>
</properties>
</rule>
<rule ref="WordPress.WP.I18n">
<properties>
<!-- Value: replace the text domain used. -->
<property name="text_domain" type="array" value="my-plugin"/>
<property name="text_domain" type="array" value="digital-license-manager"/>
</properties>
</rule>
<rule ref="WordPress.WhiteSpace.ControlStructureSpacing">
<properties>
<property name="blank_line_check" value="true"/>
</properties>
</rule>
</ruleset>
</ruleset>
21 changes: 11 additions & 10 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
<?xml version="1.0"?>
<phpunit
bootstrap="tests/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
bootstrap="tests/unit/bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="default">
<directory prefix="test-" suffix=".php">./tests/</directory>
<testsuite name="DigitalLicenseManager">
<directory suffix=".php">./tests/unit</directory>
</testsuite>
</testsuites>
</phpunit>

</phpunit>
15 changes: 15 additions & 0 deletions tests/WP_UnitTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
/**
* Class WP_UnitTestCase
*
* @package WooCommerce\Payments\Tests
*/

/**
* This stub assists IDE in recognizing PHPUnit tests.
*
* Class WP_UnitTestCase
*/
class WP_UnitTestCase extends \Yoast\PHPUnitPolyfills\TestCases\TestCase {

}
53 changes: 0 additions & 53 deletions tests/bootstrap.php

This file was deleted.

20 changes: 0 additions & 20 deletions tests/test-licenses.php

This file was deleted.

81 changes: 81 additions & 0 deletions tests/unit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* PHPUnit bootstrap file
*
* @package WooCommerce\Subscriptions
*/

$_tests_dir = getenv( 'WP_TESTS_DIR' );

if ( ! $_tests_dir ) {
$_tests_dir = rtrim( sys_get_temp_dir(), '/\\' ) . '/wordpress-tests-lib';
}

if ( ! file_exists( $_tests_dir . '/includes/functions.php' ) ) {
echo "Could not find $_tests_dir/includes/functions.php, have you run bin/install-wp-tests.sh ?" . PHP_EOL; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
exit( 1 );
}

if ( PHP_VERSION_ID >= 80000 && file_exists( $_tests_dir . '/includes/phpunit7/MockObject' ) ) {
// WP Core test library includes patches for PHPUnit 7 to make it compatible with PHP8.
require_once $_tests_dir . '/includes/phpunit7/MockObject/Builder/NamespaceMatch.php';
require_once $_tests_dir . '/includes/phpunit7/MockObject/Builder/ParametersMatch.php';
require_once $_tests_dir . '/includes/phpunit7/MockObject/InvocationMocker.php';
require_once $_tests_dir . '/includes/phpunit7/MockObject/MockMethod.php';
}


// Give access to tests_add_filter() function.
require_once $_tests_dir . '/includes/functions.php';

/**
* Manually load the plugin being tested.
*/
function _manually_load_plugin() {

$_plugin_dir = dirname( __FILE__ ) . '/../../';

if ( ! function_exists( 'activate_plugin' ) ) {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}

require_once dirname( __FILE__ ) . '/helpers/class-dlm-helper-settings.php';
DLM_Helper_Settings::setDefaults();

tests_add_filter( 'dlm_mock_is_plugin_active', '__return_true' );

// Required files for the plugin
$required = [
realpath( $_plugin_dir . '..' ) . '/woocommerce/woocommerce.php',
];
foreach ( $required as $item ) {
if ( file_exists( $item ) ) {
echo 'Loaded: '.$item . PHP_EOL;
require_once $item;
} else {
echo "Could not find " . plugin_basename( $item ) . " plugin which is required for unit tests.";
exit( 1 );
}
}

// Set a default currency to be used for the multi-currency tests because the default
// is not loaded even though it's set during the tests setup.
update_option( 'woocommerce_currency', 'USD' );
require $_plugin_dir . '/digital-license-manager.php';

IdeoLogix\DigitalLicenseManager\Setup::install( false );

}

tests_add_filter( 'muplugins_loaded', '_manually_load_plugin' );

// Need those polyfills to run tests in CI.
require_once dirname( __FILE__ ) . '/../../vendor/yoast/phpunit-polyfills/phpunitpolyfills-autoload.php';

// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';

// We use outdated PHPUnit version, which emits deprecation errors in PHP 7.4 (deprecated reflection APIs).
if ( defined( 'PHP_VERSION_ID' ) && PHP_VERSION_ID >= 70400 ) {
error_reporting( error_reporting() ^ E_DEPRECATED ); // phpcs:ignore
}
Loading

0 comments on commit 06b160e

Please sign in to comment.