| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- <?php
- /**
- * Class QRMatrixTest
- *
- * @filesource QRMatrixTest.php
- * @created 17.11.2017
- * @package chillerlan\QRCodeTest\Data
- * @author Smiley <smiley@chillerlan.net>
- * @copyright 2017 Smiley
- * @license MIT
- */
- namespace chillerlan\QRCodeTest\Data;
- use chillerlan\QRCode\QRCode;
- use chillerlan\QRCode\Data\{QRCodeDataException, QRMatrix};
- use chillerlan\QRCodeTest\QRTestAbstract;
- use ReflectionClass;
- class QRMatrixTest extends QRTestAbstract{
- protected string $FQCN = QRMatrix::class;
- protected int $version = 7;
- protected QRMatrix $matrix;
- protected function setUp():void{
- parent::setUp();
- /** @noinspection PhpFieldAssignmentTypeMismatchInspection */
- $this->matrix = $this->reflection->newInstanceArgs([$this->version, QRCode::ECC_L]);
- }
- public function testInvalidVersionException():void{
- $this->expectException(QRCodeDataException::class);
- $this->expectExceptionMessage('invalid QR Code version');
- $this->reflection->newInstanceArgs([42, 0]);
- }
- public function testInvalidEccException():void{
- $this->expectException(QRCodeDataException::class);
- $this->expectExceptionMessage('invalid ecc level');
- $this->reflection->newInstanceArgs([1, 42]);
- }
- public function testInstance():void{
- $this::assertInstanceOf($this->FQCN, $this->matrix);
- }
- public function testSize():void{
- $this::assertCount($this->matrix->size(), $this->matrix->matrix());
- }
- public function testVersion():void{
- $this::assertSame($this->version, $this->matrix->version());
- }
- public function testECC():void{
- $this::assertSame(QRCode::ECC_L, $this->matrix->eccLevel());
- }
- public function testMaskPattern():void{
- $this::assertSame(-1, $this->matrix->maskPattern());
- }
- public function testGetSetCheck():void{
- $this->matrix->set(10, 10, true, QRMatrix::M_TEST);
- $this::assertSame(65280, $this->matrix->get(10, 10));
- $this::assertTrue($this->matrix->check(10, 10));
- $this->matrix->set(20, 20, false, QRMatrix::M_TEST);
- $this::assertSame(255, $this->matrix->get(20, 20));
- $this::assertFalse($this->matrix->check(20, 20));
- }
- public function testSetDarkModule():void{
- $this->matrix->setDarkModule();
- $this::assertSame(QRMatrix::M_DARKMODULE << 8, $this->matrix->get(8, $this->matrix->size() - 8));
- }
- public function testSetFinderPattern():void{
- $this->matrix->setFinderPattern();
- $this::assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, 0));
- $this::assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get(0, $this->matrix->size() - 1));
- $this::assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($this->matrix->size() - 1, 0));
- }
- public function testSetSeparators():void{
- $this->matrix->setSeparators();
- $this::assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(7, 0));
- $this::assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, 7));
- $this::assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get(0, $this->matrix->size() - 8));
- $this::assertSame(QRMatrix::M_SEPARATOR, $this->matrix->get($this->matrix->size() - 8, 0));
- }
- public function testSetAlignmentPattern():void{
- $this->matrix
- ->setFinderPattern()
- ->setAlignmentPattern()
- ;
- $alignmentPattern = (new ReflectionClass(QRMatrix::class))->getConstant('alignmentPattern')[$this->version];
- foreach($alignmentPattern as $py){
- foreach($alignmentPattern as $px){
- if($this->matrix->get($px, $py) === QRMatrix::M_FINDER << 8){
- $this::assertSame(QRMatrix::M_FINDER << 8, $this->matrix->get($px, $py), 'skipped finder pattern');
- continue;
- }
- $this::assertSame(QRMatrix::M_ALIGNMENT << 8, $this->matrix->get($px, $py));
- }
- }
- }
- public function testSetTimingPattern():void{
- $this->matrix
- ->setAlignmentPattern()
- ->setTimingPattern()
- ;
- $size = $this->matrix->size();
- for($i = 7; $i < $size - 7; $i++){
- if($i % 2 === 0){
- $p1 = $this->matrix->get(6, $i);
- if($p1 === QRMatrix::M_ALIGNMENT << 8){
- $this::assertSame(QRMatrix::M_ALIGNMENT << 8, $p1, 'skipped alignment pattern');
- continue;
- }
- $this::assertSame(QRMatrix::M_TIMING << 8, $p1);
- $this::assertSame(QRMatrix::M_TIMING << 8, $this->matrix->get($i, 6));
- }
- }
- }
- public function testSetVersionNumber():void{
- $this->matrix->setVersionNumber(true);
- $this::assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 9, 0));
- $this::assertSame(QRMatrix::M_VERSION, $this->matrix->get($this->matrix->size() - 11, 5));
- $this::assertSame(QRMatrix::M_VERSION, $this->matrix->get(0, $this->matrix->size() - 9));
- $this::assertSame(QRMatrix::M_VERSION, $this->matrix->get(5, $this->matrix->size() - 11));
- }
- public function testSetFormatInfo():void{
- $this->matrix->setFormatInfo(0, true);
- $this::assertSame(QRMatrix::M_FORMAT, $this->matrix->get(8, 0));
- $this::assertSame(QRMatrix::M_FORMAT, $this->matrix->get(0, 8));
- $this::assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 1, 8));
- $this::assertSame(QRMatrix::M_FORMAT, $this->matrix->get($this->matrix->size() - 8, 8));
- }
- public function testSetQuietZone():void{
- $size = $this->matrix->size();
- $q = 5;
- $this->matrix->set(0, 0, true, QRMatrix::M_TEST);
- $this->matrix->set($size - 1, $size - 1, true, QRMatrix::M_TEST);
- $this->matrix->setQuietZone($q);
- $this::assertCount($size + 2 * $q, $this->matrix->matrix());
- $this::assertCount($size + 2 * $q, $this->matrix->matrix()[$size - 1]);
- $size = $this->matrix->size();
- $this::assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get(0, 0));
- $this::assertSame(QRMatrix::M_QUIETZONE, $this->matrix->get($size - 1, $size - 1));
- $this::assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($q, $q));
- $this::assertSame(QRMatrix::M_TEST << 8, $this->matrix->get($size - 1 - $q, $size - 1 - $q));
- }
- public function testSetQuietZoneException():void{
- $this->expectException(QRCodeDataException::class);
- $this->expectExceptionMessage('use only after writing data');
- $this->matrix->setQuietZone();
- }
- }
|