22
33namespace SkyVerge \WooCommerce \PluginFramework \v5_15_1 \Tests \Unit ;
44
5- use Automattic \ WooCommerce \ Utilities \ OrderUtil ;
5+ use Generator ;
66use Mockery ;
77use ReflectionException ;
88use SkyVerge \WooCommerce \PluginFramework \v5_15_1 \SV_WC_Helper ;
99use SkyVerge \WooCommerce \PluginFramework \v5_15_1 \SV_WC_Plugin_Compatibility ;
1010use SkyVerge \WooCommerce \PluginFramework \v5_15_1 \Tests \TestCase ;
11+ use WC_Data ;
1112use WP_Mock ;
1213
1314class 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}
0 commit comments