PHP Classes

File: tests/Unit/Managers/LabelManagerTest.php

Recommend this page to a friend!
  Packages of Amirreza Ebrahimi   HeroQR Powerful PHP QR Code Library to generate PNG, SVG, PDF, Logos Ready to Use with Laravel   tests/Unit/Managers/LabelManagerTest.php   Download  
File: tests/Unit/Managers/LabelManagerTest.php
Role: Class source
Content type: text/plain
Description: Class source
Class: HeroQR Powerful PHP QR Code Library to generate PNG, SVG, PDF, Logos Ready to Use with Laravel
Generate QR code images in several formats
Author: By
Last change: feat: add SVG customization support for shapes, markers, and cursors (previously only PNG)
Date: 15 days ago
Size: 6,738 bytes
 

Contents

Class file image Download
<?php

namespace HeroQR\Tests\Unit\Managers;

use
HeroQR\Managers\{ColorManager,LabelManager};
use
PHPUnit\Framework\{Attributes\Test,TestCase};
use
HeroQR\Contracts\Managers\LabelManagerInterface;

/**
 * Class LabelManagerTest
 * Test cases for LabelManager class.
 */
class LabelManagerTest extends TestCase
{
    private
LabelManager $labelManager;

   
/**
     * Initializes the LabelManager instance
     */
   
protected function setUp(): void
   
{
       
parent::setUp();

       
$this->labelManager = new LabelManager(new ColorManager());

       
$this->assertInstanceOf(LabelManagerInterface::class, $this->labelManager, 'The LabelManager must implement LabelManagerInterface');
    }

   
/***
     * Test the set and get label functionality
     */
    #[Test]
   
public function isSetAndGetLabel(): void
   
{
       
$label = $this->labelManager;

       
$label->setLabel('test label');
       
$this->assertEquals('test label', $label->getLabel(), 'Label Text Does Not Match Expected Value');

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage('Label Text Cannot Be Empty');
       
$label->setLabel('');
    }

   
/***
     * Test the behavior when setting invalid label text length
     */
    #[Test]
   
public function isInvalidLabelText(): void
   
{
       
$label = $this->labelManager;
       
$longText = str_repeat('a', 201);

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage('Label Text Cannot Exceed 200 Characters');
       
$label->setLabel($longText);
    }

   
/***
     * Test the functionality of getting label font size and font name
     */
    #[Test]
   
public function isGetLabelFont(): void
   
{
       
$label = $this->labelManager;

       
$fontSize = $label->getLabelFont()->getSize();
       
$this->assertIsInt($fontSize, 'Font Size Should Be An Integer');
       
$this->assertEquals(20, $fontSize, 'Default Font Size Should Be 20');

       
$fontClass = explode('\\', $label->getLabelFont()::class);
       
$fontName = end($fontClass);
       
$this->assertEquals('OpenSans', $fontName, 'Default Font Name Should Be OpenSans');
    }

   
/***
     * Test setting and getting a custom label size
     */
    #[Test]
   
public function isSetLabelSize(): void
   
{
       
$label = $this->labelManager;

       
$label->setLabelSize(15);
       
$this->assertEquals(15, $label->getLabelFont()->getSize(), 'Font size Should Be Updated To 15');

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage('Font Size Must Be A Positive Integer');
       
$label->setLabelSize(-10);
    }

   
/***
     * Test setting label alignment
     */
    #[Test]
   
public function isSetLabelAlign(): void
   
{
       
$label = $this->labelManager;

       
$this->assertEquals('center', $label->getLabelAlign()->value, 'Default Alignment Should Be Center');

       
$validAlignments = ['right', 'left', 'CENTER'];
        foreach (
$validAlignments as $alignment) {
           
$label->setLabelAlign($alignment);
           
$this->assertEquals(strtolower($alignment), $label->getLabelAlign()->value, "Alignment Should Be Set To {$alignment}");
        }

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage('Invalid Label Alignment. Allowed Values Are "left", "center", or "right"');
       
$label->setLabelAlign('invalid');
    }

   
/***
     * Test setting and getting a custom label color
     */
    #[Test]
   
public function isSetAndGetLabelColor(): void
   
{
       
$label = $this->labelManager;

       
$defaultColor = $label->getLabelColor();
       
$this->assertEquals([0, 0, 0], [$defaultColor->getRed(), $defaultColor->getGreen(), $defaultColor->getBlue()], 'Default label color should be black');

       
$label->setLabelColor(['Red' => 255, 'Green' => 87 , 'Blue' => 51, 'Alpha' => 0.5]);
       
$customColor = $label->getLabelColor();

       
$this->assertEqualsWithDelta([255, 87, 51, 0.5], [
           
$customColor->getRed(),
           
$customColor->getGreen(),
           
$customColor->getBlue(),
           
$customColor->getOpacity()
        ],
0.01, 'Custom color values do not match expected');
    }

   
/***
     * Test the behavior when an invalid label color format is set
     */
    #[Test]
   
public function isInvalidLabelColor(): void
   
{
       
$label = $this->labelManager;

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage("Red must be integer (0-255). Input: 260");
       
$label->setLabelColor(['Red' => 260, 'Green' => 87 , 'Blue' => 51, 'Alpha' => 0.5]);
    }

   
/***
     * Test the behavior when an invalid label alpha color format is set
     */
    #[Test]
   
public function isInvalidLabelAlphaColor(): void
   
{
       
$label = $this->labelManager;

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage("Alpha must be between 0 and 1.");
       
$label->setLabelColor(['Red' => 255, 'Green' => 87 , 'Blue' => 51, 'Alpha' => 10]);
    }

   
/***
     * Test setting and getting label margin values
     */
    #[Test]
   
public function isSetAndGetLabelMargin(): void
   
{
       
$label = $this->labelManager;

       
$defaultMargin = $label->getLabelMargin();
       
$this->assertEquals([0, 10, 10, 10], [$defaultMargin->getTop(), $defaultMargin->getRight(), $defaultMargin->getBottom(), $defaultMargin->getLeft()], 'Default margins do not match expected values');

       
$label->setLabelMargin([5, 5, 5, 5]);
       
$customMargin = $label->getLabelMargin();
       
$this->assertEquals([5, 5, 5, 5], [$customMargin->getTop(), $customMargin->getRight(), $customMargin->getBottom(), $customMargin->getLeft()], 'Custom margins do not match expected values');
    }

   
/***
     * Test the behavior when invalid label margin values are set
     */
    #[Test]
   
public function isInvalidLabelMargin(): void
   
{
       
$label = $this->labelManager;

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage('Margin Array Must Contain Exactly 4 Values [top, right, bottom, left]');
       
$label->setLabelMargin([10, 0]);

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage('Margin values must be between -200 and 200');
       
$label->setLabelMargin([-302, 0, 210, 0]);

       
$this->expectException(\InvalidArgumentException::class);
       
$this->expectExceptionMessage('All Margin Values Must Be Numeric');
       
$label->setLabelMargin(['one', '0', '5.2', '5']);
    }
}