Skip to content

Commit

Permalink
Merge pull request #716 from ebanx/feature/unit-test-payment-adapter
Browse files Browse the repository at this point in the history
Add tests for payment adapter service
  • Loading branch information
Ruanito authored Nov 21, 2018
2 parents c0693c7 + 313d212 commit c685f78
Show file tree
Hide file tree
Showing 8 changed files with 206 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
env:
- TEST_COUNTRY=
script:
- phpunit
- ./vendor/bin/phpunit
- stage: admin_tests
env:
- TEST_COUNTRY=
Expand Down
1 change: 1 addition & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<!-- Exclude paths -->
<exclude-pattern>*/vendor/*</exclude-pattern>
<exclude-pattern>*/tests/unit/*</exclude-pattern>
<exclude-pattern>*/tests/woocommerce/*</exclude-pattern>

<!-- Configs -->
Expand Down
22 changes: 8 additions & 14 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.1/phpunit.xsd"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/4.8/phpunit.xsd"
bootstrap="tests/unit/bootstrap.php"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
colors="true"
verbose="true"
>
<testsuite>
<directory prefix="test-" suffix=".php">./tests</directory>
<testsuite name="woocommerce">
<directory suffix=".php">./tests/unit</directory>
</testsuite>

<logging>
<log type="coverage-clover" target="clover.xml"/>
<log type="coverage-html" target="./report" charset="UTF-8"
yui="true" highlight="true"
lowUpperBound="50" highLowerBound="80" />
</logging>
</phpunit>
12 changes: 6 additions & 6 deletions services/class-wc-ebanx-payment-adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private static function get_document( $configs, $names, $gateway_id ) {
* @return string
* @throws Exception Throws parameter missing exception.
*/
private static function get_argentinian_document( $names, $gateway_id ) {
public static function get_argentinian_document( $names, $gateway_id ) {
$document = WC_EBANX_Request::read( $names['ebanx_billing_argentina_document'], null );

if ( null === $document
Expand All @@ -235,7 +235,7 @@ private static function get_argentinian_document( $names, $gateway_id ) {
* @return string
* @throws Exception Throws parameter missing exception.
*/
private static function get_brazilian_document( $configs, $names, $gateway_id ) {
public static function get_brazilian_document( $configs, $names, $gateway_id ) {
$cpf = WC_EBANX_Request::read( $names['ebanx_billing_brazil_document'], null ) ?: WC_EBANX_Request::read( $gateway_id, null )['ebanx_billing_brazil_document'];
$cnpj = WC_EBANX_Request::read( $names['ebanx_billing_brazil_cnpj'], null ) ?: WC_EBANX_Request::read( $gateway_id, null )['ebanx_billing_brazil_cnpj'];

Expand Down Expand Up @@ -268,7 +268,7 @@ private static function get_brazilian_document( $configs, $names, $gateway_id )
* @return string
* @throws Exception Throws parameter missing exception.
*/
private static function get_chilean_document( $names, $gateway_id ) {
public static function get_chilean_document( $names, $gateway_id ) {
$document = WC_EBANX_Request::read( $names['ebanx_billing_chile_document'], null )
?: WC_EBANX_Request::read( $gateway_id, null )['ebanx_billing_chile_document'];
if ( null === $document ) {
Expand All @@ -286,7 +286,7 @@ private static function get_chilean_document( $names, $gateway_id ) {
* @return string
* @throws Exception Throws parameter missing exception.
*/
private static function get_colombian_document( $names, $gateway_id ) {
public static function get_colombian_document( $names, $gateway_id ) {
$document = WC_EBANX_Request::read( $names['ebanx_billing_colombia_document'], null )
?: WC_EBANX_Request::read( $gateway_id, null )['ebanx_billing_colombia_document'];
if ( null === $document ) {
Expand All @@ -304,7 +304,7 @@ private static function get_colombian_document( $names, $gateway_id ) {
* @return string
* @throws Exception Throws parameter missing exception.
*/
private static function get_peruvian_document( $names, $gateway_id ) {
public static function get_peruvian_document( $names, $gateway_id ) {
$document = WC_EBANX_Request::read( $names['ebanx_billing_peru_document'], null )
?: WC_EBANX_Request::read( $gateway_id, null )['ebanx_billing_peru_document'];
if ( null === $document ) {
Expand All @@ -322,7 +322,7 @@ private static function get_peruvian_document( $names, $gateway_id ) {
* @return string
* @throws Exception Throws parameter missing exception.
*/
private static function get_person_type( $configs, $names ) {
public static function get_person_type( $configs, $names ) {
$fields_options = array();
$person_type = Person::TYPE_PERSONAL;

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

define('ABSPATH', realpath('.'));

function autoload_services($class_name) {
$base_dir = realpath('.') . '/services/';
$relative_class = 'class-' . str_replace('_', '-', strtolower($class_name));
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require_once $file;
}
}

function autoload_builders($class_name) {
$base_dir = __DIR__ . '/helpers/builders/';
$relative_class = $class_name;
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
if (file_exists($file)) {
require $file;
}
}

spl_autoload_register('autoload_builders');
spl_autoload_register('autoload_services');
32 changes: 32 additions & 0 deletions tests/unit/helpers/builders/CheckoutRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace EBANX\Tests\Helpers\Builders;

class CheckoutRequestBuilder {
private $ebanx_billing_brazil_person_type;
private $ebanx_billing_brazil_document;
private $ebanx_billing_argentina_document;
private $ebanx_billing_chile_document;
private $ebanx_billing_colombia_document;
private $ebanx_billing_peru_document;

public function __construct() {
$_REQUEST = array();
}

public function with_ebanx_billing_brazil_person_type($person_type) {
$this->ebanx_billing_brazil_person_type = $person_type;
return $this;
}

public function with_ebanx_billing_document($document_type, $document) {
$this->$document_type = $document;
return $this;
}

public function build() {
foreach (get_object_vars($this) as $attribute => $value) {
if (!is_null($value)) $_REQUEST[$attribute] = $value;
}
}
}
22 changes: 22 additions & 0 deletions tests/unit/helpers/builders/GlobalConfigBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace EBANX\Tests\Helpers\Builders;

class GlobalConfigBuilder {

/* @var array */
public $settings;

public function __construct() {
$this->settings = array();
}

public function with_brazil_taxes_options($document_types) {
$this->settings['brazil_taxes_options'] = $document_types;
return $this;
}

public function build() {
return $this;
}
}
112 changes: 112 additions & 0 deletions tests/unit/services/PaymentAdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

use Ebanx\Benjamin\Models\Person;
use EBANX\Tests\Helpers\Builders\CheckoutRequestBuilder;
use EBANX\Tests\Helpers\Builders\GlobalConfigBuilder;
use PHPUnit\Framework\TestCase;

class PaymentAdapterTest extends TestCase {

/* @var array */
private $names;

/* @var GlobalConfigBuilder */
private $global_config_builder;

/* @var CheckoutRequestBuilder */
private $checkout_request_builder;

protected function setUp() {
parent::setUp();

$this->global_config_builder = new GlobalConfigBuilder();
$this->checkout_request_builder = new CheckoutRequestBuilder();

$this->names = [
'ebanx_billing_brazil_cnpj' => 'ebanx_billing_brazil_cnpj',
'ebanx_billing_brazil_document' => 'ebanx_billing_brazil_document',
'ebanx_billing_brazil_person_type' => 'ebanx_billing_brazil_person_type',
'ebanx_billing_argentina_document' => 'ebanx_billing_argentina_document',
'ebanx_billing_chile_document' => 'ebanx_billing_chile_document',
'ebanx_billing_colombia_document' => 'ebanx_billing_colombia_document',
'ebanx_billing_peru_document' => 'ebanx_billing_peru_document'
];

// define('ABSPATH', __DIR__);
}

public function getPersonTypeCasesData() {
return [
[[], NULL, Person::TYPE_PERSONAL],
[['cnpj'], NULL, Person::TYPE_BUSINESS],
[['cpf', 'cnpj'], NULL, Person::TYPE_PERSONAL],
[['cpf', 'cnpj'], 'cnpj', Person::TYPE_BUSINESS],
];
}

public function getDocumentByCountryCasesData() {
return [
['12-34567890-1', 'get_argentinian_document', 'ebanx_billing_argentina_document'],
['1234567890', 'get_chilean_document', 'ebanx_billing_chile_document'],
['1245678901', 'get_colombian_document', 'ebanx_billing_colombia_document'],
['1234678901', 'get_peruvian_document', 'ebanx_billing_peru_document'],
];
}

/**
* @dataProvider getPersonTypeCasesData()
*
* @param array $possible_document_types
* @param string $used_document_type
* @param string $expected_person_type
*
* @throws Exception Shouldn't be thrown.
*/
public function testGetPersonType($possible_document_types, $used_document_type, $expected_person_type) {
$configs = $this->global_config_builder
->with_brazil_taxes_options($possible_document_types)
->build();

$this->checkout_request_builder
->with_ebanx_billing_brazil_person_type($used_document_type)
->build();

$person_type = WC_EBANX_Payment_Adapter::get_person_type($configs, $this->names);

$this->assertEquals($expected_person_type, $person_type);
}

public function testGetBrazilianDocument() {
$expected_document = '123.456.789-90';
$configs = $this->global_config_builder
->with_brazil_taxes_options(['cpf'])
->build();

$this->checkout_request_builder
->with_ebanx_billing_document('ebanx_billing_brazil_document', $expected_document)
->build();

$document = WC_EBANX_Payment_Adapter::get_brazilian_document($configs, $this->names, NULL);

$this->assertEquals($expected_document, $document);
}

/**
* @dataProvider getDocumentByCountryCasesData()
*
* @param string $expected_document
* @param string $adapter_country_function
* @param string $document_type
*
* @throws Exception Shouldn't be thrown.
*/
public function testGetDocument($expected_document, $adapter_country_function, $document_type) {
$this->checkout_request_builder
->with_ebanx_billing_document($document_type, $expected_document)
->build();

$return_document = WC_EBANX_Payment_Adapter::$adapter_country_function($this->names, NULL);

$this->assertEquals($expected_document, $return_document);
}
}

0 comments on commit c685f78

Please sign in to comment.