Skip to content

Commit cfacf97

Browse files
committed
feat: behat support
1 parent 555d547 commit cfacf97

10 files changed

+137
-0
lines changed

.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
/phpstan.neon export-ignore
1616
/phpunit-dama-doctrine.xml.dist export-ignore
1717
/phpunit.xml.dist export-ignore
18+
/behat.yml.dist export-ignore
1819
/tests export-ignore
20+
/features export-ignore

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/composer.lock
22
/phpunit.xml
33
/phpunit-dama-doctrine.xml
4+
/behat.yml
45
/vendor/
56
/bin/tools/*/vendor/
67
/build/

behat.yml.dist

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
default:
2+
suites:
3+
default:
4+
contexts:
5+
- Zenstruck\Foundry\Tests\Behat\TestContext
6+
- Zenstruck\Foundry\Test\Behat\FactoriesContext
7+
extensions:
8+
FriendsOfBehat\SymfonyExtension:
9+
kernel:
10+
class: Zenstruck\Foundry\Tests\Fixtures\Kernel
11+
bootstrap: tests/bootstrap.php

composer.json

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"doctrine/doctrine-migrations-bundle": "^2.2|^3.0",
2929
"doctrine/mongodb-odm-bundle": "^3.1|^4.2",
3030
"doctrine/orm": "^2.7",
31+
"friends-of-behat/symfony-extension": "^2.4",
3132
"matthiasnoback/symfony-dependency-injection-test": "^4.1",
3233
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
3334
"symfony/maker-bundle": "^1.30",
@@ -54,6 +55,9 @@
5455
"App\\Tests\\": "tests/Fixtures/tmp/tests"
5556
}
5657
},
58+
"suggest": {
59+
"friends-of-behat/symfony-extension": "To use the Behat contexts"
60+
},
5761
"extra": {
5862
"bamarni-bin": {
5963
"target-directory": "bin/tools",

features/foundry.feature

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Feature: Foundry
2+
Scenario: Ensure can create factories
3+
Given there is 1 category
4+
Then there is 1 category in the database

src/Bundle/DependencyInjection/ZenstruckFoundryExtension.php

+10
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44

55
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
66
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
7+
use FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle;
78
use Symfony\Bundle\MakerBundle\Maker\AbstractMaker;
89
use Symfony\Component\Config\FileLocator;
910
use Symfony\Component\DependencyInjection\ContainerBuilder;
1011
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
12+
use Symfony\Component\DependencyInjection\Reference;
1113
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
1214
use Zenstruck\Foundry\Bundle\Command\StubMakeFactory;
1315
use Zenstruck\Foundry\Bundle\Command\StubMakeStory;
1416
use Zenstruck\Foundry\ModelFactory;
1517
use Zenstruck\Foundry\Story;
18+
use Zenstruck\Foundry\Test\Behat\FactoriesContext;
1619
use Zenstruck\Foundry\Test\ORMDatabaseResetter;
1720

1821
/**
@@ -49,6 +52,13 @@ protected function loadInternal(array $mergedConfig, ContainerBuilder $container
4952
$container->register('.zenstruck_foundry.maker.factory_stub', StubMakeFactory::class)->addTag('console.command');
5053
$container->register('.zenstruck_foundry.maker.story_stub', StubMakeStory::class)->addTag('console.command');
5154
}
55+
56+
if (self::isBundleLoaded($container, FriendsOfBehatSymfonyExtensionBundle::class)) {
57+
$container->register('.zenstruck_foundry.behat.factories_context', FactoriesContext::class)
58+
->addArgument(new Reference('service_container'))
59+
->addTag('fob.context')
60+
;
61+
}
5262
}
5363

5464
private function configureFaker(array $config, ContainerBuilder $container): void

src/Test/Behat/FactoriesContext.php

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Zenstruck\Foundry\Test\Behat;
4+
5+
use Behat\Behat\Context\Context;
6+
use Psr\Container\ContainerInterface;
7+
use Zenstruck\Foundry\ChainManagerRegistry;
8+
use Zenstruck\Foundry\Factory;
9+
use Zenstruck\Foundry\Test\LazyManagerRegistry;
10+
use Zenstruck\Foundry\Test\TestState;
11+
12+
/**
13+
* @author Kevin Bond <[email protected]>
14+
*/
15+
final class FactoriesContext implements Context
16+
{
17+
public function __construct(private ContainerInterface $container)
18+
{
19+
}
20+
21+
/**
22+
* @BeforeScenario
23+
*/
24+
public function setUpFactories(): void
25+
{
26+
TestState::bootFromContainer($this->container);
27+
Factory::configuration()->setManagerRegistry(
28+
new LazyManagerRegistry(function (): ChainManagerRegistry {
29+
return TestState::initializeChainManagerRegistry($this->container);
30+
})
31+
);
32+
}
33+
34+
/**
35+
* @AfterScenario
36+
*/
37+
public function tearDownFactories(): void
38+
{
39+
TestState::shutdownFoundry();
40+
}
41+
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Zenstruck\Foundry\Test\Behat;
4+
5+
use Behat\Behat\Context\Context;
6+
use Psr\Container\ContainerInterface;
7+
use Zenstruck\Foundry\Test\DatabaseResetter;
8+
9+
/**
10+
* @author Kevin Bond <[email protected]>
11+
*/
12+
final class ResetDatabaseContext implements Context
13+
{
14+
public function __construct(private ContainerInterface $container)
15+
{
16+
}
17+
18+
/**
19+
* @BeforeSuite
20+
*/
21+
public function resetDatabase(): void
22+
{
23+
DatabaseResetter::resetDatabase($this->container->get('kernel'));
24+
}
25+
26+
/**
27+
* @BeforeScenario
28+
*/
29+
public function resetSchema(): void
30+
{
31+
DatabaseResetter::resetSchema($this->container->get('kernel'));
32+
}
33+
}

tests/Behat/TestContext.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Zenstruck\Foundry\Tests\Behat;
4+
5+
use Behat\Behat\Context\Context;
6+
use Zenstruck\Foundry\Tests\Fixtures\Factories\CategoryFactory;
7+
8+
/**
9+
* @author Kevin Bond <[email protected]>
10+
*/
11+
final class TestContext implements Context
12+
{
13+
/**
14+
* @Given there is :count category
15+
*/
16+
public function thereIsCategory(int $count): void
17+
{
18+
CategoryFactory::createMany($count);
19+
}
20+
21+
/**
22+
* @Then there is :count category in the database
23+
*/
24+
public function thereIsCategoryInTheDatabase(int $count): void
25+
{
26+
CategoryFactory::assert()->count($count);
27+
}
28+
}

tests/Fixtures/Kernel.php

+3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
77
use Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle;
88
use Doctrine\Bundle\MongoDBBundle\DoctrineMongoDBBundle;
9+
use FriendsOfBehat\SymfonyExtension\Bundle\FriendsOfBehatSymfonyExtensionBundle;
910
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
1011
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
1112
use Symfony\Bundle\MakerBundle\MakerBundle;
@@ -79,6 +80,8 @@ public function registerBundles(): iterable
7980
if ($this->enableDoctrine && \getenv('USE_ODM')) {
8081
yield new DoctrineMongoDBBundle();
8182
}
83+
84+
yield new FriendsOfBehatSymfonyExtensionBundle();
8285
}
8386

8487
public function getCacheDir(): string

0 commit comments

Comments
 (0)