Skip to content

Commit

Permalink
[feature] allow Symfony 6 (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Nov 19, 2021
1 parent bedd52a commit d316726
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 51 deletions.
20 changes: 2 additions & 18 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,8 @@ on:
- cron: '0 0 1,16 * *'

jobs:
tests:
name: PHP ${{ matrix.php }}, SF ${{ matrix.symfony }} - ${{ matrix.deps }}
runs-on: ubuntu-latest
strategy:
matrix:
php: [7.4, 8.0]
deps: [hightest]
symfony: [4.4.*, 5.3.*, 5.4.*]
include:
- php: 7.4
deps: lowest
symfony: '*'
steps:
- uses: zenstruck/.github@php-test-symfony
with:
php: ${{ matrix.php }}
symfony: ${{ matrix.symfony }}
deps: ${{ matrix.deps }}
test:
uses: zenstruck/.github/.github/workflows/php-test-symfony.yml@main

code-coverage:
uses: zenstruck/.github/.github/workflows/php-coverage-codecov.yml@main
Expand Down
12 changes: 6 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@
],
"require": {
"php": ">=7.4",
"symfony/framework-bundle": "^4.4|^5.0",
"symfony/mailer": "^4.4|^5.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/mailer": "^4.4|^5.0|^6.0",
"symfony/polyfill-php80": "^1.23.1",
"zenstruck/assert": "^1.0",
"zenstruck/callback": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^9.5.0",
"symfony/messenger": "^4.4|^5.0",
"symfony/phpunit-bridge": "^5.2",
"symfony/var-dumper": "^4.4|^5.0",
"symfony/yaml": "^4.4|^5.0",
"symfony/messenger": "^4.4|^5.0|^6.0",
"symfony/phpunit-bridge": "^5.3",
"symfony/var-dumper": "^4.4|^5.0|^6.0",
"symfony/yaml": "^4.4|^5.0|^6.0",
"zenstruck/browser": "^1.0@dev"
},
"config": {
Expand Down
8 changes: 5 additions & 3 deletions src/InteractsWithMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,16 @@ final protected function mailer(): TestMailer
throw new \LogicException(\sprintf('The %s trait can only be used with %s.', __TRAIT__, KernelTestCase::class));
}

if (!self::$container) {
if (!self::$booted) {
throw new \LogicException('The kernel must be booted before accessing the mailer.');
}

if (!self::$container->has('zenstruck_mailer_test.mailer')) {
$container = \method_exists(self::class, 'getContainer') ? self::getContainer() : self::$container;

if (!$container->has('zenstruck_mailer_test.mailer')) {
throw new \LogicException(\sprintf('Cannot access test mailer - is %s enabled in your test environment?', ZenstruckMailerTestBundle::class));
}

return self::$container->get('zenstruck_mailer_test.mailer');
return $container->get('zenstruck_mailer_test.mailer');
}
}
24 changes: 24 additions & 0 deletions tests/ContainerBC.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Zenstruck\Mailer\Test\Tests;

use Psr\Container\ContainerInterface;

/**
* @author Kevin Bond <kevinbond@gmail.com>
*/
trait ContainerBC
{
private static function container(): ContainerInterface
{
if (!\method_exists(static::class, 'getContainer')) {
if (!static::$booted) {
static::bootKernel();
}

return self::$container;
}

return self::getContainer();
}
}
18 changes: 15 additions & 3 deletions tests/Fixture/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use Symfony\Component\Routing\RouteCollectionBuilder;
use Zenstruck\Mailer\Test\ZenstruckMailerTestBundle;

Expand Down Expand Up @@ -44,9 +45,20 @@ protected function configureContainer(ContainerBuilder $c, LoaderInterface $load
$loader->load(\sprintf('%s/config/%s.yaml', __DIR__, $this->getEnvironment()));
}

protected function configureRoutes(RouteCollectionBuilder $routes): void
/**
* @param RouteCollectionBuilder|RoutingConfigurator $routes
*/
protected function configureRoutes($routes): void
{
$routes->add('/no-email', 'kernel::noEmail');
$routes->add('/send-email', 'kernel::sendEmail');
if ($routes instanceof RouteCollectionBuilder) {
// BC
$routes->add('/no-email', 'kernel::noEmail');
$routes->add('/send-email', 'kernel::sendEmail');

return;
}

$routes->add('no-email', '/no-email')->controller('kernel::noEmail');
$routes->add('send-email', '/send-email')->controller('kernel::sendEmail');
}
}
24 changes: 11 additions & 13 deletions tests/InteractsWithMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
final class InteractsWithMailerTest extends KernelTestCase
{
use EnvironmentProvider, InteractsWithMailer;
use ContainerBC, EnvironmentProvider, InteractsWithMailer;

/**
* @test
Expand Down Expand Up @@ -51,7 +51,7 @@ public function can_assert_email_sent(string $environment): void
{
self::bootKernel(['environment' => $environment]);

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->mailer()
->assertSentEmailCount(1)
Expand Down Expand Up @@ -93,7 +93,7 @@ public function assert_email_sent_to_fail(string $environment): void
{
self::bootKernel(['environment' => $environment]);

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('No email was sent to "jim@example.com".');
Expand All @@ -112,7 +112,7 @@ public function can_use_custom_test_email_class(string $environment): void
$email = new Email1();
$email->getHeaders()->addTextHeader('X-PM-Tag', 'reset-password');

self::$container->get('mailer')->send($email);
self::container()->get('mailer')->send($email);

$this->mailer()->assertEmailSentTo('kevin@example.com', function(CustomTestEmail $email) {
$email->assertHasPostmarkTag('reset-password');
Expand All @@ -127,7 +127,7 @@ public function can_access_sent_test_emails(string $environment): void
{
self::bootKernel(['environment' => $environment]);

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->assertInstanceOf(TestEmail::class, $this->mailer()->sentEmails()->all()[0]);
}
Expand All @@ -140,7 +140,7 @@ public function can_access_sent_test_emails_with_custom_test_email_class(string
{
self::bootKernel(['environment' => $environment]);

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->assertInstanceOf(CustomTestEmail::class, $this->mailer()->sentEmails()->all(CustomTestEmail::class)[0]);
}
Expand All @@ -150,9 +150,7 @@ public function can_access_sent_test_emails_with_custom_test_email_class(string
*/
public function sent_test_email_argument_must_be_an_instance_of_test_email(): void
{
self::bootKernel();

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->expectException(\InvalidArgumentException::class);

Expand All @@ -177,7 +175,7 @@ public function profiler_does_not_need_to_enabled(): void
{
self::bootKernel(['environment' => 'no_profiler']);

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->mailer()->assertSentEmailCount(1);
}
Expand All @@ -192,7 +190,7 @@ public function emails_are_persisted_between_reboots(string $environment): void

$this->mailer()->assertNoEmailSent();

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->mailer()->assertSentEmailCount(1);

Expand All @@ -213,7 +211,7 @@ public function can_reset_collected_emails(string $environment): void

$this->mailer()->assertNoEmailSent();

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->mailer()
->assertSentEmailCount(1)
Expand All @@ -225,7 +223,7 @@ public function can_reset_collected_emails(string $environment): void

self::bootKernel(['environment' => $environment]);

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->mailer()->assertSentEmailCount(1);
}
Expand Down
14 changes: 6 additions & 8 deletions tests/NonInteractsWithMailerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,31 @@
*/
final class NonInteractsWithMailerTest extends KernelTestCase
{
use ContainerBC;

/**
* @test
*/
public function ensure_emails_are_not_collected(): void
{
self::bootKernel();

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot access sent emails as email collection has not yet been started.');

self::$container->get('zenstruck_mailer_test.mailer')->sentEmails();
self::container()->get('zenstruck_mailer_test.mailer')->sentEmails();
}

/**
* @test
*/
public function cannot_reset_if_collection_not_yet_started(): void
{
self::bootKernel();

self::$container->get('mailer')->send(new Email1());
self::container()->get('mailer')->send(new Email1());

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('Cannot access sent emails as email collection has not yet been started.');

self::$container->get('zenstruck_mailer_test.mailer')->reset();
self::container()->get('zenstruck_mailer_test.mailer')->reset();
}
}

0 comments on commit d316726

Please sign in to comment.