:octocat: clean up builddir mess

This commit is contained in:
smiley
2024-01-07 13:32:32 +01:00
parent e9cc23dc31
commit d641fced1f
3 changed files with 106 additions and 14 deletions
+89
View File
@@ -0,0 +1,89 @@
<?php
/**
* BuildDirTrait.php
*
* @created 07.01.2024
* @author smiley <smiley@chillerlan.net>
* @copyright 2024 smiley
* @license MIT
*/
namespace chillerlan\QRCodeTest;
use RuntimeException;
use function dirname, file_exists, file_get_contents, is_file, mkdir, realpath, sprintf, trim;
/**
* Trait BuildDirTrait
*/
trait BuildDirTrait{
private static string $buildDir = __DIR__.'/../.build/';
/**
* returns the full raw path to the build dir
*/
protected function getBuildPath(string $subPath):string{
return $this::$buildDir.trim($subPath, '\\/');
}
/**
* attempts to create the build dir
*
* @throws \RuntimeException
*/
protected function createBuildDir(string $subPath):void{
$dir = $this->getBuildPath($subPath);
// attempt to write
if(!file_exists($dir)){
$created = mkdir($dir, 0777, true);
if(!$created){
throw new RuntimeException('could not create build dir');
}
}
}
/**
* returns the full (real) path to the given build path
*
* @throws \RuntimeException
*/
protected function getBuildDir(string $subPath = ''):string{
$dir = realpath($this->getBuildPath($subPath));
if(empty($dir)){
throw new RuntimeException('invalid build dir');
}
return dirname($dir);
}
/**
* returns the full (real) path to the given build file
*
* @throws \RuntimeException
*/
protected function getBuildFilePath(string $fileSubPath):string{
$file = realpath($this->getBuildPath($fileSubPath));
if(empty($file)){
throw new RuntimeException('invalid build dir/file');
}
if(!is_file($file)){
throw new RuntimeException(sprintf('the given path "%s" found in "%s" is not a file', $fileSubPath, $file));
}
return $file;
}
/**
* returns the contents of the given build file
*/
protected function getBuildFileContent(string $fileSubPath):string{
return file_get_contents($this->getBuildFilePath($fileSubPath));
}
}
+8 -10
View File
@@ -10,18 +10,19 @@
namespace chillerlan\QRCodeTest\Output;
use chillerlan\QRCodeTest\BuildDirTrait;
use chillerlan\QRCode\{QRCode, QROptions};
use chillerlan\QRCode\Data\QRMatrix;
use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use function file_exists, file_get_contents, mkdir, realpath;
/**
* Test abstract for the several (built-in) output modules,
* should also be used to test custom output modules
*/
abstract class QROutputTestAbstract extends TestCase{
use BuildDirTrait;
/** @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface */
protected QROptions $options;
@@ -29,16 +30,13 @@ abstract class QROutputTestAbstract extends TestCase{
protected QRMatrix $matrix;
protected string $type;
protected const buildDir = __DIR__.'/../../.build/output-test/';
protected const buildDir = 'output-test';
/**
* Attempts to create a directory under /.build and instances several required objects
*/
protected function setUp():void{
if(!file_exists($this::buildDir)){
mkdir($this::buildDir, 0777, true);
}
$this->createBuildDir($this::buildDir);
$this->options = new QROptions;
$this->options->outputType = $this->type;
@@ -86,11 +84,11 @@ abstract class QROutputTestAbstract extends TestCase{
$this->options->outputBase64 = false;
$this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
// create the cache file
$name = (new ReflectionClass($this->outputInterface))->getShortName();
$file = realpath($this::buildDir).'test.output.'.$name;
$data = $this->outputInterface->dump($file);
$name = (new ReflectionClass($this->outputInterface))->getShortName();
$fileSubPath = $this::buildDir.'/test.output.'.$name;
$data = $this->outputInterface->dump($this->getBuildPath($fileSubPath));
$this::assertSame($data, file_get_contents($file));
$this::assertSame($data, $this->getBuildFileContent($fileSubPath));
}
}
+9 -4
View File
@@ -14,21 +14,24 @@ use chillerlan\QRCode\{QROptions, QRCode};
use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
use PHPUnit\Framework\TestCase;
use stdClass;
use function file_get_contents;
/**
* Tests basic functions of the QRCode class
*/
final class QRCodeTest extends TestCase{
use BuildDirTrait;
private QRCode $qrcode;
private QROptions $options;
private string $builddir = __DIR__.'/../.build/output_test';
private const buildDir = 'output-test';
/**
* invoke test instances
*/
protected function setUp():void{
$this->createBuildDir($this::buildDir);
$this->qrcode = new QRCode;
$this->options = new QROptions;
}
@@ -86,12 +89,14 @@ final class QRCodeTest extends TestCase{
* Tests if a cache file is properly saved in the given path
*/
public function testRenderToCacheFile():void{
$this->options->cachefile = $this->builddir.'/test.cache.svg';
$fileSubPath = $this::buildDir.'/test.cache.svg';
$this->options->cachefile = $this->getBuildPath($fileSubPath);
$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));
$this::assertSame($data, $this->getBuildFileContent($fileSubPath));
}
}