Skip to content

Commit 2221d83

Browse files
committed
Fix generating Pharmacode 2 with invalid input #118
1 parent 5cae7eb commit 2221d83

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Types/TypePharmacodeTwoCode.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@
99

1010
use Picqer\Barcode\Barcode;
1111
use Picqer\Barcode\BarcodeBar;
12+
use Picqer\Barcode\Exceptions\InvalidLengthException;
1213

1314
class TypePharmacodeTwoCode implements TypeInterface
1415
{
1516
public function getBarcodeData(string $code): Barcode
1617
{
1718
$code = intval($code);
1819

20+
if ($code < 1) {
21+
throw new InvalidLengthException('Pharmacode 2 needs a number of 1 or larger');
22+
}
23+
1924
$seq = '';
2025

2126
do {

tests/PharmacodeTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class PharmacodeTest extends TestCase
6+
{
7+
public function test_validation_triggerd_when_generating_zero_code()
8+
{
9+
$pharmacode = new Picqer\Barcode\Types\TypePharmacodeTwoCode();
10+
11+
$this->expectException(Picqer\Barcode\Exceptions\InvalidLengthException::class);
12+
13+
$pharmacode->getBarcodeData('0');
14+
}
15+
}

0 commit comments

Comments
 (0)