Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: behat support #378

Draft
wants to merge 3 commits into
base: 1.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@
/phpstan.neon export-ignore
/phpunit-dama-doctrine.xml.dist export-ignore
/phpunit.xml.dist export-ignore
/behat.yml.dist export-ignore
/tests export-ignore
/features export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/composer.lock
/phpunit.xml
/phpunit-dama-doctrine.xml
/behat.yml
/vendor/
/bin/tools/*/vendor/
/build/
Expand Down
11 changes: 11 additions & 0 deletions behat.yml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
default:
suites:
default:
contexts:
- Zenstruck\Foundry\Tests\Behat\TestContext
- Zenstruck\Foundry\Test\Behat\FactoriesContext
extensions:
FriendsOfBehat\SymfonyExtension:
Copy link
Member Author

@kbond kbond Dec 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need some help figuring out why this doesn't seem to be working. When I run vendor/bin/behat I get the following error:

Can not find a matching value for an argument $container of the method Zenstruck\Foundry\Test\Behat\FactoriesContext::__construct().

I've confirmed the kernel isn't being booted but not sure why.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the problem is to do with this: src/Listener/KernelOrchestrator.php:tearDown in symfony extension...

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its loaded in the service extension here:
\FriendsOfBehat\SymfonyExtension\ServiceContainer\SymfonyExtension::loadKernelRebooter

Copy link

@roboticflamingo roboticflamingo Feb 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably try to restructure the new foundry behat contexts in this PR as a listener (like KernelOrchestrator) and perhaps try to use the priority (int) on the subscribed callbacks (e.g. 16 or something) to trigger database/foundry registry reset. Ive found it possible to induce memory leaks quite easily or race conditions in a few integrations ive attempted previously.

kernel:
class: Zenstruck\Foundry\Tests\Fixtures\Kernel
bootstrap: tests/bootstrap.php
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"doctrine/doctrine-migrations-bundle": "^2.2|^3.0",
"doctrine/mongodb-odm-bundle": "^3.1|^4.2",
"doctrine/orm": "^2.7",
"friends-of-behat/symfony-extension": "^2.4",
"matthiasnoback/symfony-dependency-injection-test": "^4.1",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/maker-bundle": "^1.30",
Expand All @@ -54,6 +55,9 @@
"App\\Tests\\": "tests/Fixtures/tmp/tests"
}
},
"suggest": {
"friends-of-behat/symfony-extension": "To use the Behat contexts"
},
"extra": {
"bamarni-bin": {
"target-directory": "bin/tools",
Expand Down
4 changes: 4 additions & 0 deletions features/foundry.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Feature: Foundry
Scenario: Ensure can create factories
Given there is 1 category
Then there is 1 category in the database
10 changes: 10 additions & 0 deletions src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@

use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle;
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
use Zenstruck\Foundry\Bundle\Command\StubMakeFactory;
use Zenstruck\Foundry\Bundle\Command\StubMakeStory;
use Zenstruck\Foundry\ModelFactory;
use Zenstruck\Foundry\Story;
use Zenstruck\Foundry\Test\Behat\FactoriesContext;
use Zenstruck\Foundry\Test\ORMDatabaseResetter;

/**
Expand Down Expand Up @@ -49,6 +52,13 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
$container->register('.zenstruck_foundry.maker.factory_stub', StubMakeFactory::class)->addTag('console.command');
$container->register('.zenstruck_foundry.maker.story_stub', StubMakeStory::class)->addTag('console.command');
}

if (self::isBundleLoaded($container, FriendsOfBehatSymfonyExtensionBundle::class)) {
$container->register('.zenstruck_foundry.behat.factories_context', FactoriesContext::class)
->addArgument(new Reference('service_container'))
->addTag('fob.context')
;
}
}

private function configureFaker(array $config, ContainerBuilder $container): void
Expand Down
41 changes: 41 additions & 0 deletions src/Test/Behat/FactoriesContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Zenstruck\Foundry\Test\Behat;

use Behat\Behat\Context\Context;
use Psr\Container\ContainerInterface;
use Zenstruck\Foundry\ChainManagerRegistry;
use Zenstruck\Foundry\Factory;
use Zenstruck\Foundry\Test\LazyManagerRegistry;
use Zenstruck\Foundry\Test\TestState;

/**
* @author Kevin Bond <[email protected]>
*/
final class FactoriesContext implements Context
{
public function __construct(private ContainerInterface $container)
{
}

/**
* @BeforeScenario

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These annotations cause race conditions because you cannot guarantee when they are run... hence why I think subscriber is the way to go.

*/
public function setUpFactories(): void
{
TestState::bootFromContainer($this->container);
Factory::configuration()->setManagerRegistry(
new LazyManagerRegistry(function (): ChainManagerRegistry {
return TestState::initializeChainManagerRegistry($this->container);
})
);
}

/**
* @AfterScenario
*/
public function tearDownFactories(): void
{
TestState::shutdownFoundry();
}
}
33 changes: 33 additions & 0 deletions src/Test/Behat/ResetDatabaseContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Zenstruck\Foundry\Test\Behat;

use Behat\Behat\Context\Context;
use Psr\Container\ContainerInterface;
use Zenstruck\Foundry\Test\DatabaseResetter;

/**
* @author Kevin Bond <[email protected]>
*/
final class ResetDatabaseContext implements Context
{
public function __construct(private ContainerInterface $container)
{
}

/**
* @BeforeSuite
*/
public function resetDatabase(): void
{
DatabaseResetter::resetDatabase($this->container->get('kernel'));
}

/**
* @BeforeScenario
*/
public function resetSchema(): void
{
DatabaseResetter::resetSchema($this->container->get('kernel'));
}
}
28 changes: 28 additions & 0 deletions tests/Behat/TestContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Zenstruck\Foundry\Tests\Behat;

use Behat\Behat\Context\Context;
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory;

/**
* @author Kevin Bond <[email protected]>
*/
final class TestContext implements Context
{
/**
* @Given there is :count category
*/
public function thereIsCategory(int $count): void
{
CategoryFactory::createMany($count);
}

/**
* @Then there is :count category in the database
*/
public function thereIsCategoryInTheDatabase(int $count): void
{
CategoryFactory::assert()->count($count);
}
}
3 changes: 3 additions & 0 deletions tests/Fixtures/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle;
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
use FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Bundle\MakerBundle\MakerBundle;
Expand Down Expand Up @@ -79,6 +80,8 @@ public function registerBundles(): iterable
if ($this->enableDoctrine && \getenv('USE_ODM')) {
yield new DoctrineMongoDBBundle();
}

yield new FriendsOfBehatSymfonyExtensionBundle();
}

public function getCacheDir(): string
Expand Down