PHP code example of laracasts / testdummy

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

    

laracasts / testdummy example snippets


use Laracasts\TestDummy\Factory;

$post = Factory::build('Post');

use Laracasts\TestDummy\Factory;

$post = Factory::build('Post', ['title' => 'Override Title']);

$post = Factory::attributesFor('Post');

use Laracasts\TestDummy\Factory;

$song = Factory::create('Song');

use Laracasts\TestDummy\Factory;

Factory::times(3)->create('Comment');

$factory('Album', [
    'name' => 'Rock or Bust',
    'artist' => 'AC/DC'
]);

use Laracasts\TestDummy\Factory;

$album = Factory::create('Album');

$factory('Comment', [
    'body' => $faker->sentence
]);

$factory('Comment', [
    'user_id' => 'factory:User',
    'body' => $faker->sentence
]);

$factory('App\Song', [
    'album_id' => 'factory:App\Album',
    'name' => $faker->sentence
]);

$factory('App\Album', [
    'artist_id' => 'factory:App\Artist',
    'name' => $faker->word
]);

$factory('App\Artist', [
    'name' => $faker->word
]);

use Laracasts\TestDummy\Factory;

$song = Factory::create('App\Song');

Factory::create('App\User', ['role' => 'admin']);

// A generic factory for users...

$factory('App\User', [
    'username' => $faker->username,
    'password' => $faker->password,
    'role'     => 'member'
]);

// And a custom one for administrators

$factory('App\User', 'admin_user', [
    'username' => $faker->username,
    'password' => $faker->password,
    'role'     => 'admin'
]);

use Laracasts\TestDummy\Factory;

$adminUser = Factory::create('admin_user');

$factory('App\Artist', function($faker) {
    $name = sprintf('Some Band Named %s', $faker->word);
    
    return [
        'name' => $name
    ];
});

public function setUp()
{
    parent::setUp();

    Artisan::call('migrate');
}


use Laracasts\TestDummy\DbTestCase;

class ExampleTest extends DbTestCase {

    /** @test */
    function it_does_something()
    {
        // Before each test, your database will be rolled back
    }
}


use Laracasts\TestDummy\Factory;

$comment = Factory::create('Comment');

// create three songs, and explicitly set the length
Factory::times(3)->create('Song', ['length' => 200]);

$album = Album::first(); // this will be created once automatically.

$this->assertEquals(600, $album->getTotalLength());
bash
array(4) {
  ["title"]=>
  string(21) "The Title of the Post"
  ["author_id"]=>
  string(1) "5"
  ["body"]=>
  string(226) "Iusto qui optio et iste. Cumque aliquid et omnis enim. Nesciunt ad esse a reiciendis expedita quidem veritatis. Nostrum repellendus reiciendis distinctio amet sapiente. Eum molestias a recusandae modi aut et adipisci corrupti."
  ["publish_date"]=>
  string(19) "2014-03-02 11:05:48"
}