Skip to content

Commit

Permalink
Fix various stuff that failed in Contao 4.13
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Nov 26, 2024
1 parent 0d59759 commit 5105eb8
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 17 deletions.
7 changes: 7 additions & 0 deletions composer-dependency-analyser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;

return (new Configuration())
->ignoreUnknownClasses([Symfony\Component\HttpKernel\UriSigner::class])
;
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
parameters:
excludePaths:
- src/Legacy/LostPasswordModule.php
- src/Legacy/LostPasswordModule.php
ignoreErrors:
- '#Symfony\\Component\\HttpKernel\\UriSigner#'
5 changes: 3 additions & 2 deletions src/BulkyItem/BulkyItemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Contao\CoreBundle\Filesystem\ExtraMetadata;
use Contao\CoreBundle\Filesystem\VirtualFilesystemException;
use Contao\CoreBundle\Filesystem\VirtualFilesystemInterface;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpFoundation\UriSigner as HttpFoundationUriSigner;
use Symfony\Component\HttpKernel\UriSigner as HttpKernelUriSigner;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Uid\Uuid;
Expand All @@ -19,7 +20,7 @@ class BulkyItemStorage
public function __construct(
private readonly VirtualFilesystemInterface $filesystem,
private readonly RouterInterface $router,
private readonly UriSigner $uriSigner,
private readonly HttpFoundationUriSigner|HttpKernelUriSigner $uriSigner,
private readonly int $retentionPeriodInDays = 7,
) {
}
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/DownloadBulkyItemController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpFoundation\UriSigner as HttpFoundationUriSigner;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\UriSigner as HttpKernelUriSigner;
use Symfony\Component\Routing\Attribute\Route;
use Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemStorage;

#[Route('/notifications/download/{voucher}', 'nc_bulky_item_download', requirements: ['voucher' => BulkyItemStorage::VOUCHER_REGEX])]
class DownloadBulkyItemController
{
public function __construct(
private readonly UriSigner $uriSigner,
private readonly HttpFoundationUriSigner|HttpKernelUriSigner $uriSigner,
private readonly BulkyItemStorage $bulkyItemStorage,
) {
}
Expand Down
22 changes: 17 additions & 5 deletions tests/BulkyItem/BulkItemStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
use Contao\CoreBundle\Filesystem\FilesystemItem;
use Contao\CoreBundle\Filesystem\FilesystemItemIterator;
use Contao\CoreBundle\Filesystem\VirtualFilesystemInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpFoundation\UriSigner as HttpFoundationUriSigner;
use Symfony\Component\HttpKernel\UriSigner as HttpKernelUriSigner;
use Symfony\Component\Routing\RouterInterface;
use Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemStorage;
use Terminal42\NotificationCenterBundle\BulkyItem\FileItem;
Expand Down Expand Up @@ -74,7 +76,7 @@ function (ExtraMetadata $meta) {
)
;

$storage = new BulkyItemStorage($vfs, $this->createMock(RouterInterface::class), $this->createMock(UriSigner::class));
$storage = new BulkyItemStorage($vfs, $this->createMock(RouterInterface::class), $this->mockUriSigner());
$voucher = $storage->store($this->createFileItem());

$this->assertTrue(BulkyItemStorage::validateVoucherFormat($voucher));
Expand All @@ -90,7 +92,7 @@ public function testHas(): void
->willReturn(true)
;

$storage = new BulkyItemStorage($vfs, $this->createMock(RouterInterface::class), $this->createMock(UriSigner::class));
$storage = new BulkyItemStorage($vfs, $this->createMock(RouterInterface::class), $this->mockUriSigner());
$this->assertTrue($storage->has('a10aed4d-abe1-498f-adfc-b2e54fbbcbde'));
}

Expand Down Expand Up @@ -120,7 +122,7 @@ public function testRetrieve(): void
->willReturn($this->createStream())
;

$storage = new BulkyItemStorage($vfs, $this->createMock(RouterInterface::class), $this->createMock(UriSigner::class));
$storage = new BulkyItemStorage($vfs, $this->createMock(RouterInterface::class), $this->mockUriSigner());
$item = $storage->retrieve('a10aed4d-abe1-498f-adfc-b2e54fbbcbde');

$this->assertInstanceOf(FileItem::class, $item);
Expand Down Expand Up @@ -156,7 +158,7 @@ public function testPrune(): void
->with('20220101')
;

$storage = new BulkyItemStorage($vfs, $this->createMock(RouterInterface::class), $this->createMock(UriSigner::class));
$storage = new BulkyItemStorage($vfs, $this->createMock(RouterInterface::class), $this->mockUriSigner());
$storage->prune();
}

Expand All @@ -176,4 +178,14 @@ private function createStream()

return $stream;
}

/**
* For compatibility with Symfony 5, 6 and 7.
*/
private function mockUriSigner(): HttpFoundationUriSigner|HttpKernelUriSigner|MockObject
{
$class = class_exists(HttpFoundationUriSigner::class) ? HttpFoundationUriSigner::class : HttpKernelUriSigner::class;

return $this->createMock($class);
}
}
53 changes: 46 additions & 7 deletions tests/Gateway/MailerGatewayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@
use Contao\FrontendTemplate;
use Contao\TestCase\ContaoTestCase;
use League\Flysystem\InMemory\InMemoryFilesystemAdapter;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\HttpFoundation\UriSigner;
use Symfony\Component\HttpFoundation\UriSigner as HttpFoundationUriSigner;
use Symfony\Component\HttpKernel\UriSigner as HttpKernelUriSigner;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\ParameterizedHeader;
use Symfony\Component\Routing\RouterInterface;
use Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemStorage;
use Terminal42\NotificationCenterBundle\Config\LanguageConfig;
Expand Down Expand Up @@ -57,7 +60,20 @@ static function (Email $email) use ($parsedTemplateHtml, $expectedAttachmentsCon
$attachments = [];

foreach ($email->getAttachments() as $attachment) {
$attachments[$attachment->getBody()] = $attachment->getName();
// getName() method does not exist in Symfony 5 (Contao 4.13)
// see https://github.com/symfony/symfony/commit/ebd8697c7ee8daa7011da3222ebbb6dfb5e30171
if (method_exists($attachment, 'getName')) {
$attachments[$attachment->getBody()] = $attachment->getName();
continue;
}

$header = $attachment->getPreparedHeaders()->get('Content-Type');

if (!$header instanceof ParameterizedHeader || !($name = $header->getParameter('name'))) {
continue;
}

$attachments[$attachment->getBody()] = $name;
}

$expectedHtml = $parsedTemplateHtml;
Expand Down Expand Up @@ -90,7 +106,7 @@ static function (Email $email) use ($parsedTemplateHtml, $expectedAttachmentsCon
$mailer,
);
$container = new Container();
$container->set(AbstractGateway::SERVICE_NAME_BULKY_ITEM_STORAGE, new BulkyItemStorage($vfsCollection->get('bulky_item'), $this->createMock(RouterInterface::class), $this->createMock(UriSigner::class)));
$container->set(AbstractGateway::SERVICE_NAME_BULKY_ITEM_STORAGE, new BulkyItemStorage($vfsCollection->get('bulky_item'), $this->createMock(RouterInterface::class), $this->mockUriSigner()));
$container->set(AbstractGateway::SERVICE_NAME_SIMPLE_TOKEN_PARSER, new SimpleTokenParser(new ExpressionLanguage()));
$gateway->setContainer($container);

Expand Down Expand Up @@ -164,13 +180,36 @@ private function createFrameWorkWithTemplate(string $parsedTemplateHtml): Contao
->willReturn($parsedTemplateHtml)
;

return $this->mockContaoFramework(
$framework = $this->mockContaoFramework(
[
Controller::class => $controllerAdapter,
],
[
FrontendTemplate::class => $templateInstance,
],
);

// contao/test-case 4.13 does not support "$instances" on `mockContaoFramework`
$framework
->method('createInstance')
->willReturnCallback(
static function (string $key) use ($templateInstance): mixed {
if (FrontendTemplate::class === $key) {
return $templateInstance;
}

return null;
},
)
;

return $framework;
}

/**
* For compatibility with Symfony 5, 6 and 7.
*/
private function mockUriSigner(): HttpFoundationUriSigner|HttpKernelUriSigner|MockObject
{
$class = class_exists(HttpFoundationUriSigner::class) ? HttpFoundationUriSigner::class : HttpKernelUriSigner::class;

return $this->createMock($class);
}
}

0 comments on commit 5105eb8

Please sign in to comment.