PHP code example of treast / behat-fixture-bridge

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

    

treast / behat-fixture-bridge example snippets


return [
    // ...
    Treast\BehatFixtureBridge\BehatFixtureBridgeBundle::class => ['dev' => true, 'test' => true],
];

namespace App\Factory;

use Treast\BehatFixtureBridge\Attribute\AsFixtureBridge;
use App\Entity\Book;
use Zenstruck\Foundry\Persistence\PersistentObjectFactory;

#[AsFixtureBridge(name: 'book', identifier: 'id', properties: ['title', 'slug'])]
final class BookFactory extends PersistentObjectFactory
{
    public static function class(): string
    {
        return Book::class;
    }

    protected function defaults(): array
    {
        return [
            'title' => self::faker()->sentence(3),
            'slug' => self::faker()->slug(),
        ];
    }
}

#[AsFixtureBridge(
    name: 'book',
    identifier: 'id',
    if: 'isFeatured',
    properties: ['title', 'slug', 'year' => 'releaseYear'],
)]

#[AsFixtureBridge(name: 'book')]         // → "book:42"
#[AsFixtureBridge(name: 'art:book')]  // → "art:book:42"

#[AsFixtureBridge(name: 'book', identifier: 'id')]    // → "book:42"
#[AsFixtureBridge(name: 'book', identifier: 'slug')]  // → "book:les-miserables"

#[AsFixtureBridge(name: 'book', identifier: '@buildSlug')]
public static function buildSlug(Book $book): string
{
    return sprintf('%d-%s', $book->getYear(), $book->getSlug());
}
// → "book:2024-les-miserables"

#[AsFixtureBridge(name: 'order', identifier: ['year', 'number'])]
// → "order:2024-0001"

#[AsFixtureBridge(name: 'art:book', identifier: [])]
// → "art:book"

#[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);
    }
}

#[AsFixtureBridge(name: 'book', identifier: 'id', properties: ['title', 'slug'])]
bash
composer dump-autoload
php bin/console cache:clear --env=test