Skip to content

Commit

Permalink
Drop the dependency on OrderUtil, add a compat method with tests inst…
Browse files Browse the repository at this point in the history
…ead. Update tests and remove the OrderUtil mock.
  • Loading branch information
ajaynes-godaddy committed Oct 22, 2024
1 parent 5249fea commit 9f45812
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 64 deletions.
11 changes: 0 additions & 11 deletions tests/Mocks/OrderUtil.php

This file was deleted.

107 changes: 63 additions & 44 deletions tests/unit/HelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace SkyVerge\WooCommerce\PluginFramework\v5_15_1\Tests\Unit;

use Automattic\WooCommerce\Utilities\OrderUtil;
use Generator;
use Mockery;
use ReflectionException;
use SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper;
use SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Plugin_Compatibility;
use SkyVerge\WooCommerce\PluginFramework\v5_15_1\Tests\TestCase;
use WC_Data;
use WP_Mock;

class HelperTest extends TestCase
Expand Down Expand Up @@ -58,20 +59,13 @@ public function testAlwaysDetermineNavigationFeaturedDisabled() : void
/**
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
*
* @runInSeparateProcess
* @preserveGlobalState
*
* @throws ReflectionException
*/
public function testCanGetWooCommerceDataObjectMetaValueUsingOrderUtil() : void
{
require_once PLUGIN_ROOT_DIR.'/tests/Mocks/OrderUtil.php';

$object = Mockery::mock('WC_Data');

$object->expects('get_meta')->never();

$this->mockStaticMethod(OrderUtil::class, 'get_post_or_object_meta')
$this->mockStaticMethod(SV_WC_Helper::class, 'getPostOrObjectMetaCompat')
->once()
->with(null, $object, $field = 'TEST_FIELD', true)
->andReturn($value = 'WC_DATA_META_VALUE');
Expand All @@ -82,61 +76,86 @@ public function testCanGetWooCommerceDataObjectMetaValueUsingOrderUtil() : void
/**
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
*
* @runInSeparateProcess
* @preserveGlobalState
* @throws ReflectionException
*/
public function testCanGetWooCommerceDataObjectMetaValueWithoutUsingOrderUtil() : void
public function testCanGetWordPressPostMetaValueUsingOrderUtil() : void
{
$object = Mockery::mock('WC_Data');
$object = Mockery::mock('WP_Post');

$object->expects('get_meta')
$this->mockStaticMethod(SV_WC_Helper::class, 'getPostOrObjectMetaCompat')
->once()
->with($field = 'TEST_FIELD', true)
->andReturn($value = 'WC_DATA_META_VALUE');
->with($object, null, $field = 'TEST_FIELD', true)
->andReturn($value = 'WP_POST_META_VALUE');

$this->assertSame($value, SV_WC_Helper::getWooCommerceObjectMetaValue($object, $field));
}

/**
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
* @dataProvider providerCanGetPostOrObjectMetaCompat()
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getPostOrObjectMetaCompat()
*
* @runInSeparateProcess
* @preserveGlobalState
* @param bool $hasData
* @param bool $hasPostId
* @param mixed $expected
*
* @throws ReflectionException
*/
public function testCanGetWordPressPostMetaValueUsingOrderUtil() : void
public function testCanGetPostOrObjectMetaCompat(
bool $hasData,
bool $hasPostId,
$expected
) : void
{
require_once PLUGIN_ROOT_DIR.'/tests/Mocks/OrderUtil.php';
$key = 'test';
$object = null;

$object = Mockery::mock('WP_Post');
$post = Mockery::mock('WP_Post');

WP_Mock::userFunction('get_post_meta')->never();
if ($hasPostId) {
$post->ID = 123;
}

$this->mockStaticMethod(OrderUtil::class, 'get_post_or_object_meta')
->once()
->with($object, null, $field = 'TEST_FIELD', true)
->andReturn($value = 'WP_POST_META_VALUE');
if ($hasData) {
$object = Mockery::mock('WC_Data');

$this->assertSame($value, SV_WC_Helper::getWooCommerceObjectMetaValue($object, $field));
}

/**
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
*
* @runInSeparateProcess
* @preserveGlobalState
*/
public function testCanGetWordPressPostMetaValueWithoutUsingOrderUtil() : void
{
$object = Mockery::mock('WP_Post');
$object->ID = 123;
$object->expects('get_meta')
->times((int) ($hasData))
->with($key, true)
->andReturn('WC_DATA_META_VALUE');
}

WP_Mock::userFunction('get_post_meta')
->once()
->with($object->ID, $field = 'TEST_FIELD', true)
->andReturn($value = 'WP_POST_META_VALUE');
->times((int) (! $hasData && $hasPostId))
->with(123, $key, true)
->andReturn('WP_POST_META_VALUE');

$this->assertSame($value, SV_WC_Helper::getWooCommerceObjectMetaValue($object, $field));
$this->assertSame($expected, SV_WC_Helper::getPostOrObjectMetaCompat($post, $object, $key, true));
}

/** @see testCanGetPostOrObjectMetaCompat() */
public function providerCanGetPostOrObjectMetaCompat() : Generator
{
yield 'data is set, method does not exist' => [
'hasData' => true,
'hasPostId' => false,
'expected' => 'WC_DATA_META_VALUE',
];

/*
* Note: It seems there's no sane way to test the get$key() method
* on a WC_Data mock, thus no test case for 'data is set, method exists'.
*/

yield 'data not set, no post ID' => [
'hasData' => false,
'hasPostId' => false,
'expected' => false,
];

yield 'data not set, has post ID' => [
'hasData' => false,
'hasPostId' => true,
'expected' => 'WP_POST_META_VALUE',
];
}
}
33 changes: 24 additions & 9 deletions woocommerce/class-sv-wc-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

namespace SkyVerge\WooCommerce\PluginFramework\v5_15_1;

use Automattic\WooCommerce\Utilities\OrderUtil;
use SkyVerge\WooCommerce\PluginFramework\v5_15_1\Helpers\NumberHelper;
use WC_Data;
use WP_Post;
Expand Down Expand Up @@ -1211,23 +1210,39 @@ public static function get_escaped_id_list( array $ids ) {
*/
public static function getWooCommerceObjectMetaValue($object, string $field, bool $single = true)
{
$orderUtilExists = class_exists(OrderUtil::class);

if ($object instanceof WP_Post) {
return $orderUtilExists ?
OrderUtil::get_post_or_object_meta($object, null, $field, $single) :
get_post_meta($object->ID, $field, $single);
return static::getPostOrObjectMetaCompat($object, null, $field, $single);
}

if ($object instanceof WC_Data) {
return $orderUtilExists ?
OrderUtil::get_post_or_object_meta(null, $object, $field, $single) :
$object->get_meta($field, $single);
return static::getPostOrObjectMetaCompat(null, $object, $field, $single);
}

return null;
}

/**
* Adds local compatibility for Woo's internal OrderUtil::get_post_or_object_meta() method.
*
* @param WP_Post|null $post
* @param WC_Data|null $data
* @param string $key
* @param bool $single
*
* @return array|false|mixed|string
*/
public static function getPostOrObjectMetaCompat(?\WP_Post $post, ?\WC_Data $data, string $key, bool $single)
{
if (isset($data)) {
if (method_exists($data, "get$key")) {
return $data->{"get$key"}();
}
return $data->get_meta($key, $single);
} else {
return isset($post->ID) ? get_post_meta($post->ID, $key, $single) : false;
}
}

}


Expand Down

0 comments on commit 9f45812

Please sign in to comment.