PHP code example of adamwathan / facktory
1. Go to this page and download the library: Download adamwathan/facktory 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 / facktory example snippets
'providers' => array(
//...
'AdamWathan\Facktory\FacktoryServiceProvider'
),
'aliases' => array(
//...
'Facktory' => 'AdamWathan\Facktory\Facades\Facktory'
),
Facktory::define('User', function($f) {
$f->first_name = 'John';
$f->last_name = 'Doe';
});
$facktory = new AdamWathan\Facktory\Facktory;
// app/bootstrap/testing.php
Facktory::define('Album', function($f) {
$f->name = 'Diary of a madman';
$f->release_date = new DateTime('1981-11-07');
});
Facktory::define('Song', function($f) {
$f->name = 'Over the mountain';
$f->length = 271;
});
// Returns an Album object with the default attribute values
$album = Facktory::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 = Facktory::create('Album');
$album->id
// 1
// Create an instance and override some properties
$album = Facktory::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 = Facktory::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
Facktory::define(['album_with_copies_sold', 'Album'], function($f) {
$f->name = 'Diary of a madman';
$f->release_date = '1981-11-07';
$f->copies_sold = 3200000;
});
$album = Facktory::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
Facktory::define(['basic_user', '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 = Facktory::build('admin');
$user->first_name;
// 'John'
$user->last_name;
// 'Doe'
$user->is_admin;
// true
Facktory::define('User', function($f) {
$f->username = 'john.doe';
$f->created_at = function() {
return new DateTime;
};
});
$user1 = Facktory::build('User');
$user1->created_at;
// '2014-04-22 14:10:05'
sleep(7);
$user2 = Facktory::build('User');
$user2->created_at;
// '2014-04-22 14:10:12'
Facktory::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 = Facktory::build('User');
$user->first_name;
// 'John'
$user->last_name;
// 'Doe'
$user->email;
// '[email protected] '
$user = Facktory::build('User', ['first_name' => 'Bob']);
$user->first_name;
// 'Bob'
$user->last_name;
// 'Doe'
$user->email;
// '[email protected] '
Facktory::define('User', function($f) {
$f->first_name = 'John';
$f->last_name = 'Doe';
$f->email = function($f, $i) {
return "example{$i}@example.com";
};
});
$user1 = Facktory::build('User');
$user1->email;
// '[email protected] '
$user2 = Facktory::build('User');
$user2->email;
// '[email protected] '
$facktory->define(['song_with_album', 'Song'], function($f) {
$f->name = 'Concatenation';
$f->length = 257;
$f->album = $f->belongsTo('album', 'album_id', [
'name' => 'Chaosphere',
]);
});
$facktory->define(['album', 'Album'], function($f) {
$f->name = 'Destroy Erase Improve';
});
// Build the objects in memory without persisting to the database
$song = Facktory::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 = Facktory::create('song_with_album');
$song->album_id;
// 1
Album::find(1);
// object(Album)(
// 'name' => 'Destroy Erase Improve'
// )
$facktory->define(['user_with_profile', 'User'], function($f) {
$f->username = 'johndoe';
$f->password = 'top-secret';
$f->profile = $f->hasOne('profile', 'user_id');
});
$facktory->define(['profile', 'Profile'], function($f) {
$f->email = '[email protected] ';
});
// Build the objects in memory without persisting to the database
$user = Facktory::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 = Facktory::create('user_with_profile');
$user->id;
// 1
Profile::first();
// object(Album)(
// 'user_id' => 1,
// 'email' => '[email protected] '
// )
$facktory->define(['album_with_songs', 'Album'], function($f) {
$f->name = 'Master of Puppets';
$f->release_date = new DateTime('1986-02-24');
$f->songs = $f->hasMany('song', 'album_id', 8);
});
$facktory->define(['song', 'Song'], function($f) {
$f->title = 'The Thing That Should Not Be';
$f->length = 397;
});
// Define the factories
$facktory->define(['song_with_album', 'Song'], function($f) {
$f->name = 'Concatenation';
$f->length = 257;
$f->album = $f->belongsTo('album', 'album_id');
});
$facktory->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 = Facktory::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 = Facktory::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 = Facktory::buildList('Album', 5);
// Create multiple instances with some overridden properties
$songs = Facktory::buildList('Song', 5, [ 'length' => 100 ])
$songs[0]->length;
// 100
// ...
$songs[4]->length;
// 100
// Add a nested relationship where each item is different
$album = Facktory::build('Album', [
'name' => 'Bark at the moon',
'songs' => [
Facktory::build('Song', [ 'length' => 143 ]),
Facktory::build('Song', [ 'length' => 251 ]),
Facktory::build('Song', [ 'length' => 167 ]),
Facktory::build('Song', [ 'length' => 229 ]),
],
]);
// Add a nested relationship where each item shares the same
// properties
$album = Facktory::build('Album', [
'name' => 'Bark at the moon',
'songs' => Facktory::buildList('Song', 5, [ 'length' => 100 ]
),
]);
// Add a nested relationship where each item is different,
// but using buildList
$album = Facktory::build('Album', [
'name' => 'Bark at the moon',
'songs' => Facktory::buildList('Song', 4, [
'length' => [143, 251, 167, 229]
]),
]);
// Add a nested relationship using buildList, but wrap
// it in a collection
$album = Facktory::build('Album', [
'name' => 'Bark at the moon',
'songs' => function() {
return new Collection(Facktory::buildList('Song', 4, [
'length' => [143, 251, 167, 229]
]));
}
]);