PHP code example of maymeow / cakephp-testing

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

    

maymeow / cakephp-testing example snippets




namespace Identity\Database\Factories;

use Faker\Generator;
use MayMeow\Testing\Factories\ModelFactory;
use MayMeow\Testing\Factories\ModelFactoryInterface;

class UserFactory implements ModelFactoryInterface
{
    /**
     * @param array|null $data
     * @return \Cake\Datasource\EntityInterface|false
     */
    public function get(array $data = null)
    {
        $factory = new ModelFactory($data);
   
        return $factory->define('Identity.Users', function (Generator $faker) {
            return [
                // Your model data
            ];
        });
    }
}

// ...
eturn $factory->define('Identity.Users', function (Generator $faker) use ($api_key_plain) {
    return [
        'email' => $faker->email,
        'password' => (new \Cake\Auth\DefaultPasswordHasher())->hash('pa$$word'),
        // Belogs to delation can be added by calling another factory
        'role_id' => function () {
            return (new RoleFactory())->get()->id;
        },
        'api_key_plain' => $api_key_plain,
        'api_key' => (new \Cake\Auth\DefaultPasswordHasher())->hash($api_key_plain)
    ];
});
// ...

$this->user = (new UserFactory())->get();

$this->post = (new PostFactory())->get(['address_id' => $address->id]);

// EXAMPLE Create 10 posts for one user
$this->user = (new UserFactory())->get();

for ($i=1; $i<=10; $i++) {
    (new PostFactory())->get(['user_id' => $this->user->id]);
}