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_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 */
$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());
}
class UserFactory extends Factory
{
public function defaultWithAddress()
{
return AddressFactory::new();
}
public function defaultHasCustomers()
{
return CustomerFactory::times(10);
}
}