Skip to content

Commit 5d0d894

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

9 files changed

+126
-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

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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

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/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)