From c81abb9dd4cdc1a26dc8e76f423d8da63f6ca23c Mon Sep 17 00:00:00 2001 From: Edd Turtle Date: Tue, 28 Nov 2023 08:24:44 +0000 Subject: [PATCH 1/3] Add gh actions for phpstan + phpunit --- .github/workflows/phpstan.yml | 14 ++++++++++++++ .github/workflows/phpunit.yml | 12 ++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 .github/workflows/phpstan.yml create mode 100644 .github/workflows/phpunit.yml diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml new file mode 100644 index 0000000..71e81d9 --- /dev/null +++ b/.github/workflows/phpstan.yml @@ -0,0 +1,14 @@ +name: PHPStan (Static Analysis) + +on: [push] + +jobs: + build-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - uses: php-actions/composer@v6 # or alternative dependency management + - uses: php-actions/phpstan@v3 + with: + path: src/ diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml new file mode 100644 index 0000000..9742f07 --- /dev/null +++ b/.github/workflows/phpunit.yml @@ -0,0 +1,12 @@ +name: PHPUnit (Unit Tests) + +on: [push] + +jobs: + build-test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - uses: php-actions/composer@v6 # or alternative dependency management + - uses: php-actions/phpunit@v3 \ No newline at end of file From 98ea2b97d8d18d6f05fa7b85e7e7e93daaaf4b19 Mon Sep 17 00:00:00 2001 From: Edd Turtle Date: Wed, 13 Dec 2023 21:14:37 +0000 Subject: [PATCH 2/3] Change phpunit style to call direct --- .github/workflows/phpunit.yml | 7 ++++++- src/Pdf.php | 23 +++++++++++++++++++++++ tests/PdfTest.php | 19 ++++++++++++++++--- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/.github/workflows/phpunit.yml b/.github/workflows/phpunit.yml index 9742f07..40f6292 100644 --- a/.github/workflows/phpunit.yml +++ b/.github/workflows/phpunit.yml @@ -8,5 +8,10 @@ jobs: steps: - uses: actions/checkout@v3 + - uses: php-actions/composer@v6 # or alternative dependency management - - uses: php-actions/phpunit@v3 \ No newline at end of file + + - name: Install QPDF & Run PHPUnit + run: | + sudo apt install -y qpdf + vendor/bin/phpunit --color=always \ No newline at end of file diff --git a/src/Pdf.php b/src/Pdf.php index 087feb8..c178604 100644 --- a/src/Pdf.php +++ b/src/Pdf.php @@ -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. * @@ -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); @@ -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); @@ -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); diff --git a/tests/PdfTest.php b/tests/PdfTest.php index d000126..c0b17da 100644 --- a/tests/PdfTest.php +++ b/tests/PdfTest.php @@ -10,6 +10,8 @@ class PdfTest extends TestCase public function testGetVersion() { + echo $this->getCmdLocation(); + $pdf = new Pdf(); $version = $pdf->getVersion(); @@ -23,33 +25,44 @@ public function testGetVersion() public function testMerge() { $pdf = new Pdf(); + $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->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->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->addPage(__DIR__ . "/files/TestPdfTwoPage.pdf"); $pdf->addPage(__DIR__ . "/files/TestPdf.pdf"); + $count = $pdf->getPageCount(); $this->assertEmpty($pdf->getError()); $this->assertEquals(3, $count); From f2a0230be81edc02bd85c887c06da29c94c7f82f Mon Sep 17 00:00:00 2001 From: Edd Turtle Date: Wed, 13 Dec 2023 21:16:00 +0000 Subject: [PATCH 3/3] Fix stray func call --- tests/PdfTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/PdfTest.php b/tests/PdfTest.php index c0b17da..f4f2f3b 100644 --- a/tests/PdfTest.php +++ b/tests/PdfTest.php @@ -10,8 +10,6 @@ class PdfTest extends TestCase public function testGetVersion() { - echo $this->getCmdLocation(); - $pdf = new Pdf(); $version = $pdf->getVersion();