PHP code example of lukeraymonddowning / poser

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

    

lukeraymonddowning / poser example snippets


/** @test */
public function a_user_can_have_customers()
{
    UserFactory::times(20)
               ->hasAddress()
               ->withCustomers(CustomerFactory::times(20)->withBooks(5))();

    $this->assertCount(20 * 20 * 5, Book::all());
}

namespace Tests\Factories;

use Lukeraymonddowning\Poser\Factory;

class UserFactory extends Factory {

    // No need to write any code here
    
}



namespace App;

class User extends Authenticatable
{

    // ...a little while later

    public function customers()
    {
        return $this->hasMany(Customer::class);
    }
}


/** @test */
public function user_has_customers()
{
    $user = UserFactory::new()
        ->withCustomers(CustomerFactory::times(30))
        ->create();

    $this->assertCount(30, $user->customers);
}

class Customer extends Model
{

    public function books()
    {
        return $this->hasMany(Book::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }

}

/** @test */
public function user_has_customers()
{
    $user = UserFactory::new()
        ->withCustomers(
            CustomerFactory::times(30)->withBooks(BookFactory::times(5))
        )
        ->create();

    $this->assertCount(30, $user->customers);
    $this->assertCount(150, Book::all());
}

/** @test */
public function user_has_customers()
{
    $user = UserFactory::new()
        ->withCustomers(CustomerFactory::times(30))
        ->create();

    $this->assertCount(30, $user->customers);
}

/** @test */
public function user_has_customers()
{
    $user = UserFactory::new()
        ->withCustomers(30)
        ->create();

    $this->assertCount(30, $user->customers);
}

/** @test */
public function user_has_customers()
{
    $user = UserFactory::new()
        ->withCustomers(30, [
            "name" => "Joe Bloggs"
        ])
        ->create();

    $this->assertCount(30, $user->customers);
}

/** @test */
public function user_has_customers()
{
    $user = UserFactory::new()
        ->withCustomers(30, ["name" => "Joe Bloggs"], ["name" => "Jane Bloggs"], ["name" => "John Doe"])
        ->create();

    $this->assertCount(10, $user->customers->filter(fn($customer) => $customer->name == "Joe Bloggs"));
    $this->assertCount(10, $user->customers->filter(fn($customer) => $customer->name == "Jane Bloggs"));
    $this->assertCount(10, $user->customers->filter(fn($customer) => $customer->name == "John Doe"));
}

/** @test */
public function user_has_address()
{
    $user = UserFactory::new()
        ->withAddress()
        ->create();

    $this->assertNotEmpty($user->address);
}

/** @test */
public function user_has_address()
{
    $user = UserFactory::new()
        ->withAddress([
            "line_1" => "1 Test Street" 
        ])
        ->create();

    $this->assertNotEmpty($user->address);
}

/** @test */
public function user_has_address()
{
    $user = UserFactory::new()
        ->hasAddress([
            "line_1" => "1 Test Street" 
        ])
        ->create();

    $this->assertNotEmpty($user->address);
}

/** @test */
public function user_has_address()
{
    $user = UserFactory::new()
        ->hasAddress([
            "line_1" => "1 Test Street" 
        ]);

    $this->assertNotEmpty($user->address); // When we access the $address property, Poser automatically calls `create` for us.
}

/** @test */
public function users_with_addresses_can_have_customers_with_books() {
    UserFactory::times(10)
               ->hasAddress()
               ->withCustomers(CustomerFactory::times(20)->withBooks(5))();

    $this->assertCount(1000, Book::all());
    $this->assertCount(200, Customer::all());
    $this->assertCount(10, User::all());
    $this->assertCount(10, Address::all());
}

/** @test */
public function users_with_addresses_can_have_customers_with_books() {
    $user = factory(User::class)->times(10)->create();
    $user->each(function($user) {
        $user->address()->save(factory(Address::class)->make());
        $customers = factory(Customer::class)->times(20)->make();
        $user->customers()->saveMany($customers);
        $user->customers->each(function($customer) {
            $customer->books()->saveMany(factory(Book::class)->times(5)->make());
        });
    });

    $this->assertCount(1000, Book::all());
    $this->assertCount(200, Customer::all());
    $this->assertCount(10, User::all());
    $this->assertCount(10, Address::all());
}

/** @test */
public function a_user_can_have_many_roles() {
    $user = UserFactory::new()->withRoles(3)();

    $this->assertCount(3, $user->roles);
}

/** @test */
public function a_role_can_have_many_users() {
    $role = RoleFactory::new()->hasUsers(5)();

    $this->assertCount(5, $role->users);
}

/** @test */
public function a_user_can_have_many_roles() {
    $expiry = now();
    $user = UserFactory::new()->withRoles(RoleFactory::new()->withPivotAttributes([
        'expires_at' => $expiry
    ]))();

    $this->assertDatabaseHas('role_user', [
        'user_id' => $user->id,
        'expires_at' => $expiry
    ]);
}

/** @test */
public function a_user_can_have_many_roles() {
    $user = UserFactory::new()->withRoles(RoleFactory::times(3)->withPivotAttributes(
        ['expires_at' => now()->addDay()],
        ['expires_at' => now()->addDays(2)],
        ['expires_at' => now()->addDays(3)]
    ))();

    // ...assertions go here
}

$user = UserFactory::new()->withRoles(RoleFactory::new()->withPivotAttributes([
    'expires_at' => $expiry
])->make())(); // Don't do this

$user = UserFactory::new()->withRoles(RoleFactory::new()->withPivotAttributes([
    'expires_at' => $expiry
]))(); // Do this instead

/** @test */
public function a_user_can_have_many_comments() {
    $user = UserFactory::new()->withComments(10)();

    $this->assertCount(10, $user->comments);
}

/** @test */
public function a_customer_can_have_many_comments() {
    $customer = CustomerFactory::new()->withComments(25)->forUser(UserFactory::new())();

    $this->assertCount(25, $customer->comments);
}

/** @test */
public function customer_has_user()
{
    $customer = CustomerFactory::new()
        ->forUser(
            UserFactory::new()
        )->create();

    $this->assertNotEmpty($customer->user);
}

$factory->state(Customer::class, 'active', function (Faker $faker) {
    return [
        'active' => true,
    ];
});

$factory->state(Customer::class, 'inactive', function (Faker $faker) {
    return [
        'active' => false,
    ];
});


/** @test */
public function customer_is_active()
{
    $customer = CustomerFactory::new()
        ->state('active')
        ->create();

    $this->assertTrue($customer->active);
}

/** @test */
public function customer_is_active()
{
    $customer = CustomerFactory::new()
        ->as('active')
        ->create();

    $this->assertTrue($customer->active);
}

$customer = CustomerFactory::new()
    ->states('state1', 'state2', 'etc')
    ->create();


CompanyFactory::new()
    ->afterCreating(\App\Company $company) {
        $company->setMainUser(UserFactory::new()->forCompany($company)->create());
    })->create();

CompanyFactory::times(3)
    ->afterCreating(\App\Company $company) {
        $company->setMainUser(UserFactory::new()->forCompany($company)->create());
    })->create();

class CompanyFactory extends Factory {
    public function withMainUser()
    {
        return $this->afterCreating(function(Company $company) {
            $company->setMainUser(UserFactory::new()->forCompany($company)->create());
        });
    }
}

CompanyFactory::times(3)
    ->withMainUser()
    ->create();

/** @test */

$user = UserFactory::new()->withCustomers(CustomerFactory::times(10)->afterCreating(function($customer, $user) {
    // Perform an action to the newly created (and linked) customer model
}))->create();

public function user_has_customers()
{
    $user = UserFactory::new()
        ->withCustomers(
            CustomerFactory::times(30)->withBooks(BookFactory::times(5))
        )();

    $this->assertCount(30, $user->customers);
    $this->assertCount(150, Book::all());
}

$users = UserFactory::times(3)->create(["name" => "Joe"], ["name" => "Jane"], ["name" => "Jimmy"]);

UserFactory::new()->withCustomers(3, ['name' => "Joe"], ["name" => "Jane"], ["name" => "Jimmy"])();

$users = UserFactory::times(10)->create(["name" => "Joe"], ["name" => "Jane"]);

class CustomerFactory extends Factory
{

    public function defaultForUser()
    {
        return UserFactory::new()->withAttributes(["name" => "Joe Bloggs"]);
    }

}

CustomerFactory::new()->create();

CustomerFactory::new()->forUser(UserFactory::new()->withAttributes(["name" => "John Doe"]))->create();

class UserFactory extends Factory
{

    public function defaultWithAddress() 
    {
        return AddressFactory::new();
    }

    public function defaultHasCustomers()
    {
        return CustomerFactory::times(10);
    }

}

UserFactory::new()->withoutDefaults()->create();

UserFactory::new()->withoutDefaults('customers')->create();

/** @test */
public function a_user_can_have_customers()
{
    UserFactory::withCustomers(5)->assertCount(5, 'customers')();
}

/** @test */
public function a_user_can_have_customers()
{
    UserFactory::assertEquals('John Doe', 'name')(['name' => 'John Doe']);
}

/** @test */
public function a_user_can_have_customers()
{
    UserFactory::assertEquals('John', fn($user) => $user->locateFirstName())(['name' => 'John Doe']);
}

/** @test */
public function a_user_can_have_customers()
{
    UserFactory::times(5)->assertCount(5, fn($users) => $users)();
}

/** @test */
public function a_user_can_have_customers()
{
    UserFactory::times(5)->withCustomers(10)->assertCount(10, fn(User $user) => $user->customers)();
}

UserFactory::new()->withClients(CustomerFactory::times(10))();

$factory->define(Customer::class, function (Faker $faker) {
    return [
        'name' => $faker->name,
        'user_id' => function() {
            return factory(User::class)->create()->id;
        }
    ];
});

$factory->define(Customer::class, function (Faker $faker) {
    return [
        'name' => $faker->name
    ];
});

class CustomerFactory extends Factory {

    public function defaultForUser()
    {
        return UserFactory::new();
    }

}

// Without Poser

/** @test */
function a_user_can_find_friends_with_all_achievements()
{
    $user = factory(User::class)->create();
    $achievements = factory(Achievement::class)->times(10)->create();

    $friendsWithAchievements = factory(User::class)->times(15)->create();
    $friendsWithAchievements->each(
        function ($friend) {
            $friend->achievements()->saveMany($achievements);
        }
    );

    $user->friends()->saveMany($friendsWithAchievements);

    $friendsWithoutAchievements = factory(User::class)->times(20)->create();

    $user->friends()->saveMany($friendsWithoutAchievements);

    $this->assertCount(15, $user->friendsWithAllAchievements);
}

// With Poser

/** @test */
function a_user_can_find_friends_with_all_achievements()
{
    $achievements = AchievementFactory::times(10)();

    UserFactory::withFriends(UserFactory::times(15)->withAchievements($achievements))
               ->withFriends(UserFactory::times(20))
               ->assertCount(15, 'friendsWithAllAchievements')();
}

// Without Poser

/** @test */
function a_user_may_have_customers()
{
    $userWithCustomers = factory(User::class)->create();
    $userWithCustomers->customers()->saveMany(factory(Customer::class)->times(30)->make());

    $userWithoutCustomers = factory(User::class)->create();

    $this->assertCount(30, $userWithCustomers->customers);
    $this->assertTrue($userWithoutCustomers->customers->isEmpty());
}

// With Poser

/** @test */
function a_user_may_have_customers()
{
    UserFactory::withCustomers(30)->assertCount(30, 'customers')();
    UserFactory::assertTrue(fn($user) => $user->customers->isEmpty())();
}