Skip to content

Commit

Permalink
Add tests for different payment reference modes
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Jan 17, 2019
1 parent 6619f1f commit 09285f8
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 2 deletions.
16 changes: 15 additions & 1 deletion src/QrBill.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ class QrBill implements Validatable

const SWISS_CROSS_LOGO_FILE = __DIR__ . '/../assets/swiss-cross.png';

const ERROR_CORRECTION_LEVEL_HIGH = ErrorCorrectionLevel::HIGH;
const ERROR_CORRECTION_LEVEL_MEDIUM = ErrorCorrectionLevel::MEDIUM;
const ERROR_CORRECTION_LEVEL_LOW = ErrorCorrectionLevel::LOW;

/** @var Header */
private $header;

Expand All @@ -50,6 +54,9 @@ class QrBill implements Validatable
/** @var AlternativeScheme[] */
private $alternativeSchemes = [];

/** @var string */
private $errorCorrectionLevel = self::ERROR_CORRECTION_LEVEL_HIGH;

public static function create() : self
{
$header = new Header();
Expand Down Expand Up @@ -166,6 +173,13 @@ public function addAlternativeScheme(AlternativeScheme $alternativeScheme) : sel
return $this;
}

public function setErrorCorrectionLevel(string $errorCorrectionLevel) : self
{
$this->errorCorrectionLevel = $errorCorrectionLevel;

return $this;
}

public function getQrCode() : QrCode
{
if (!$this->isValid()) {
Expand All @@ -181,7 +195,7 @@ public function getQrCode() : QrCode
$qrCode->setLogoWidth(83); // recommended 7x7 mm in px @ 300dpi
$qrCode->setRoundBlockSize(false);
$qrCode->setMargin(0);
$qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel(ErrorCorrectionLevel::HIGH));
$qrCode->setErrorCorrectionLevel(new ErrorCorrectionLevel($this->errorCorrectionLevel));

return $qrCode;
}
Expand Down
54 changes: 54 additions & 0 deletions tests/QrBillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,44 @@ public function testPaymentReferenceMustBeValid()
$this->assertFalse($qrBill->isValid());
}

public function testPaymentReferenceScor()
{
$qrBill = $this->createQrBill([
'header',
'creditorInformation',
'creditor',
'paymentAmountInformation',
'paymentReferenceScor'
]);

# $qrBill->getQrCode()->writeFile(__DIR__ . '/TestData/qr-payment-reference-scor.png');

$this->assertSame(
(new QrReader(__DIR__ . '/TestData/qr-payment-reference-scor.png'))->text(),
$qrBill->getQrCode()->getText()
);
}

public function testPaymentReferenceNon()
{
$qrBill = $this->createQrBill([
'header',
'creditorInformation',
'creditor',
'paymentAmountInformation',
'paymentReferenceNon'
]);

# $qrBill
# ->setErrorCorrectionLevel(QrBill::ERROR_CORRECTION_LEVEL_MEDIUM) // due to limitations of QrReader class used in assert below
# ->getQrCode()->writeFile(__DIR__ . '/TestData/qr-payment-reference-non.png');

$this->assertSame(
(new QrReader(__DIR__ . '/TestData/qr-payment-reference-non.png'))->text(),
$qrBill->getQrCode()->getText()
);
}

public function testOptionalUltimateDebtorCanBeSet()
{
$qrBill = $this->createQrBill([
Expand Down Expand Up @@ -433,6 +471,22 @@ public function paymentReference(QrBill &$qrBill)
$qrBill->setPaymentReference($paymentReference);
}

public function paymentReferenceScor(QrBill &$qrBill)
{
$paymentReference = (new PaymentReference())
->setType(PaymentReference::TYPE_SCOR)
->setReference('RF18539007547034');
$qrBill->setPaymentReference($paymentReference);
}

public function paymentReferenceNon(QrBill &$qrBill)
{
$paymentReference = (new PaymentReference())
->setType(PaymentReference::TYPE_NON);

$qrBill->setPaymentReference($paymentReference);
}

public function invalidPaymentReference(QrBill &$qrBill)
{
$paymentReference = (new PaymentReference())
Expand Down
4 changes: 3 additions & 1 deletion tests/TestData/TestDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ public function qrFileProvider()
[__DIR__ . '/qr-full-set.png', '65461878a3a797567c9a2640b6a784e2'],
[__DIR__ . '/qr-minimal-setup.png', '00d82b2797622e24b4736b1974094c75'],
[__DIR__ . '/qr-payment-information-without-amount.png', '82abf9e7e2efb63d2b8d1d3809b82ddd'],
[__DIR__ . '/qr-payment-reference-non.png', 'e999c59e2b83e8adba46d4eca3242ac1'],
[__DIR__ . '/qr-payment-reference-scor.png', 'de185228c42c39b361a2a0f01627ec1f'],
[__DIR__ . '/qr-ultimate-debtor.png', '835efdaebe41b0b30e3f967b7aa6a0cb'],

[__DIR__ . '/proof-of-validation.png', '726bd835f52cb98d3874309222f7a727'],
[__DIR__ . '/proof-of-validation.png', 'a825cef70eb1b942d2b643439149bda1'],
];
}
}
Binary file modified tests/TestData/proof-of-validation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/TestData/qr-payment-reference-non.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/TestData/qr-payment-reference-scor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 09285f8

Please sign in to comment.