woocommerce/plugins/woocommerce-blocks/tests/php/StoreApi/ExtendSchemaTests.php

101 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Automattic\WooCommerce\StoreApi\Schemas\ExtendSchema;
use Automattic\WooCommerce\StoreApi\Formatters;
use Automattic\WooCommerce\StoreApi\Formatters\CurrencyFormatter;
use Automattic\WooCommerce\StoreApi\Formatters\HtmlFormatter;
use Automattic\WooCommerce\StoreApi\Formatters\MoneyFormatter;
/**
* Tests Extend Schema Functionality and helpers.
*
* @since $VID:$
*/
class ExtendSchemaTests extends TestCase {
/**
* Extend mock.
*
* @var ExtendSchema
*/
private $mock_extend;
/**
* Dummy function to ensure API gives the same function back.
* @var \Closure
*/
private $dummy;
/**
Add PHP8 Unit Testing (https://github.com/woocommerce/woocommerce-blocks/pull/7528) * fixed method sig * Updated to @wordpress/env@5.5.0 and set default PHP 7.4 for wp-env. * updated Coding Standards flow to use PHP 8.0 * Added comment to E2E flows explaining what PHP version is used * Revert "Updated to @wordpress/env@5.5.0 and set default PHP 7.4 for wp-env." This reverts commit 696cd7f42edc9d9726b777cf4f83a501a6d63936. * Added comment to Unit test flows explaining what PHP version is used. Specified PHP version on .wp-env.json * Fixed composer-lock.json version. * Updated tests to run on PHP Unit 9.2.6 * Updated tests to run on PHP 8 * Reverted test, mismatched results between local and pipeline * Removed Todo * Updated platform overrides * Update Migrationb tests with Mockery for PHP8 compat * try at PHP unit flow matrix * Fix blocks.ini invalid config * Temp disable E2E * Downgraded woocommerce/woocommerce-sniffs as it introduced new sniffs we should be handling on a different PR * re-enable E2E tests * blocks.ini fix * revert blocks.ini fix * Update @wordpress/env * remove .htaccess mapping * Fix permissions for tests * Debug permissions * Attempt at perm fix * Attempt at perm fix * Downgraded @wordpress/env * Another attempt at upgrade @wordpress/env * Attempt at cleaning wp-env before run * Attempt at destroying wp-env before run. Disabled E2E. * Attempt at destroying wp-env before run. * debug wp-env data * attempt at deleting wp-env data (destroy won't work due to prompt) * re-enable E2E * Fix deprecation warnings * Cleaned wp-env data for E2E * Fix perms for E2E * Updated RateLimitsTests * debug * Force 7.4 for wp-env * Run sh outside of npm * Reverted E2E flow * reverted wp-env-config.sh debug test * reverted .wp-env.json phpVersion force * Update tests/php/StoreApi/Utilities/ProductQueryFilters.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartExtensions.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartItems.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Products.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/ProductCollectionData.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Batch.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Checkout.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/CartCoupons.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/ProductAttributes.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Update tests/php/StoreApi/Routes/Cart.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * downgraded @wordpress/env to v4 * Reverted back to reflection class for pivate attribs manipulation on tests * reverted JS unit testing job name * Update tests/php/StoreApi/Formatters/TestMoneyFormatter.php Co-authored-by: Mike Jolley <mike.jolley@me.com> * Typo fix Co-authored-by: Mike Jolley <mike.jolley@me.com>
2022-11-09 15:28:08 +00:00
* Setup test product data. Called before every test.
*/
protected function setUp(): void {
parent::setUp();
$formatters = new Formatters();
$formatters->register( 'money', MoneyFormatter::class );
$formatters->register( 'html', HtmlFormatter::class );
$formatters->register( 'currency', CurrencyFormatter::class );
$this->mock_extend = new ExtendSchema( $formatters );
$this->dummy = function () {
return null;
};
}
/**
* Test that we can register a callback and the same function is returned.
*/
public function test_register_callback() {
$this->mock_extend->register_update_callback(
array(
'namespace' => 'test-plugin',
'callback' => $this->dummy,
)
);
$this->assertSame( $this->dummy, $this->mock_extend->get_update_callback( 'test-plugin' ) );
}
/**
* Test that we can register a callback and the same function is returned.
*/
public function test_fail_register_callback() {
$this->expectException( \Exception::class );
$this->expectExceptionMessage( 'You must provide a plugin namespace when extending a Store REST endpoint.' );
$this->mock_extend->register_update_callback(
array(
'callback' => $this->dummy,
)
);
}
/**
* Test that we can register a callback and the same function is returned.
*/
public function test_fail_get_callback() {
$this->expectException( \Exception::class );
$this->expectExceptionMessage( 'There is no such namespace registered: nonexistent-plugin.' );
$this->mock_extend->register_update_callback(
array(
'namespace' => 'test-plugin',
'callback' => $this->dummy,
)
);
$this->mock_extend->get_update_callback( 'nonexistent-plugin' );
}
/**
* Test that we can register a callback and the same function is returned.
*/
public function test_fail_get_callback_with_uncallable() {
$this->expectException( \Exception::class );
$this->expectExceptionMessage( 'There is no valid callback supplied to register_update_callback.' );
$this->mock_extend->register_update_callback(
array(
'namespace' => 'test-plugin',
)
);
$this->mock_extend->get_update_callback( 'nonexistent-plugin' );
}
}