Compare commits

..

4 Commits

Author SHA1 Message Date
Philipp Lang 18650ba7ad Add merge 2023-11-29 23:01:01 +01:00
Philipp Lang 8654d13e81 Add full body assertion 2023-11-29 23:00:47 +01:00
Philipp Lang 5a5f1756e1 Set uuid string for temporary filename 2023-11-29 13:03:37 +01:00
Philipp Lang 6d88ccb8f1 Add full string assertion for Document 2023-11-29 12:05:56 +01:00
3 changed files with 70 additions and 18 deletions

View File

@ -10,7 +10,14 @@ use Log;
abstract class BaseCompiler implements Responsable
{
protected File $file;
public function __construct(protected ?File $file = null)
{
}
public static function fromFile(string $file): static
{
return new static(new File($file, false));
}
public function compile(Document $document): self
{
@ -41,6 +48,32 @@ abstract class BaseCompiler implements Responsable
return $this;
}
/**
* @param array<int, Documents> $documents
*/
public function merge(array $documents): self
{
$outfile = '/tmp/' . Str::uuid()->toString() . '.pdf';
$paths = array_map(fn ($document) => static::compile($document)->getPath(), $documents);
$command = collect([
'pdfjam --nup 1x1 --outfile ' . $outfile,
...array_map(fn ($path) => escapeshellarg($path), $paths),
])->implode(' ');
exec($command, $output, $returnVar);
foreach ($paths as $path) {
@unlink($path);
}
if (0 !== $returnVar) {
Log::error('Merging failed', ['output' => $output]);
throw (new CompilerException('Compilation failed.'))->setOutput($output);
}
return static::fromFile($outfile);
}
public function storeAs(string $directory, string $name, string $disk): string
{
$contents = $this->file->getContent();
@ -72,7 +105,7 @@ abstract class BaseCompiler implements Responsable
protected function prepareForCompilation(Document $document): void
{
$workDir = '/tmp/'.Str::random(32);
$workDir = '/tmp/' . Str::uuid()->toString();
mkdir($workDir, 0777, true);
$this->file = new File($workDir . '/' . $document->compiledFilename(), false);
}

View File

@ -8,8 +8,9 @@ use PHPUnit\Framework\Assert;
use ReflectionClass;
use ReflectionMethod;
use ReflectionProperty;
use Spatie\LaravelData\Data;
abstract class Document
abstract class Document extends Data
{
abstract public function basename(): string;
@ -35,6 +36,15 @@ abstract class Document
);
}
public function assertIsContent(string $content): void
{
Assert::assertEquals(
$content,
$this->renderBody(),
"Failed asserting that Content is {$content}"
);
}
/**
* @param array<int, string> $content
*/

View File

@ -30,6 +30,15 @@ trait FakesCompilation
Assert::assertNotEmpty($validDocuments, 'Failed that TeX Document "' . $documentClass . '" has been compiled with given check.');
}
/**
* @param class-string<Document> $documentClass
* @param callable(Document): bool $check
*/
public function assertCompiledContent(string $documentClass, string $content): void
{
$this->assertCompiled($documentClass, fn ($document) => $document->renderBody() === $content);
}
/**
* @param class-string<Document> $documentClass
*/