Skip to content

Commit

Permalink
Added Brain package to mock WP functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Aashish committed Nov 14, 2023
1 parent 154eb00 commit c8ffd66
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -5,7 +5,8 @@
"license": "MIT",
"require-dev": {
"phpunit/phpunit": "^5.7 || ^9.5",
"mockery/mockery": "^1.6"
"mockery/mockery": "^1.6",
"brain/monkey": "^2.6"
},
"scripts": {
"test": "vendor/bin/phpunit --testdox --colors"
Expand Down
4 changes: 2 additions & 2 deletions languages/omise-ja.po
Expand Up @@ -127,7 +127,7 @@ msgid "Opn Payments Credit / Debit Card"
msgstr "Opn Payments クレジットカード / デビットカード"

#: includes/gateway/class-omise-payment-creditcard.php:23
msgid "Accept payment through <strong>Credit / Debit Card</strong> via Opn Payments payment gateway."
msgid "Accept payment through <strong>Credit / Debit Card</strong> via Opn Payments."
msgstr "Opn Payments決済ゲートウェイを経由して<strong>クレジットカード/デビットカード</strong>決済を受け付けます。"

#: includes/gateway/class-omise-payment-creditcard.php:58
Expand Down Expand Up @@ -175,7 +175,7 @@ msgid "Supported card icons"
msgstr "ご利用可能ブランド"

#: includes/gateway/class-omise-payment-creditcard.php:134
msgid "This only controls the icons displayed on the checkout page.<br />It is not related to card processing on Opn Payments payment gateway."
msgid "This only controls the icons displayed on the checkout page.<br />It is not related to card processing on Opn Payments."
msgstr "ご提供可能なカードブランドのアイコンを決済画面に表示できます。<br/>本項目の選択内容は、カード処理システムには影響いたしません。"

#: includes/gateway/class-omise-payment-creditcard.php:191
Expand Down
4 changes: 2 additions & 2 deletions languages/omise.pot
Expand Up @@ -126,7 +126,7 @@ msgid "Opn Payments Credit / Debit Card"
msgstr "Opn Payments クレジットカード / デビットカード"

#: includes/gateway/class-omise-payment-creditcard.php:23
msgid "Accept payment through <strong>Credit / Debit Card</strong> via Opn Payments payment gateway."
msgid "Accept payment through <strong>Credit / Debit Card</strong> via Opn Payments."
msgstr "Opn Payments決済ゲートウェイを経由して<strong>クレジットカード/デビットカード</strong>決済を受け付けます。"

#: includes/gateway/class-omise-payment-creditcard.php:58
Expand Down Expand Up @@ -174,7 +174,7 @@ msgid "Supported card icons"
msgstr "ご利用可能ブランド"

#: includes/gateway/class-omise-payment-creditcard.php:134
msgid "This only controls the icons displayed on the checkout page.<br />It is not related to card processing on Opn Payments payment gateway."
msgid "This only controls the icons displayed on the checkout page.<br />It is not related to card processing on Opn Payments."
msgstr "ご提供可能なカードブランドのアイコンを決済画面に表示できます。<br/>本項目の選択内容は、カード処理システムには影響いたしません。"

#: includes/gateway/class-omise-payment-creditcard.php:191
Expand Down
15 changes: 7 additions & 8 deletions tests/unit/includes/gateway/bootstrap-test-setup.php
@@ -1,28 +1,27 @@
<?php

use PHPUnit\Framework\TestCase;
use Brain;

abstract class Bootstrap_Test_Setup extends TestCase
{
public $sourceType;

public function setUp(): void
{
// mocking WP built-in functions
if (!function_exists('wp_kses')) {
function wp_kses() {}
}

if (!function_exists('add_action')) {
function add_action() {}
}
Brain\Monkey\setUp();
Brain\Monkey\Functions\stubs( [
'wp_kses' => null,
'add_action' => null,
] );
}

/**
* close mockery after tests are done
*/
public function tearDown(): void
{
Brain\Monkey\tearDown();
Mockery::close();
}

Expand Down
@@ -0,0 +1,65 @@
<?php

use PHPUnit\Framework\TestCase;
use Brain;

class Omise_Payment_CreditCard_Test extends TestCase
{
public function setUp(): void
{
Brain\Monkey\setUp();

$omisePaymentMock = Mockery::mock('overload:Omise_Payment');
$omisePaymentMock->shouldReceive('init_settings');
$omisePaymentMock->shouldReceive('get_option');
$omisePaymentMock->shouldReceive('is_test')
->andReturn(true);

$omiseCardImage = Mockery::mock('alias:Omise_Card_Image');
$omiseCardImage->shouldReceive('get_css')->times(6);
$omiseCardImage->shouldReceive('get_visa_image')->once();
$omiseCardImage->shouldReceive('get_visa_default_display')->once();
$omiseCardImage->shouldReceive('get_mastercard_image')->once();
$omiseCardImage->shouldReceive('get_mastercard_default_display')->once();
$omiseCardImage->shouldReceive('get_jcb_image')->once();
$omiseCardImage->shouldReceive('get_jcb_default_display')->once();
$omiseCardImage->shouldReceive('get_diners_image')->once();
$omiseCardImage->shouldReceive('get_diners_default_display')->once();
$omiseCardImage->shouldReceive('get_amex_image')->once();
$omiseCardImage->shouldReceive('get_amex_default_display')->once();
$omiseCardImage->shouldReceive('get_discover_image')->once();
$omiseCardImage->shouldReceive('get_discover_default_display')->once();

require_once __DIR__ . '/../../../../includes/gateway/traits/charge-request-builder-trait.php';
require_once __DIR__ . '/../../../../includes/gateway/abstract-omise-payment-base-card.php';
require_once __DIR__ . '/../../../../includes/gateway/class-omise-payment-creditcard.php';
}

public function tearDown(): void
{
Brain\Monkey\tearDown();
Mockery::close();
}

/**
* @test
*/
public function testClassIsInitializedProperly()
{
Brain\Monkey\Functions\stubs( [
'wp_kses' => null,
] );
$creditCard = new Omise_Payment_Creditcard;

$this->assertEquals($creditCard->source_type, 'credit_card');
$this->assertEquals(
$creditCard->method_description,
'Accept payment through <strong>Credit / Debit Card</strong> via Opn Payments.'
);

$this->assertEquals(
$creditCard->form_fields['accept_amex']['description'],
'This only controls the icons displayed on the checkout page.<br />It is not related to card processing on Opn Payments.'
);
}
}
Expand Up @@ -21,12 +21,6 @@ function plugins_url() {
}
}

if (!function_exists('apply_filters')) {
function apply_filters() {
return "http://localhost/image.png";
}
}

if (!function_exists('wc_get_user_agent')) {
function wc_get_user_agent() {
return "Chrome Web";
Expand Down Expand Up @@ -80,7 +74,10 @@ public function supportsIsCorrect()
public function getIconReturnsCorrectImageLink()
{
$result = $this->obj->get_icon();
$this->assertEquals("http://localhost/image.png", $result);
$this->assertEquals(
"<img src='/ocbc-digital.png' class='Omise-Image' style='width: 60px; max-height: 30px;' alt='OCBC Digital' />",
$result
);
}

/**
Expand Down

0 comments on commit c8ffd66

Please sign in to comment.