From e021b4e6457f9b7dd0f97280e8b076531e9de17d Mon Sep 17 00:00:00 2001 From: bgehrels Date: Sat, 16 Sep 2023 23:09:42 +0200 Subject: [PATCH 01/16] cake 5 upgrade - upgrade to PhpUnit 10, which is needed when working with CakePHP 5, and fixing the PHPUnit tests afterwards --- composer.json | 2 +- phpunit.xml.dist | 39 +++++++------------ src/View/PdfView.php | 8 ++++ .../TestCase/Pdf/Engine/DomPdfEngineTest.php | 23 ++++++----- tests/TestCase/Pdf/Engine/MpdfEngineTest.php | 5 ++- 5 files changed, 40 insertions(+), 37 deletions(-) diff --git a/composer.json b/composer.json index 7d9c05c6..63817f8c 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,7 @@ "cakephp/cakephp": "^5.0" }, "require-dev": { - "phpunit/phpunit": "^9.5", + "phpunit/phpunit": "^10.3", "dompdf/dompdf": "^2.0", "mpdf/mpdf": "^8.0.4", "tecnickcom/tcpdf": "^6.3", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 24af0446..0305898f 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,27 +1,14 @@ - - - - - ./tests/TestCase - - - - - - ./src/ - - + + + + + + ./tests/TestCase + + + + + ./src/ + + diff --git a/src/View/PdfView.php b/src/View/PdfView.php index b4abd005..fcc1e3c1 100644 --- a/src/View/PdfView.php +++ b/src/View/PdfView.php @@ -130,6 +130,14 @@ public function render(?string $template = null, false|string|null $layout = nul return $this->Blocks->get('content'); } + /** + * @inheritDoc + */ + public function getConfig(?string $key = null, mixed $default = null): mixed + { + return parent::getConfig($key, $default); + } + /** * Get or build a filename for forced download * diff --git a/tests/TestCase/Pdf/Engine/DomPdfEngineTest.php b/tests/TestCase/Pdf/Engine/DomPdfEngineTest.php index 8aa5898f..15c7d81c 100644 --- a/tests/TestCase/Pdf/Engine/DomPdfEngineTest.php +++ b/tests/TestCase/Pdf/Engine/DomPdfEngineTest.php @@ -27,7 +27,7 @@ public function setUp(): void */ public function testReceiveOptions() { - $engineClass = $this->getMockClass(DomPdfEngine::class, ['_createInstance']); + $engineClass = $this->getMockBuilder(DomPdfEngine::class)->disableOriginalConstructor()->onlyMethods(['_createInstance'])->getMock()::class; $Pdf = new CakePdf([ 'engine' => [ @@ -65,8 +65,7 @@ public function testReceiveOptions() */ public function testSetOptions() { - $engineClass = $this->getMockClass(DomPdfEngine::class, ['_output']); - + $engineClass = $this->getMockBuilder(DomPdfEngine::class)->disableOriginalConstructor()->onlyMethods(['_output'])->getMock()::class; $Pdf = new CakePdf([ 'engine' => [ 'className' => '\\' . $engineClass, @@ -114,11 +113,14 @@ public function testOutput() */ public function testControlFlow() { - $engineClass = $this->getMockClass(DomPdfEngine::class, [ - '_createInstance', - '_render', - '_output', - ]); + $engineClass = $this->getMockBuilder(DomPdfEngine::class) + ->disableOriginalConstructor() + ->onlyMethods([ + '_createInstance', + '_render', + '_output', + ]) + ->getMock()::class; $Pdf = new CakePdf([ 'engine' => '\\' . $engineClass, @@ -149,7 +151,10 @@ public function testControlFlow() */ public function testDompdfControlFlow() { - $engineClass = $this->getMockClass(DomPdfEngine::class, ['_createInstance']); + $engineClass = $this->getMockBuilder(DomPdfEngine::class) + ->disableOriginalConstructor() + ->onlyMethods(['_createInstance']) + ->getMock()::class; $Pdf = new CakePdf([ 'engine' => '\\' . $engineClass, diff --git a/tests/TestCase/Pdf/Engine/MpdfEngineTest.php b/tests/TestCase/Pdf/Engine/MpdfEngineTest.php index 4e697a54..6f9e07d0 100644 --- a/tests/TestCase/Pdf/Engine/MpdfEngineTest.php +++ b/tests/TestCase/Pdf/Engine/MpdfEngineTest.php @@ -27,7 +27,10 @@ public function setUp(): void */ public function testSetOptions() { - $engineClass = $this->getMockClass(MpdfEngine::class, ['_createInstance']); + $engineClass = $this->getMockBuilder(MpdfEngine::class) + ->disableOriginalConstructor() + ->onlyMethods(['_createInstance']) + ->getMock()::class; $Pdf = new CakePdf([ 'engine' => [ From 753a8b46bbc18adeb3a84f54086c34aa85e116ad Mon Sep 17 00:00:00 2001 From: bgehrels Date: Mon, 18 Sep 2023 15:53:35 +0200 Subject: [PATCH 02/16] cake 5 upgrade - The request handler component has been removed from Cake, so no need to have special logic for it --- config/bootstrap.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/config/bootstrap.php b/config/bootstrap.php index b5f82d14..fdd1f7b6 100644 --- a/config/bootstrap.php +++ b/config/bootstrap.php @@ -1,16 +1,4 @@ on( - 'Controller.initialize', - function (Event $event) { - $controller = $event->getSubject(); - if ($controller->components()->has('RequestHandler')) { - $controller->RequestHandler->setConfig('viewClassMap.pdf', 'CakePdf.Pdf'); - } - } - ); From 5989f4cdd9652d2b923158e8f101de8746b46f33 Mon Sep 17 00:00:00 2001 From: bgehrels Date: Mon, 18 Sep 2023 15:53:59 +0200 Subject: [PATCH 03/16] cake 5 upgrade - Fix style issues reported by phpcs --- src/View/PdfView.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/View/PdfView.php b/src/View/PdfView.php index fcc1e3c1..59244131 100644 --- a/src/View/PdfView.php +++ b/src/View/PdfView.php @@ -46,9 +46,9 @@ class PdfView extends View /** * Constructor * - * @param \Cake\Http\ServerRequest $request Request instance. - * @param \Cake\Http\Response $response Response instance. - * @param \Cake\Event\EventManager $eventManager Event manager instance. + * @param ?\Cake\Http\ServerRequest $request Request instance. + * @param ?\Cake\Http\Response $response Response instance. + * @param ?\Cake\Event\EventManager $eventManager Event manager instance. * @param array $viewOptions View options. See View::$_passedVars for list of * options which get set as class properties. * @throws \Cake\Core\Exception\CakeException @@ -104,7 +104,7 @@ public function renderer(?array $config = null): ?CakePdf * @param string|false|null $layout The layout being rendered. * @return string The rendered view. */ - public function render(?string $template = null, false|string|null $layout = null): string + public function render(?string $template = null, string|false|null $layout = null): string { $content = parent::render($template, $layout); From d861f9348e0c0289c269b05859ed382ed43d9dd3 Mon Sep 17 00:00:00 2001 From: bgehrels Date: Sun, 17 Sep 2023 02:47:03 +0200 Subject: [PATCH 04/16] cake 5 upgrade - Fix Coding Standard & Static Analysis Errors / Warnings --- src/Pdf/CakePdf.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Pdf/CakePdf.php b/src/Pdf/CakePdf.php index 4523e306..45d5c096 100644 --- a/src/Pdf/CakePdf.php +++ b/src/Pdf/CakePdf.php @@ -357,11 +357,11 @@ public function write(string $destination, bool $create = true, ?string $html = /** * Load PdfEngine * - * @param array|string $name Classname of pdf engine without `Engine` suffix. For example `CakePdf.DomPdf` + * @param array|string|null $name Classname of pdf engine without `Engine` suffix. For example `CakePdf.DomPdf` * @throws \Cake\Core\Exception\CakeException * @return \CakePdf\Pdf\Engine\AbstractPdfEngine|null */ - public function engine(string|array|null $name = null): ?AbstractPdfEngine + public function engine(array|string|null $name = null): ?AbstractPdfEngine { if ($name === null) { return $this->_engineClass; From ec7fb17baf7a7b37458166dac1b2e6b6ca0ce4dd Mon Sep 17 00:00:00 2001 From: bgehrels Date: Sun, 17 Sep 2023 03:05:07 +0200 Subject: [PATCH 05/16] cake 5 upgrade - Static code analysis A warns that $fileInfo->getPathInfo() might return null on errors leading to a possible NPE. Static code analysis B does not know that $fileInfo->getPathInfo() might return null and therefore complains that the comparison is always true. Fix one of them, ignore the other --- src/Pdf/CakePdf.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Pdf/CakePdf.php b/src/Pdf/CakePdf.php index 45d5c096..7a97c2c9 100644 --- a/src/Pdf/CakePdf.php +++ b/src/Pdf/CakePdf.php @@ -332,6 +332,7 @@ public function html(?string $html = null): mixed /** * Writes output to file * + * @throws \Cake\Core\Exception\CakeException * @param string $destination Absolute file path to write to * @param bool $create Create file if it does not exist (if true) * @param string|null $html Html to write @@ -347,7 +348,12 @@ public function write(string $destination, bool $create = true, ?string $html = return (bool)file_put_contents($destination, $output); } - if (!$fileInfo->getPathInfo()->getRealPath()) { + $splFileInfo = $fileInfo->getPathInfo(); + /** @phpstan-ignore-next-line */ + if ($splFileInfo === null) { + throw new CakeException('Failed to retrieve path information'); + } + if (!$splFileInfo->getRealPath()) { mkdir($fileInfo->getPath(), 0777, true); } From 439ec18fffb9c05b4e3eb8264abff38d8c9baae0 Mon Sep 17 00:00:00 2001 From: bgehrels Date: Mon, 18 Sep 2023 15:40:29 +0200 Subject: [PATCH 06/16] cake 5 upgrade - readd psalm and phpstan to fix the build --- .github/workflows/ci.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8b2fe683..77fabc13 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -74,7 +74,7 @@ jobs: php-version: '8.1' extensions: mbstring, intl coverage: none - tools: cs2pr + tools: cs2pr, vimeo/psalm:4.26, phpstan:1.8 - name: Composer Install run: composer install @@ -84,8 +84,8 @@ jobs: - name: Run psalm if: success() || failure() - run: vendor/bin/psalm --output-format=github + run: psalm --output-format=github - name: Run phpstan if: success() || failure() - run: vendor/bin/phpstan analyse + run: phpstan analyse From 99eb126f766cbd1ba957760f21bfa62a298c31ea Mon Sep 17 00:00:00 2001 From: bgehrels Date: Mon, 18 Sep 2023 15:38:12 +0200 Subject: [PATCH 07/16] cake 5 upgrade - psalm 4 does not have support for intersecion types, so let's update to psalm 5 --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 77fabc13..b6382ae6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -74,7 +74,7 @@ jobs: php-version: '8.1' extensions: mbstring, intl coverage: none - tools: cs2pr, vimeo/psalm:4.26, phpstan:1.8 + tools: cs2pr, vimeo/psalm:5.0, phpstan:1.8 - name: Composer Install run: composer install From 945a72fb4e18c205397a88bebfbcd9498f01183e Mon Sep 17 00:00:00 2001 From: bgehrels Date: Mon, 18 Sep 2023 15:17:17 +0200 Subject: [PATCH 08/16] cake 5 upgrade - psalm 5.0 throws a "Could not get class storage for cake\cache\cacheengineinterface" error, so let's upgrade to a more current version --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b6382ae6..7d621818 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -74,7 +74,7 @@ jobs: php-version: '8.1' extensions: mbstring, intl coverage: none - tools: cs2pr, vimeo/psalm:5.0, phpstan:1.8 + tools: cs2pr, vimeo/psalm:5.15, phpstan:1.8 - name: Composer Install run: composer install From 57eb38e6cdf29ef00e8084884d64e9331fea013f Mon Sep 17 00:00:00 2001 From: bgehrels Date: Mon, 18 Sep 2023 15:18:22 +0200 Subject: [PATCH 09/16] cake 5 upgrade - phpunit 10 remoced the --verbose option --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 7d621818..7c90b67d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -52,7 +52,7 @@ jobs: - name: Run PHPUnit run: | if [[ ${{ matrix.php-version }} == '8.1' ]]; then - vendor/bin/phpunit --verbose --coverage-clover=coverage.xml + vendor/bin/phpunit --coverage-clover=coverage.xml else vendor/bin/phpunit fi From 5af9bfed2e6707d92ed6be7a21f65b7f871ea6ff Mon Sep 17 00:00:00 2001 From: bgehrels Date: Mon, 18 Sep 2023 15:19:02 +0200 Subject: [PATCH 10/16] cake 5 upgrade - no need for dev versions anymore, as cake 5 is declared stable --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 63817f8c..b153f5dc 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,7 @@ "TestApp\\": "tests/test_app/src/" } }, - "minimum-stability": "dev", + "minimum-stability": "stable", "scripts": { "check": [ "@test", From ac96c8a9353dc6242e6c7252ec15d8f72b25172c Mon Sep 17 00:00:00 2001 From: bgehrels Date: Mon, 18 Sep 2023 15:30:37 +0200 Subject: [PATCH 11/16] cake 5 upgrade - update to mpdf 8.1.6, because it's the first minor release to support psr log 3 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index b153f5dc..e94bbaac 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "require-dev": { "phpunit/phpunit": "^10.3", "dompdf/dompdf": "^2.0", - "mpdf/mpdf": "^8.0.4", + "mpdf/mpdf": "^8.1.6", "tecnickcom/tcpdf": "^6.3", "cakephp/cakephp-codesniffer": "^5.0" }, From fe876b6db128b5d834db6183d257042716e923f7 Mon Sep 17 00:00:00 2001 From: bgehrels Date: Mon, 18 Sep 2023 20:35:18 +0200 Subject: [PATCH 12/16] cake 5 upgrade - implement pr feedback --- config/bootstrap.php | 4 ---- phpunit.xml.dist | 1 - src/{Plugin.php => CakePdfPlugin.php} | 9 ++++++++- src/View/PdfView.php | 6 +++--- 4 files changed, 11 insertions(+), 9 deletions(-) delete mode 100644 config/bootstrap.php rename src/{Plugin.php => CakePdfPlugin.php} (73%) diff --git a/config/bootstrap.php b/config/bootstrap.php deleted file mode 100644 index fdd1f7b6..00000000 --- a/config/bootstrap.php +++ /dev/null @@ -1,4 +0,0 @@ - - ./tests/TestCase diff --git a/src/Plugin.php b/src/CakePdfPlugin.php similarity index 73% rename from src/Plugin.php rename to src/CakePdfPlugin.php index be71a1fe..6bcb90ee 100644 --- a/src/Plugin.php +++ b/src/CakePdfPlugin.php @@ -5,8 +5,15 @@ use Cake\Core\BasePlugin; -class Plugin extends BasePlugin +class CakePdfPlugin extends BasePlugin { + /** + * Do bootstrap or not + * + * @var bool + */ + protected bool $bootstrapEnabled = false; + /** * Load routes or not * diff --git a/src/View/PdfView.php b/src/View/PdfView.php index 59244131..3260c291 100644 --- a/src/View/PdfView.php +++ b/src/View/PdfView.php @@ -46,9 +46,9 @@ class PdfView extends View /** * Constructor * - * @param ?\Cake\Http\ServerRequest $request Request instance. - * @param ?\Cake\Http\Response $response Response instance. - * @param ?\Cake\Event\EventManager $eventManager Event manager instance. + * @param \Cake\Http\ServerRequest|null $request Request instance. + * @param \Cake\Http\Response|null $response Response instance. + * @param \Cake\Event\EventManager|null $eventManager Event manager instance. * @param array $viewOptions View options. See View::$_passedVars for list of * options which get set as class properties. * @throws \Cake\Core\Exception\CakeException From 21c39383e877e945e607adeb4d5d47b443427b9f Mon Sep 17 00:00:00 2001 From: bgehrels Date: Sun, 1 Oct 2023 20:23:57 +0200 Subject: [PATCH 13/16] cake 5 upgrade - upgrade to cakephp 5.0.1 and remove the temporary workaround for a php 5.0.0 bug again --- composer.json | 2 +- src/View/PdfView.php | 8 -------- 2 files changed, 1 insertion(+), 9 deletions(-) diff --git a/composer.json b/composer.json index e94bbaac..53d70c8c 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "license": "MIT", "require": { "php": ">=8.1", - "cakephp/cakephp": "^5.0" + "cakephp/cakephp": "^5.0.1" }, "require-dev": { "phpunit/phpunit": "^10.3", diff --git a/src/View/PdfView.php b/src/View/PdfView.php index 3260c291..42db70ae 100644 --- a/src/View/PdfView.php +++ b/src/View/PdfView.php @@ -130,14 +130,6 @@ public function render(?string $template = null, string|false|null $layout = nul return $this->Blocks->get('content'); } - /** - * @inheritDoc - */ - public function getConfig(?string $key = null, mixed $default = null): mixed - { - return parent::getConfig($key, $default); - } - /** * Get or build a filename for forced download * From cf112d2bc57045de3844f1b8afd6746d5071db78 Mon Sep 17 00:00:00 2001 From: bgehrels Date: Sun, 1 Oct 2023 20:29:44 +0200 Subject: [PATCH 14/16] cake 5 upgrade - adapt the PdfView for content type negotiation --- src/View/PdfView.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/View/PdfView.php b/src/View/PdfView.php index 42db70ae..f30852e2 100644 --- a/src/View/PdfView.php +++ b/src/View/PdfView.php @@ -82,6 +82,14 @@ public function __construct( $this->renderer($pdfConfig); } + /** + * {@inheritDoc} + */ + public static function contentType(): string { + return 'application/pdf'; + } + + /** * Return CakePdf instance, optionally set engine to be used * From 00e40ef376187189909b2518d17e97536fc208b6 Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 3 Oct 2023 09:25:48 +0530 Subject: [PATCH 15/16] Fix CS error --- src/View/PdfView.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/View/PdfView.php b/src/View/PdfView.php index f30852e2..5f8c12cb 100644 --- a/src/View/PdfView.php +++ b/src/View/PdfView.php @@ -83,13 +83,12 @@ public function __construct( } /** - * {@inheritDoc} + * @inheritDoc */ public static function contentType(): string { return 'application/pdf'; } - /** * Return CakePdf instance, optionally set engine to be used * From 6f83dce51c05b48101710336f387e4f963ab9924 Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 3 Oct 2023 10:00:22 +0530 Subject: [PATCH 16/16] Fix CS error --- src/View/PdfView.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/View/PdfView.php b/src/View/PdfView.php index 5f8c12cb..554a39c6 100644 --- a/src/View/PdfView.php +++ b/src/View/PdfView.php @@ -85,7 +85,8 @@ public function __construct( /** * @inheritDoc */ - public static function contentType(): string { + public static function contentType(): string + { return 'application/pdf'; }