diff --git a/composer.json b/composer.json index f59abf92..7c57eab3 100644 --- a/composer.json +++ b/composer.json @@ -4,6 +4,10 @@ "homepage": "https://www.omise.co/", "license": "MIT", "require-dev": { - "phpunit/phpunit": "^5.7 || ^9.5" + "phpunit/phpunit": "^5.7 || ^9.5", + "mockery/mockery": "^1.6" + }, + "scripts": { + "test": "vendor/bin/phpunit --testdox --colors" } } diff --git a/omise-woocommerce.php b/omise-woocommerce.php index 84bb6de3..f0511243 100644 --- a/omise-woocommerce.php +++ b/omise-woocommerce.php @@ -47,12 +47,26 @@ class Omise */ public function __construct() { + add_action('before_woocommerce_init', [$this, 'enable_hpos']); add_action('plugins_loaded', array($this, 'check_dependencies')); add_action('woocommerce_init', array($this, 'init')); do_action('omise_initiated'); add_action('admin_notices', [$this, 'embedded_form_notice']); } + /** + * enable high performance order storage(HPOS) feature + */ + public function enable_hpos() { + if (class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)) { + \Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility( + 'custom_order_tables', + __FILE__, + true + ); + } + } + /** * Notice for users informing about embedded form */ diff --git a/tests/unit/omise-woocommerce-test.php b/tests/unit/omise-woocommerce-test.php new file mode 100644 index 00000000..98b84154 --- /dev/null +++ b/tests/unit/omise-woocommerce-test.php @@ -0,0 +1,59 @@ +model = Omise::instance(); + } + + /** + * close mockery after test cases are done + */ + public function tearDown(): void + { + Mockery::close(); + } + + /** + * Making sure that when FeaturesUtil class do not exist, + * it doesn't throw any error + */ + public function test_when_features_util_class_do_not_exist() + { + $this->model->enable_hpos(); + $this->assertFalse(class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)); + } + + /** + * Making sure that when FeaturesUti class exist, + * it doesn't throw any error and the 'declare_compatibility' method should be called once + */ + public function test_when_features_util_class_exist() + { + $featuresUtilMock = Mockery::mock('alias:\Automattic\WooCommerce\Utilities\FeaturesUtil'); + $featuresUtilMock->shouldReceive('declare_compatibility')->once(); + $this->model->enable_hpos(); + $this->assertTrue(class_exists(\Automattic\WooCommerce\Utilities\FeaturesUtil::class)); + } +}