Skip to content

Commit ded4a12

Browse files
committed
:octocat: clean up builddir mess
1 parent 2aeb7b8 commit ded4a12

File tree

3 files changed

+105
-14
lines changed

3 files changed

+105
-14
lines changed

tests/BuildDirTrait.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
/**
3+
* BuildDirTrait.php
4+
*
5+
* @created 07.01.2024
6+
* @author smiley <[email protected]>
7+
* @copyright 2024 smiley
8+
* @license MIT
9+
*/
10+
11+
namespace chillerlan\QRCodeTest;
12+
13+
use RuntimeException;
14+
use function dirname, file_exists, file_get_contents, is_file, mkdir, realpath, sprintf, trim;
15+
16+
/**
17+
* Trait BuildDirTrait
18+
*/
19+
trait BuildDirTrait{
20+
21+
private const _buildDir = __DIR__.'/../.build/';
22+
23+
/**
24+
* returns the full raw path to the build dir
25+
*/
26+
protected function getBuildPath(string $subPath):string{
27+
return self::_buildDir.trim($subPath, '\\/');
28+
}
29+
30+
/**
31+
* attempts to create the build dir
32+
*
33+
* @throws \RuntimeException
34+
*/
35+
protected function createBuildDir(string $subPath):void{
36+
$dir = $this->getBuildPath($subPath);
37+
38+
// attempt to write
39+
if(!file_exists($dir)){
40+
$created = mkdir($dir, 0777, true);
41+
42+
if(!$created){
43+
throw new RuntimeException('could not create build dir');
44+
}
45+
}
46+
}
47+
48+
/**
49+
* returns the full (real) path to the given build path
50+
*
51+
* @throws \RuntimeException
52+
*/
53+
protected function getBuildDir(string $subPath = ''):string{
54+
$dir = realpath($this->getBuildPath($subPath));
55+
56+
if(empty($dir)){
57+
throw new RuntimeException('invalid build dir');
58+
}
59+
60+
return dirname($dir);
61+
}
62+
63+
/**
64+
* returns the full (real) path to the given build file
65+
*
66+
* @throws \RuntimeException
67+
*/
68+
protected function getBuildFilePath(string $fileSubPath):string{
69+
$file = realpath($this->getBuildPath($fileSubPath));
70+
71+
if(empty($file)){
72+
throw new RuntimeException('invalid build dir/file');
73+
}
74+
75+
if(!is_file($file)){
76+
throw new RuntimeException(sprintf('the given path "%s" found in "%s" is not a file', $fileSubPath, $file));
77+
}
78+
79+
return $file;
80+
}
81+
82+
/**
83+
* returns the contents of the given build file
84+
*/
85+
protected function getBuildFileContent(string $fileSubPath):string{
86+
return file_get_contents($this->getBuildFilePath($fileSubPath));
87+
}
88+
89+
}

tests/Output/QROutputTestAbstract.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,35 +10,33 @@
1010

1111
namespace chillerlan\QRCodeTest\Output;
1212

13+
use chillerlan\QRCodeTest\BuildDirTrait;
1314
use chillerlan\QRCode\{QRCode, QROptions};
1415
use chillerlan\QRCode\Data\QRMatrix;
1516
use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
1617
use chillerlan\Settings\SettingsContainerInterface;
1718
use PHPUnit\Framework\Attributes\DataProvider;
1819
use PHPUnit\Framework\TestCase;
1920
use ReflectionClass;
20-
use function file_exists, file_get_contents, mkdir, realpath;
2121

2222
/**
2323
* Test abstract for the several (built-in) output modules,
2424
* should also be used to test custom output modules
2525
*/
2626
abstract class QROutputTestAbstract extends TestCase{
27+
use BuildDirTrait;
2728

2829
protected SettingsContainerInterface|QROptions $options;
2930
protected QROutputInterface $outputInterface;
3031
protected QRMatrix $matrix;
3132

32-
protected const buildDir = __DIR__.'/../../.build/output-test/';
33+
protected const buildDir = 'output-test';
3334

3435
/**
3536
* Attempts to create a directory under /.build and instances several required objects
3637
*/
3738
protected function setUp():void{
38-
39-
if(!file_exists($this::buildDir)){
40-
mkdir($this::buildDir, 0777, true);
41-
}
39+
$this->createBuildDir($this::buildDir);
4240

4341
$this->options = new QROptions;
4442
$this->matrix = (new QRCode($this->options))->addByteSegment('testdata')->getQRMatrix();
@@ -83,11 +81,11 @@ public function testRenderToCacheFile():void{
8381
$this->options->outputBase64 = false;
8482
$this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
8583
// create the cache file
86-
$name = (new ReflectionClass($this->outputInterface))->getShortName();
87-
$file = realpath($this::buildDir).'test.output.'.$name;
88-
$data = $this->outputInterface->dump($file);
84+
$name = (new ReflectionClass($this->outputInterface))->getShortName();
85+
$fileSubPath = $this::buildDir.'/test.output.'.$name;
86+
$data = $this->outputInterface->dump($this->getBuildPath($fileSubPath));
8987

90-
$this::assertSame($data, file_get_contents($file));
88+
$this::assertSame($data, $this->getBuildFileContent($fileSubPath));
9189
}
9290

9391
}

tests/QRCodeTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,24 @@
1414
use chillerlan\QRCode\Output\QRCodeOutputException;
1515
use PHPUnit\Framework\TestCase;
1616
use stdClass;
17-
use function file_get_contents;
1817

1918
/**
2019
* Tests basic functions of the QRCode class
2120
*/
2221
final class QRCodeTest extends TestCase{
22+
use BuildDirTrait;
2323

2424
private QRCode $qrcode;
2525
private QROptions $options;
2626

27-
private const buildDir = __DIR__.'/../.build/output-test/';
27+
private const buildDir = 'output-test';
2828

2929
/**
3030
* invoke test instances
3131
*/
3232
protected function setUp():void{
33+
$this->createBuildDir($this::buildDir);
34+
3335
$this->qrcode = new QRCode;
3436
$this->options = new QROptions;
3537
}
@@ -74,12 +76,14 @@ public function testSaveException():void{
7476
* Tests if a cache file is properly saved in the given path
7577
*/
7678
public function testRenderToCacheFile():void{
77-
$this->options->cachefile = $this::buildDir.'test.cache.svg';
79+
$fileSubPath = $this::buildDir.'/test.cache.svg';
80+
81+
$this->options->cachefile = $this->getBuildPath($fileSubPath);
7882
$this->options->outputBase64 = false;
7983
// create the cache file
8084
$data = $this->qrcode->setOptions($this->options)->render('test');
8185

82-
$this::assertSame($data, file_get_contents($this->options->cachefile));
86+
$this::assertSame($data, $this->getBuildFileContent($fileSubPath));
8387
}
8488

8589
}

0 commit comments

Comments
 (0)