PHP code example of adamwathan / faktory

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

    

adamwathan / faktory example snippets


'providers' => array(
        //...
        'AdamWathan\Faktory\FaktoryServiceProvider'
    ),

'aliases' => array(
        //...
        'Faktory' => 'AdamWathan\Faktory\Facades\Faktory'
    ),

Faktory::define('User', function ($f) {
    $f->first_name = 'John';
    $f->last_name = 'Doe';
});

$faktory = new AdamWathan\Faktory\Faktory;

// app/bootstrap/testing.php

Faktory::define('Album', function ($f) {
    $f->name = 'Diary of a madman';
    $f->release_date = new DateTime('1981-11-07');
});

Faktory::define('Song', function ($f) {
    $f->name = 'Over the mountain';
    $f->length = 271;
});

// Returns an Album object with the default attribute values
$album = Faktory::build('Album');
$album->name;
// 'Diary of a madman'
$album->release_date;
// '1981-11-07'


// Create a basic instance and persist it to
// the database
$album = Faktory::create('Album');
$album->id
// 1

// Create an instance and override some properties
$album = Faktory::build('Album', ['name' => 'Bark at the moon']),
]);

$album->name;
// 'Bark at the moon'
$album->release_date;
// '1981-11-07'

// Create an instance and override some properties
$album = Faktory::build('Album', function ($album) {
    $album->name => 'Bark at the moon';
    $album->songs->quantity(4)->attributes(['length' => 267]);
});

$album->name;
// 'Bark at the moon'
$album->songs->count();
// 4
$album->songs[0]->length;
// 267

Faktory::define('Album', 'album_with_copies_sold', function ($f) {
    $f->name = 'Diary of a madman';
    $f->release_date = '1981-11-07';
    $f->copies_sold = 3200000;
});


$album = Faktory::build('album_with_copies_sold');

get_class($album);
// 'Album'
$album->name;
// 'Diary of a madman'
$album->release_date;
// '1981-11-07'
$album->copies_sold;
// 3200000

Faktory::define('User', 'basic_user', function ($f) {
    $f->first_name = 'John';
    $f->last_name = 'Doe';
    $f->is_admin = false;

    $f->define('admin', function ($f) {
        $f->is_admin = true;
    });
});


$user = Faktory::build('admin');

$user->first_name;
// 'John'
$user->last_name;
// 'Doe'
$user->is_admin;
// true

Faktory::define('User', function ($f) {
    $f->username = 'john.doe';

    $f->created_at = function () {
        return new DateTime;
    };
});


$user1 = Faktory::build('User');
$user1->created_at;
// '2014-04-22 14:10:05'

sleep(7);

$user2 = Faktory::build('User');
$user2->created_at;
// '2014-04-22 14:10:12'

Faktory::define('User', function ($f) {
    $f->first_name = 'John';
    $f->last_name = 'Doe';
    $f->email = function ($f) {
        return "{$f->first_name}.{$f->last_name}@example.com";
    };
});


$user = Faktory::build('User');
$user->first_name;
// 'John'
$user->last_name;
// 'Doe'
$user->email;
// '[email protected]'

$user = Faktory::build('User', ['first_name' => 'Bob']);
$user->first_name;
// 'Bob'
$user->last_name;
// 'Doe'
$user->email;
// '[email protected]'

Faktory::define('User', function ($f) {
    $f->first_name = 'John';
    $f->last_name = 'Doe';
    $f->email = function ($f, $i) {
        return "example{$i}@example.com";
    };
});


$user1 = Faktory::build('User');
$user1->email;
// '[email protected]'

$user2 = Faktory::build('User');
$user2->email;
// '[email protected]'

$faktory->define('Song', 'song_with_album', function ($f) {
    $f->name = 'Concatenation';
    $f->length = 257;
    $f->album = $f->belongsTo('album', 'album_id', [
        'name' => 'Chaosphere',
    ]);
});

$faktory->define('Album', 'album', function ($f) {
    $f->name = 'Destroy Erase Improve';
});


// Build the objects in memory without persisting to the database
$song = Faktory::build('song_with_album');
$song->album;
// object(Album)(
//    'name' => 'Destroy Erase Improve'
// )
$song->album_id;
// NULL


// Save the objects to the database and set up the correct
// foreign key associations
$song = Faktory::create('song_with_album');
$song->album_id;
// 1

Album::find(1);
// object(Album)(
//    'name' => 'Destroy Erase Improve'
// )

$faktory->define('User', 'user_with_profile', function ($f) {
    $f->username = 'johndoe';
    $f->password = 'top-secret';
    $f->profile = $f->hasOne('profile', 'user_id');
});

$faktory->define('Profile', 'profile', function ($f) {
    $f->email = '[email protected]';
});


// Build the objects in memory without persisting to the database
$user = Faktory::build('user_with_profile');
$user->profile;
// object(Profile)(
//    'email' => '[email protected]'
// )


// Save the objects to the database and set up the correct
// foreign key associations
$user = Faktory::create('user_with_profile');
$user->id;
// 1

Profile::first();
// object(Album)(
//    'user_id' => 1,
//    'email' => '[email protected]'
// )

$faktory->define('Album', 'album_with_songs', function ($f) {
    $f->name = 'Master of Puppets';
    $f->release_date = new DateTime('1986-02-24');
    $f->songs = $f->hasMany('song', 8, 'album_id');
});

$faktory->define('Song', 'song', function ($f) {
    $f->title = 'The Thing That Should Not Be';
    $f->length = 397;
});

// Define the factories
$faktory->define('Song', 'song_with_album', function ($f) {
    $f->name = 'Concatenation';
    $f->length = 257;
    $f->album = $f->belongsTo('album', 'album_id');
});
$faktory->define('Album', 'album', function ($f) {
    $f->name = 'Destroy Erase Improve';
    $f->release_date = new DateTime('1995-07-25');
});


// Build a song but override the album name
$song = Faktory::build('song_with_album', function ($song) {
    $song->album->name = 'Chaosphere';
});
$song->album;
// object(Album)(
//    'name' => 'Chaosphere'
// )

// Build a song but override a couple attributes at once
$song = Faktory::build('song_with_album', function ($song) {
    $song->album->attributes([
        'name' => 'Chaosphere',
        'release_date' => new DateTime('1998-11-10'),
    ]);
});
$song->album;
// object(Album)(
//    'name' => 'Chaosphere'
// )
$song->album->release_date;
// '1998-11-10'

// Create multiple instances
$albums = Faktory::buildMany('Album', 5);

// Create multiple instances with some overridden properties
$songs = Faktory::buildMany('Song', 5, [ 'length' => 100 ])
$songs[0]->length;
// 100
// ...
$songs[4]->length;
// 100

// Add a nested relationship where each item is different
$album = Faktory::build('Album', [
    'name' => 'Bark at the moon',
    'songs' => [
        Faktory::build('Song', [ 'length' => 143 ]),
        Faktory::build('Song', [ 'length' => 251 ]),
        Faktory::build('Song', [ 'length' => 167 ]),
        Faktory::build('Song', [ 'length' => 229 ]),
    ],
]);

// Add a nested relationship where each item shares the same
// properties
$album = Faktory::build('Album', [
    'name' => 'Bark at the moon',
    'songs' => Faktory::buildMany('Song', 5, [ 'length' => 100 ]
    ),
]);

// Add a nested relationship where each item is different,
// but using buildMany
$album = Faktory::build('Album', [
    'name' => 'Bark at the moon',
    'songs' => Faktory::buildMany('Song', 4, [
        'length' => [143, 251, 167, 229]
    ]),
]);

// Add a nested relationship using buildMany, but wrap
// it in a collection
$album = Faktory::build('Album', [
    'name' => 'Bark at the moon',
    'songs' => function () {
        return new Collection(Faktory::buildMany('Song', 4, [
            'length' => [143, 251, 167, 229]
        ]));
    }
]);