woocommerce/plugins/woocommerce-blocks/tests/php/Helpers/TestValidateSchema.php

129 lines
3.4 KiB
PHP
Raw Normal View History

<?php
/**
* Ensures the helper works.
*/
namespace Automattic\WooCommerce\Blocks\Tests\Helpers;
require_once __DIR__ . '/ValidateSchema.php';
/**
* Test Validate schema.
*/
class TestValidateSchema extends \WP_UnitTestCase {
/**
* ValidateSchema instance.
*
* @var ValidateSchema
*/
protected $validate;
/**
* Setup schema.
*/
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
protected function setUp(): void {
$this->validate = new ValidateSchema(
[
'properties' => [
'test_number' => [
'type' => 'number',
],
'test_string' => [
'type' => 'string',
],
'test_integer' => [
'type' => 'integer',
],
'test_object' => [
'type' => 'object',
'properties' => [
'property_1' => [
'type' => 'string',
],
'property_2' => [
'type' => 'string',
],
],
],
'test_array' => [
'type' => 'array',
'items' => [
'type' => 'object',
'properties' => [
'property_1' => [
'type' => 'string',
],
'property_2' => [
'type' => 'string',
],
],
],
],
'test_integer_or_null' => [
'type' => [ 'null', 'integer' ],
],
],
]
);
}
/**
* Validate an object.
*/
public function test_get_diff_from_valid_object() {
$test_object = (object) [
'test_number' => 1.2,
'test_string' => 'Hello',
'test_integer' => 1,
'test_object' => (object) [
'property_1' => 'Prop 1',
'property_2' => 'Prop 2',
],
'test_array' => [
(object) [
'property_1' => 'Prop 1',
'property_2' => 'Prop 2',
],
],
'test_integer_or_null' => null,
];
$diff = $this->validate->get_diff_from_object( $test_object );
$this->assertEmpty( $diff, print_r( $diff, true ) );
}
/**
* Validate an object.
*/
public function test_get_diff_from_invalid_object() {
$test_object = (object) [
'test_number' => 'Invalid',
'test_string' => 666,
'test_integer' => 'Nope',
'test_object' => (object) [
'property_1' => 1,
'property_2' => 2,
],
'test_array' => [
(object) [
'property_1' => 1,
'invalid_key' => 2,
],
],
'test_integer_or_null' => 'string',
];
$diff = $this->validate->get_diff_from_object( $test_object );
$this->assertContains( 'test_array:property_2', $diff['missing'], print_r( $diff['missing'], true ) );
$this->assertContains( 'test_array:invalid_key', $diff['no_schema'], print_r( $diff['no_schema'], true ) );
$this->assertContains( 'test_number (string, expected number)', $diff['invalid_type'], print_r( $diff['invalid_type'], true ) );
$this->assertContains( 'test_string (integer, expected string)', $diff['invalid_type'], print_r( $diff['invalid_type'], true ) );
$this->assertContains( 'test_integer (string, expected integer)', $diff['invalid_type'], print_r( $diff['invalid_type'], true ) );
$this->assertContains( 'test_number (string, expected number)', $diff['invalid_type'], print_r( $diff['invalid_type'], true ) );
$this->assertContains( 'test_array:property_1 (integer, expected string)', $diff['invalid_type'], print_r( $diff['invalid_type'], true ) );
$this->assertContains( 'test_integer_or_null (string, expected null, integer)', $diff['invalid_type'], print_r( $diff['invalid_type'], true ) );
}
}