2016-11-08 09:39:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Data Store Tests
|
|
|
|
* @package WooCommerce\Tests\Product
|
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
|
|
|
*/
|
|
|
|
function test_invalid_store_throws_exception() {
|
|
|
|
try {
|
|
|
|
$product_store = new WC_Data_Store( 'bogus' );
|
|
|
|
} 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
|
|
|
*/
|
2017-03-01 17:24:30 +00:00
|
|
|
function test_invalid_store_load_throws_exception() {
|
|
|
|
try {
|
|
|
|
$product_store = WC_Data_Store::load( 'does-not-exist' );
|
|
|
|
} 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
|
|
|
*/
|
|
|
|
function test_store_swap() {
|
|
|
|
$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
|
|
|
*/
|
|
|
|
function test_store_sub_type() {
|
|
|
|
$this->load_dummy_store();
|
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() );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
function load_dummy_store() {
|
|
|
|
include_once( dirname( dirname( dirname( __FILE__ ) ) ) . '/framework/class-wc-dummy-data-store.php' );
|
|
|
|
add_filter( 'woocommerce_data_stores', array( $this, 'add_dummy_data_store' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a default class for the 'dummy' data store.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
|
|
|
function add_dummy_data_store( $stores ) {
|
|
|
|
$stores['dummy'] = 'WC_Dummy_Data_Store_CPT';
|
|
|
|
return $stores;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function/filter to swap out the default dummy store for a different one.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
|
|
|
function set_dummy_store( $store ) {
|
|
|
|
return 'WC_Dummy_Data_Store_Custom_Table';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function/filter to swap out the 'dummy' store for the default one.
|
|
|
|
*
|
2017-03-15 16:36:53 +00:00
|
|
|
* @since 3.0.0
|
2016-11-08 09:39:47 +00:00
|
|
|
*/
|
|
|
|
function set_default_product_store( $store ) {
|
|
|
|
return 'WC_Dummy_Data_Store_CPT';
|
|
|
|
}
|
|
|
|
}
|