Merge pull request #18551 from liquidweb/standards/test-cleanup

Coding standards cleanup, primarily for the coupon unit tests
This commit is contained in:
Mike Jolley 2018-01-22 15:45:38 +00:00 committed by GitHub
commit 8ffa1c2d36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 95 additions and 76 deletions

View File

@ -52,5 +52,15 @@
</rule>
<rule ref="WordPress.Files.FileName.InvalidClassFileName">
<exclude-pattern>includes/**/abstract-*.php</exclude-pattern>
<exclude-pattern>tests/</exclude-pattern>
</rule>
<rule ref="Generic.Commenting">
<exclude-pattern>tests/</exclude-pattern>
</rule>
<rule ref="Squiz.Commenting">
<exclude-pattern>tests/</exclude-pattern>
</rule>
<rule ref="PEAR.Functions.FunctionCallSignature.EmptyLine">
<exclude-pattern>tests/e2e-tests/</exclude-pattern>
</rule>
</ruleset>

View File

@ -25,20 +25,24 @@ class WC_Unit_Tests_Bootstrap {
*/
public function __construct() {
ini_set( 'display_errors','on' );
// phpcs:disable WordPress.PHP.DiscouragedPHPFunctions, WordPress.PHP.DevelopmentFunctions
ini_set( 'display_errors', 'on' );
error_reporting( E_ALL );
// phpcs:enable WordPress.PHP.DiscouragedPHPFunctions, WordPress.PHP.DevelopmentFunctions
// Ensure server variable is set for WP email functions.
// phpcs:disable WordPress.VIP.SuperGlobalInputUsage.AccessDetected
if ( ! isset( $_SERVER['SERVER_NAME'] ) ) {
$_SERVER['SERVER_NAME'] = 'localhost';
}
// phpcs:enable WordPress.VIP.SuperGlobalInputUsage.AccessDetected
$this->tests_dir = dirname( __FILE__ );
$this->plugin_dir = dirname( $this->tests_dir );
$this->wp_tests_dir = getenv( 'WP_TESTS_DIR' ) ? getenv( 'WP_TESTS_DIR' ) : '/tmp/wordpress-tests-lib';
// load test function so tests_add_filter() is available
require_once( $this->wp_tests_dir . '/includes/functions.php' );
require_once $this->wp_tests_dir . '/includes/functions.php';
// load WC
tests_add_filter( 'muplugins_loaded', array( $this, 'load_wc' ) );
@ -47,7 +51,7 @@ class WC_Unit_Tests_Bootstrap {
tests_add_filter( 'setup_theme', array( $this, 'install_wc' ) );
// load the WP testing environment
require_once( $this->wp_tests_dir . '/includes/bootstrap.php' );
require_once $this->wp_tests_dir . '/includes/bootstrap.php';
// load WC testing framework
$this->includes();
@ -60,7 +64,7 @@ class WC_Unit_Tests_Bootstrap {
*/
public function load_wc() {
define( 'WC_TAX_ROUNDING_MODE', 'auto' );
require_once( $this->plugin_dir . '/woocommerce.php' );
require_once $this->plugin_dir . '/woocommerce.php';
}
/**
@ -73,7 +77,7 @@ class WC_Unit_Tests_Bootstrap {
// Clean existing install first.
define( 'WP_UNINSTALL_PLUGIN', true );
define( 'WC_REMOVE_ALL_DATA', true );
include( $this->plugin_dir . '/uninstall.php' );
include $this->plugin_dir . '/uninstall.php';
WC_Install::install();
@ -81,11 +85,11 @@ class WC_Unit_Tests_Bootstrap {
if ( version_compare( $GLOBALS['wp_version'], '4.7', '<' ) ) {
$GLOBALS['wp_roles']->reinit();
} else {
$GLOBALS['wp_roles'] = null;
$GLOBALS['wp_roles'] = null; // WPCS: override ok.
wp_roles();
}
echo 'Installing WooCommerce...' . PHP_EOL;
echo esc_html( 'Installing WooCommerce...' . PHP_EOL );
}
/**
@ -96,28 +100,28 @@ class WC_Unit_Tests_Bootstrap {
public function includes() {
// framework
require_once( $this->tests_dir . '/framework/class-wc-unit-test-factory.php' );
require_once( $this->tests_dir . '/framework/class-wc-mock-session-handler.php' );
require_once( $this->tests_dir . '/framework/class-wc-mock-wc-data.php' );
require_once( $this->tests_dir . '/framework/class-wc-mock-wc-object-query.php' );
require_once( $this->tests_dir . '/framework/class-wc-payment-token-stub.php' );
require_once( $this->tests_dir . '/framework/vendor/class-wp-test-spy-rest-server.php' );
require_once $this->tests_dir . '/framework/class-wc-unit-test-factory.php';
require_once $this->tests_dir . '/framework/class-wc-mock-session-handler.php';
require_once $this->tests_dir . '/framework/class-wc-mock-wc-data.php';
require_once $this->tests_dir . '/framework/class-wc-mock-wc-object-query.php';
require_once $this->tests_dir . '/framework/class-wc-payment-token-stub.php';
require_once $this->tests_dir . '/framework/vendor/class-wp-test-spy-rest-server.php';
// test cases
require_once( $this->tests_dir . '/framework/class-wc-unit-test-case.php' );
require_once( $this->tests_dir . '/framework/class-wc-api-unit-test-case.php' );
require_once( $this->tests_dir . '/framework/class-wc-rest-unit-test-case.php' );
require_once $this->tests_dir . '/framework/class-wc-unit-test-case.php';
require_once $this->tests_dir . '/framework/class-wc-api-unit-test-case.php';
require_once $this->tests_dir . '/framework/class-wc-rest-unit-test-case.php';
// Helpers
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-product.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-coupon.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-fee.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-shipping.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-customer.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-order.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-shipping-zones.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-payment-token.php' );
require_once( $this->tests_dir . '/framework/helpers/class-wc-helper-settings.php' );
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-product.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-coupon.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-fee.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-shipping.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-customer.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-order.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-shipping-zones.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-payment-token.php';
require_once $this->tests_dir . '/framework/helpers/class-wc-helper-settings.php';
}
/**

View File

@ -15,12 +15,12 @@ class WC_Tests_Coupon extends WC_Unit_Test_Case {
$string_code_1 = 'test';
// Coupon with a standard string code.
$coupon_1 = new WC_Coupon;
$coupon_1 = new WC_Coupon();
$coupon_1->set_code( $string_code_1 );
$coupon_1->save();
// Coupon with a string code that is the same as coupon 1's ID.
$coupon_2 = new WC_Coupon;
$coupon_2 = new WC_Coupon();
$coupon_2->set_code( (string) $coupon_1->get_id() );
$coupon_2->save();

View File

@ -10,7 +10,7 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
*
* @since 3.0.0
*/
function test_coupon_store_loads() {
public function test_coupon_store_loads() {
$store = new WC_Data_Store( 'coupon' );
$this->assertTrue( is_callable( array( $store, 'read' ) ) );
$this->assertEquals( 'WC_Coupon_Data_Store_CPT', $store->get_current_class_name() );
@ -20,9 +20,9 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
* Test coupon create.
* @since 3.0.0
*/
function test_coupon_create() {
public function test_coupon_create() {
$code = 'coupon-' . time();
$coupon = new WC_Coupon;
$coupon = new WC_Coupon();
$coupon->set_code( $code );
$coupon->set_description( 'This is a test comment.' );
$coupon->save();
@ -35,7 +35,7 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
* Test coupon deletion.
* @since 3.0.0
*/
function test_coupon_delete() {
public function test_coupon_delete() {
$coupon = WC_Helper_Coupon::create_coupon();
$coupon_id = $coupon->get_id();
$this->assertNotEquals( 0, $coupon_id );
@ -61,7 +61,7 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
* Test coupon update.
* @since 3.0.0
*/
function test_coupon_update() {
public function test_coupon_update() {
$coupon = WC_Helper_Coupon::create_coupon();
$coupon_id = $coupon->get_id();
$this->assertEquals( 'dummycoupon', $coupon->get_code() );
@ -75,9 +75,9 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
* Test coupon reading from the DB.
* @since 3.0.0
*/
function test_coupon_read() {
public function test_coupon_read() {
$code = 'coupon-' . time();
$coupon = new WC_Coupon;
$coupon = new WC_Coupon();
$coupon->set_code( $code );
$coupon->set_description( 'This is a test coupon.' );
$coupon->set_usage_count( 5 );
@ -95,7 +95,7 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
* Test coupon saving.
* @since 3.0.0
*/
function test_coupon_save() {
public function test_coupon_save() {
$coupon = WC_Helper_Coupon::create_coupon();
$coupon_id = $coupon->get_id();
$coupon->set_code( 'dummycoupon2' );
@ -104,7 +104,7 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
$this->assertEquals( 'dummycoupon2', $coupon->get_code() );
$this->assertEquals( $coupon_id, $coupon->get_id() );
$new_coupon = new WC_Coupon;
$new_coupon = new WC_Coupon();
$new_coupon->set_code( 'dummycoupon3' );
$new_coupon->save();
$new_coupon_id = $new_coupon->get_id();
@ -116,7 +116,7 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
* Test coupon date saving/loading.
* @since 3.0.0
*/
function test_coupon_date_saving() {
public function test_coupon_date_saving() {
$expiry_date = time() - 10;
$coupon = WC_Helper_Coupon::create_coupon( 'coupon-' . time() );
@ -132,7 +132,7 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case {
* Test coupon increase, decrease, user usage count methods.
* @since 3.0.0
*/
function test_coupon_usage_magic_methods() {
public function test_coupon_usage_magic_methods() {
$coupon = WC_Helper_Coupon::create_coupon();
$user_id = 1;

View File

@ -12,7 +12,7 @@ class WC_Tests_Coupon_Data extends WC_Unit_Test_Case {
* @return string
* @since 3.0.0
*/
function get_function_name( $function ) {
public function get_function_name( $function ) {
if ( 'exclude_product_ids' === $function ) {
$function = 'excluded_product_ids';
} elseif ( 'exclude_product_categories' === $function ) {
@ -122,7 +122,13 @@ class WC_Tests_Coupon_Data extends WC_Unit_Test_Case {
* setting these values this way will also throw a deprecated notice so we will let
* PHPUnit know that its okay to continue.
*/
$legacy_keys = array( 'product_ids', 'exclude_product_ids', 'individual_use', 'free_shipping', 'exclude_sale_items' );
$legacy_keys = array(
'product_ids',
'exclude_product_ids',
'individual_use',
'free_shipping',
'exclude_sale_items',
);
$this->expected_doing_it_wrong = array_merge( $this->expected_doing_it_wrong, $legacy_keys );
$code = 'bc_manual_coupon_' . time();
$coupon = new WC_Coupon( $code );
@ -157,7 +163,6 @@ class WC_Tests_Coupon_Data extends WC_Unit_Test_Case {
* @since 3.0.0
*/
public function test_coupon_getters_and_setters() {
$time = time();
$standard_getters_and_setters = array(
'code' => 'test',
'description' => 'hello world',
@ -180,7 +185,7 @@ class WC_Tests_Coupon_Data extends WC_Unit_Test_Case {
'used_by' => array( 1 ),
);
$coupon = new WC_Coupon;
$coupon = new WC_Coupon();
foreach ( $standard_getters_and_setters as $function => $value ) {
$function = $this->get_function_name( $function );
$coupon->{"set_{$function}"}( $value );

View File

@ -40,7 +40,7 @@ class WC_Tests_Functions extends WC_Unit_Test_Case {
* @since 2.5.0
*/
public function test_wc_coupons_enabled() {
$this->assertEquals( apply_filters( 'woocommerce_coupons_enabled', get_option( 'woocommerce_enable_coupons' ) == 'yes' ), wc_coupons_enabled() );
$this->assertEquals( apply_filters( 'woocommerce_coupons_enabled', get_option( 'woocommerce_enable_coupons' ) === 'yes' ), wc_coupons_enabled() );
}
/**