Skip to content

Commit

Permalink
Fix build failures for the cake 5 upgrade (#326)
Browse files Browse the repository at this point in the history
* cake 5 upgrade - upgrade to PhpUnit 10, which is needed when working with CakePHP 5, and fixing the PHPUnit tests afterwards

* cake 5 upgrade - The request handler component has been removed from Cake, so no need to have special logic for it

* cake 5 upgrade - Fix style issues reported by phpcs

* cake 5 upgrade - Fix Coding Standard & Static Analysis Errors / Warnings

* 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

* cake 5 upgrade - readd psalm and phpstan to fix the build

* cake 5 upgrade - psalm 4 does not have support for intersecion types, so let's update to psalm 5

* 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

* cake 5 upgrade - phpunit 10 remoced the --verbose option

* cake 5 upgrade - no need for dev versions anymore, as cake 5 is declared stable

* cake 5 upgrade - update to mpdf 8.1.6, because it's the first minor release to support psr log 3

* cake 5 upgrade - implement pr feedback

* cake 5 upgrade - upgrade to cakephp 5.0.1 and remove the temporary workaround for a php 5.0.0 bug again

* cake 5 upgrade - adapt the PdfView for content type negotiation

* Fix CS error

* Fix CS error

---------

Co-authored-by: bgehrels <[email protected]>
Co-authored-by: ADmad <[email protected]>
  • Loading branch information
3 people authored Oct 3, 2023
1 parent e65d3af commit 47e8f98
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 68 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -74,7 +74,7 @@ jobs:
php-version: '8.1'
extensions: mbstring, intl
coverage: none
tools: cs2pr
tools: cs2pr, vimeo/psalm:5.15, phpstan:1.8

- name: Composer Install
run: composer install
Expand All @@ -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
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"license": "MIT",
"require": {
"php": ">=8.1",
"cakephp/cakephp": "^5.0"
"cakephp/cakephp": "^5.0.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"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"
},
Expand All @@ -33,7 +33,7 @@
"TestApp\\": "tests/test_app/src/"
}
},
"minimum-stability": "dev",
"minimum-stability": "stable",
"scripts": {
"check": [
"@test",
Expand Down
16 changes: 0 additions & 16 deletions config/bootstrap.php

This file was deleted.

38 changes: 12 additions & 26 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,27 +1,13 @@
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
>

<testsuites>
<testsuite name="CakePdf Plugin Tests">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="./tests/bootstrap.php" colors="true" processIsolation="false" stopOnError="false" stopOnFailure="false" stopOnIncomplete="false" stopOnSkipped="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="CakePdf Plugin Tests">
<directory>./tests/TestCase</directory>
</testsuite>
</testsuites>
<source>
<include>
<directory suffix=".php">./src/</directory>
</include>
</source>
</phpunit>
9 changes: 8 additions & 1 deletion src/Plugin.php → src/CakePdfPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
12 changes: 9 additions & 3 deletions src/Pdf/CakePdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}

Expand All @@ -357,11 +363,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;
Expand Down
16 changes: 12 additions & 4 deletions src/View/PdfView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand All @@ -104,7 +112,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);

Expand Down
23 changes: 14 additions & 9 deletions tests/TestCase/Pdf/Engine/DomPdfEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion tests/TestCase/Pdf/Engine/MpdfEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down

0 comments on commit 47e8f98

Please sign in to comment.