Skip to content
This repository has been archived by the owner on Jun 8, 2020. It is now read-only.

Commit

Permalink
Add 'there are <number> <name> with' step
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentchalamon committed Aug 3, 2018
1 parent 2664974 commit b6b64da
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ FixturesContext provides the following steps:
* `there is an <name>`
* `there are <nb> <name>`
* `there are <name>`
* `there are <nb> <name> with:`

ApiContext provides the following steps:
* `I get a list of <name>`
Expand Down
29 changes: 29 additions & 0 deletions src/Context/FixturesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,35 @@ public function theFollowing($name, TableNode $table)
}
}

/**
* @Given /^there are (?P<number>\d+) (?P<name>[\w\-]+) with:$/
*/
public function thereAreWith($number, $name, TableNode $table)
{
$reflectionClass = $this->helper->getReflectionClass($name);
$em = $this->registry->getManagerForClass($reflectionClass->name);
$rows = $table->getRows();
$headers = array_shift($rows);
$row = array_shift($rows);
/** @var ClassMetadataInfo $classMetadata */
$classMetadata = $this->registry->getManagerForClass($reflectionClass->name)->getClassMetadata($reflectionClass->name);
if (array_intersect($headers, $classMetadata->getIdentifierFieldNames())) {
$idGenerator = $classMetadata->idGenerator;
$classMetadata->setIdGenerator(new AssignedGenerator());
$generatorType = $classMetadata->generatorType;
$classMetadata->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_NONE);
}
for ($i = 0; $i < $number; ++$i) {
$em->persist($this->populator->getObject($reflectionClass, array_combine($headers, $row)));
}
$em->flush();
$em->clear();
if (isset($idGenerator) && isset($generatorType)) {
$classMetadata->setIdGenerator($idGenerator);
$classMetadata->setIdGeneratorType($generatorType);
}
}

/**
* @Given /^there (?:is|are) (?P<number>\d+) (?P<name>[\w\-]+)$/
*/
Expand Down

0 comments on commit b6b64da

Please sign in to comment.