From ded4a12f9baa57aab284456c816e54dd9f74045f Mon Sep 17 00:00:00 2001 From: smiley Date: Sun, 7 Jan 2024 20:24:30 +0100 Subject: [PATCH] :octocat: clean up builddir mess --- tests/BuildDirTrait.php | 89 +++++++++++++++++++++++++++ tests/Output/QROutputTestAbstract.php | 18 +++--- tests/QRCodeTest.php | 12 ++-- 3 files changed, 105 insertions(+), 14 deletions(-) create mode 100644 tests/BuildDirTrait.php diff --git a/tests/BuildDirTrait.php b/tests/BuildDirTrait.php new file mode 100644 index 000000000..77e30e560 --- /dev/null +++ b/tests/BuildDirTrait.php @@ -0,0 +1,89 @@ + + * @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 const _buildDir = __DIR__.'/../.build/'; + + /** + * returns the full raw path to the build dir + */ + protected function getBuildPath(string $subPath):string{ + return self::_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)); + } + +} diff --git a/tests/Output/QROutputTestAbstract.php b/tests/Output/QROutputTestAbstract.php index 3c9f8e2e4..814336745 100644 --- a/tests/Output/QROutputTestAbstract.php +++ b/tests/Output/QROutputTestAbstract.php @@ -10,6 +10,7 @@ namespace chillerlan\QRCodeTest\Output; +use chillerlan\QRCodeTest\BuildDirTrait; use chillerlan\QRCode\{QRCode, QROptions}; use chillerlan\QRCode\Data\QRMatrix; use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface}; @@ -17,28 +18,25 @@ use chillerlan\Settings\SettingsContainerInterface; use PHPUnit\Framework\Attributes\DataProvider; 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; protected SettingsContainerInterface|QROptions $options; protected QROutputInterface $outputInterface; protected QRMatrix $matrix; - 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->matrix = (new QRCode($this->options))->addByteSegment('testdata')->getQRMatrix(); @@ -83,11 +81,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)); } } diff --git a/tests/QRCodeTest.php b/tests/QRCodeTest.php index 5a405b628..08c3b85a4 100755 --- a/tests/QRCodeTest.php +++ b/tests/QRCodeTest.php @@ -14,22 +14,24 @@ 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{ + use BuildDirTrait; private QRCode $qrcode; private QROptions $options; - private const 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; } @@ -74,12 +76,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)); } }