-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #716 from ebanx/feature/unit-test-payment-adapter
Add tests for payment adapter service
- Loading branch information
Showing
8 changed files
with
206 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |