woocommerce/plugins/woocommerce-blocks/tests/php/StoreApi/Routes/CartCoupons.php

186 lines
4.6 KiB
PHP
Raw Normal View History

<?php
/**
* Controller Tests.
*/
namespace Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes;
use Automattic\WooCommerce\Blocks\Tests\StoreApi\Routes\ControllerTestCase;
use Automattic\WooCommerce\Blocks\Tests\Helpers\FixtureData;
use Automattic\WooCommerce\Blocks\Tests\Helpers\ValidateSchema;
/**
* Cart Coupons Controller Tests.
*/
class CartCoupons extends ControllerTestCase {
/**
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.
*/
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 {
parent::setUp();
$fixtures = new FixtureData();
$this->product = $fixtures->get_simple_product(
array(
'name' => 'Test Product 1',
'regular_price' => 10,
)
);
$this->coupon = $fixtures->get_coupon( array( 'code' => 'test_coupon' ) );
wc_empty_cart();
wc()->cart->add_to_cart( $this->product->get_id(), 2 );
wc()->cart->apply_coupon( $this->coupon->get_code() );
}
/**
* Test getting cart.
*/
public function test_get_items() {
$this->assertAPIResponse(
'/wc/store/v1/cart/coupons',
200,
array(
0 => array(
'code' => 'test_coupon',
'discount_type' => 'fixed_cart',
'totals' => array(
'total_discount' => '0',
'total_discount_tax' => '0',
),
),
)
);
}
/**
* Test getting cart item by key.
*/
public function test_get_item() {
$this->assertAPIResponse(
'/wc/store/v1/cart/coupons/' . $this->coupon->get_code(),
200,
array(
'code' => 'test_coupon',
'discount_type' => 'fixed_cart',
'totals' => array(
'total_discount' => '0',
'total_discount_tax' => '0',
),
)
);
}
/**
* Test add to cart.
*/
public function test_create_item() {
wc()->cart->remove_coupons();
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/coupons' );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'code' => $this->coupon->get_code(),
)
);
$this->assertAPIResponse(
$request,
201,
array(
'code' => $this->coupon->get_code(),
)
);
}
/**
* Test add to cart does not allow invalid items.
*/
public function test_invalid_create_item() {
wc()->cart->remove_coupons();
$request = new \WP_REST_Request( 'POST', '/wc/store/v1/cart/coupons' );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$request->set_body_params(
array(
'code' => 'IDONOTEXIST',
)
);
$this->assertAPIResponse(
$request,
400
);
}
/**
* Test delete item.
*/
public function test_delete_item() {
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/coupons/' . $this->coupon->get_code() );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$this->assertAPIResponse(
$request,
204
);
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/coupons/' . $this->coupon->get_code() );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$this->assertAPIResponse(
$request,
404
);
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/coupons/i-do-not-exist' );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$this->assertAPIResponse(
$request,
404
);
}
/**
* Test delete all items.
*/
public function test_delete_items() {
$request = new \WP_REST_Request( 'DELETE', '/wc/store/v1/cart/coupons' );
$request->set_header( 'Nonce', wp_create_nonce( 'wc_store_api' ) );
$this->assertAPIResponse(
$request,
200,
array()
);
}
/**
* Test conversion of cart item to rest response.
*/
public function test_prepare_item() {
$routes = new \Automattic\WooCommerce\StoreApi\RoutesController( new \Automattic\WooCommerce\StoreApi\SchemaController( $this->mock_extend ) );
$controller = $routes->get( 'cart-coupons', 'v1' );
$response = $controller->prepare_item_for_response( $this->coupon->get_code(), new \WP_REST_Request() );
$data = $response->get_data();
$this->assertArrayHasKey( 'code', $data );
$this->assertArrayHasKey( 'totals', $data );
}
/**
* Test schema matches responses.
*/
public function test_get_item_schema() {
$routes = new \Automattic\WooCommerce\StoreApi\RoutesController( new \Automattic\WooCommerce\StoreApi\SchemaController( $this->mock_extend ) );
$controller = $routes->get( 'cart-coupons', 'v1' );
$schema = $controller->get_item_schema();
$response = $controller->prepare_item_for_response( $this->coupon->get_code(), new \WP_REST_Request() );
$schema = $controller->get_item_schema();
$validate = new ValidateSchema( $schema );
$diff = $validate->get_diff_from_object( $response->get_data() );
$this->assertEmpty( $diff );
}
}