VersionTest.php 2.3 KB

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