Kocal/SymfonyMailerTesting

Doc : Using with APIs

silverbackdan opened this issue · 9 comments

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.

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

I think this is related to this issue: FriendsOfBehat/SymfonyExtension#149

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);
    }
}

Maybe you could try if FriendsOfBehat/SymfonyExtension#190 fixes the issue for you?

@silverbackdan FriendsOfBehat/SymfonyExtension#190 is released. Try updating to 2.4, and try to reproduce it again)

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

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.

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

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.