-
-
Notifications
You must be signed in to change notification settings - Fork 86
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
base: 1.x
Are you sure you want to change the base?
feat: behat support #378
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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/ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
default: | ||
extensions: | ||
FriendsOfBehat\SymfonyExtension: | ||
kernel: | ||
class: Zenstruck\Foundry\Tests\Fixtures\Kernel | ||
bootstrap: tests/bootstrap.php | ||
|
||
suites: | ||
default: | ||
paths: [ "%paths.base%/tests/Behat" ] | ||
contexts: | ||
- Zenstruck\Foundry\Tests\Behat\TestContext | ||
- Zenstruck\Foundry\Test\Behat\FactoriesContext | ||
- Zenstruck\Foundry\Test\Behat\ResetDatabaseContext |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
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 Symfony\Component\HttpKernel\KernelInterface; | ||
use Zenstruck\Foundry\Test\DatabaseResetter; | ||
|
||
/** | ||
* @author Kevin Bond <[email protected]> | ||
*/ | ||
final class ResetDatabaseContext implements Context | ||
{ | ||
public function __construct(private KernelInterface $kernel) | ||
{ | ||
} | ||
|
||
/** | ||
* @BeforeScenario | ||
*/ | ||
public function resetDatabase(): void | ||
{ | ||
DatabaseResetter::resetDatabase($this->kernel); | ||
} | ||
|
||
/** | ||
* @AfterScenario | ||
*/ | ||
public function resetSchema(): void | ||
{ | ||
DatabaseResetter::resetSchema($this->kernel); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
Feature: Behat support for Foundry | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also probably add an example of using backgrounds to implement the fixtures. I use the back ground to set up core fixtures (that are used in all tests). |
||
|
||
As a developer writing tests with Behat, I want to be able to | ||
use Foundry for creating test data. Foundry needs to be configured, | ||
started and stopped accordingly. The database has to be reset before | ||
scenarios are run. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PS might be worth adding a test for "example tested" syntax. Associated events for subscriber: ExampleTested::BEFORE, ExampleTested::AFTER |
||
Scenario: Creating two categories | ||
When I create 2 categories | ||
Then there should be 2 categories in the database | ||
|
||
Scenario: Adding to the database | ||
Given 1 category has been created | ||
When I create 2 categories | ||
Then there should be 3 categories in the database |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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 /^(?P<count>\d+) (?:category has|categories have) been created$/ | ||
* @When /^I create (?P<count>\d+) categor(?:y|ies)$/ | ||
*/ | ||
public function createCategories(int $count): void | ||
{ | ||
CategoryFactory::createMany($count); | ||
} | ||
|
||
/** | ||
* @Then /^there should be (?P<count>\d+) categor(?:y|ies) in the database$/ | ||
*/ | ||
public function assertCategoryCount(int $count): void | ||
{ | ||
CategoryFactory::assert()->count($count); | ||
} | ||
} |
There was a problem hiding this comment.
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:I've confirmed the kernel isn't being booted but not sure why.
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.