1. Go to this page and download the library: Download noj/fabrica 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/ */
$user = Fabrica::create(User::class);
// Check properties have been set
assertEquals('user123', $user->username);
assertEquals('Test', $user->firstName);
assertEquals('User', $user->lastName);
Fabrica::define(Comment::class, function () {
return [
'title' => 'A test comment',
'body' => 'This is a test',
'author' => Fabrica::create(User::class),
];
});
Fabrica::define(User::class, function () {
return [
'comments' => Fabrica::createMany(Comment::class, 3),
// or if you have a setter method, use the `*` suffix to call the method
// once for each element of the array
'@addComment*' => Fabrica::createMany(Comment::class, 3),
];
});
$user = Fabrica::create(User::class, function () {
return [
'comments.1.title' => 'Only the 2nd comment has this title'
];
});
assertEquals('A test comment', $user->comments[0]->title);
assertEquals('Only the 2nd comment has this title', $user->comments[1]->title);
$user = Fabrica::create(User::class, function () {
return [
'comments.*.title' => 'Each comment now has this title'
];
});
foreach ($user->comments as $comment) {
assertEquals('Each comment now has this title', $comment->title);
}
class MyTest extends TestCase
{
use \Noj\Fabrica\Adapter\Doctrine\PHPUnit\NeedsDatabase;
}
class MyTest extends TestCase
{
use \Noj\Fabrica\Adapter\Doctrine\PHPUnit\DatabaseAssertions;
}
public function test_it_creates_a_user()
{
(new UserCreator)->create('test');
self::assertDatabaseContainsEntity(User::class, ['username' => 'test'])'
}