PHP code example of tijmen-wierenga / bogus

1. Go to this page and download the library: Download tijmen-wierenga/bogus 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/ */

    

tijmen-wierenga / bogus example snippets


$fixtures = new Fixtures(new UserFactory());
$users = $fixtures->create(User::class, ['city' => 'Amsterdam'], 5); // Generates 5 users from Amsterdam

$user = $fixtures->create(User::class, [
    "name" => "Tijmen"
]); // Will create a user with Tijmen as a name and a random email address

$users = $fixtures->create(User::class, [], 3); // Will create 3 random users
 php
use TijmenWierenga\Bogus\Factory\AbstractFactory;

final class UserFactory extends AbstractFactory
{
    /**
     * Whether or not the Factory creates the entity passed as an argument
     */
    public function creates(string $entityClassName): bool
    {
        return $entityClassName === User::class;
    }

    /**
     * An iterable list of key => value pairs with default values. The result of the merged attributes
     * is passed to the 'create' method.
     */
    protected function attributes(): iterable
    {
        $factory = \Faker\Factory::create();

        return [
            "name" => $factory->firstName,
            "email" => $factory->email
        ];
    }

    /**
     * Creates the actual entity based on the merged attributes
     */
    protected function create(iterable $attributes): object
    {
        return new User($attributes["name"], $attributes["email"]);
    }
}

class User
{
    public function __construct(string $name, string $email)
    {
        $this->name = $name;
        $this->email = $email;
    }
}