Skip to content
This repository has been archived by the owner on Sep 25, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from supertanuki/load-several-fixtures-on-same…
Browse files Browse the repository at this point in the history
…-time

Load several fixtures on same time
  • Loading branch information
theofidry committed Oct 26, 2015
2 parents e4615fb + f627db8 commit dfcb439
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 4 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ Then simply load your fixtures with the following step:
```gherkin
Given the fixtures file "dummy.yml" is loaded
Given the fixtures file "dummy.yml" is loaded with the persister "doctrine.orm.entity_manager"
Given the following fixtures files are loaded:
| fixtures1.yml |
| fixtures2.yml |
```

## Steps
Expand All @@ -80,6 +83,12 @@ For each context, you have the following steps available:
@Given the fixtures file "fixturesFile" is loaded
@Given the fixtures "fixturesFile" are loaded with the persister "persister_service_id"
@Given the fixtures file "fixturesFile" is loaded with the persister "persister_service_id"
@Given the following fixtures files are loaded:
| fixtures1.yml |
| fixtures2.yml |
@Given the following fixtures files are loaded with the persister "persister_service_id":
| fixtures1.yml |
| fixtures2.yml |
```

Loading a file can be done in three ways:
Expand Down
10 changes: 10 additions & 0 deletions src/Context/AliceContextInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Fidry\AliceBundleExtension\Context;

use Behat\Gherkin\Node\TableNode;
use Nelmio\Alice\PersisterInterface;

/**
Expand Down Expand Up @@ -45,6 +46,15 @@ public function emptyDatabase();
*/
public function thereAreFixtures($fixturesFile, $persister = null);

/**
* @Given the following fixtures files are loaded:
* @Given the following fixtures files are loaded with the persister :persister:
*
* @param TableNode $fixturesFiles Path to the fixtures
* @param PersisterInterface $persister
*/
public function thereAreSeveralFixtures(TableNode $fixturesFiles, $persister = null);

/**
* @param string $basePath
*
Expand Down
32 changes: 29 additions & 3 deletions src/Context/Doctrine/AbstractAliceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Fidry\AliceBundleExtension\Context\Doctrine;

use Behat\Gherkin\Node\TableNode;
use Behat\Symfony2Extension\Context\KernelAwareContext;
use Doctrine\Common\Persistence\ObjectManager;
use Fidry\AliceBundleExtension\Context\AliceContextInterface;
Expand Down Expand Up @@ -141,6 +142,29 @@ public function castServiceIdToPersister($serviceId)
* {@inheritdoc}
*/
public function thereAreFixtures($fixturesFile, $persister = null)
{
$this->loadFixtures([$fixturesFile], $persister);
}

/**
* {@inheritdoc}
*/
public function thereAreSeveralFixtures(TableNode $fixturesFileRows, $persister = null)
{
$fixturesFiles = [];

foreach ($fixturesFileRows->getRows() as $fixturesFileRow) {
$fixturesFiles[] = $fixturesFileRow[0];
}

$this->loadFixtures($fixturesFiles, $persister);
}

/**
* @param array $fixturesFiles
* @param PersisterInterface $persister
*/
private function loadFixtures($fixturesFiles, $persister = null)
{
if (null === $persister) {
$persister = $this->persister;
Expand All @@ -150,13 +174,15 @@ public function thereAreFixtures($fixturesFile, $persister = null)
$persister = $this->castServiceIdToPersister($persister);
}

if (0 !== strpos($fixturesFile, '/') && 0 !== strpos($fixturesFile, '@')) {
$fixturesFile = sprintf('%s/%s', $this->basePath, $fixturesFile);
foreach ($fixturesFiles as $key => $fixturesFile) {
if (0 !== strpos($fixturesFile, '/') && 0 !== strpos($fixturesFile, '@')) {
$fixturesFiles[$key] = sprintf('%s/%s', $this->basePath, $fixturesFile);
}
}

$this->loader->load(
$persister,
$this->fixturesFinder->resolveFixtures($this->kernel, [$fixturesFile])
$this->fixturesFinder->resolveFixtures($this->kernel, $fixturesFiles)
);
}

Expand Down
3 changes: 3 additions & 0 deletions tests/Features/fixtures/ORM/one_another_dummy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Entity\AnotherDummy:
dummy_{10}:
name: Dummy <current()>
16 changes: 15 additions & 1 deletion tests/Features/loader.feature
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Feature: Test Doctrine ORM context
Given the fixtures file "@TestBundle/DataFixtures/ORM/dummy.yml" is loaded
Then the database should contain 10 "dummy" entities

Scenario: Loads a fixture file base on basePath
Scenario: Loads a fixture file based on basePath
Given the database is empty
Given the fixtures file "another_dummy.yml" is loaded
Then the database should contain 10 "another_dummy" entities
Expand All @@ -26,3 +26,17 @@ Feature: Test Doctrine ORM context
Given the database is empty
Given the fixtures file "another_dummy.yml" is loaded with the persister "doctrine.orm.entity_manager"
Then the database should contain 10 "another_dummy" entities

Scenario: Loads several fixtures files based on basePath
Given the database is empty
Given the following fixtures files are loaded:
| another_dummy.yml |
| one_another_dummy.yml |
Then the database should contain 11 "another_dummy" entities

Scenario: Loads several fixtures files with @Bundlename notation
Given the database is empty
Given the following fixtures files are loaded:
| @TestBundle/DataFixtures/ORM/dummy.yml |
| @TestBundle/DataFixtures/ORM/one_another_dummy.yml |
Then the database should contain 11 "dummy" entities
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fidry\AliceBundleExtension\Tests\Functional\Bundle\TestBundle\Entity\Dummy:
dummy_{10}:
name: Dummy <current()>

0 comments on commit dfcb439

Please sign in to comment.