2016-11-08 09:39:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2019-11-21 13:41:27 +00:00
|
|
|
* Data Store tests
|
|
|
|
*
|
2016-11-08 09:39:47 +00:00
|
|
|
* @package WooCommerce\Tests\Product
|
2019-11-21 13:41:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class for Data Store tests
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
|
|
|
class WC_Tests_Data_Store extends WC_Unit_Test_Case {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure WC_Data_Store returns an exception if we try to load a data
|
|
|
|
* store that doesn't exist.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
2019-11-21 13:41:27 +00:00
|
|
|
public function test_invalid_store_throws_exception() {
|
2016-11-08 09:39:47 +00:00
|
|
|
try {
|
2019-11-21 13:41:27 +00:00
|
|
|
new WC_Data_Store( 'bogus' );
|
2016-11-08 09:39:47 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$this->assertEquals( $e->getMessage(), 'Invalid data store.' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->fail( 'Invalid data store exception not correctly raised.' );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-03-01 17:24:30 +00:00
|
|
|
* Make sure ::load throws an exception for invalid data stores
|
2016-11-08 09:39:47 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
2019-11-21 13:41:27 +00:00
|
|
|
public function test_invalid_store_load_throws_exception() {
|
2017-03-01 17:24:30 +00:00
|
|
|
try {
|
2019-11-21 13:41:27 +00:00
|
|
|
WC_Data_Store::load( 'does-not-exist' );
|
2017-03-01 17:24:30 +00:00
|
|
|
} catch ( Exception $e ) {
|
|
|
|
$this->assertEquals( $e->getMessage(), 'Invalid data store.' );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->fail( 'Invalid data store exception not correctly raised.' );
|
2016-11-08 09:39:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make sure we can swap out stores.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
2019-11-21 13:41:27 +00:00
|
|
|
public function test_store_swap() {
|
2016-11-08 09:39:47 +00:00
|
|
|
$this->load_dummy_store();
|
|
|
|
|
|
|
|
$store = new WC_Data_Store( 'dummy' );
|
|
|
|
$this->assertEquals( 'WC_Dummy_Data_Store_CPT', $store->get_current_class_name() );
|
|
|
|
|
|
|
|
add_filter( 'woocommerce_dummy_data_store', array( $this, 'set_dummy_store' ) );
|
|
|
|
|
|
|
|
$store = new WC_Data_Store( 'dummy' );
|
|
|
|
$this->assertEquals( 'WC_Dummy_Data_Store_Custom_Table', $store->get_current_class_name() );
|
|
|
|
|
|
|
|
add_filter( 'woocommerce_dummy_data_store', array( $this, 'set_default_dummy_store' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test to see if `first_second ``-> returns to `first` if unregistered.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
2019-11-21 13:41:27 +00:00
|
|
|
public function test_store_sub_type() {
|
2016-11-08 09:39:47 +00:00
|
|
|
$this->load_dummy_store();
|
2019-11-21 14:52:47 +00:00
|
|
|
|
2016-11-22 13:54:51 +00:00
|
|
|
$store = WC_Data_Store::load( 'dummy-sub' );
|
2016-11-08 09:39:47 +00:00
|
|
|
$this->assertEquals( 'WC_Dummy_Data_Store_CPT', $store->get_current_class_name() );
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:52:47 +00:00
|
|
|
/**
|
|
|
|
* Test WC_Data_Store::__call().
|
|
|
|
*/
|
|
|
|
public function test_data_store_method_overloading() {
|
|
|
|
$this->load_dummy_store();
|
|
|
|
$store = WC_Data_Store::load( 'dummy-sub' );
|
|
|
|
$this->assertEquals(
|
|
|
|
array( 'first param', 'second param', 'third param' ),
|
|
|
|
$store->custom_method( 'first param', 'second param', 'third param', 'asdfsdf' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-11-08 20:27:21 +00:00
|
|
|
/* Helper Functions. */
|
2016-11-08 09:39:47 +00:00
|
|
|
|
|
|
|
/**
|
2016-11-22 13:54:51 +00:00
|
|
|
* Loads two dummy data store classes that can be swapped out for each other. Adds to the `woocommerce_data_stores` filter.
|
2016-11-08 09:39:47 +00:00
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
2019-11-21 13:41:27 +00:00
|
|
|
private function load_dummy_store() {
|
2019-05-01 22:05:00 +00:00
|
|
|
include_once dirname( dirname( dirname( __FILE__ ) ) ) . '/framework/class-wc-dummy-data-store.php';
|
2016-11-08 09:39:47 +00:00
|
|
|
add_filter( 'woocommerce_data_stores', array( $this, 'add_dummy_data_store' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a default class for the 'dummy' data store.
|
|
|
|
*
|
2019-11-21 13:41:27 +00:00
|
|
|
* @param array $stores Loaded data stores.
|
|
|
|
* @return array Modified list of data stores.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
2019-11-21 13:41:27 +00:00
|
|
|
public function add_dummy_data_store( $stores ) {
|
2016-11-08 09:39:47 +00:00
|
|
|
$stores['dummy'] = 'WC_Dummy_Data_Store_CPT';
|
|
|
|
return $stores;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function/filter to swap out the default dummy store for a different one.
|
|
|
|
*
|
2019-11-21 13:41:27 +00:00
|
|
|
* @param string $store Data store class name.
|
|
|
|
* @return string New data store class name.
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
2019-11-21 13:41:27 +00:00
|
|
|
public function set_dummy_store( $store ) {
|
2016-11-08 09:39:47 +00:00
|
|
|
return 'WC_Dummy_Data_Store_Custom_Table';
|
|
|
|
}
|
|
|
|
}
|