Skip to content

Commit 9f45812

Browse files
Drop the dependency on OrderUtil, add a compat method with tests instead. Update tests and remove the OrderUtil mock.
1 parent 5249fea commit 9f45812

File tree

3 files changed

+87
-64
lines changed

3 files changed

+87
-64
lines changed

tests/Mocks/OrderUtil.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

tests/unit/HelperTest.php

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

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

5-
use Automattic\WooCommerce\Utilities\OrderUtil;
5+
use Generator;
66
use Mockery;
77
use ReflectionException;
88
use SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper;
99
use SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Plugin_Compatibility;
1010
use SkyVerge\WooCommerce\PluginFramework\v5_15_1\Tests\TestCase;
11+
use WC_Data;
1112
use WP_Mock;
1213

1314
class HelperTest extends TestCase
@@ -58,20 +59,13 @@ public function testAlwaysDetermineNavigationFeaturedDisabled() : void
5859
/**
5960
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
6061
*
61-
* @runInSeparateProcess
62-
* @preserveGlobalState
63-
*
6462
* @throws ReflectionException
6563
*/
6664
public function testCanGetWooCommerceDataObjectMetaValueUsingOrderUtil() : void
6765
{
68-
require_once PLUGIN_ROOT_DIR.'/tests/Mocks/OrderUtil.php';
69-
7066
$object = Mockery::mock('WC_Data');
7167

72-
$object->expects('get_meta')->never();
73-
74-
$this->mockStaticMethod(OrderUtil::class, 'get_post_or_object_meta')
68+
$this->mockStaticMethod(SV_WC_Helper::class, 'getPostOrObjectMetaCompat')
7569
->once()
7670
->with(null, $object, $field = 'TEST_FIELD', true)
7771
->andReturn($value = 'WC_DATA_META_VALUE');
@@ -82,61 +76,86 @@ public function testCanGetWooCommerceDataObjectMetaValueUsingOrderUtil() : void
8276
/**
8377
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
8478
*
85-
* @runInSeparateProcess
86-
* @preserveGlobalState
79+
* @throws ReflectionException
8780
*/
88-
public function testCanGetWooCommerceDataObjectMetaValueWithoutUsingOrderUtil() : void
81+
public function testCanGetWordPressPostMetaValueUsingOrderUtil() : void
8982
{
90-
$object = Mockery::mock('WC_Data');
83+
$object = Mockery::mock('WP_Post');
9184

92-
$object->expects('get_meta')
85+
$this->mockStaticMethod(SV_WC_Helper::class, 'getPostOrObjectMetaCompat')
9386
->once()
94-
->with($field = 'TEST_FIELD', true)
95-
->andReturn($value = 'WC_DATA_META_VALUE');
87+
->with($object, null, $field = 'TEST_FIELD', true)
88+
->andReturn($value = 'WP_POST_META_VALUE');
9689

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

10093
/**
101-
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
94+
* @dataProvider providerCanGetPostOrObjectMetaCompat()
95+
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getPostOrObjectMetaCompat()
10296
*
103-
* @runInSeparateProcess
104-
* @preserveGlobalState
97+
* @param bool $hasData
98+
* @param bool $hasPostId
99+
* @param mixed $expected
105100
*
106101
* @throws ReflectionException
107102
*/
108-
public function testCanGetWordPressPostMetaValueUsingOrderUtil() : void
103+
public function testCanGetPostOrObjectMetaCompat(
104+
bool $hasData,
105+
bool $hasPostId,
106+
$expected
107+
) : void
109108
{
110-
require_once PLUGIN_ROOT_DIR.'/tests/Mocks/OrderUtil.php';
109+
$key = 'test';
110+
$object = null;
111111

112-
$object = Mockery::mock('WP_Post');
112+
$post = Mockery::mock('WP_Post');
113113

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

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

121-
$this->assertSame($value, SV_WC_Helper::getWooCommerceObjectMetaValue($object, $field));
122-
}
123-
124-
/**
125-
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
126-
*
127-
* @runInSeparateProcess
128-
* @preserveGlobalState
129-
*/
130-
public function testCanGetWordPressPostMetaValueWithoutUsingOrderUtil() : void
131-
{
132-
$object = Mockery::mock('WP_Post');
133-
$object->ID = 123;
121+
$object->expects('get_meta')
122+
->times((int) ($hasData))
123+
->with($key, true)
124+
->andReturn('WC_DATA_META_VALUE');
125+
}
134126

135127
WP_Mock::userFunction('get_post_meta')
136-
->once()
137-
->with($object->ID, $field = 'TEST_FIELD', true)
138-
->andReturn($value = 'WP_POST_META_VALUE');
128+
->times((int) (! $hasData && $hasPostId))
129+
->with(123, $key, true)
130+
->andReturn('WP_POST_META_VALUE');
139131

140-
$this->assertSame($value, SV_WC_Helper::getWooCommerceObjectMetaValue($object, $field));
132+
$this->assertSame($expected, SV_WC_Helper::getPostOrObjectMetaCompat($post, $object, $key, true));
133+
}
134+
135+
/** @see testCanGetPostOrObjectMetaCompat() */
136+
public function providerCanGetPostOrObjectMetaCompat() : Generator
137+
{
138+
yield 'data is set, method does not exist' => [
139+
'hasData' => true,
140+
'hasPostId' => false,
141+
'expected' => 'WC_DATA_META_VALUE',
142+
];
143+
144+
/*
145+
* Note: It seems there's no sane way to test the get$key() method
146+
* on a WC_Data mock, thus no test case for 'data is set, method exists'.
147+
*/
148+
149+
yield 'data not set, no post ID' => [
150+
'hasData' => false,
151+
'hasPostId' => false,
152+
'expected' => false,
153+
];
154+
155+
yield 'data not set, has post ID' => [
156+
'hasData' => false,
157+
'hasPostId' => true,
158+
'expected' => 'WP_POST_META_VALUE',
159+
];
141160
}
142161
}

woocommerce/class-sv-wc-helper.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
namespace SkyVerge\WooCommerce\PluginFramework\v5_15_1;
2626

27-
use Automattic\WooCommerce\Utilities\OrderUtil;
2827
use SkyVerge\WooCommerce\PluginFramework\v5_15_1\Helpers\NumberHelper;
2928
use WC_Data;
3029
use WP_Post;
@@ -1211,23 +1210,39 @@ public static function get_escaped_id_list( array $ids ) {
12111210
*/
12121211
public static function getWooCommerceObjectMetaValue($object, string $field, bool $single = true)
12131212
{
1214-
$orderUtilExists = class_exists(OrderUtil::class);
1215-
12161213
if ($object instanceof WP_Post) {
1217-
return $orderUtilExists ?
1218-
OrderUtil::get_post_or_object_meta($object, null, $field, $single) :
1219-
get_post_meta($object->ID, $field, $single);
1214+
return static::getPostOrObjectMetaCompat($object, null, $field, $single);
12201215
}
12211216

12221217
if ($object instanceof WC_Data) {
1223-
return $orderUtilExists ?
1224-
OrderUtil::get_post_or_object_meta(null, $object, $field, $single) :
1225-
$object->get_meta($field, $single);
1218+
return static::getPostOrObjectMetaCompat(null, $object, $field, $single);
12261219
}
12271220

12281221
return null;
12291222
}
12301223

1224+
/**
1225+
* Adds local compatibility for Woo's internal OrderUtil::get_post_or_object_meta() method.
1226+
*
1227+
* @param WP_Post|null $post
1228+
* @param WC_Data|null $data
1229+
* @param string $key
1230+
* @param bool $single
1231+
*
1232+
* @return array|false|mixed|string
1233+
*/
1234+
public static function getPostOrObjectMetaCompat(?\WP_Post $post, ?\WC_Data $data, string $key, bool $single)
1235+
{
1236+
if (isset($data)) {
1237+
if (method_exists($data, "get$key")) {
1238+
return $data->{"get$key"}();
1239+
}
1240+
return $data->get_meta($key, $single);
1241+
} else {
1242+
return isset($post->ID) ? get_post_meta($post->ID, $key, $single) : false;
1243+
}
1244+
}
1245+
12311246
}
12321247

12331248

0 commit comments

Comments
 (0)