Skip to content

Commit

Permalink
Unit test SV_WC_Helper::getWooCommerceObjectMetaValue method
Browse files Browse the repository at this point in the history
  • Loading branch information
nmolham-godaddy committed Oct 10, 2024
1 parent 18f7def commit 3a6fc7a
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
11 changes: 11 additions & 0 deletions tests/Mocks/OrderUtil.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Automattic\WooCommerce\Utilities;

class OrderUtil
{
public static function get_post_or_object_meta(?\WP_Post $post, ?\WC_Data $data, string $key, bool $single)
{
return '';
}
}
86 changes: 85 additions & 1 deletion tests/unit/HelperTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
<?php

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

use Automattic\WooCommerce\Utilities\OrderUtil;
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 WP_Mock;

class HelperTest extends TestCase
{
Expand Down Expand Up @@ -51,4 +54,85 @@ public function testAlwaysDetermineNavigationFeaturedDisabled() : void

$this->assertFalse(SV_WC_Helper::is_wc_navigation_enabled());
}

/**
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
*
* @runInSeparateProcess
*
* @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')
->once()
->with(null, $object, $field = 'TEST_FIELD', true)
->andReturn($value = 'WC_DATA_META_VALUE');

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

/**
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
*
* @runInSeparateProcess
*/
public function testCanGetWooCommerceDataObjectMetaValueWithoutUsingOrderUtil() : void
{
$object = Mockery::mock('WC_Data');

$object->expects('get_meta')
->once()
->with($field = 'TEST_FIELD', true)
->andReturn($value = 'WC_DATA_META_VALUE');

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

/**
* @covers \SkyVerge\WooCommerce\PluginFramework\v5_15_1\SV_WC_Helper::getWooCommerceObjectMetaValue()
*
* @runInSeparateProcess
*
* @throws ReflectionException
*/
public function testCanGetWordPressPostMetaValueUsingOrderUtil() : void
{
require_once PLUGIN_ROOT_DIR.'/tests/Mocks/OrderUtil.php';

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

WP_Mock::userFunction('get_post_meta')->never();

$this->mockStaticMethod(OrderUtil::class, 'get_post_or_object_meta')
->once()
->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()
*
* @runInSeparateProcess
*/
public function testCanGetWordPressPostMetaValueWithoutUsingOrderUtil() : void
{
$object = Mockery::mock('WP_Post');
$object->ID = 123;

WP_Mock::userFunction('get_post_meta')
->once()
->with($object->ID, $field = 'TEST_FIELD', true)
->andReturn($value = 'WP_POST_META_VALUE');

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

0 comments on commit 3a6fc7a

Please sign in to comment.