From 740e6eb4a24dca51f8f399172cafbfdee24ea447 Mon Sep 17 00:00:00 2001 From: Maciej Kwiatkowski Date: Tue, 26 Nov 2024 14:03:50 +0100 Subject: [PATCH] SP-1103 Updating tests, revmoing modal flow type related settings, PHPCBF --- BitPayLib/class-bitpayinvoicecreate.php | 2 +- BitPayLib/class-bitpayinvoicefactory.php | 7 +- BitPayLib/class-bitpaypages.php | 2 +- BitPayLib/class-bitpaypaymentsettings.php | 18 - BitPayLib/class-wcgatewaybitpay.php | 14 +- scoper.inc.php | 104 +-- tests/EndToEnd.suite.yml | 1 - tests/EndToEnd/BitPayAdminSettingsCest.php | 24 +- tests/EndToEnd/CreateOrderWithBitPayCest.php | 14 +- tests/EndToEnd/Support/EndToEndTester.php | 208 +++--- tests/EndToEnd/_bootstrap.php | 8 +- tests/EndToEnd/_output/.gitignore | 2 - .../Unit/BitPayLib/test-bitpayipnprocess.php | 624 +++++++++--------- .../test-classbitpayinvoicefactory.php | 84 ++- tests/Unit/bootstrap.php | 11 +- tests/Unit/wp-config.dist.php | 16 +- 16 files changed, 556 insertions(+), 583 deletions(-) delete mode 100644 tests/EndToEnd/_output/.gitignore diff --git a/BitPayLib/class-bitpayinvoicecreate.php b/BitPayLib/class-bitpayinvoicecreate.php index 668f1ba..3d3a9b4 100644 --- a/BitPayLib/class-bitpayinvoicecreate.php +++ b/BitPayLib/class-bitpayinvoicecreate.php @@ -72,7 +72,7 @@ public function execute(): void { $this->bitpay_checkout_insert_order_note( $order_id, $invoice_id ); - wp_redirect( $bitpay_invoice->getUrl() ); // phpcs:ignore + wp_redirect( $bitpay_invoice->getURL() ); // phpcs:ignore exit(); } catch ( BitPayException $e ) { $this->bitpay_logger->execute( $e->getMessage(), 'NEW BITPAY INVOICE', false, true ); diff --git a/BitPayLib/class-bitpayinvoicefactory.php b/BitPayLib/class-bitpayinvoicefactory.php index 7736859..20ff83c 100644 --- a/BitPayLib/class-bitpayinvoicefactory.php +++ b/BitPayLib/class-bitpayinvoicefactory.php @@ -68,12 +68,7 @@ private function get_redirect_url( \WC_Order $order ): string { return $custom_redirect_page . '?custompage=true'; } - $url_suffix = '?key=' . $order->get_order_key() . '&redirect=false'; - $checkout_slug = $this->bitpay_payment_settings->get_checkout_slug(); - if ( $checkout_slug ) { - return get_home_url() . DIRECTORY_SEPARATOR . $checkout_slug . '/order-received/' - . $order->get_id() . DIRECTORY_SEPARATOR . $url_suffix; - } + $url_suffix = '?key=' . $order->get_order_key() . '&redirect=false'; return $this->wordpress_helper->get_endpoint_url( 'order-received', diff --git a/BitPayLib/class-bitpaypages.php b/BitPayLib/class-bitpaypages.php index 9aaf09f..c710971 100644 --- a/BitPayLib/class-bitpaypages.php +++ b/BitPayLib/class-bitpaypages.php @@ -21,7 +21,7 @@ public function __construct( BitPayPaymentSettings $bitpay_payment_settings ) { public function checkout_thank_you( int $order_id ): void { global $woocommerce; - $order = new \WC_Order( $order_id ); + $order = new \WC_Order( $order_id ); if ( $order->get_payment_method() !== 'bitpay_checkout_gateway' ) { return; } diff --git a/BitPayLib/class-bitpaypaymentsettings.php b/BitPayLib/class-bitpaypaymentsettings.php index 8569c45..dec7a5e 100644 --- a/BitPayLib/class-bitpaypaymentsettings.php +++ b/BitPayLib/class-bitpaypaymentsettings.php @@ -62,7 +62,6 @@ public function redirect_after_purchase( int $order_id ): void { return; } - $checkout_message = $this->get_checkout_message(); if ( $order->get_status() === 'pending' ) { $close_url = $this->get_close_url(); @@ -71,10 +70,6 @@ public function redirect_after_purchase( int $order_id ): void { die(); } } - - if ( '' !== $checkout_message ) { - echo '
' . $checkout_message . '


'; // phpcs:ignore - } } public function check_token(): void { @@ -114,10 +109,6 @@ public function get_bitpay_token(): ?string { return $this->get_bitpay_gateway_setting( 'bitpay_checkout_token_' . $suffix, null ); } - public function get_checkout_message(): string { - return $this->get_bitpay_gateway_setting( 'bitpay_checkout_checkout_message', '' ); - } - public function get_close_url(): ?string { return $this->get_bitpay_gateway_setting( 'bitpay_close_url', null ); } @@ -135,15 +126,6 @@ public function get_custom_redirect_page(): ?string { return $custom_redirect_page; } - public function get_checkout_slug(): ?string { - $slug = $this->get_bitpay_gateway_setting( 'bitpay_checkout_slug', null ); - if ( '' === $slug ) { - return null; - } - - return $slug; - } - public function get_payment_logo_url(): string { $logo = $this->get_bitpay_gateway_setting( 'bitpay_logo', 'BitPay-Accepted-CardGroup' ); diff --git a/BitPayLib/class-wcgatewaybitpay.php b/BitPayLib/class-wcgatewaybitpay.php index a481e0f..030ae42 100644 --- a/BitPayLib/class-wcgatewaybitpay.php +++ b/BitPayLib/class-wcgatewaybitpay.php @@ -155,12 +155,6 @@ public function init_form_fields() { ), 'default' => 'test', ), - - 'bitpay_checkout_slug' => array( - 'title' => __( 'Checkout Page', 'woocommerce' ), - 'type' => 'text', - 'description' => __( 'If you have a different custom checkout page, enter the page slug.
ie. ' . get_home_url() . '/checkout

View your pages here, your current checkout page should have Checkout Page next to the title.

Click the "quick edit" and copy and paste a custom slug here if needed.', 'woocommerce' ), // phpcs:ignore - ), 'bitpay_custom_redirect' => array( 'title' => __( 'Custom Redirect Page', 'woocommerce' ), 'type' => 'text', @@ -193,18 +187,14 @@ public function init_form_fields() { ), 'default' => '1', ), - 'bitpay_checkout_checkout_message' => array( - 'title' => __( 'Checkout Message', 'woocommerce' ), - 'type' => 'textarea', - 'description' => __( 'Insert your custom message for the Order Received page, so the customer knows that the order will not be completed until BitPay releases the funds.', 'woocommerce' ), - 'default' => 'Thank you. We will notify you when BitPay has processed your transaction.', - ), + 'bitpay_checkout_error' => array( 'title' => __( 'Error handling', 'woocommerce' ), 'type' => 'text', 'description' => __( 'If there is an error with creating the invoice, enter the page slug.
ie. ' . get_home_url() . '/error

View your pages here,.

Click the "quick edit" and copy and paste a custom slug here.', 'woocommerce' ), // phpcs:ignore ), + 'bitpay_checkout_error_message' => array( 'title' => __( 'Error Message', 'woocommerce' ), 'type' => 'textarea', diff --git a/scoper.inc.php b/scoper.inc.php index dfb153f..f2a8c1e 100644 --- a/scoper.inc.php +++ b/scoper.inc.php @@ -4,54 +4,58 @@ use Isolated\Symfony\Component\Finder\Finder; -$excludedFiles = []; +$excludedFiles = array(); -return [ - 'prefix' => 'BitPayVendor', - 'finders' => [ - Finder::create()->files()->in('BitPayLib'), - Finder::create()->files()->in('vendor') - ->ignoreVCS(true) - ->notName('/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/') - ->exclude([ - 'humbug' - ]), - Finder::create()->append([ - 'composer.json', - ]), - ], - 'exclude-files' => [ - ...$excludedFiles, - ], - 'patchers' => [ - static function (string $filePath, string $prefix, string $contents): string { - return $contents; - }, - ], - 'exclude-namespaces' => [ - 'Humbug\PhpScoper', - 'PHP_CodeSniffer', - 'PHPCSUtils' - ], - 'exclude-classes' => [ - 'WC', - 'WC_Payment_Gateway', - 'WP_User', - 'WC_Order', - 'WP_REST_Request', - 'WC_Admin_Settings', - 'Automattic\WooCommerce\Blocks\Package', - 'Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry', - 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType', - 'wpdb' - ], - 'exclude-functions' => [], - 'exclude-constants' => [], - 'expose-global-constants' => true, - 'expose-global-classes' => true, - 'expose-global-functions' => true, - 'expose-namespaces' => [], - 'expose-classes' => [], - 'expose-functions' => [], - 'expose-constants' => [], -]; +return array( + 'prefix' => 'BitPayVendor', + 'finders' => array( + Finder::create()->files()->in( 'BitPayLib' ), + Finder::create()->files()->in( 'vendor' ) + ->ignoreVCS( true ) + ->notName( '/LICENSE|.*\\.md|.*\\.dist|Makefile|composer\\.json|composer\\.lock/' ) + ->exclude( + array( + 'humbug', + ) + ), + Finder::create()->append( + array( + 'composer.json', + ) + ), + ), + 'exclude-files' => array( + ...$excludedFiles, + ), + 'patchers' => array( + static function ( string $filePath, string $prefix, string $contents ): string { + return $contents; + }, + ), + 'exclude-namespaces' => array( + 'Humbug\PhpScoper', + 'PHP_CodeSniffer', + 'PHPCSUtils', + ), + 'exclude-classes' => array( + 'WC', + 'WC_Payment_Gateway', + 'WP_User', + 'WC_Order', + 'WP_REST_Request', + 'WC_Admin_Settings', + 'Automattic\WooCommerce\Blocks\Package', + 'Automattic\WooCommerce\Blocks\Payments\PaymentMethodRegistry', + 'Automattic\WooCommerce\Blocks\Payments\Integrations\AbstractPaymentMethodType', + 'wpdb', + ), + 'exclude-functions' => array(), + 'exclude-constants' => array(), + 'expose-global-constants' => true, + 'expose-global-classes' => true, + 'expose-global-functions' => true, + 'expose-namespaces' => array(), + 'expose-classes' => array(), + 'expose-functions' => array(), + 'expose-constants' => array(), +); diff --git a/tests/EndToEnd.suite.yml b/tests/EndToEnd.suite.yml index e8f3faa..9636aad 100644 --- a/tests/EndToEnd.suite.yml +++ b/tests/EndToEnd.suite.yml @@ -22,7 +22,6 @@ modules: capabilities: "goog:chromeOptions": args: - - "--headless" - "--disable-gpu" - "--disable-dev-shm-usage" - "--proxy-server='direct://'" diff --git a/tests/EndToEnd/BitPayAdminSettingsCest.php b/tests/EndToEnd/BitPayAdminSettingsCest.php index 3f532d4..f62d66b 100644 --- a/tests/EndToEnd/BitPayAdminSettingsCest.php +++ b/tests/EndToEnd/BitPayAdminSettingsCest.php @@ -8,17 +8,17 @@ class BitPayAdminSettingsCest { - public function it_should_shows_the_description_on_the_frontend(EndToEndTester $i): void { - $expectedDescription = 'Pay with BitPay using one of the supported cryptocurrencies ' . rand(); + public function it_should_show_the_description_on_the_frontend( EndToEndTester $i ): void { + $expectedDescription = 'Pay with BitPay using one of the supported cryptocurrencies ' . rand(); - $i->loginAsAdmin(); - $i->amOnBitPayAdminSettings(); - $i->resetBitPayAdminSettings(); - $i->fillField('#woocommerce_bitpay_checkout_gateway_description', $expectedDescription); - $i->saveBitPayAdminSettings(); - $i->prepareDataToMakeAnOrder(); - $i->amOnOrderPage(); - $i->selectBitPayPaymentMethod(); - $i->seeInSource($expectedDescription); - } + $i->loginAsAdmin(); + $i->amOnBitPayAdminSettings(); + $i->resetBitPayAdminSettings(); + $i->fillField( '#woocommerce_bitpay_checkout_gateway_description', $expectedDescription ); + $i->saveBitPayAdminSettings(); + $i->prepareDataToMakeAnOrder(); + $i->amOnOrderPage(); + $i->selectBitPayPaymentMethod(); + $i->seeInSource( $expectedDescription ); + } } diff --git a/tests/EndToEnd/CreateOrderWithBitPayCest.php b/tests/EndToEnd/CreateOrderWithBitPayCest.php index e815ef6..d0c6271 100644 --- a/tests/EndToEnd/CreateOrderWithBitPayCest.php +++ b/tests/EndToEnd/CreateOrderWithBitPayCest.php @@ -6,6 +6,16 @@ use BitPayLib\Tests\EndToEnd\Support\EndToEndTester; -class CreateOrderWithBitPayCest -{ +class CreateOrderWithBitPayCest { + + public function it_should_use_redirect_flow_for_create_invoice( EndToEndTester $i ): void { + $i->loginAsAdmin(); + $i->amOnBitPayAdminSettings(); + $i->resetBitPayAdminSettings(); + $i->saveBitPayAdminSettings(); + $i->prepareDataToMakeAnOrder(); + $i->placeOrder(); + $i->wait( 5 ); + $i->seeInTitle( 'BitPay Invoice' ); + } } diff --git a/tests/EndToEnd/Support/EndToEndTester.php b/tests/EndToEnd/Support/EndToEndTester.php index e888302..78ce351 100644 --- a/tests/EndToEnd/Support/EndToEndTester.php +++ b/tests/EndToEnd/Support/EndToEndTester.php @@ -8,6 +8,7 @@ /** * Inherited Methods + * * @method void wantTo($text) * @method void wantToTest($text) * @method void execute($callable) @@ -20,110 +21,105 @@ * @method void pause($vars = []) * * @SuppressWarnings(PHPMD) -*/ -class EndToEndTester extends \Codeception\Actor -{ - use EndToEndTesterActions; - - public function getShopPageSlug(): string { - return $_ENV['WORDPRESS_SHOP_PAGE_SLUG'] ?? 'shop'; - } - - public function getProductPageSlug(): string { - return $_ENV['WORDPRESS_PRODUCT_PAGE_SLUG'] ?? 'products'; - } - - public function amOnSomeProductPage() - { - $this->amOnAdminProductPage(); - $productName = $this->grabTextFrom('td.name'); - - $this->amOnPage($this->getProductPageSlug() . '/' . $productName); - } - - public function addAnyProductToCart() - { - $this->amOnSomeProductPage(); - $priceValue = $this->grabTextFrom('.woocommerce-Price-amount.amount'); - $firstDigitIndex = strcspn($priceValue, '0123456789'); - $price = substr($priceValue, $firstDigitIndex); - - $qtyRequired = (int)(round(10 / $price, 0, PHP_ROUND_HALF_UP)); - $this->fillField('input[name="quantity"]', $qtyRequired); - $this->click('.single_add_to_cart_button'); - } - - private function amOnAdminProductPage() - { - $this->amOnAdminPage('edit.php?post_type=product'); - } - - public function amOnOrderPage() - { - $this->amOnPage('zamowienie'); // @todo - } - - public function fillOrderInformation() - { - $this->fillField('#billing_first_name', 'Bruce'); - $this->fillField('#billing_last_name', 'Wayne'); - $this->fillField('#billing_address_1', 'Dark Knight 1/2'); - $this->fillField('#billing_city', 'Gotham City'); - $this->fillField('#billing_postcode', '12345'); - $this->fillField('#billing_phone', '12345'); - $this->fillField('#billing_email', 'office@batman.com'); - } - - public function selectBitPayPaymentMethod() - { - $this->waitForElementClickable('.woocommerce-checkout-payment', 30); - - $this->waitForDisappearElement('.blockUI', 10); - $this->click('label[for="payment_method_bitpay_checkout_gateway"]'); - } - - public function placeOrder() - { - $this->click('#place_order'); - } - - public function resetBitPayAdminSettings(): void { - $this->fillField('#woocommerce_bitpay_checkout_gateway_bitpay_close_url', null); - $this->fillField('#woocommerce_bitpay_checkout_gateway_bitpay_checkout_slug', null); - $this->fillField('#woocommerce_bitpay_checkout_gateway_bitpay_custom_redirect', null); - } - - public function amOnBitPayAdminSettings(): void - { - $this->amOnPage('/wp-admin/admin.php?page=wc-settings&tab=checkout§ion=bitpay_checkout_gateway'); - $endpoint = $this->grabValueFrom('#woocommerce_bitpay_checkout_gateway_bitpay_checkout_endpoint'); - if ('test' !== $endpoint) { - throw new \RuntimeException('Tests can be run ONLY on test endpoint. Please change it manually'); - } - } - - public function prepareDataToMakeAnOrder(): void - { - $this->addAnyProductToCart(); - $this->amOnOrderPage(); - $this->fillOrderInformation(); - $this->selectBitPayPaymentMethod(); - } - - private function waitForDisappearElement(string $element, int $maxTries = 10, int $waitTimeInSeconds = 1): void - { - for($i=0; $i<=$maxTries; $i++) { - $isPresent = $this->executeJS('return document.querySelector("' . $element . '") !== null'); - if (!$isPresent) { - return; - } - $this->wait($waitTimeInSeconds); - } - throw new \RuntimeException('Element ' . $element . ' still exists after wait ' . $waitTimeInSeconds . ' in sec'); - } - - public function saveBitPayAdminSettings() - { - $this->click('button[name="save"]'); - } + */ +class EndToEndTester extends \Codeception\Actor { + + use EndToEndTesterActions; + + public function getShopPageSlug(): string { + return $_ENV['WORDPRESS_SHOP_PAGE_SLUG'] ?? 'shop'; + } + + public function getProductPageSlug(): string { + return $_ENV['WORDPRESS_PRODUCT_PAGE_SLUG'] ?? 'products'; + } + + public function amOnSomeProductPage() { + $this->amOnAdminProductPage(); + $productName = $this->grabTextFrom( 'td.name a' ); + + $this->amOnPage( $this->getProductPageSlug() . '/' . $productName ); + } + + public function addAnyProductToCart() { + $this->amOnSomeProductPage(); + $priceValue = $this->grabTextFrom( '.woocommerce-Price-amount.amount' ); + $price = (float) preg_replace( '/[^0-9.]/', '', $priceValue ); + + $qtyRequired = max( 1, (int) ( round( 10 / $price, 0, PHP_ROUND_HALF_UP ) ) ); + $this->fillField( 'input[name="quantity"]', $qtyRequired ); + $this->click( '.single_add_to_cart_button' ); + } + + private function amOnAdminProductPage() { + $this->amOnAdminPage( 'edit.php?post_type=product' ); + } + + public function amOnOrderPage() { + $this->amOnPage( 'checkout' ); + } + + public function fillOrderInformation() { + if ( $this->grabValueFrom( '#billing-first_name' ) === '' ) { + $this->fillField( '#billing-first_name', 'Bruce' ); + } + if ( $this->grabValueFrom( '#billing-last_name' ) === '' ) { + $this->fillField( '#billing-last_name', 'Wayne' ); + } + if ( $this->grabValueFrom( '#billing-address_1' ) === '' ) { + $this->fillField( '#billing-address_1', 'Dark Knight 1/2' ); + } + if ( $this->grabValueFrom( '#billing-city' ) === '' ) { + $this->fillField( '#billing-city', 'Gotham City' ); + } + if ( $this->grabValueFrom( '#billing-postcode' ) === '' ) { + $this->fillField( '#billing-postcode', '12345' ); + } + if ( $this->grabValueFrom( '#billing-phone' ) === '' ) { + $this->fillField( '#billing-phone', '12345' ); + } + } + + public function selectBitPayPaymentMethod() { + $this->click( 'label[for="radio-control-wc-payment-method-options-bitpay_checkout_gateway"]' ); + } + + public function placeOrder() { + $this->click( '.wc-block-components-checkout-place-order-button' ); + } + + public function resetBitPayAdminSettings(): void { + $this->fillField( '#woocommerce_bitpay_checkout_gateway_bitpay_close_url', null ); + $this->fillField( '#woocommerce_bitpay_checkout_gateway_bitpay_custom_redirect', null ); + } + + public function amOnBitPayAdminSettings(): void { + $this->amOnPage( '/wp-admin/admin.php?page=wc-settings&tab=checkout§ion=bitpay_checkout_gateway' ); + $endpoint = $this->grabValueFrom( '#woocommerce_bitpay_checkout_gateway_bitpay_checkout_endpoint' ); + if ( 'test' !== $endpoint ) { + throw new \RuntimeException( 'Tests can be run ONLY on test endpoint. Please change it manually' ); + } + } + + public function prepareDataToMakeAnOrder(): void { + $this->addAnyProductToCart(); + $this->amOnOrderPage(); + $this->fillOrderInformation(); + $this->selectBitPayPaymentMethod(); + } + + private function waitForDisappearElement( string $element, int $maxTries = 10, int $waitTimeInSeconds = 1 ): void { + for ( $i = 0; $i <= $maxTries; $i++ ) { + $isPresent = $this->executeJS( 'return document.querySelector("' . $element . '") !== null' ); + if ( ! $isPresent ) { + return; + } + $this->wait( $waitTimeInSeconds ); + } + throw new \RuntimeException( 'Element ' . $element . ' still exists after wait ' . $waitTimeInSeconds . ' in sec' ); + } + + public function saveBitPayAdminSettings() { + $this->click( 'button[name="save"]' ); + } } diff --git a/tests/EndToEnd/_bootstrap.php b/tests/EndToEnd/_bootstrap.php index 5667be2..ab2a090 100644 --- a/tests/EndToEnd/_bootstrap.php +++ b/tests/EndToEnd/_bootstrap.php @@ -1,13 +1,13 @@ ` command * to run WP-CLI commands on the WordPress site and database used by the EndToEnd suite. * E.g.: @@ -15,4 +15,4 @@ * `vendor/bin/codecept wp:cli EndToEnd plugin activate woocommerce` to activate the WooCommerce plugin. * `vendor/bin/codecept wp:cli EndToEnd user create alice alice@example.com --role=administrator` to create a new user. * `vendor/bin/codecept wp:cli EndToEnd db export tests/Support/Data/dump.sql` to update the dump file. - */ \ No newline at end of file + */ diff --git a/tests/EndToEnd/_output/.gitignore b/tests/EndToEnd/_output/.gitignore deleted file mode 100644 index c1aefd2..0000000 --- a/tests/EndToEnd/_output/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -.gitignore -!.gitignore diff --git a/tests/Unit/BitPayLib/test-bitpayipnprocess.php b/tests/Unit/BitPayLib/test-bitpayipnprocess.php index 23fa2dd..db448d1 100644 --- a/tests/Unit/BitPayLib/test-bitpayipnprocess.php +++ b/tests/Unit/BitPayLib/test-bitpayipnprocess.php @@ -13,7 +13,7 @@ class BitPayInProcessTest extends TestCase { - private const WC_ORDER_ID = 'someWcId'; + private const WC_ORDER_ID = 'someWcId'; private const BITPAY_INVOICE_ID = 'someId'; /** @@ -21,32 +21,31 @@ class BitPayInProcessTest extends TestCase { */ public function it_should_do_not_allow_to_process_non_bitpay_orders() { // given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->getMockBuilder(\WC_Order::class)->getMock(); + $wc_order = $this->getMockBuilder( \WC_Order::class )->getMock(); $bitpay_checkout_transactions = $this->get_checkout_transactions(); - $wc_order->method('get_payment_method')->willReturn('invalidMethod'); - $wc_order->method('get_id')->willReturn(self::WC_ORDER_ID); - $request->method('get_body') + $wc_order->method( 'get_payment_method' )->willReturn( 'invalidMethod' ); + $wc_order->method( 'get_id' )->willReturn( self::WC_ORDER_ID ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_paid_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_paid_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); - + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -56,21 +55,22 @@ public function it_should_do_not_allow_to_process_non_bitpay_orders() { ); // then - $logger->expects(self::exactly(2))->method('execute')->with( - self::callback(function($msg) { - if (is_array($msg)) { - return true; - } - - return $msg === 'Order id = someWcId, BitPay invoice id = someId. Current payment method = invalidMethod'; - }, + $logger->expects( self::exactly( 2 ) )->method( 'execute' )->with( + self::callback( + function ( $msg ) { + if ( is_array( $msg ) ) { + return true; + } + + return $msg === 'Order id = someWcId, BitPay invoice id = someId. Current payment method = invalidMethod'; + }, ) ); - $bitpay_checkout_transactions->expects(self::never())->method('update_transaction_status'); + $bitpay_checkout_transactions->expects( self::never() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** @@ -78,30 +78,30 @@ public function it_should_do_not_allow_to_process_non_bitpay_orders() { */ public function it_should_do_not_allow_to_process_with_wrong_transaction() { // given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->get_wc_order(); + $wc_order = $this->get_wc_order(); - $transactions->method('count_transaction_id')->willReturn(0); - $request->method('get_body') + $transactions->method( 'count_transaction_id' )->willReturn( 0 ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_paid_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_paid_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -110,20 +110,21 @@ public function it_should_do_not_allow_to_process_with_wrong_transaction() { $logger ); - $logger->expects(self::exactly(2))->method('execute')->with( - self::callback(function($msg) { - if (is_array($msg)) { - return true; - } + $logger->expects( self::exactly( 2 ) )->method( 'execute' )->with( + self::callback( + function ( $msg ) { + if ( is_array( $msg ) ) { + return true; + } - return $msg === 'Order id = someWcId, BitPay invoice id = someId. Wrong transaction id someId'; - }, + return $msg === 'Order id = someWcId, BitPay invoice id = someId. Wrong transaction id someId'; + }, ) ); - $transactions->expects(self::never())->method('update_transaction_status'); + $transactions->expects( self::never() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** @@ -131,32 +132,32 @@ public function it_should_do_not_allow_to_process_with_wrong_transaction() { */ public function it_should_process_ipn_request() { // given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->get_wc_order(); + $wc_order = $this->get_wc_order(); - $transactions->method('count_transaction_id')->willReturn(1); - $bitpay_invoice->method('getStatus')->willReturn('paid'); - $bitpay_invoice->method('getId')->willReturn(self::BITPAY_INVOICE_ID); - $request->method('get_body') + $transactions->method( 'count_transaction_id' )->willReturn( 1 ); + $bitpay_invoice->method( 'getStatus' )->willReturn( 'paid' ); + $bitpay_invoice->method( 'getId' )->willReturn( self::BITPAY_INVOICE_ID ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_paid_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_paid_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -167,13 +168,13 @@ public function it_should_process_ipn_request() { // then $wc_order - ->expects(self::once()) - ->method('add_order_note') - ->with('BitPay Invoice ID: someId is paid and awaiting confirmation.'); - $transactions->expects(self::once())->method('update_transaction_status'); + ->expects( self::once() ) + ->method( 'add_order_note' ) + ->with( 'BitPay Invoice ID: someId is paid and awaiting confirmation.' ); + $transactions->expects( self::once() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** @@ -181,32 +182,32 @@ public function it_should_process_ipn_request() { */ public function it_should_confirm_order(): void { // given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->get_wc_order(); + $wc_order = $this->get_wc_order(); - $transactions->method('count_transaction_id')->willReturn(1); - $bitpay_invoice->method('getStatus')->willReturn('confirmed'); - $bitpay_invoice->method('getId')->willReturn(self::BITPAY_INVOICE_ID); - $request->method('get_body') + $transactions->method( 'count_transaction_id' )->willReturn( 1 ); + $bitpay_invoice->method( 'getStatus' )->willReturn( 'confirmed' ); + $bitpay_invoice->method( 'getId' )->willReturn( self::BITPAY_INVOICE_ID ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_confirmed_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_confirmed_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->expects(self::once())->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->expects( self::once() )->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -216,17 +217,17 @@ public function it_should_confirm_order(): void { ); $wc_order - ->expects(self::exactly(2)) - ->method('add_order_note') + ->expects( self::exactly( 2 ) ) + ->method( 'add_order_note' ) ->withConsecutive( - ['BitPay Invoice ID: someId has changed to Completed.'], - ['Payment Completed'], + array( 'BitPay Invoice ID: someId has changed to Completed.' ), + array( 'Payment Completed' ), ); - $wc_order->expects(self::once())->method('payment_complete'); - $transactions->expects(self::once())->method('update_transaction_status'); + $wc_order->expects( self::once() )->method( 'payment_complete' ); + $transactions->expects( self::once() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** @@ -234,33 +235,33 @@ public function it_should_confirm_order(): void { */ public function it_should_complete_order(): void { // given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->get_wc_order(); + $wc_order = $this->get_wc_order(); - $transactions->method('count_transaction_id')->willReturn(1); - $bitpay_invoice->method('getStatus')->willReturn('complete'); - $bitpay_invoice->method('getId')->willReturn(self::BITPAY_INVOICE_ID); - $wc_order->method('get_status')->willReturn('wc-pending'); - $request->method('get_body') + $transactions->method( 'count_transaction_id' )->willReturn( 1 ); + $bitpay_invoice->method( 'getStatus' )->willReturn( 'complete' ); + $bitpay_invoice->method( 'getId' )->willReturn( self::BITPAY_INVOICE_ID ); + $wc_order->method( 'get_status' )->willReturn( 'wc-pending' ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_completed_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_completed_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->expects(self::once())->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->expects( self::once() )->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -270,17 +271,17 @@ public function it_should_complete_order(): void { ); $wc_order - ->expects(self::exactly(2)) - ->method('add_order_note') + ->expects( self::exactly( 2 ) ) + ->method( 'add_order_note' ) ->withConsecutive( - ['BitPay Invoice ID: someId has changed to Completed.'], - ['Payment Completed'], + array( 'BitPay Invoice ID: someId has changed to Completed.' ), + array( 'Payment Completed' ), ); - $wc_order->expects(self::once())->method('payment_complete'); - $transactions->expects(self::once())->method('update_transaction_status'); + $wc_order->expects( self::once() )->method( 'payment_complete' ); + $transactions->expects( self::once() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** @@ -288,32 +289,32 @@ public function it_should_complete_order(): void { */ public function it_should_decline_order(): void { // given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->get_wc_order(); + $wc_order = $this->get_wc_order(); - $transactions->method('count_transaction_id')->willReturn(1); - $bitpay_invoice->method('getStatus')->willReturn('declined'); - $bitpay_invoice->method('getId')->willReturn(self::BITPAY_INVOICE_ID); - $request->method('get_body') + $transactions->method( 'count_transaction_id' )->willReturn( 1 ); + $bitpay_invoice->method( 'getStatus' )->willReturn( 'declined' ); + $bitpay_invoice->method( 'getId' )->willReturn( self::BITPAY_INVOICE_ID ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_declined_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_declined_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->expects(self::once())->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->expects( self::once() )->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -323,14 +324,14 @@ public function it_should_decline_order(): void { ); $wc_order - ->expects(self::once()) - ->method('add_order_note') - ->with('BitPay Invoice ID: someId has been declined.'); - $wc_order->expects(self::once())->method('update_status')->with('failed'); - $transactions->expects(self::once())->method('update_transaction_status'); + ->expects( self::once() ) + ->method( 'add_order_note' ) + ->with( 'BitPay Invoice ID: someId has been declined.' ); + $wc_order->expects( self::once() )->method( 'update_status' )->with( 'failed' ); + $transactions->expects( self::once() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** @@ -338,32 +339,32 @@ public function it_should_decline_order(): void { */ public function it_should_fail_order(): void { // given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->get_wc_order(); + $wc_order = $this->get_wc_order(); - $transactions->method('count_transaction_id')->willReturn(1); - $bitpay_invoice->method('getStatus')->willReturn('invalid'); - $bitpay_invoice->method('getId')->willReturn(self::BITPAY_INVOICE_ID); - $request->method('get_body') + $transactions->method( 'count_transaction_id' )->willReturn( 1 ); + $bitpay_invoice->method( 'getStatus' )->willReturn( 'invalid' ); + $bitpay_invoice->method( 'getId' )->willReturn( self::BITPAY_INVOICE_ID ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_invalid_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_invalid_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->expects(self::once())->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->expects( self::once() )->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -373,14 +374,14 @@ public function it_should_fail_order(): void { ); $wc_order - ->expects(self::once()) - ->method('add_order_note') - ->with('BitPay Invoice ID: someId has become invalid because of network congestion. Order will automatically update when the status changes.'); - $wc_order->expects(self::once())->method('update_status')->with('failed'); - $transactions->expects(self::once())->method('update_transaction_status'); + ->expects( self::once() ) + ->method( 'add_order_note' ) + ->with( 'BitPay Invoice ID: someId has become invalid because of network congestion. Order will automatically update when the status changes.' ); + $wc_order->expects( self::once() )->method( 'update_status' )->with( 'failed' ); + $transactions->expects( self::once() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** @@ -388,32 +389,32 @@ public function it_should_fail_order(): void { */ public function it_should_expire_order(): void { // given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->get_wc_order(); + $wc_order = $this->get_wc_order(); - $transactions->method('count_transaction_id')->willReturn(1); - $bitpay_invoice->method('getStatus')->willReturn('expired'); - $bitpay_invoice->method('getId')->willReturn(self::BITPAY_INVOICE_ID); - $request->method('get_body') + $transactions->method( 'count_transaction_id' )->willReturn( 1 ); + $bitpay_invoice->method( 'getStatus' )->willReturn( 'expired' ); + $bitpay_invoice->method( 'getId' )->willReturn( self::BITPAY_INVOICE_ID ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_expired_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_expired_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->expects(self::once())->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->expects( self::once() )->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -423,46 +424,46 @@ public function it_should_expire_order(): void { ); $wc_order - ->expects(self::once()) - ->method('add_order_note') - ->with('BitPay Invoice ID: someId has expired.'); - $wc_order->expects(self::once())->method('update_status')->with('cancelled'); - $transactions->expects(self::once())->method('update_transaction_status'); + ->expects( self::once() ) + ->method( 'add_order_note' ) + ->with( 'BitPay Invoice ID: someId has expired.' ); + $wc_order->expects( self::once() )->method( 'update_status' )->with( 'cancelled' ); + $transactions->expects( self::once() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** * @test */ public function it_should_refund_order(): void { - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->get_wc_order(); + $wc_order = $this->get_wc_order(); - $transactions->method('count_transaction_id')->willReturn(1); - $bitpay_invoice->method('getStatus')->willReturn('invalid'); - $bitpay_invoice->method('getId')->willReturn(self::BITPAY_INVOICE_ID); - $request->method('get_body') + $transactions->method( 'count_transaction_id' )->willReturn( 1 ); + $bitpay_invoice->method( 'getStatus' )->willReturn( 'invalid' ); + $bitpay_invoice->method( 'getId' )->willReturn( self::BITPAY_INVOICE_ID ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_refunded_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_refunded_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->expects(self::once())->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->expects( self::once() )->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -472,14 +473,14 @@ public function it_should_refund_order(): void { ); $wc_order - ->expects(self::once()) - ->method('add_order_note') - ->with('BitPay Invoice ID: someId has been refunded.'); - $wc_order->expects(self::once())->method('update_status')->with('refunded'); - $transactions->expects(self::once())->method('update_transaction_status'); + ->expects( self::once() ) + ->method( 'add_order_note' ) + ->with( 'BitPay Invoice ID: someId has been refunded.' ); + $wc_order->expects( self::once() )->method( 'update_status' )->with( 'refunded' ); + $transactions->expects( self::once() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** @@ -487,33 +488,33 @@ public function it_should_refund_order(): void { */ public function it_should_complete_for_wcorder_wccomplete_status_and_wccompleted_for_complete_action_in_admin() { // given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->get_wc_order(); + $wc_order = $this->get_wc_order(); - $transactions->method('count_transaction_id')->willReturn(1); - $bitpay_invoice->method('getStatus')->willReturn('complete'); - $bitpay_invoice->method('getId')->willReturn(self::BITPAY_INVOICE_ID); - $wc_order->method('get_status')->willReturn('wc-completed'); - $request->method('get_body') + $transactions->method( 'count_transaction_id' )->willReturn( 1 ); + $bitpay_invoice->method( 'getStatus' )->willReturn( 'complete' ); + $bitpay_invoice->method( 'getId' )->willReturn( self::BITPAY_INVOICE_ID ); + $wc_order->method( 'get_status' )->willReturn( 'wc-completed' ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_completed_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_completed_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->expects(self::once())->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->expects( self::once() )->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -523,51 +524,51 @@ public function it_should_complete_for_wcorder_wccomplete_status_and_wccompleted ); $wc_order - ->expects(self::exactly(2)) - ->method('add_order_note') + ->expects( self::exactly( 2 ) ) + ->method( 'add_order_note' ) ->withConsecutive( - ['BitPay Invoice ID: someId has changed to Completed.'], - ['Payment Completed'], + array( 'BitPay Invoice ID: someId has changed to Completed.' ), + array( 'Payment Completed' ), ); - $wc_order->expects(self::once())->method('payment_complete'); - $transactions->expects(self::once())->method('update_transaction_status'); + $wc_order->expects( self::once() )->method( 'payment_complete' ); + $transactions->expects( self::once() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** * @test */ public function it_should_complete_for_wcorder_wcprocessing_status_and_wccompleted_for_complete_action_in_admin() { -// given - $wordpress_helper = $this->get_wordpress_helper(); - $request = $this->getMockBuilder(\WP_REST_Request::class)->getMock(); - $transactions = $this->get_checkout_transactions(); - $bitpay_invoice = $this->getMockBuilder(\BitPaySDK\Model\Invoice\Invoice::class)->getMock(); - $bitpay_client = $this->getMockBuilder(\BitPaySDK\Client::class) + // given + $wordpress_helper = $this->get_wordpress_helper(); + $request = $this->getMockBuilder( \WP_REST_Request::class )->getMock(); + $transactions = $this->get_checkout_transactions(); + $bitpay_invoice = $this->getMockBuilder( \BitPaySDK\Model\Invoice\Invoice::class )->getMock(); + $bitpay_client = $this->getMockBuilder( \BitPaySDK\Client::class ) ->disableOriginalConstructor() ->getMock(); - $logger = $this->get_bitpay_logger(); - $bitpay_client_factory = $this->getMockBuilder(BitPayClientFactory::class) + $logger = $this->get_bitpay_logger(); + $bitpay_client_factory = $this->getMockBuilder( BitPayClientFactory::class ) ->disableOriginalConstructor()->getMock(); - $transactions->method('count_transaction_id')->willReturn(1); - $bitpay_invoice->method('getStatus')->willReturn('complete'); - $bitpay_invoice->method('getId')->willReturn(self::BITPAY_INVOICE_ID); + $transactions->method( 'count_transaction_id' )->willReturn( 1 ); + $bitpay_invoice->method( 'getStatus' )->willReturn( 'complete' ); + $bitpay_invoice->method( 'getId' )->willReturn( self::BITPAY_INVOICE_ID ); $wc_order = $this->get_wc_order(); - $wc_order->method('get_status')->willReturn('wc-processing'); - $request->method('get_body') + $wc_order->method( 'get_status' )->willReturn( 'wc-processing' ); + $request->method( 'get_body' ) ->willReturn( - file_get_contents(__DIR__ . '/json/bitpay_completed_ipn_webhook.json') + file_get_contents( __DIR__ . '/json/bitpay_completed_ipn_webhook.json' ) ); - $bitpay_invoice->method('getOrderId')->willReturn( self::BITPAY_INVOICE_ID ); - $bitpay_client_factory->method('create')->willReturn( $bitpay_client ); - $bitpay_client->method('getInvoice')->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) - ->willReturn($bitpay_invoice); - $wordpress_helper->expects(self::once())->method('get_order') - ->with(self::BITPAY_INVOICE_ID) - ->willReturn($wc_order); + $bitpay_invoice->method( 'getOrderId' )->willReturn( self::BITPAY_INVOICE_ID ); + $bitpay_client_factory->method( 'create' )->willReturn( $bitpay_client ); + $bitpay_client->method( 'getInvoice' )->with( self::BITPAY_INVOICE_ID, \BitPaySDK\Model\Facade::POS, false ) + ->willReturn( $bitpay_invoice ); + $wordpress_helper->expects( self::once() )->method( 'get_order' ) + ->with( self::BITPAY_INVOICE_ID ) + ->willReturn( $wc_order ); $testedClass = $this->getTestedClass( $wordpress_helper, @@ -577,43 +578,41 @@ public function it_should_complete_for_wcorder_wcprocessing_status_and_wccomplet ); $wc_order - ->expects(self::exactly(2)) - ->method('add_order_note') + ->expects( self::exactly( 2 ) ) + ->method( 'add_order_note' ) ->withConsecutive( - ['BitPay Invoice ID: someId has changed to Completed.'], - ['Payment Completed'], + array( 'BitPay Invoice ID: someId has changed to Completed.' ), + array( 'Payment Completed' ), ); - $wc_order->expects(self::once())->method('payment_complete'); - $transactions->expects(self::once())->method('update_transaction_status'); + $wc_order->expects( self::once() )->method( 'payment_complete' ); + $transactions->expects( self::once() )->method( 'update_transaction_status' ); // when - $testedClass->execute($request); + $testedClass->execute( $request ); } /** * @return (BitPayLogger|\PHPUnit\Framework\MockObject\MockObject) */ private function get_bitpay_logger() { - return $this->getMockBuilder(BitPayLogger::class)->disableOriginalConstructor()->getMock(); + return $this->getMockBuilder( BitPayLogger::class )->disableOriginalConstructor()->getMock(); } /** * @return (BitPayCheckoutTransactions&\PHPUnit\Framework\MockObject\MockObject) */ - private function get_checkout_transactions(): BitPayCheckoutTransactions|\PHPUnit\Framework\MockObject\MockObject - { - return $this->getMockBuilder(BitPayCheckoutTransactions::class) + private function get_checkout_transactions(): BitPayCheckoutTransactions|\PHPUnit\Framework\MockObject\MockObject { + return $this->getMockBuilder( BitPayCheckoutTransactions::class ) ->disableOriginalConstructor()->getMock(); } /** * @return \PHPUnit\Framework\MockObject\MockObject|(\WC_Order&\PHPUnit\Framework\MockObject\MockObject) */ - private function get_wc_order(): \PHPUnit\Framework\MockObject\MockObject|\WC_Order - { - $wc_order = $this->getMockBuilder(\WC_Order::class)->getMock(); - $wc_order->method('get_payment_method')->willReturn('bitpay_checkout_gateway'); - $wc_order->method('get_id')->willReturn(self::WC_ORDER_ID); + private function get_wc_order(): \PHPUnit\Framework\MockObject\MockObject|\WC_Order { + $wc_order = $this->getMockBuilder( \WC_Order::class )->getMock(); + $wc_order->method( 'get_payment_method' )->willReturn( 'bitpay_checkout_gateway' ); + $wc_order->method( 'get_id' )->willReturn( self::WC_ORDER_ID ); return $wc_order; } @@ -621,18 +620,17 @@ private function get_wc_order(): \PHPUnit\Framework\MockObject\MockObject|\WC_Or /** * @return (BitPayWordpressHelper&\PHPUnit\Framework\MockObject\MockObject) */ - private function get_wordpress_helper(): BitPayWordpressHelper|\PHPUnit\Framework\MockObject\MockObject - { - $helper = $this->getMockBuilder(BitPayWordpressHelper::class)->getMock(); + private function get_wordpress_helper(): BitPayWordpressHelper|\PHPUnit\Framework\MockObject\MockObject { + $helper = $this->getMockBuilder( BitPayWordpressHelper::class )->getMock(); $helper->method( 'get_bitpay_gateway_option' ) ->willReturnCallback( - function ($name) { - return match ($name) { + function ( $name ) { + return match ( $name ) { 'bitpay_checkout_endpoint' => 'test', 'bitpay_checkout_order_process_complete_status', 'bitpay_checkout_order_process_confirmed_status' => 'wc-completed', 'bitpay_checkout_order_process_paid_status' => 'wc-processing', 'bitpay_checkout_order_expired_status' => '1', - default => throw new RuntimeException('Wrong option'), + default => throw new RuntimeException( 'Wrong option' ), }; } ); @@ -646,6 +644,6 @@ private function getTestedClass( BitPayCheckoutTransactions $bitpay_checkout_transactions, BitPayLogger $logger ): BitPayIpnProcess { - return new BitPayIpnProcess($bitpay_checkout_transactions, $bitpay_client_factory, $wordpress_helper, $logger); + return new BitPayIpnProcess( $bitpay_checkout_transactions, $bitpay_client_factory, $wordpress_helper, $logger ); } } diff --git a/tests/Unit/BitPayLib/test-classbitpayinvoicefactory.php b/tests/Unit/BitPayLib/test-classbitpayinvoicefactory.php index d0fc98c..bb21505 100644 --- a/tests/Unit/BitPayLib/test-classbitpayinvoicefactory.php +++ b/tests/Unit/BitPayLib/test-classbitpayinvoicefactory.php @@ -16,42 +16,41 @@ class BitPayInvoiceFactoryTest extends WP_UnitTestCase { */ public function it_should_create_bitpay_invoice_by_wc_order_id(): void { // given - $expected_price = 12.34; + $expected_price = 12.34; $expected_currency = 'USD'; $expected_order_id = '123'; - $buyer_name = 'SomeName'; - $buyer_email = 'some@email.com'; - $wc_order_id = 44444; - $checkout_url = 'https://checkout-url.com'; + $buyer_name = 'SomeName'; + $buyer_email = 'some@email.com'; + $wc_order_id = 44444; + $checkout_url = 'https://checkout-url.com'; - $payment_settings = $this->getMockBuilder(BitPayPaymentSettings::class)->getMock(); - $wordpress_helper = $this->getMockBuilder(BitPayWordpressHelper::class) + $payment_settings = $this->getMockBuilder( BitPayPaymentSettings::class )->getMock(); + $wordpress_helper = $this->getMockBuilder( BitPayWordpressHelper::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->getMockBuilder(\WC_Order::class)->getMock(); - $tested_class = new BitPayInvoiceFactory($payment_settings, $wordpress_helper); + $wc_order = $this->getMockBuilder( \WC_Order::class )->getMock(); + $tested_class = new BitPayInvoiceFactory( $payment_settings, $wordpress_helper ); - $wc_order->method('get_total')->willReturn($expected_price); - $wc_order->method('get_currency')->willReturn($expected_currency); - $wc_order->method('get_order_number')->willReturn($expected_order_id); - $wc_order->method('get_order_key')->willReturn('123'); - $wc_order->method('get_id')->willReturn($wc_order_id); - $wp_user = new \WP_User(); + $wc_order->method( 'get_total' )->willReturn( $expected_price ); + $wc_order->method( 'get_currency' )->willReturn( $expected_currency ); + $wc_order->method( 'get_order_number' )->willReturn( $expected_order_id ); + $wc_order->method( 'get_order_key' )->willReturn( '123' ); + $wc_order->method( 'get_id' )->willReturn( $wc_order_id ); + $wp_user = new \WP_User(); $wp_user->display_name = $buyer_name; - $wp_user->user_email = $buyer_email; - $wordpress_helper->method('get_home_url')->willReturn('https://some-url.com'); - $wordpress_helper->method('wp_get_current_user')->willReturn($wp_user); - $wordpress_helper->method('get_checkout_url')->willReturn($checkout_url); - $payment_settings->method('should_capture_email')->willReturn(true); - $payment_settings->method('get_custom_redirect_page')->willReturn(null); - $payment_settings->method('get_checkout_slug')->willReturn(null); + $wp_user->user_email = $buyer_email; + $wordpress_helper->method( 'get_home_url' )->willReturn( 'https://some-url.com' ); + $wordpress_helper->method( 'wp_get_current_user' )->willReturn( $wp_user ); + $wordpress_helper->method( 'get_checkout_url' )->willReturn( $checkout_url ); + $payment_settings->method( 'should_capture_email' )->willReturn( true ); + $payment_settings->method( 'get_custom_redirect_page' )->willReturn( null ); $endpoint_url = 'https://some-endpoint_url.com'; - $wordpress_helper->method('get_endpoint_url') - ->with('order-received', (string) $wc_order_id, $checkout_url) - ->willReturn($endpoint_url); + $wordpress_helper->method( 'get_endpoint_url' ) + ->with( 'order-received', (string) $wc_order_id, $checkout_url ) + ->willReturn( $endpoint_url ); // when - $bitpay_invoice = $tested_class->create_by_wc_order($wc_order); + $bitpay_invoice = $tested_class->create_by_wc_order( $wc_order ); // then self::assertSame( $expected_price, $bitpay_invoice->getPrice() ); @@ -66,30 +65,29 @@ public function it_should_create_bitpay_invoice_by_wc_order_id(): void { /** * @test */ - public function it_should_use_custom_checkout_page_for_redirect_url(): void { + public function it_should_use_custom_redirect_page_for_redirect_url(): void { // given $checkout_url = 'https://checkout-url.com'; - $payment_settings = $this->getMockBuilder(BitPayPaymentSettings::class)->getMock(); - $wordpress_helper = $this->getMockBuilder(BitPayWordpressHelper::class) + $payment_settings = $this->getMockBuilder( BitPayPaymentSettings::class )->getMock(); + $wordpress_helper = $this->getMockBuilder( BitPayWordpressHelper::class ) ->disableOriginalConstructor()->getMock(); - $wc_order = $this->getMockBuilder(\WC_Order::class)->getMock(); - $tested_class = new BitPayInvoiceFactory($payment_settings, $wordpress_helper); + $wc_order = $this->getMockBuilder( \WC_Order::class )->getMock(); + $tested_class = new BitPayInvoiceFactory( $payment_settings, $wordpress_helper ); - $wc_order->method('get_total')->willReturn(12.34); - $wc_order->method('get_currency')->willReturn('USD'); - $wc_order->method('get_order_number')->willReturn('123'); - $wc_order->method('get_order_key')->willReturn('1234'); - $wc_order->method('get_id')->willReturn(44444); - $wordpress_helper->method('get_home_url')->willReturn('https://some-url.com'); - $wordpress_helper->method('wp_get_current_user')->willReturn(new \WP_User()); - $wordpress_helper->method('get_checkout_url')->willReturn($checkout_url); - $payment_settings->method('should_capture_email')->willReturn(true); - $payment_settings->method('get_custom_redirect_page')->willReturn('https://some-custom.com'); - $payment_settings->method('get_checkout_slug')->willReturn(null); + $wc_order->method( 'get_total' )->willReturn( 12.34 ); + $wc_order->method( 'get_currency' )->willReturn( 'USD' ); + $wc_order->method( 'get_order_number' )->willReturn( '123' ); + $wc_order->method( 'get_order_key' )->willReturn( '1234' ); + $wc_order->method( 'get_id' )->willReturn( 44444 ); + $wordpress_helper->method( 'get_home_url' )->willReturn( 'https://some-url.com' ); + $wordpress_helper->method( 'wp_get_current_user' )->willReturn( new \WP_User() ); + $wordpress_helper->method( 'get_checkout_url' )->willReturn( $checkout_url ); + $payment_settings->method( 'should_capture_email' )->willReturn( true ); + $payment_settings->method( 'get_custom_redirect_page' )->willReturn( 'https://some-custom.com' ); // when - $bitpay_invoice = $tested_class->create_by_wc_order($wc_order); + $bitpay_invoice = $tested_class->create_by_wc_order( $wc_order ); // then self::assertSame( 'https://some-custom.com?custompage=true', $bitpay_invoice->getRedirectURL() ); diff --git a/tests/Unit/bootstrap.php b/tests/Unit/bootstrap.php index cb1fbd5..b170c27 100644 --- a/tests/Unit/bootstrap.php +++ b/tests/Unit/bootstrap.php @@ -6,8 +6,11 @@ require_once getenv( 'WP_PHPUNIT__DIR' ) . '/includes/functions.php'; -tests_add_filter( 'muplugins_loaded', function() { - require __DIR__ . '/../../../woocommerce/woocommerce.php'; -}); +tests_add_filter( + 'muplugins_loaded', + function () { + require __DIR__ . '/../../../woocommerce/woocommerce.php'; + } +); -require getenv( 'WP_PHPUNIT__DIR' ) . '/includes/bootstrap.php'; \ No newline at end of file +require getenv( 'WP_PHPUNIT__DIR' ) . '/includes/bootstrap.php'; diff --git a/tests/Unit/wp-config.dist.php b/tests/Unit/wp-config.dist.php index 854c423..59353f8 100644 --- a/tests/Unit/wp-config.dist.php +++ b/tests/Unit/wp-config.dist.php @@ -50,14 +50,14 @@ * Change these to different unique phrases! * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service} */ -define( 'AUTH_KEY', 'put your unique phrase here' ); -define( 'SECURE_AUTH_KEY', 'put your unique phrase here' ); -define( 'LOGGED_IN_KEY', 'put your unique phrase here' ); -define( 'NONCE_KEY', 'put your unique phrase here' ); -define( 'AUTH_SALT', 'put your unique phrase here' ); +define( 'AUTH_KEY', 'put your unique phrase here' ); +define( 'SECURE_AUTH_KEY', 'put your unique phrase here' ); +define( 'LOGGED_IN_KEY', 'put your unique phrase here' ); +define( 'NONCE_KEY', 'put your unique phrase here' ); +define( 'AUTH_SALT', 'put your unique phrase here' ); define( 'SECURE_AUTH_SALT', 'put your unique phrase here' ); -define( 'LOGGED_IN_SALT', 'put your unique phrase here' ); -define( 'NONCE_SALT', 'put your unique phrase here' ); +define( 'LOGGED_IN_SALT', 'put your unique phrase here' ); +define( 'NONCE_SALT', 'put your unique phrase here' ); $table_prefix = 'wptests_'; // Only numbers, letters, and underscores please! @@ -67,4 +67,4 @@ define( 'WP_PHP_BINARY', 'php' ); -define( 'WPLANG', '' ); \ No newline at end of file +define( 'WPLANG', '' );