Explorar o código

:octocat: clean up builddir mess

smiley %!s(int64=2) %!d(string=hai) anos
pai
achega
ded4a12f9b
Modificáronse 3 ficheiros con 105 adicións e 14 borrados
  1. 89 0
      tests/BuildDirTrait.php
  2. 8 10
      tests/Output/QROutputTestAbstract.php
  3. 8 4
      tests/QRCodeTest.php

+ 89 - 0
tests/BuildDirTrait.php

@@ -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 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));
+	}
+
+}

+ 8 - 10
tests/Output/QROutputTestAbstract.php

@@ -10,6 +10,7 @@
 
 
 namespace chillerlan\QRCodeTest\Output;
 namespace chillerlan\QRCodeTest\Output;
 
 
+use chillerlan\QRCodeTest\BuildDirTrait;
 use chillerlan\QRCode\{QRCode, QROptions};
 use chillerlan\QRCode\{QRCode, QROptions};
 use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\QRCode\Data\QRMatrix;
 use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
 use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
@@ -17,28 +18,25 @@ use chillerlan\Settings\SettingsContainerInterface;
 use PHPUnit\Framework\Attributes\DataProvider;
 use PHPUnit\Framework\Attributes\DataProvider;
 use PHPUnit\Framework\TestCase;
 use PHPUnit\Framework\TestCase;
 use ReflectionClass;
 use ReflectionClass;
-use function file_exists, file_get_contents, mkdir, realpath;
 
 
 /**
 /**
  * Test abstract for the several (built-in) output modules,
  * Test abstract for the several (built-in) output modules,
  * should also be used to test custom output modules
  * should also be used to test custom output modules
  */
  */
 abstract class QROutputTestAbstract extends TestCase{
 abstract class QROutputTestAbstract extends TestCase{
+	use BuildDirTrait;
 
 
 	protected SettingsContainerInterface|QROptions $options;
 	protected SettingsContainerInterface|QROptions $options;
 	protected QROutputInterface                    $outputInterface;
 	protected QROutputInterface                    $outputInterface;
 	protected QRMatrix                             $matrix;
 	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
 	 * Attempts to create a directory under /.build and instances several required objects
 	 */
 	 */
 	protected function setUp():void{
 	protected function setUp():void{
-
-		if(!file_exists($this::buildDir)){
-			mkdir($this::buildDir, 0777, true);
-		}
+		$this->createBuildDir($this::buildDir);
 
 
 		$this->options         = new QROptions;
 		$this->options         = new QROptions;
 		$this->matrix          = (new QRCode($this->options))->addByteSegment('testdata')->getQRMatrix();
 		$this->matrix          = (new QRCode($this->options))->addByteSegment('testdata')->getQRMatrix();
@@ -83,11 +81,11 @@ abstract class QROutputTestAbstract extends TestCase{
 		$this->options->outputBase64 = false;
 		$this->options->outputBase64 = false;
 		$this->outputInterface       = $this->getOutputInterface($this->options, $this->matrix);
 		$this->outputInterface       = $this->getOutputInterface($this->options, $this->matrix);
 		// create the cache file
 		// 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));
 	}
 	}
 
 
 }
 }

+ 8 - 4
tests/QRCodeTest.php

@@ -14,22 +14,24 @@ use chillerlan\QRCode\{QROptions, QRCode};
 use chillerlan\QRCode\Output\QRCodeOutputException;
 use chillerlan\QRCode\Output\QRCodeOutputException;
 use PHPUnit\Framework\TestCase;
 use PHPUnit\Framework\TestCase;
 use stdClass;
 use stdClass;
-use function file_get_contents;
 
 
 /**
 /**
  * Tests basic functions of the QRCode class
  * Tests basic functions of the QRCode class
  */
  */
 final class QRCodeTest extends TestCase{
 final class QRCodeTest extends TestCase{
+	use BuildDirTrait;
 
 
 	private QRCode    $qrcode;
 	private QRCode    $qrcode;
 	private QROptions $options;
 	private QROptions $options;
 
 
-	private const buildDir = __DIR__.'/../.build/output-test/';
+	private const buildDir = 'output-test';
 
 
 	/**
 	/**
 	 * invoke test instances
 	 * invoke test instances
 	 */
 	 */
 	protected function setUp():void{
 	protected function setUp():void{
+		$this->createBuildDir($this::buildDir);
+
 		$this->qrcode  = new QRCode;
 		$this->qrcode  = new QRCode;
 		$this->options = new QROptions;
 		$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
 	 * Tests if a cache file is properly saved in the given path
 	 */
 	 */
 	public function testRenderToCacheFile():void{
 	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;
 		$this->options->outputBase64 = false;
 		// create the cache file
 		// create the cache file
 		$data = $this->qrcode->setOptions($this->options)->render('test');
 		$data = $this->qrcode->setOptions($this->options)->render('test');
 
 
-		$this::assertSame($data, file_get_contents($this->options->cachefile));
+		$this::assertSame($data, $this->getBuildFileContent($fileSubPath));
 	}
 	}
 
 
 }
 }