Skip to content

Commit

Permalink
Merge branch 'main' into fix/attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar authored Nov 26, 2024
2 parents 80b942d + dda5fb1 commit a3daaab
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 21 deletions.
12 changes: 10 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ jobs:
uses: 'terminal42/contao-build-tools/.github/workflows/build-tools.yml@main'

tests:
name: Unit tests
name: Unit tests (PHP ${{ matrix.php }} / Contao ${{ matrix.contao }})
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php: [ '8.1', '8.2', '8.3' ]
php: [ '8.1', '8.2', '8.3', '8.4' ]
contao: [ '4.13', '5.3', '5.4' ]
steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand All @@ -27,6 +28,13 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Require Contao version for tests
run: composer require contao/core-bundle:${{ matrix.contao }}.* --dev --no-update

# Remove this once https://github.com/contao/contao/pull/7751 is merged and Contao 4.13.51 is released
- name: Require TestCase version for tests
run: composer require contao/test-case:${{ matrix.contao }}.* --dev --no-update

- name: Install the dependencies
run: |
composer install --no-interaction --no-progress --no-plugins
Expand Down
10 changes: 10 additions & 0 deletions composer-dependency-analyser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

use ShipMonk\ComposerDependencyAnalyser\Config\Configuration;
use ShipMonk\ComposerDependencyAnalyser\Config\ErrorType;

return (new Configuration())
->ignoreErrorsOnPackage('symfony/translation', [ErrorType::SHADOW_DEPENDENCY])
->ignoreErrorsOnPackage('contao/newsletter-bundle', [ErrorType::DEV_DEPENDENCY_IN_PROD])
->ignoreUnknownClasses([Symfony\Component\HttpKernel\UriSigner::class, Contao\ModulePassword::class])
;
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"doctrine/orm": "^2.19",
"knplabs/knp-menu": "^3.1",
"psr/container": "^1.0 || ^2.0",
"psr/log": "^2.0 || ^3.0",
"ramsey/collection": "^1.2",
"soundasleep/html2text": "^2.0",
"symfony/asset": "^5.4 || ^6.0 || ^7.0",
Expand All @@ -54,8 +55,8 @@
},
"require-dev": {
"contao/manager-plugin": "^2.0",
"contao/newsletter-bundle": "^5.0",
"contao/test-case": "^5.3",
"contao/newsletter-bundle": "^4.13 || ^5.0",
"contao/test-case": "^4.13 || ^5.3",
"league/flysystem-memory": "^3.25",
"phpunit/phpunit": "^9.6",
"symfony/expression-language": "^5.4 || ^6.0 || ^7.0",
Expand Down
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,14 +7,15 @@
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 Terminal42\NotificationCenterBundle\BulkyItem\BulkyItemStorage;

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 a3daaab

Please sign in to comment.