| 1 |
<?php |
| 2 |
declare(strict_types = 1); |
| 3 |
|
| 4 |
namespace BaconQrCodeTest\Common; |
| 5 |
|
| 6 |
use BaconQrCode\Common\ErrorCorrectionLevel; |
| 7 |
use BaconQrCode\Common\Version; |
| 8 |
use PHPUnit\Framework\Attributes\DataProvider; |
| 9 |
use PHPUnit\Framework\TestCase; |
| 10 |
|
| 11 |
class VersionTest extends TestCase |
| 12 |
{ |
| 13 |
public static function versions() : array |
| 14 |
{ |
| 15 |
$array = []; |
| 16 |
|
| 17 |
for ($i = 1; $i <= 40; ++$i) { |
| 18 |
$array[] = [$i, 4 * $i + 17]; |
| 19 |
} |
| 20 |
|
| 21 |
return $array; |
| 22 |
} |
| 23 |
|
| 24 |
public static function decodeInformation() : array |
| 25 |
{ |
| 26 |
return [ |
| 27 |
[7, 0x07c94], |
| 28 |
[12, 0x0c762], |
| 29 |
[17, 0x1145d], |
| 30 |
[22, 0x168c9], |
| 31 |
[27, 0x1b08e], |
| 32 |
[32, 0x209d5], |
| 33 |
]; |
| 34 |
} |
| 35 |
|
| 36 |
#[DataProvider('versions')] |
| 37 |
public function testVersionForNumber(int $versionNumber, int $dimension) : void |
| 38 |
{ |
| 39 |
$version = Version::getVersionForNumber($versionNumber); |
| 40 |
|
| 41 |
$this->assertNotNull($version); |
| 42 |
$this->assertEquals($versionNumber, $version->getVersionNumber()); |
| 43 |
$this->assertNotNull($version->getAlignmentPatternCenters()); |
| 44 |
|
| 45 |
if ($versionNumber > 1) { |
| 46 |
$this->assertTrue(count($version->getAlignmentPatternCenters()) > 0); |
| 47 |
} |
| 48 |
|
| 49 |
$this->assertEquals($dimension, $version->getDimensionForVersion()); |
| 50 |
$this->assertNotNull($version->getEcBlocksForLevel(ErrorCorrectionLevel::H())); |
| 51 |
$this->assertNotNull($version->getEcBlocksForLevel(ErrorCorrectionLevel::L())); |
| 52 |
$this->assertNotNull($version->getEcBlocksForLevel(ErrorCorrectionLevel::M())); |
| 53 |
$this->assertNotNull($version->getEcBlocksForLevel(ErrorCorrectionLevel::Q())); |
| 54 |
$this->assertNotNull($version->buildFunctionPattern()); |
| 55 |
} |
| 56 |
|
| 57 |
#[DataProvider('versions')] |
| 58 |
public function testGetProvisionalVersionForDimension(int $versionNumber, int $dimension) : void |
| 59 |
{ |
| 60 |
$this->assertSame( |
| 61 |
$versionNumber, |
| 62 |
Version::getProvisionalVersionForDimension($dimension)->getVersionNumber() |
| 63 |
); |
| 64 |
} |
| 65 |
|
| 66 |
#[DataProvider('decodeInformation')] |
| 67 |
public function testDecodeVersionInformation(int $expectedVersion, int $mask) : void |
| 68 |
{ |
| 69 |
$version = Version::decodeVersionInformation($mask); |
| 70 |
$this->assertNotNull($version); |
| 71 |
$this->assertSame($expectedVersion, $version->getVersionNumber()); |
| 72 |
} |
| 73 |
} |