Files
php-qrcode/tests/QRCodeTest.php
T
smiley 0578456800 🚿
2024-01-06 17:33:32 +01:00

86 lines
2.3 KiB
PHP
Executable File

<?php
/**
* Class QRCodeTest
*
* @created 17.11.2017
* @author Smiley <smiley@chillerlan.net>
* @copyright 2017 Smiley
* @license MIT
*/
namespace chillerlan\QRCodeTest;
use chillerlan\QRCode\{QROptions, QRCode};
use chillerlan\QRCode\Output\QRCodeOutputException;
use PHPUnit\Framework\TestCase;
use stdClass;
use function file_get_contents;
/**
* Tests basic functions of the QRCode class
*/
final class QRCodeTest extends TestCase{
private QRCode $qrcode;
private QROptions $options;
private const buildDir = __DIR__.'/../.build/output-test/';
/**
* invoke test instances
*/
protected function setUp():void{
$this->qrcode = new QRCode;
$this->options = new QROptions;
}
/**
* tests if an exception is thrown if the given output class does not exist
*/
public function testInitCustomOutputInterfaceNotExistsException():void{
$this->expectException(QRCodeOutputException::class);
$this->expectExceptionMessage('invalid output class');
$this->options->outputInterface = 'foo';
$this->qrcode->setOptions($this->options)->render('test');
}
/**
* tests if an exception is thrown if the given output class does not implement QROutputInterface
*/
public function testInitCustomOutputInterfaceNotImplementsException():void{
$this->expectException(QRCodeOutputException::class);
$this->expectExceptionMessage('output class does not implement QROutputInterface');
$this->options->outputInterface = stdClass::class;
$this->qrcode->setOptions($this->options)->render('test');
}
/**
* Tests if an exception is thrown when trying to write a cache file to an invalid destination
*/
public function testSaveException():void{
$this->expectException(QRCodeOutputException::class);
$this->expectExceptionMessage('Cannot write data to cache file: /foo/bar.test');
$this->options->cachefile = '/foo/bar.test';
$this->qrcode->setOptions($this->options)->render('test');
}
/**
* Tests if a cache file is properly saved in the given path
*/
public function testRenderToCacheFile():void{
$this->options->cachefile = $this::buildDir.'test.cache.svg';
$this->options->outputBase64 = false;
// create the cache file
$data = $this->qrcode->setOptions($this->options)->render('test');
$this::assertSame($data, file_get_contents($this->options->cachefile));
}
}