Skip to content

Commit d641fce

Browse files
committed
:octocat: clean up builddir mess
1 parent e9cc23d commit d641fce

File tree

3 files changed

+106
-14
lines changed

3 files changed

+106
-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 static string $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 $this::$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 PHPUnit\Framework\TestCase;
1718
use ReflectionClass;
18-
use function file_exists, file_get_contents, mkdir, realpath;
1919

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

2627
/** @var \chillerlan\QRCode\QROptions|\chillerlan\Settings\SettingsContainerInterface */
2728
protected QROptions $options;
2829
protected QROutputInterface $outputInterface;
2930
protected QRMatrix $matrix;
3031

3132
protected string $type;
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->options->outputType = $this->type;
@@ -86,11 +84,11 @@ public function testRenderToCacheFile():void{
8684
$this->options->outputBase64 = false;
8785
$this->outputInterface = $this->getOutputInterface($this->options, $this->matrix);
8886
// create the cache file
89-
$name = (new ReflectionClass($this->outputInterface))->getShortName();
90-
$file = realpath($this::buildDir).'test.output.'.$name;
91-
$data = $this->outputInterface->dump($file);
87+
$name = (new ReflectionClass($this->outputInterface))->getShortName();
88+
$fileSubPath = $this::buildDir.'/test.output.'.$name;
89+
$data = $this->outputInterface->dump($this->getBuildPath($fileSubPath));
9290

93-
$this::assertSame($data, file_get_contents($file));
91+
$this::assertSame($data, $this->getBuildFileContent($fileSubPath));
9492
}
9593

9694
}

tests/QRCodeTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,24 @@
1414
use chillerlan\QRCode\Output\{QRCodeOutputException, QROutputInterface};
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;
26-
private string $builddir = __DIR__.'/../.build/output_test';
26+
27+
private const buildDir = 'output-test';
2728

2829
/**
2930
* invoke test instances
3031
*/
3132
protected function setUp():void{
33+
$this->createBuildDir($this::buildDir);
34+
3235
$this->qrcode = new QRCode;
3336
$this->options = new QROptions;
3437
}
@@ -86,12 +89,14 @@ public function testSaveException():void{
8689
* Tests if a cache file is properly saved in the given path
8790
*/
8891
public function testRenderToCacheFile():void{
89-
$this->options->cachefile = $this->builddir.'/test.cache.svg';
92+
$fileSubPath = $this::buildDir.'/test.cache.svg';
93+
94+
$this->options->cachefile = $this->getBuildPath($fileSubPath);
9095
$this->options->outputBase64 = false;
9196
// create the cache file
9297
$data = $this->qrcode->setOptions($this->options)->render('test');
9398

94-
$this::assertSame($data, file_get_contents($this->options->cachefile));
99+
$this::assertSame($data, $this->getBuildFileContent($fileSubPath));
95100
}
96101

97102
}

0 commit comments

Comments
 (0)