Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactors to constants and adds tests #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/Sermepa/Tpv/Tpv.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@ class Tpv
CONST READ_TIMEOUT = 120;
CONST SSLVERSION_TLSv1_2 = 6;

public const INSTALLMENTS = 'I';
public const RECURRING = 'R';
public const REAUTHORIZATION = 'H';
public const RESUBMISSION = 'E';
public const DELAYED = 'D';
public const INCREMENTAL = 'M';
public const NO_SHOW = 'N';
public const OTHER = 'C';

protected $_setEnvironment;
protected $_setNameForm;
protected $_setIdForm;
Expand Down Expand Up @@ -918,16 +927,34 @@ public function setMerchantCofIni($isFirstTransaction)
*/
public function setMerchantCofType($value)
{
$validOptions = ['I', 'R', 'H', 'E', 'D', 'M', 'N', 'C'];
$value = strtoupper($value);
if (!in_array($value, $validOptions, true)) {
if (!self::isValidCofType($value)) {
throw new TpvException('Set Merchant COF type');
}
$this->_setParameters['DS_MERCHANT_COF_TYPE'] = $value;

return $this;
}

private static function isValidCofType($value): bool
{
return in_array($value, self::getValidCofTypes(), true);
}

private static function getValidCofTypes(): array
{
return [
self::INSTALLMENTS,
self::RECURRING,
self::REAUTHORIZATION,
self::RESUBMISSION,
self::DELAYED,
self::INCREMENTAL,
self::NO_SHOW,
self::OTHER,
];
}

/**
* COF identifier identifier. Optional. This
* identifier is returned in the answer of the first
Expand Down
67 changes: 67 additions & 0 deletions tests/TpvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -539,4 +539,71 @@ public function set_new_parameters()

}

public function validTransactionTypeProvider(): array
{
return [
'Installments' => [Tpv::INSTALLMENTS],
'Recurring' => [Tpv::RECURRING],
'Reauthorization' => [Tpv::REAUTHORIZATION],
'Resubmission' => [Tpv::RESUBMISSION],
'Delayed' => [Tpv::DELAYED],
'Incremental' => [Tpv::INCREMENTAL],
'No Show' => [Tpv::NO_SHOW],
'Otras' => [Tpv::OTHER],
];
}

/**
* @test
* @dataProvider validTransactionTypeProvider
*/
public function transaction_type_is_valid($type): void
{
$redsys = new Tpv();
$redsys->setMerchantCofType($type);
$ds = $redsys->getParameters();
$this->assertEquals($type, $ds['DS_MERCHANT_COF_TYPE']);
}



public function invalidTransactionTypeProvider(): array
{
return [
'A' => ['A'],
'B' => ['B'],
'F' => ['F'],
'G' => ['G'],
'J' => ['J'],
'K' => ['K'],
'L' => ['L'],
'O' => ['O'],
'P' => ['P'],
'Q' => ['Q'],
'S' => ['S'],
'T' => ['T'],
'U' => ['U'],
'V' => ['V'],
'W' => ['W'],
'X' => ['X'],
'Y' => ['Y'],
'Z' => ['Z'],
];
}

/**
* @test
* @dataProvider invalidTransactionTypeProvider
*/

public function throw_when_transaction_type_is_not_valid($type): void
{
$this->expectExceptionMessage('Set Merchant COF type');
$this->expectException(\Sermepa\Tpv\TpvException::class);
$redsys = new Tpv();
$redsys->setMerchantCofType($type);

$redsys->setParameters($type);

}
}