woocommerce/tests/unit-tests/crud/data-store.php

110 lines
2.8 KiB
PHP

<?php
/**
* Data Store Tests
* @package WooCommerce\Tests\Product
* @since 3.0.0
*/
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.
*
* @since 3.0.0
*/
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.' );
}
/**
* Make sure ::load throws an exception for invalid data stores
*
* @since 3.0.0
*/
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.' );
}
/**
* Make sure we can swap out stores.
*
* @since 3.0.0
*/
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.
*
* @since 3.0.0
*/
function test_store_sub_type() {
$this->load_dummy_store();
$store = WC_Data_Store::load( 'dummy-sub' );
$this->assertEquals( 'WC_Dummy_Data_Store_CPT', $store->get_current_class_name() );
}
/* Helper Functions. */
/**
* Loads two dummy data store classes that can be swapped out for each other. Adds to the `woocommerce_data_stores` filter.
*
* @since 3.0.0
*/
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.
*
* @since 3.0.0
*/
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.
*
* @since 3.0.0
*/
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.
*
* @since 3.0.0
*/
function set_default_product_store( $store ) {
return 'WC_Dummy_Data_Store_CPT';
}
}