Kocal / SymfonyMailerTesting

Provide Behat and Cypress API for testing emails sent by the Symfony Mailer.
MIT License
10 stars 2 forks source link

Doc : Using with APIs #17

Open silverbackdan opened 3 years ago

silverbackdan commented 3 years ago

I've got a solution if you are using this with Behat and API calls. It may be useful in the docs or if it's wrong I'm happy for comments.

I extend the MailerContext to my own like this:

<?php

declare(strict_types=1);

namespace App\Tests\Behat;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\MinkExtension\Context\MinkContext;
use Kocal\SymfonyMailerTesting\MailerLogger;

/**
 * @author Daniel West <daniel@silverback.is>
 */
class MailerContext extends \Kocal\SymfonyMailerTesting\Bridge\Behat\MailerContext
{
    /**
     * @BeforeScenario
     */
    public function setMinkContainerLogger(BeforeScenarioScope $scope)
    {
        /** @var MinkContext $minkContext */
        $minkContext = $scope->getEnvironment()->getContext(MinkContext::class);
        $this->setMailerLogger($minkContext->getSession()->getDriver()->getClient()->getContainer()->get('test.service_container')->get(MailerLogger::class));
    }
}

This way we are getting the logger service being used in the container for the final request when the Mink extension is used.

silverbackdan commented 3 years ago

This actually has an issue whereby between scenarios I lose the logger. I run a single scenario it works. I'm trying to figure this out.

walva commented 3 years ago

I think this is related to this issue: https://github.com/FriendsOfBehat/SymfonyExtension/issues/149

silverbackdan commented 2 years ago

Update: you need to disable reboot using $client->disableReboot();

This works as a workaround for now.

<?php

declare(strict_types=1);

namespace App\Tests\Behat;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\MinkExtension\Context\MinkContext;
use Kocal\SymfonyMailerTesting\Bridge\Behat\MailerContext as BaseMailerContext;
use Kocal\SymfonyMailerTesting\MailerLogger;

/**
 * @author Daniel West <daniel@silverback.is>
 */
class MailerContext extends BaseMailerContext
{
    /**
     * @BeforeScenario
     */
    public function setMinkContainerLogger(BeforeScenarioScope $scope)
    {
        /** @var MinkContext $minkContext */
        $minkContext = $scope->getEnvironment()->getContext(MinkContext::class);
        $client = $minkContext->getSession()->getDriver()->getClient();
        $client->disableReboot();
        $container = $client->getContainer();
        $testContainer = $container->get('test.service_container');
        $logger = $testContainer->get(MailerLogger::class);
        $this->setMailerLogger($logger);
    }
}
mpdude commented 1 year ago

Maybe you could try if https://github.com/FriendsOfBehat/SymfonyExtension/pull/190 fixes the issue for you?

Yozhef commented 1 year ago

@silverbackdan https://github.com/FriendsOfBehat/SymfonyExtension/pull/190 is released. Try updating to 2.4, and try to reproduce it again)

develth commented 1 year ago

Out of the box, this extension only works with @silverbackdan provided extended addtion with @BeforeScenario.

silverbackdan commented 1 year ago

Hi both, I'm sorry I will need to remember the project I had this issue on to revisit and I'm totally snowed under at the moment.

silverbackdan commented 1 year ago

Hopefully someone else who is still experiencing the issue could confirm. Seems like @develth is still having the issue though.

acardielwata commented 1 year ago

In my case, according @develth solution, this is my final custom context

<?php

namespace App\Tests\Behat;

use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\MinkExtension\Context\MinkContext;
use Kocal\SymfonyMailerTesting\Bridge\Behat\MailerContext;
use Kocal\SymfonyMailerTesting\MailerLogger;

class EmailContext extends MailerContext
{
    /**
     * @BeforeScenario
     */
    public function setMinkContainerLogger(BeforeScenarioScope $scope): void
    {
        /** @var MinkContext $minkContext */
        $minkContext = $scope->getEnvironment()->getContext(MinkContext::class);

        $client = $minkContext
            ->getSession()
            ->getDriver()
            ->getClient();

        $client->disableReboot();

        $this->setMailerLogger(
            $client->getContainer()
                ->get('test.service_container')
                ->get(MailerLogger::class)
        );
    }

}

Now, it's working properly checking mails in different scenarios, but is not possible to make chained requests because the old it's keeping and is not considering the newest.