Skip to content

Commit

Permalink
Start on tests for testing CRUD meta.
Browse files Browse the repository at this point in the history
Includes Mock object for testing against the abstract WC_Data.
  • Loading branch information
justinshreve committed Mar 14, 2016
1 parent f572a88 commit 97c0edf
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 7 deletions.
10 changes: 5 additions & 5 deletions includes/abstracts/abstract-wc-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ abstract class WC_Data {
* Returns the unique ID for this object.
* @return int
*/
abstract public function get_id();
abstract public function get_id();

/**
* Returns all data for this object.
Expand All @@ -80,7 +80,7 @@ abstract public function update();
/**
* Updates object data in the database.
*/
abstract public function delete();
abstract public function delete();

/**
* Save should create or update based on object existance.
Expand Down Expand Up @@ -127,7 +127,7 @@ public function get_meta( $key = '', $single = true ) {
if ( $single ) {
$value = $this->_meta_data[ current( $meta_ids ) ]->value;
} else {
$value = array_intersect_key( $this->_meta_data, $meta_ids );
$value = array_intersect_key( $this->_meta_data, array_flip( $meta_ids ) );
}
}

Expand Down Expand Up @@ -286,10 +286,10 @@ private function _get_db_info() {
$table = $wpdb->prefix;
// If we are dealing with a type of metadata that is not a core type, the table should be prefixed.
if ( ! in_array( $this->_meta_type, array( 'post', 'user', 'comment', 'term' ) ) ) {
$table = 'woocommerce_';
$table .= 'woocommerce_';
}

$table = $this->_meta_type . 'meta';
$table .= $this->_meta_type . 'meta';

// Figure out our field names.
if ( 'post' === $this->_meta_type ) {
Expand Down
2 changes: 1 addition & 1 deletion includes/abstracts/abstract-wc-payment-token.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @category Abstract Class
* @author WooThemes
*/
abstract class WC_Payment_Token implements WC_Data {
abstract class WC_Payment_Token {

/** @protected int Token ID. */
protected $id;
Expand Down
1 change: 1 addition & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ 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-payment-token-stub.php' );

// test cases
Expand Down
140 changes: 140 additions & 0 deletions tests/framework/class-wc-mock-wc-data.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* Used for exposing and testing the various Abstract WC_Data methods.
*/
class WC_Mock_WC_data extends WC_Data {

/**
* Data array
*/
protected $_data = array(
'id' => 0,
'content' => '',
);

// see WC_Data
protected $_cache_group = '';
protected $_meta_type = 'post';
protected $object_id_field_for_meta = '';
protected $_internal_meta_keys = array();

/*
|--------------------------------------------------------------------------
| Setters for internal WC_Data properties.
|--------------------------------------------------------------------------
| Normally we wouldn't want to be able to change this once the class is defined,
| but to make testing different types of meta/storage, we should be able to
| switch out our class settings.
| These functions just change the properties set above.
*/


function set_cache_group( $cache_group ) {
$this->_cache_group = $cache_group;
}

function set_meta_type( $meta_type ) {
$this->_meta_type = $meta_type;
}

function set_object_id_field( $object_id_field ) {
$this->object_id_field_for_meta = $object_id_field;
}

function set_internal_meta_keys( $internal_meta_keys ) {
$this->_internal_meta_keys = $internal_meta_keys;
}

/*
|--------------------------------------------------------------------------
| Abstract methods.
|--------------------------------------------------------------------------
| Define the abstract methods WC_Data classes expect, so we can go on to
| testing the good bits.
*/

public function __construct( $id = '' ) {
if ( ! empty( $id ) ) {
$this->read( $id );
}
}

public function get_id() {
return intval( $this->_data['id'] );
}

public function get_content() {
return $this->_data['content'];
}

public function set_content( $content ) {
$this->_data['content'] = $content;
}

public function get_data() {
return array_merge(
$this->_data,
array(
'meta_data' => $this->get_meta_data(),
)
);
}

public function create() {
if ( 'user' === $this->_meta_type ) {
$content_id = wc_create_new_customer( $this->get_content(), 'username-' . time(), 'hunter2' );
} else {
$content_id = wp_insert_post( array ( 'post_title' => $this->get_content() ) );
}

if ( $content_id ) {
$this->_data['id'] = $content_id;
}
}

public function read( $id ) {
if ( empty( $id ) || ! ( $post_object = get_post( $id ) ) ) {
return;
}
$this->_data['id'] = absint( $post_object->ID );
$this->set_content( $post_object->post_title );
$this->read_meta_data();
}

public function update() {
global $wpdb;
$content_id = $this->get_id();

if ( 'user' === $this->_meta_type ) {
wp_update_user( array( 'ID' => $customer_id, 'user_email' => $this->get_content() ) );
} else {
wp_update_post( array( 'ID' => $content_id, 'post_title' => $this->get_content() ) );
}
}

public function delete() {
if ( 'user' === $this->_meta_type ) {
wp_delete_user( $this->get_id() );
} else {
wp_delete_post( $this->get_id() );
}
}

public function save() {
if ( ! $this->get_id() ) {
$this->create();
} else {
$this->update();
}
$this->save_meta_data();
}

/*
|--------------------------------------------------------------------------
| Provide a public interface to a few of our.
|--------------------------------------------------------------------------
| Define the abstract methods WC_Data classes expect, so we can go on to
| testing the good bits.
*/

}
106 changes: 106 additions & 0 deletions tests/unit-tests/crud/meta.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php
namespace WooCommerce\Tests\CRUD;

/**
* Meta
* @package WooCommerce\Tests\CRUD
*/
class Meta extends \WC_Unit_Test_Case {

public function create_test_post() {
$object = new \WC_Mock_WC_data();
$object->set_content( 'testing' );
$object->save();
$object_id = $object->get_id();
add_metadata( 'post', $object_id, 'test_meta_key', 'val1', true );
add_metadata( 'post', $object_id, 'test_multi_meta_key', 'val2' );
add_metadata( 'post', $object_id, 'test_multi_meta_key', 'val3' );
$object->read( $object_id ); // reload to make sure we get our meta...
return $object;
}

/**
* Tests reading and getting set metadata.
*/
function test_get_meta_data() {
$object = $this->create_test_post();
$meta_data = $object->get_meta_data();
$i = 1;

$this->assertNotEmpty( $meta_data );
foreach ( $meta_data as $mid => $data ) {
$this->assertEquals( "val{$i}", $data->value );
$i++;
}
}

/**
* Test getting meta by ID.
*/
function test_get_meta() {
$object = $this->create_test_post();

// test single meta key
$single_meta = $object->get_meta( 'test_meta_key' );
$this->assertEquals( 'val1', $single_meta );

// test getting multiple
$meta = $object->get_meta( 'test_multi_meta_key', false );
$i = 2;
foreach ( $meta as $data ) {
$this->assertEquals( 'test_multi_meta_key', $data->key );
$this->assertEquals( "val{$i}", $data->value );
$i++;
}
}

/**
* Test setting meta.
*/
function test_set_meta_data() {

}

/**
* Test adding meta data.
*/
function test_add_meta_data() {

}

/**
* Test updating meta data.
*/
function test_update_meta_data() {

}

/**
* Test getting user meta data.
*/
function test_user_get_meta_data() {

}

/**
* Test setting user meta data.
*/
function test_user_set_meta_data() {

}

/**
* Test adding user meta data.
*/
function test_user_add_meta_data() {

}

/**
* Test updating user meta data.
*/
function test_user_update_meta_data() {

}

}
2 changes: 1 addition & 1 deletion woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ private function is_request( $type ) {
* Include required core files used in admin and on the frontend.
*/
public function includes() {
include_once( 'includes/interfaces/interface-wc-data.php' );
include_once( 'includes/class-wc-autoloader.php' );
include_once( 'includes/wc-core-functions.php' );
include_once( 'includes/wc-widget-functions.php' );
Expand Down Expand Up @@ -256,6 +255,7 @@ public function includes() {

include_once( 'includes/class-wc-auth.php' ); // Auth Class
include_once( 'includes/class-wc-post-types.php' ); // Registers post types
include_once( 'includes/abstracts/abstract-wc-data.php' ); // WC_Data for CRUD
include_once( 'includes/abstracts/abstract-wc-payment-token.php' ); // Payment Tokens
include_once( 'includes/abstracts/abstract-wc-product.php' ); // Products
include_once( 'includes/abstracts/abstract-wc-order.php' ); // Orders
Expand Down

0 comments on commit 97c0edf

Please sign in to comment.