Skip to content

Commit

Permalink
feat: behat support
Browse files Browse the repository at this point in the history
  • Loading branch information
kbond committed Dec 9, 2022
1 parent 555d547 commit cfacf97
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 0 deletions.
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:
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
*/
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

0 comments on commit cfacf97

Please sign in to comment.