Skip to content

Commit

Permalink
Add fixes and tests for alternative schemes
Browse files Browse the repository at this point in the history
  • Loading branch information
sprain committed Mar 13, 2018
1 parent f501b94 commit cc2b962
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 5 deletions.
24 changes: 21 additions & 3 deletions src/QrBill.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,13 @@ public function setAlternativeSchemes(array $alternativeSchemes) : self
return $this;
}

public function addAlternativeScheme(AlternativeScheme $alternativeScheme) : self
{
$this->alternativeSchemes[] = $alternativeScheme;

return $this;
}

public function getQrCode() : QrCode
{
if (!$this->isValid()) {
Expand Down Expand Up @@ -187,16 +194,20 @@ private function getQrCodeData() : string
$this->getAlternativeSchemes()
];

return $this->extractQrCodeDataFromElements($elements);
$qrCodeStringElements = $this->extractQrCodeDataFromElements($elements);

return implode("\r\n", $qrCodeStringElements);
}

private function extractQrCodeDataFromElements(array $elements) : string
private function extractQrCodeDataFromElements(array $elements) : array
{
$qrCodeElements = [];

foreach ($elements as $element) {
if ($element instanceof QrCodeData) {
$qrCodeElements = array_merge($qrCodeElements, $element->getQrCodeData());
} elseif (is_array($element)) {
$qrCodeElements = array_merge($qrCodeElements, $this->extractQrCodeDataFromElements($element));
}
}

Expand All @@ -206,7 +217,7 @@ private function extractQrCodeDataFromElements(array $elements) : string
$string = trim($string);
});

return implode("\r\n", $qrCodeElements);
return $qrCodeElements;
}

public static function loadValidatorMetadata(ClassMetadata $metadata)
Expand Down Expand Up @@ -243,5 +254,12 @@ public static function loadValidatorMetadata(ClassMetadata $metadata)
new Assert\NotNull(),
new Assert\Valid()
]);

$metadata->addPropertyConstraints('alternativeSchemes', [
new Assert\Count([
'max' => 2
]),
new Assert\Valid()
]);
}
}
66 changes: 65 additions & 1 deletion tests/QrBillTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,63 @@ public function testOptionalUltimateDebtorMustBeValid()

$this->assertFalse($qrBill->isValid());
}


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

$this->assertSame(
file_get_contents(__DIR__ . '/TestData/qr-alternative-schemes.png'),
$qrBill->getQrCode()->writeString()
);
}

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

$this->assertFalse($qrBill->isValid());
}

public function testFullQrCodeSet()
{
$qrBill = $this->createQrBill([
'header',
'creditorInformation',
'creditor',
'ultimateCreditor',
'paymentAmountInformation',
'paymentReferenceWithMessage',
'ultimateDebtor',
'alternativeScheme',
'alternativeScheme'
]);

$qrBill->getQrCode()->writeFile(__DIR__ . '/TestData/qr-full-set.png');

$this->assertSame(
file_get_contents(__DIR__ . '/TestData/qr-full-set.png'),
$qrBill->getQrCode()->writeString()
);
}

/**
* @expectedException \Sprain\SwissQrBill\Exception\InvalidQrBillDataException
*/
Expand Down Expand Up @@ -371,6 +427,14 @@ public function invalidUltimateDebtor(QrBill &$qrBill)
$qrBill->setUltimateDebtor($this->invalidAddress());
}

public function alternativeScheme(QrBill &$qrBill)
{
$alternativeScheme = (new AlternativeScheme())
->setParameter('alternativeSchemeParameter');

$qrBill->addAlternativeScheme($alternativeScheme);
}

public function address()
{
return (new Address())
Expand Down
3 changes: 2 additions & 1 deletion tests/TestData/TestDataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ public function testQrFile($file, $hash)
public function qrFileProvider()
{
return [
[__DIR__ . '/qr-alternative-schemes.png', '4600113fa99186059b159550c312abdb'],
[__DIR__ . '/qr-full-set.png', '2453cd8ab49dfbc3d4da9b8081557f47'],
[__DIR__ . '/qr-minimal-setup.png', 'c8ea1adfa1e22189c0b491073e0f9c7b'],
[__DIR__ . '/qr-payment-reference-with-message.png', 'd17781636acb9de913c189e3ca78d0b0'],
[__DIR__ . '/qr-ultimate-creditor.png', '6830f77bb6f80221891a3e9de3bcbcbd'],
[__DIR__ . '/qr-ultimate-debtor.png', 'f05a37e51fa10c89a6e8da99a69848a0'],

];
}
}
Binary file added tests/TestData/qr-alternative-schemes.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-full-set.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 cc2b962

Please sign in to comment.