[ApiBundle] Test one case with swiftmailer enabled

This commit is contained in:
Mateusz Zalewski 2022-09-07 15:02:39 +02:00
parent f54368b917
commit 0006ffd10f
No known key found for this signature in database
GPG key ID: 9BECA0BB71612E52
16 changed files with 172 additions and 15 deletions

View file

@ -44,7 +44,7 @@ jobs:
runs-on: ubuntu-latest
name: "${{ matrix.package }}, PHP ${{ matrix.php }}, Symfony ${{ matrix.symfony }}"
timeout-minutes: 10
strategy:
@ -55,16 +55,21 @@ jobs:
package: "${{ fromJson(needs.list.outputs.packages) }}"
swiftmailer: [false]
include:
-
-
php: "8.0"
symfony: "^5.4"
package: "Bundle/CoreBundle"
swiftmailer: true
-
-
php: "8.1"
symfony: "^5.4"
package: "Bundle/CoreBundle"
swiftmailer: true
-
php: "8.1"
symfony: "^5.4"
package: "Bundle/ApiBundle"
swiftmailer: true
steps:
-
@ -148,9 +153,9 @@ jobs:
-
name: Setup Database (for ApiBundle)
working-directory: "src/Sylius/${{ matrix.package }}"
run: test/bin/console doctrine:schema:update --force -e test
run: test/bin/console doctrine:schema:update --force
if: matrix.package == 'Bundle/ApiBundle'
-
name: Remove Specifications not supported on Symfony >= 6.0
working-directory: "src/Sylius/${{ matrix.package }}"

View file

@ -42,6 +42,10 @@ final class SendResetPasswordEmailHandlerTest extends KernelTestCase
/** @test */
public function it_sends_password_reset_token_email(): void
{
if ($this->isItSwiftmailerTestEnv()) {
$this->markTestSkipped('Test is relevant only for the environment without swiftmailer');
}
/** @var TranslatorInterface $translator */
$translator = $this->getContainer()->get('translator');
@ -71,4 +75,11 @@ final class SendResetPasswordEmailHandlerTest extends KernelTestCase
'sylius@example.com',
));
}
private function isItSwiftmailerTestEnv(): bool
{
$env = $this->getContainer()->getParameter('kernel.environment');
return $env === 'test_with_swiftmailer';
}
}

View file

@ -18,10 +18,12 @@ use Sylius\Bundle\ApiBundle\Command\Account\SendAccountRegistrationEmail;
use Sylius\Bundle\ApiBundle\CommandHandler\Account\SendAccountRegistrationEmailHandler;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Core\Test\Services\EmailChecker;
use Sylius\Component\User\Model\UserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Bundle\FrameworkBundle\Test\MailerAssertionsTrait;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Contracts\Translation\TranslatorInterface;
final class SendAccountRegistrationEmailHandlerTest extends KernelTestCase
@ -31,6 +33,10 @@ final class SendAccountRegistrationEmailHandlerTest extends KernelTestCase
/** @test */
public function it_sends_account_registration_email(): void
{
if ($this->isItSwiftmailerTestEnv()) {
$this->markTestSkipped('Test is relevant only for the environment without swiftmailer');
}
$container = self::bootKernel()->getContainer();
/** @var TranslatorInterface $translator */
@ -72,4 +78,69 @@ final class SendAccountRegistrationEmailHandlerTest extends KernelTestCase
$this->assertEmailAddressContains($email, 'To', 'user@example.com');
$this->assertEmailHtmlBodyContains($email, $translator->trans('sylius.email.user_registration.start_shopping', [], null, 'en_US'));
}
/** @test */
public function it_sends_account_registration_email_with_swiftmailer(): void
{
if (!$this->isItSwiftmailerTestEnv()) {
$this->markTestSkipped('Test is relevant only for the environment with swiftmailer');
}
$container = self::bootKernel()->getContainer();
/** @var Filesystem $filesystem */
$filesystem = $container->get('test.filesystem.public');
/** @var TranslatorInterface $translator */
$translator = $container->get('translator');
/** @var EmailChecker $emailChecker */
$emailChecker = $container->get('sylius.behat.email_checker');
$filesystem->remove($emailChecker->getSpoolDirectory());
$emailSender = $container->get('sylius.email_sender');
/** @var ChannelRepositoryInterface|ObjectProphecy $channelRepository */
$channelRepository = $this->prophesize(ChannelRepositoryInterface::class);
/** @var UserRepositoryInterface|ObjectProphecy $userRepository */
$userRepository = $this->prophesize(UserRepositoryInterface::class);
/** @var ChannelInterface|ObjectProphecy $channel */
$channel = $this->prophesize(ChannelInterface::class);
/** @var UserInterface|ObjectProphecy $user */
$user = $this->prophesize(UserInterface::class);
$user->getUsername()->willReturn('username');
$user->getEmailVerificationToken()->willReturn('token');
$channelRepository->findOneByCode('CHANNEL_CODE')->willReturn($channel->reveal());
$userRepository->findOneByEmail('user@example.com')->willReturn($user->reveal());
$sendAccountRegistrationEmailHandler = new SendAccountRegistrationEmailHandler(
$userRepository->reveal(),
$channelRepository->reveal(),
$emailSender,
);
$sendAccountRegistrationEmailHandler(
new SendAccountRegistrationEmail(
'user@example.com',
'en_US',
'CHANNEL_CODE',
),
);
self::assertSame(1, $emailChecker->countMessagesTo('user@example.com'));
self::assertTrue($emailChecker->hasMessageTo(
$translator->trans('sylius.email.user_registration.start_shopping', [], null, 'en_US'),
'user@example.com',
));
}
private function isItSwiftmailerTestEnv(): bool
{
$env = $this->getContainer()->getParameter('kernel.environment');
return $env === 'test_with_swiftmailer';
}
}

View file

@ -31,6 +31,10 @@ final class SendAccountVerificationEmailHandlerTest extends KernelTestCase
/** @test */
public function it_sends_account_verification_token_email(): void
{
if ($this->isItSwiftmailerTestEnv()) {
$this->markTestSkipped('Test is relevant only for the environment without swiftmailer');
}
$container = self::bootKernel()->getContainer();
/** @var TranslatorInterface $translator */
@ -72,4 +76,11 @@ final class SendAccountVerificationEmailHandlerTest extends KernelTestCase
$this->assertEmailAddressContains($email, 'To', 'user@example.com');
$this->assertEmailHtmlBodyContains($email, $translator->trans('sylius.email.verification_token.message', [], null, 'en_US'));
}
private function isItSwiftmailerTestEnv(): bool
{
$env = $this->getContainer()->getParameter('kernel.environment');
return $env === 'test_with_swiftmailer';
}
}

View file

@ -29,6 +29,10 @@ final class SendContactRequestHandlerTest extends KernelTestCase
/** @test */
public function it_sends_contact_request(): void
{
if ($this->isItSwiftmailerTestEnv()) {
$this->markTestSkipped('Test is relevant only for the environment without swiftmailer');
}
$container = self::bootKernel()->getContainer();
/** @var TranslatorInterface $translator */
@ -62,4 +66,11 @@ final class SendContactRequestHandlerTest extends KernelTestCase
$this->assertEmailAddressContains($email, 'To', 'shop@example.com');
$this->assertEmailHtmlBodyContains($email, $translator->trans('sylius.email.contact_request.content', [], null, 'en_US'));
}
private function isItSwiftmailerTestEnv(): bool
{
$env = $this->getContainer()->getParameter('kernel.environment');
return $env === 'test_with_swiftmailer';
}
}

View file

@ -31,11 +31,11 @@ final class SendOrderConfirmationEmailHandlerTest extends KernelTestCase
/** @test */
public function it_sends_order_confirmation_email(): void
{
if ($this->isItSwiftmailerTestEnv()) {
$this->markTestSkipped('Test is relevant only for the environment without swiftmailer');
}
$container = self::bootKernel()->getContainer();
/** @var TranslatorInterface $translator */
$translator = $container->get('translator');
$emailSender = $container->get('sylius.email_sender');
/** @var OrderInterface|ObjectProphecy $order */
@ -69,4 +69,11 @@ final class SendOrderConfirmationEmailHandlerTest extends KernelTestCase
$this->assertEmailAddressContains($email, 'To', 'johnny.bravo@email.com');
$this->assertEmailHtmlBodyContains($email, '#000001');
}
private function isItSwiftmailerTestEnv(): bool
{
$env = $this->getContainer()->getParameter('kernel.environment');
return $env === 'test_with_swiftmailer';
}
}

View file

@ -31,6 +31,10 @@ final class SendResetPasswordEmailHandlerTest extends KernelTestCase
/** @test */
public function it_sends_password_reset_token_email(): void
{
if ($this->isItSwiftmailerTestEnv()) {
$this->markTestSkipped('Test is relevant only for the environment without swiftmailer');
}
$container = self::bootKernel()->getContainer();
/** @var TranslatorInterface $translator */
@ -70,4 +74,11 @@ final class SendResetPasswordEmailHandlerTest extends KernelTestCase
$this->assertEmailAddressContains($email, 'To', 'user@example.com');
$this->assertEmailHtmlBodyContains($email, $translator->trans('sylius.email.password_reset.to_reset_your_password_token', [], null, 'en_US'));
}
private function isItSwiftmailerTestEnv(): bool
{
$env = $this->getContainer()->getParameter('kernel.environment');
return $env === 'test_with_swiftmailer';
}
}

View file

@ -31,6 +31,10 @@ final class SendResetPasswordEmailHandlerTest extends KernelTestCase
/** @test */
public function it_sends_password_reset_token_email(): void
{
if ($this->isItSwiftmailerTestEnv()) {
$this->markTestSkipped('Test is relevant only for the environment without swiftmailer');
}
/** @var TranslatorInterface $translator */
$translator = $this->getContainer()->get('translator');
@ -59,4 +63,11 @@ final class SendResetPasswordEmailHandlerTest extends KernelTestCase
$this->assertEmailAddressContains($email, 'To', 'sylius@example.com');
$this->assertEmailHtmlBodyContains($email, $translator->trans('sylius.email.admin_password_reset.to_reset_your_password_token', [], null, 'en_US'));
}
private function isItSwiftmailerTestEnv(): bool
{
$env = $this->getContainer()->getParameter('kernel.environment');
return $env === 'test_with_swiftmailer';
}
}

View file

@ -0,0 +1 @@
APP_ENV=test_with_swiftmailer

View file

@ -13,6 +13,7 @@ declare(strict_types=1);
return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['test_with_swiftmailer' => true],
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
@ -54,9 +55,9 @@ return [
Sylius\Bundle\PayumBundle\SyliusPayumBundle::class => ['all' => true],
ApiPlatform\Core\Bridge\Symfony\Bundle\ApiPlatformBundle::class => ['all' => true],
Sylius\Bundle\ApiBundle\SyliusApiBundle::class => ['all' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true],
Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true],
Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true],
Symfony\Bundle\DebugBundle\DebugBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true, 'test_with_swiftmailer' => true],
Nelmio\Alice\Bridge\Symfony\NelmioAliceBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true, 'test_with_swiftmailer' => true],
Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['dev' => true, 'test' => true, 'test_cached' => true, 'test_with_swiftmailer' => true],
Lexik\Bundle\JWTAuthenticationBundle\LexikJWTAuthenticationBundle::class => ['all' => true],
SyliusLabs\DoctrineMigrationsExtraBundle\SyliusLabsDoctrineMigrationsExtraBundle::class => ['all' => true],
SyliusLabs\Polyfill\Symfony\Security\Bundle\SyliusLabsPolyfillSymfonySecurityBundle::class => ['all' => true],

View file

@ -79,9 +79,6 @@ sylius_resource:
classes:
model: Sylius\Bundle\ApiBundle\Application\Entity\FooSyliusResource
sylius_mailer:
sender_adapter: sylius.email_sender.adapter.symfony_mailer
services:
test.channel.id_filter:
parent: api_platform.doctrine.orm.search_filter

View file

@ -0,0 +1,2 @@
sylius_mailer:
sender_adapter: sylius.email_sender.adapter.symfony_mailer

View file

@ -0,0 +1,4 @@
services:
test.filesystem.public:
alias: filesystem
public: true

View file

@ -0,0 +1,6 @@
swiftmailer:
disable_delivery: true
logging: true
spool:
type: file
path: "%kernel.cache_dir%/spool"

View file

@ -0,0 +1,2 @@
sylius_mailer:
sender_adapter: sylius.email_sender.adapter.swiftmailer

View file

@ -62,5 +62,11 @@ final class Kernel extends BaseKernel
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load($this->getProjectDir() . '/config/config.yaml');
$confDir = $this->getProjectDir() . '/config';
$loader->load($confDir . '/{packages}/*' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{packages}/' . $this->environment . '/**/*' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{services}' . self::CONFIG_EXTS, 'glob');
$loader->load($confDir . '/{services}_' . $this->environment . self::CONFIG_EXTS, 'glob');
}
}