1. Go to this page and download the library: Download treast/behat-fixture-bridge library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
#[AsFixtureBridge(name: 'book', identifier: 'id', if: 'isFeatured')]
public static function isFeatured(Book $book): bool
{
return $book->isFeatured();
}
// List form: the property name is both the source and the exposed name
'properties' => ['title', 'slug']
// Map form: expose under a different name
'properties' => ['year' => 'releaseYear', 'author' => '@authorName']
public static function authorName(Book $book): string
{
return $book->getAuthor()?->getFullName() ?? '';
}
#[AsFixtureBridge(name: 'book', identifier: 'id', properties: ['author' => '@authorName'])]
public static function authorName(Book $book): string
{
return $book->getAuthor()?->getFullName() ?? '';
}
use Treast\BehatFixtureBridge\Registry\FixtureRegistryInterface;
final class MyContext implements Context
{
public function __construct(private FixtureRegistryInterface $registry) {}
/**
* @Then /^the fixture "([^"]+)" should exist$/
*/
public function theFixtureShouldExist(string $name): void
{
Assert::assertTrue($this->registry->has($name));
}
}
namespace App\DataFixtures;
use Treast\BehatFixtureBridge\Factory\FixtureBridgeAwareInterface;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
final class AppFixtures extends Fixture
{
use FixtureBridgeAwareInterface;
public function load(ObjectManager $manager): void
{
$book = new Book();
$book->setTitle('Les Misérables');
$book->setSlug('les-miserables');
$manager->persist($book);
$manager->flush();
// Register under two names for the same entity.
$this->registerFixture($book, 'book:' . $book->getId());
$this->registerFixture($book, 'art:' . $book->getId());
}
}
// src/Factory/ArtFactory.php
abstract class ArtFactory extends PersistentObjectFactory
{
protected function defaults(): array
{
return [
'title' => self::faker()->sentence(3),
'slug' => self::faker()->unique()->slug(4),
];
}
}
// src/Factory/BookFactory.php
#[AsFixtureBridge(name: 'book', identifier: 'id', properties: ['title'])]
#[AsFixtureBridge(name: 'art', identifier: 'id', properties: ['title'])]
final class BookFactory extends ArtFactory
{
public static function class(): string
{
return Book::class;
}
protected function defaults(): array
{
return array_merge(parent::defaults(), [
'pages' => self::faker()->numberBetween(60, 180),
]);
}
}
// src/DataFixtures/AppFixtures.php
final class AppFixtures extends Fixture
{
public function load(ObjectManager $manager): void
{
// Named fixtures: referenced in scenarios
BookFactory::createOne(['title' => 'Les Misérables', 'slug' => 'les-miserables']);
BookFactory::createOne(['title' => 'Germinal', 'slug' => 'germinal']);
// Volume: not referenced by name, only used to fill lists
BookFactory::createMany(150);
}
}