Add Formatter Classes to the Store API for Extensions to Consume (https://github.com/woocommerce/woocommerce-blocks/pull/3503)
* Create and register formatter classes * Update Schema to use formatter classes * Update tests * Currency formatter * Fix tests * Use wc_get_price_decimals() as default * Exception on non existing formatter call * Feedback * remove setter
This commit is contained in:
parent
daa6454138
commit
6825dc909f
|
@ -21,6 +21,10 @@ use Automattic\WooCommerce\Blocks\Domain\Services\DraftOrders;
|
|||
use Automattic\WooCommerce\Blocks\Domain\Services\CreateAccount;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\Email\CustomerNewAccount;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* Takes care of bootstrapping the plugin.
|
||||
|
@ -193,10 +197,20 @@ class Bootstrap {
|
|||
return new CreateAccount( $container->get( Package::class ) );
|
||||
}
|
||||
);
|
||||
$this->container->register(
|
||||
Formatters::class,
|
||||
function( Container $container ) {
|
||||
$formatters = new Formatters();
|
||||
$formatters->register( 'money', MoneyFormatter::class );
|
||||
$formatters->register( 'html', HtmlFormatter::class );
|
||||
$formatters->register( 'currency', CurrencyFormatter::class );
|
||||
return $formatters;
|
||||
}
|
||||
);
|
||||
$this->container->register(
|
||||
ExtendRestApi::class,
|
||||
function( Container $container ) {
|
||||
return new ExtendRestApi( $container->get( Package::class ) );
|
||||
return new ExtendRestApi( $container->get( Package::class ), $container->get( Formatters::class ) );
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -4,6 +4,7 @@ namespace Automattic\WooCommerce\Blocks\Domain\Services;
|
|||
use Automattic\WooCommerce\Blocks\Domain\Package;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Schemas\CartItemSchema;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Schemas\CartSchema;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Throwable;
|
||||
use Exception;
|
||||
|
||||
|
@ -18,13 +19,32 @@ class ExtendRestApi {
|
|||
*/
|
||||
private $package;
|
||||
|
||||
/**
|
||||
* Holds the formatters class instance.
|
||||
*
|
||||
* @var Formatters
|
||||
*/
|
||||
private $formatters;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param Package $package An instance of the package class.
|
||||
* @param Package $package An instance of the package class.
|
||||
* @param Formatters $formatters An instance of the formatters class.
|
||||
*/
|
||||
public function __construct( Package $package ) {
|
||||
$this->package = $package;
|
||||
public function __construct( Package $package, Formatters $formatters ) {
|
||||
$this->package = $package;
|
||||
$this->formatters = $formatters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a formatter instance.
|
||||
*
|
||||
* @param string $name Formatter name.
|
||||
* @return FormatterInterface
|
||||
*/
|
||||
public function get_formatter( $name ) {
|
||||
return $this->formatters->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\StoreApi;
|
||||
|
||||
use \Exception;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\DefaultFormatter;
|
||||
|
||||
/**
|
||||
* Formatters class.
|
||||
*
|
||||
* Allows formatter classes to be registered. Formatters are exposed to extensions via the ExtendRestApi class.
|
||||
*/
|
||||
class Formatters {
|
||||
/**
|
||||
* Holds an array of formatter class instances.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private $formatters = [];
|
||||
|
||||
/**
|
||||
* Get a new instance of a formatter class.
|
||||
*
|
||||
* @throws Exception An Exception is thrown if a non-existing formatter is used and the user is admin.
|
||||
*
|
||||
* @param string $name Name of the formatter.
|
||||
* @return FormatterInterface Formatter class instance.
|
||||
*/
|
||||
public function __get( $name ) {
|
||||
if ( ! isset( $this->formatters[ $name ] ) ) {
|
||||
if ( defined( 'WP_DEBUG' ) && WP_DEBUG && current_user_can( 'manage_woocommerce' ) ) {
|
||||
throw new Exception( $name . ' formatter does not exist' );
|
||||
}
|
||||
return new DefaultFormatter();
|
||||
}
|
||||
return $this->formatters[ $name ];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a formatter class for usage.
|
||||
*
|
||||
* @param string $name Name of the formatter.
|
||||
* @param string $class A formatter class name.
|
||||
*/
|
||||
public function register( $name, $class ) {
|
||||
$this->formatters[ $name ] = new $class();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
|
||||
/**
|
||||
* Currency Formatter.
|
||||
*
|
||||
* Formats an array of monetary values by inserting currency data.
|
||||
*
|
||||
* @internal This API is used internally by Blocks--it is still in flux and may be subject to revisions.
|
||||
*/
|
||||
class CurrencyFormatter implements FormatterInterface {
|
||||
/**
|
||||
* Format a given value and return the result.
|
||||
*
|
||||
* @param array $value Value to format.
|
||||
* @param array $options Options that influence the formatting.
|
||||
* @return array
|
||||
*/
|
||||
public function format( $value, array $options = [] ) {
|
||||
$position = get_option( 'woocommerce_currency_pos' );
|
||||
$symbol = html_entity_decode( get_woocommerce_currency_symbol() );
|
||||
$prefix = '';
|
||||
$suffix = '';
|
||||
|
||||
switch ( $position ) {
|
||||
case 'left_space':
|
||||
$prefix = $symbol . ' ';
|
||||
break;
|
||||
case 'left':
|
||||
$prefix = $symbol;
|
||||
break;
|
||||
case 'right_space':
|
||||
$suffix = ' ' . $symbol;
|
||||
break;
|
||||
case 'right':
|
||||
$suffix = $symbol;
|
||||
break;
|
||||
}
|
||||
|
||||
return array_merge(
|
||||
(array) $value,
|
||||
[
|
||||
'currency_code' => get_woocommerce_currency(),
|
||||
'currency_symbol' => $symbol,
|
||||
'currency_minor_unit' => wc_get_price_decimals(),
|
||||
'currency_decimal_separator' => wc_get_price_decimal_separator(),
|
||||
'currency_thousand_separator' => wc_get_price_thousand_separator(),
|
||||
'currency_prefix' => $prefix,
|
||||
'currency_suffix' => $suffix,
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
|
||||
/**
|
||||
* Default Formatter.
|
||||
*
|
||||
* @internal This API is used internally by Blocks--it is still in flux and may be subject to revisions.
|
||||
*/
|
||||
class DefaultFormatter implements FormatterInterface {
|
||||
/**
|
||||
* Format a given value and return the result.
|
||||
*
|
||||
* @param mixed $value Value to format.
|
||||
* @param array $options Options that influence the formatting.
|
||||
* @return mixed
|
||||
*/
|
||||
public function format( $value, array $options = [] ) {
|
||||
return $value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
|
||||
/**
|
||||
* FormatterInterface.
|
||||
*
|
||||
* @internal This API is used internally by Blocks--it is still in flux and may be subject to revisions.
|
||||
*/
|
||||
interface FormatterInterface {
|
||||
/**
|
||||
* Format a given value and return the result.
|
||||
*
|
||||
* @param mixed $value Value to format.
|
||||
* @param array $options Options that influence the formatting.
|
||||
* @return mixed
|
||||
*/
|
||||
public function format( $value, array $options = [] );
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
|
||||
/**
|
||||
* Html Formatter.
|
||||
*
|
||||
* Formats HTML in API responses.
|
||||
*
|
||||
* @internal This API is used internally by Blocks--it is still in flux and may be subject to revisions.
|
||||
*/
|
||||
class HtmlFormatter implements FormatterInterface {
|
||||
/**
|
||||
* Format a given value and return the result.
|
||||
*
|
||||
* The wptexturize, convert_chars, and trim functions are also used in the `the_title` filter.
|
||||
* The function wp_kses_post removes disallowed HTML tags.
|
||||
*
|
||||
* @param string|array $value Value to format.
|
||||
* @param array $options Options that influence the formatting.
|
||||
* @return string
|
||||
*/
|
||||
public function format( $value, array $options = [] ) {
|
||||
if ( is_array( $value ) ) {
|
||||
return array_map( [ $this, 'format' ], $value );
|
||||
}
|
||||
return is_scalar( $value ) ? wp_kses_post( trim( convert_chars( wptexturize( $value ) ) ) ) : $value;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
namespace Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
|
||||
/**
|
||||
* Money Formatter.
|
||||
*
|
||||
* Formats monetary values using store settings.
|
||||
*
|
||||
* @internal This API is used internally by Blocks--it is still in flux and may be subject to revisions.
|
||||
*/
|
||||
class MoneyFormatter implements FormatterInterface {
|
||||
/**
|
||||
* Format a given value and return the result.
|
||||
*
|
||||
* @param mixed $value Value to format.
|
||||
* @param array $options Options that influence the formatting.
|
||||
* @return mixed
|
||||
*/
|
||||
public function format( $value, array $options = [] ) {
|
||||
$options = wp_parse_args(
|
||||
$options,
|
||||
[
|
||||
'decimals' => wc_get_price_decimals(),
|
||||
'rounding_mode' => PHP_ROUND_HALF_UP,
|
||||
]
|
||||
);
|
||||
|
||||
return (string) intval(
|
||||
round(
|
||||
( (float) wc_format_decimal( $value ) ) * ( 10 ** absint( $options['decimals'] ) ),
|
||||
0,
|
||||
absint( $options['rounding_mode'] )
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -232,45 +232,13 @@ abstract class AbstractSchema {
|
|||
}
|
||||
|
||||
/**
|
||||
* Prepares a list of store currency data to return in responses.
|
||||
* Adds currency data to an array of monetary values.
|
||||
*
|
||||
* @todo Core could use a more defined currency object format, making use of
|
||||
* constants for currency format rather than strings, and holding this type
|
||||
* of information instead of plugins/blocks needed to normalize things
|
||||
* themselves.
|
||||
*
|
||||
* @return array
|
||||
* @param array $values Monetary amounts.
|
||||
* @return array Monetary amounts with currency data appended.
|
||||
*/
|
||||
protected function get_store_currency_response() {
|
||||
$position = get_option( 'woocommerce_currency_pos' );
|
||||
$symbol = html_entity_decode( get_woocommerce_currency_symbol() );
|
||||
$prefix = '';
|
||||
$suffix = '';
|
||||
|
||||
switch ( $position ) {
|
||||
case 'left_space':
|
||||
$prefix = $symbol . ' ';
|
||||
break;
|
||||
case 'left':
|
||||
$prefix = $symbol;
|
||||
break;
|
||||
case 'right_space':
|
||||
$suffix = ' ' . $symbol;
|
||||
break;
|
||||
case 'right':
|
||||
$suffix = $symbol;
|
||||
break;
|
||||
}
|
||||
|
||||
return [
|
||||
'currency_code' => get_woocommerce_currency(),
|
||||
'currency_symbol' => $symbol,
|
||||
'currency_minor_unit' => wc_get_price_decimals(),
|
||||
'currency_decimal_separator' => wc_get_price_decimal_separator(),
|
||||
'currency_thousand_separator' => wc_get_price_thousand_separator(),
|
||||
'currency_prefix' => $prefix,
|
||||
'currency_suffix' => $suffix,
|
||||
];
|
||||
protected function prepare_currency_response( $values ) {
|
||||
return $this->extend->get_formatter( 'currency' )->format( $values );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -283,28 +251,22 @@ abstract class AbstractSchema {
|
|||
* @return string The new amount.
|
||||
*/
|
||||
protected function prepare_money_response( $amount, $decimals = 2, $rounding_mode = PHP_ROUND_HALF_UP ) {
|
||||
return (string) intval(
|
||||
round(
|
||||
( (float) wc_format_decimal( $amount ) ) * ( 10 ** $decimals ),
|
||||
0,
|
||||
absint( $rounding_mode )
|
||||
)
|
||||
return $this->extend->get_formatter( 'money' )->format(
|
||||
$amount,
|
||||
[
|
||||
'decimals' => $decimals,
|
||||
'rounding_mode' => $rounding_mode,
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares HTML based content, such as post titles and content, for the API response.
|
||||
*
|
||||
* The wptexturize, convert_chars, and trim functions are also used in the `the_title` filter.
|
||||
* The function wp_kses_post removes disallowed HTML tags.
|
||||
*
|
||||
* @param string|array $response Data to format.
|
||||
* @return string|array Formatted data.
|
||||
*/
|
||||
protected function prepare_html_response( $response ) {
|
||||
if ( is_array( $response ) ) {
|
||||
return array_map( [ $this, 'prepare_html_response' ], $response );
|
||||
}
|
||||
return is_scalar( $response ) ? wp_kses_post( trim( convert_chars( wptexturize( $response ) ) ) ) : $response;
|
||||
return $this->extend->get_formatter( 'html' )->format( $response );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -101,8 +101,7 @@ class CartCouponSchema extends AbstractSchema {
|
|||
return [
|
||||
'code' => $coupon_code,
|
||||
'discount_type' => $coupon->get_discount_type(),
|
||||
'totals' => (object) array_merge(
|
||||
$this->get_store_currency_response(),
|
||||
'totals' => (object) $this->prepare_currency_response(
|
||||
[
|
||||
'total_discount' => $this->prepare_money_response( isset( $total_discounts[ $coupon_code ] ) ? $total_discounts[ $coupon_code ] : 0, wc_get_price_decimals() ),
|
||||
'total_discount_tax' => $this->prepare_money_response( isset( $total_discount_taxes[ $coupon_code ] ) ? $total_discount_taxes[ $coupon_code ] : 0, wc_get_price_decimals(), PHP_ROUND_HALF_DOWN ),
|
||||
|
|
|
@ -296,8 +296,7 @@ class CartItemSchema extends ProductSchema {
|
|||
'images' => $this->get_images( $product ),
|
||||
'variation' => $this->format_variation_data( $cart_item['variation'], $product ),
|
||||
'prices' => (object) $this->prepare_product_price_response( $product, get_option( 'woocommerce_tax_display_cart' ) ),
|
||||
'totals' => (object) array_merge(
|
||||
$this->get_store_currency_response(),
|
||||
'totals' => (object) $this->prepare_currency_response(
|
||||
[
|
||||
'line_subtotal' => $this->prepare_money_response( $cart_item['line_subtotal'], wc_get_price_decimals() ),
|
||||
'line_subtotal_tax' => $this->prepare_money_response( $cart_item['line_subtotal_tax'], wc_get_price_decimals() ),
|
||||
|
|
|
@ -320,8 +320,7 @@ class CartSchema extends AbstractSchema {
|
|||
'needs_payment' => $cart->needs_payment(),
|
||||
'needs_shipping' => $cart->needs_shipping(),
|
||||
'has_calculated_shipping' => $has_calculated_shipping,
|
||||
'totals' => (object) array_merge(
|
||||
$this->get_store_currency_response(),
|
||||
'totals' => (object) $this->prepare_currency_response(
|
||||
[
|
||||
'total_items' => $this->prepare_money_response( $cart->get_subtotal(), wc_get_price_decimals() ),
|
||||
'total_items_tax' => $this->prepare_money_response( $cart->get_subtotal_tax(), wc_get_price_decimals() ),
|
||||
|
|
|
@ -299,7 +299,7 @@ class CartShippingRateSchema extends AbstractSchema {
|
|||
* @return array
|
||||
*/
|
||||
protected function get_rate_response( $rate, $selected_rate = '' ) {
|
||||
return array_merge(
|
||||
return $this->prepare_currency_response(
|
||||
[
|
||||
'rate_id' => $this->get_rate_prop( $rate, 'id' ),
|
||||
'name' => $this->prepare_html_response( $this->get_rate_prop( $rate, 'label' ) ),
|
||||
|
@ -311,8 +311,7 @@ class CartShippingRateSchema extends AbstractSchema {
|
|||
'method_id' => $this->get_rate_prop( $rate, 'method_id' ),
|
||||
'meta_data' => $this->get_rate_meta_data( $rate ),
|
||||
'selected' => $selected_rate === $this->get_rate_prop( $rate, 'id' ),
|
||||
],
|
||||
$this->get_store_currency_response()
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -69,8 +69,7 @@ class OrderCouponSchema extends AbstractSchema {
|
|||
public function get_item_response( \WC_Order_Item_Coupon $coupon ) {
|
||||
return [
|
||||
'code' => $coupon->get_code(),
|
||||
'totals' => (object) array_merge(
|
||||
$this->get_store_currency_response(),
|
||||
'totals' => (object) $this->prepare_currency_response(
|
||||
[
|
||||
'total_discount' => $this->prepare_money_response( $coupon->get_discount(), wc_get_price_decimals() ),
|
||||
'total_discount_tax' => $this->prepare_money_response( $coupon->get_discount_tax(), wc_get_price_decimals(), PHP_ROUND_HALF_DOWN ),
|
||||
|
|
|
@ -109,8 +109,7 @@ class ProductCollectionDataSchema extends AbstractSchema {
|
|||
*/
|
||||
public function get_item_response( $data ) {
|
||||
return [
|
||||
'price_range' => ! is_null( $data['min_price'] ) && ! is_null( $data['max_price'] ) ? (object) array_merge(
|
||||
$this->get_store_currency_response(),
|
||||
'price_range' => ! is_null( $data['min_price'] ) && ! is_null( $data['max_price'] ) ? (object) $this->prepare_currency_response(
|
||||
[
|
||||
'min_price' => $this->prepare_money_response( $data['min_price'], wc_get_price_decimals() ),
|
||||
'max_price' => $this->prepare_money_response( $data['max_price'], wc_get_price_decimals() ),
|
||||
|
|
|
@ -672,7 +672,7 @@ class ProductSchema extends AbstractSchema {
|
|||
* @return array
|
||||
*/
|
||||
protected function prepare_product_price_response( \WC_Product $product, $tax_display_mode = '' ) {
|
||||
$prices = $this->get_store_currency_response();
|
||||
$prices = [];
|
||||
$tax_display_mode = $this->get_tax_display_mode( $tax_display_mode );
|
||||
$price_function = $this->get_price_function_from_tax_display_mode( $tax_display_mode );
|
||||
|
||||
|
@ -681,7 +681,7 @@ class ProductSchema extends AbstractSchema {
|
|||
$prices['sale_price'] = $this->prepare_money_response( $price_function( $product, [ 'price' => $product->get_sale_price() ] ), wc_get_price_decimals() );
|
||||
$prices['price_range'] = $this->get_price_range( $product, $tax_display_mode );
|
||||
|
||||
return $prices;
|
||||
return $this->prepare_currency_response( $prices );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
/**
|
||||
* Controller Tests.
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Formatters;
|
||||
|
||||
use \WC_REST_Unit_Test_Case as TestCase;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* TestCurrencyFormatter tests.
|
||||
*/
|
||||
class TestCurrencyFormatter extends TestCase {
|
||||
|
||||
private $mock_formatter;
|
||||
|
||||
/**
|
||||
* Setup test products data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->mock_formatter = new CurrencyFormatter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test formatting.
|
||||
*/
|
||||
public function test_format() {
|
||||
$value = $this->mock_formatter->format( [] );
|
||||
$this->assertArrayHasKey( 'currency_code', $value );
|
||||
$this->assertArrayHasKey( 'currency_symbol', $value );
|
||||
$this->assertArrayHasKey( 'currency_minor_unit', $value );
|
||||
$this->assertArrayHasKey( 'currency_decimal_separator', $value );
|
||||
$this->assertArrayHasKey( 'currency_thousand_separator', $value );
|
||||
$this->assertArrayHasKey( 'currency_prefix', $value );
|
||||
$this->assertArrayHasKey( 'currency_suffix', $value );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
/**
|
||||
* Controller Tests.
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Formatters;
|
||||
|
||||
use \WC_REST_Unit_Test_Case as TestCase;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\DefaultFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* TestFormatters tests.
|
||||
*/
|
||||
class TestFormatters extends TestCase {
|
||||
|
||||
private $mock_formatters;
|
||||
|
||||
/**
|
||||
* Setup test products data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->mock_formatters = new Formatters();
|
||||
$this->mock_formatters->register( 'money', MoneyFormatter::class );
|
||||
$this->mock_formatters->register( 'html', HtmlFormatter::class );
|
||||
$this->mock_formatters->register( 'currency', CurrencyFormatter::class );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test get formatter.
|
||||
*/
|
||||
public function test_get_formatter() {
|
||||
$this->assertInstanceOf( MoneyFormatter::class, $this->mock_formatters->money );
|
||||
$this->assertInstanceOf( HtmlFormatter::class, $this->mock_formatters->html );
|
||||
$this->assertInstanceOf( CurrencyFormatter::class, $this->mock_formatters->currency );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting non existent formatter.
|
||||
*/
|
||||
public function test_get_default_formatter() {
|
||||
$this->assertInstanceOf( DefaultFormatter::class, $this->mock_formatters->wrong );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
/**
|
||||
* Controller Tests.
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Formatters;
|
||||
|
||||
use \WC_REST_Unit_Test_Case as TestCase;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
|
||||
/**
|
||||
* TestHtmlFormatter tests.
|
||||
*/
|
||||
class TestHtmlFormatter extends TestCase {
|
||||
|
||||
private $mock_formatter;
|
||||
|
||||
/**
|
||||
* Setup test products data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->mock_formatter = new HtmlFormatter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test formatting.
|
||||
*/
|
||||
public function test_format() {
|
||||
$this->assertEquals( "“Quotes”", $this->mock_formatter->format( '"Quotes"' ) );
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
/**
|
||||
* Controller Tests.
|
||||
*/
|
||||
|
||||
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Formatters;
|
||||
|
||||
use \WC_REST_Unit_Test_Case as TestCase;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
|
||||
/**
|
||||
* TestMoneyFormatter tests.
|
||||
*/
|
||||
class TestMoneyFormatter extends TestCase {
|
||||
|
||||
private $mock_formatter;
|
||||
|
||||
/**
|
||||
* Setup test products data. Called before every test.
|
||||
*/
|
||||
public function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->mock_formatter = new MoneyFormatter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Test formatting.
|
||||
*/
|
||||
public function test_format() {
|
||||
$this->assertEquals( "1000", $this->mock_formatter->format( 10 ) );
|
||||
$this->assertEquals( "1000", $this->mock_formatter->format( "10" ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test formatting with custom DP.
|
||||
*/
|
||||
public function test_format_dp() {
|
||||
$this->assertEquals( "100000", $this->mock_formatter->format( 10, [ 'decimals' => 4 ] ) );
|
||||
$this->assertEquals( "100000", $this->mock_formatter->format( "10", [ 'decimals' => 4 ] ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test formatting with custom DP.
|
||||
*/
|
||||
public function test_format_rounding_mode() {
|
||||
$this->assertEquals( "156", $this->mock_formatter->format( 1.555, [ 'rounding_mode' => PHP_ROUND_HALF_UP ] ) );
|
||||
$this->assertEquals( "155", $this->mock_formatter->format( 1.555, [ 'rounding_mode' => PHP_ROUND_HALF_DOWN ] ) );
|
||||
}
|
||||
}
|
|
@ -14,7 +14,10 @@ use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
|||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Package;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
|
||||
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* Cart Controller Tests.
|
||||
|
@ -35,7 +38,11 @@ class Cart extends TestCase {
|
|||
|
||||
$this->products = [];
|
||||
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating( 2 ) ) );
|
||||
$this->mock_formatters = new Formatters();
|
||||
$this->mock_formatters->register( 'money', MoneyFormatter::class );
|
||||
$this->mock_formatters->register( 'html', HtmlFormatter::class );
|
||||
$this->mock_formatters->register( 'currency', CurrencyFormatter::class );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating( 2 ) ), $this->mock_formatters );
|
||||
|
||||
// Create some test products.
|
||||
$this->products[0] = ProductHelper::create_simple_product( false );
|
||||
|
|
|
@ -13,6 +13,10 @@ use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
|||
use Automattic\WooCommerce\Blocks\Domain\Package;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* Cart Coupons Controller Tests.
|
||||
|
@ -29,7 +33,11 @@ class CartCoupons extends TestCase {
|
|||
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating( 2 ) ) );
|
||||
$this->mock_formatters = new Formatters();
|
||||
$this->mock_formatters->register( 'money', MoneyFormatter::class );
|
||||
$this->mock_formatters->register( 'html', HtmlFormatter::class );
|
||||
$this->mock_formatters->register( 'currency', CurrencyFormatter::class );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating( 2 ) ), $this->mock_formatters );
|
||||
|
||||
$this->product = ProductHelper::create_simple_product( false );
|
||||
$this->coupon = CouponHelper::create_coupon();
|
||||
|
|
|
@ -12,6 +12,10 @@ use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
|||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Package;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* Cart Controller Tests.
|
||||
|
@ -28,7 +32,11 @@ class CartItems extends TestCase {
|
|||
|
||||
parent::setUp();
|
||||
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating( 2 ) ) );
|
||||
$this->mock_formatters = new Formatters();
|
||||
$this->mock_formatters->register( 'money', MoneyFormatter::class );
|
||||
$this->mock_formatters->register( 'html', HtmlFormatter::class );
|
||||
$this->mock_formatters->register( 'currency', CurrencyFormatter::class );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating( 2 ) ), $this->mock_formatters );
|
||||
|
||||
wp_set_current_user( 0 );
|
||||
|
||||
|
|
|
@ -12,6 +12,10 @@ use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
|||
use Automattic\WooCommerce\Blocks\Domain\Package;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* Product Attributes Controller Tests.
|
||||
|
@ -27,7 +31,11 @@ class ProductAttributeTerms extends TestCase {
|
|||
parent::setUp();
|
||||
|
||||
wp_set_current_user( 0 );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating() ) );
|
||||
$this->mock_formatters = new Formatters();
|
||||
$this->mock_formatters->register( 'money', MoneyFormatter::class );
|
||||
$this->mock_formatters->register( 'html', HtmlFormatter::class );
|
||||
$this->mock_formatters->register( 'currency', CurrencyFormatter::class );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating() ), $this->mock_formatters );
|
||||
|
||||
$this->attributes = [];
|
||||
$this->attributes[0] = ProductHelper::create_attribute( 'color', [ 'red', 'green', 'blue' ] );
|
||||
|
|
|
@ -12,6 +12,10 @@ use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
|||
use Automattic\WooCommerce\Blocks\Domain\Package;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* Product Attributes Controller Tests.
|
||||
|
@ -27,7 +31,11 @@ class ProductAttributes extends TestCase {
|
|||
parent::setUp();
|
||||
|
||||
wp_set_current_user( 0 );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating() ) );
|
||||
$this->mock_formatters = new Formatters();
|
||||
$this->mock_formatters->register( 'money', MoneyFormatter::class );
|
||||
$this->mock_formatters->register( 'html', HtmlFormatter::class );
|
||||
$this->mock_formatters->register( 'currency', CurrencyFormatter::class );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating() ), $this->mock_formatters );
|
||||
|
||||
$color_attribute = ProductHelper::create_attribute( 'color', [ 'red', 'green', 'blue' ] );
|
||||
$size_attribute = ProductHelper::create_attribute( 'size', [ 'small', 'medium', 'large' ] );
|
||||
|
|
|
@ -12,6 +12,10 @@ use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
|||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Package;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* Controller Tests.
|
||||
|
@ -27,7 +31,11 @@ class ProductCollectionData extends TestCase {
|
|||
parent::setUp();
|
||||
|
||||
wp_set_current_user( 0 );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating() ) );
|
||||
$this->mock_formatters = new Formatters();
|
||||
$this->mock_formatters->register( 'money', MoneyFormatter::class );
|
||||
$this->mock_formatters->register( 'html', HtmlFormatter::class );
|
||||
$this->mock_formatters->register( 'currency', CurrencyFormatter::class );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating() ), $this->mock_formatters );
|
||||
|
||||
$this->products = [];
|
||||
$this->products[0] = ProductHelper::create_simple_product( false );
|
||||
|
|
|
@ -12,6 +12,11 @@ use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
|
|||
use Automattic\WooCommerce\Blocks\Domain\Services\ExtendRestApi;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Package;
|
||||
use Automattic\WooCommerce\Blocks\Domain\Services\FeatureGating;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\MoneyFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\HtmlFormatter;
|
||||
use Automattic\WooCommerce\Blocks\StoreApi\Formatters\CurrencyFormatter;
|
||||
|
||||
/**
|
||||
* Products Controller Tests.
|
||||
*/
|
||||
|
@ -27,7 +32,11 @@ class Products extends TestCase {
|
|||
parent::setUp();
|
||||
|
||||
wp_set_current_user( 0 );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating() ) );
|
||||
$this->mock_formatters = new Formatters();
|
||||
$this->mock_formatters->register( 'money', MoneyFormatter::class );
|
||||
$this->mock_formatters->register( 'html', HtmlFormatter::class );
|
||||
$this->mock_formatters->register( 'currency', CurrencyFormatter::class );
|
||||
$this->mock_extend = new ExtendRestApi( new Package( '', '', new FeatureGating() ), $this->mock_formatters );
|
||||
|
||||
$this->products = [];
|
||||
$this->products[0] = ProductHelper::create_simple_product( true );
|
||||
|
|
Loading…
Reference in New Issue