Skip to content

Commit

Permalink
Add binary for gh actions test running
Browse files Browse the repository at this point in the history
  • Loading branch information
eddturtle committed Dec 13, 2023
1 parent 67b2b9f commit 8cdcbbc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
23 changes: 23 additions & 0 deletions src/Pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,14 @@ public function getPages()
return $this->pages;
}

private function checkFolderIfExists($target)
{
$folder = dirname($target);
if (!empty($folder) && $folder !== '.' && !file_exists($folder)) {
mkdir($folder);
}
}

/**
* Merge all added pages in the object into a single output file.
*
Expand All @@ -124,6 +132,11 @@ public function getPages()
*/
public function merge($target)
{
if (empty($target)) {
return false;
}
$this->checkFolderIfExists($target);

$cmd = $this->getCommand();
$cmd->addArg('--empty', null, false);
$cmd->addArg('--pages', $this->pages);
Expand All @@ -148,6 +161,11 @@ public function split($target, $pagesPerGroup=1)
throw new \Exception("Error! Currently unable to split when more than one PDF file is specified.");
}

if (empty($target)) {
return false;
}
$this->checkFolderIfExists($target);

$cmd = $this->getCommand();
$cmd->addArg('--split-pages=', $pagesPerGroup, false);
$cmd->addArg('', $this->pages, false);
Expand Down Expand Up @@ -180,6 +198,11 @@ public function rotate($target, $direction='+90', $pageString=null)
throw new \Exception("Error! Currently unable to rotate when more than one PDF file is specified.");
}

if (empty($target)) {
return false;
}
$this->checkFolderIfExists($target);

$cmd = $this->getCommand();
$cmd->addArg('--rotate=', $rotationCmd, false);
$cmd->addArg('', $this->pages, false);
Expand Down
39 changes: 30 additions & 9 deletions tests/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,20 @@

class PdfTest extends TestCase
{
private function getCmdLocation()
{
if (getenv('GITHUB_ACTIONS') || PHP_OS === 'Linux') {
// Use location binary to run against
return __DIR__ . '/bin/qpdf';
} else {
// Default: we expect it installed (and in PATH)
return 'qpdf';
}
}

public function testGetVersion()
{
$pdf = new Pdf();
$pdf = new Pdf(null, ['command' => $this->getCmdLocation()]);
$version = $pdf->getVersion();

// Test it works
Expand All @@ -22,42 +32,53 @@ public function testGetVersion()

public function testMerge()
{
$pdf = new Pdf();
$pdf = new Pdf(null, ['command' => $this->getCmdLocation()]);

$pdf->addPage(__DIR__ . "/files/TestPdf.pdf");
$pdf->addPage(__DIR__ . "/files/TestPdf2.pdf");
$pdf->merge(__DIR__ . "/output/test-merge.pdf");

$result = $pdf->merge(__DIR__ . "/output/test-merge.pdf");
$this->assertTrue($result);
$this->assertEmpty($pdf->getError());
}

public function testSplit()
{
$pdf = new Pdf();
$pdf = new Pdf(null, ['command' => $this->getCmdLocation()]);

$pdf->addPage(__DIR__ . "/files/TestPdfTwoPage.pdf");
$pdf->split(__DIR__."/output/test-split.pdf");

$result = $pdf->split(__DIR__."/output/test-split.pdf");
$this->assertTrue($result);
$this->assertEmpty($pdf->getError());
}

public function testRotate()
{
$pdf = new Pdf();
$pdf = new Pdf(null, ['command' => $this->getCmdLocation()]);

$pdf->addPage(__DIR__ . "/files/TestPdfTwoPage.pdf");
$pdf->rotate(__DIR__ . "/output/rotated.pdf", "+90", "1");

$result = $pdf->rotate(__DIR__ . "/output/rotated.pdf", "+90", "1");
$this->assertTrue($result);
$this->assertEmpty($pdf->getError());
}

public function testGetPageCount()
{
$pdf = new Pdf();
$pdf = new Pdf(null, ['command' => $this->getCmdLocation()]);

$pdf->addPage(__DIR__ . "/files/TestPdfTwoPage.pdf");
$pdf->addPage(__DIR__ . "/files/TestPdf.pdf");

$count = $pdf->getPageCount();
$this->assertEmpty($pdf->getError());
$this->assertEquals(3, $count);
}

public function testEnsureErrorWithMultiFileRotate()
{
$pdf = new Pdf();
$pdf = new Pdf(null, ['command' => $this->getCmdLocation()]);
$pdf->addPage(__DIR__ . "/files/TestPdfTwoPage.pdf");
$pdf->addPage(__DIR__ . "/files/TestPdf.pdf");
$this->expectExceptionMessage('Error! Currently unable to rotate when more than one PDF file is specified.');
Expand Down
Binary file added tests/bin/qpdf
Binary file not shown.

0 comments on commit 8cdcbbc

Please sign in to comment.