PHP code example of hidayetov / auto-testify

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

    

hidayetov / auto-testify example snippets


namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = ['name', 'email', 'password'];
    protected $unique = ['email']; // Optional if not using database detection
}

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Tests\TestCase;

class UserTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_be_created_with_fillable_fields()
    {
        $attributes = [
            'name' => 'Test Name',
            'email' => '[email protected]',
            'password' => 'password',
        ];
        $user = \App\Models\User::create($attributes);
        $this->assertEquals($attributes['name'], $user->name);
        $this->assertEquals($attributes['email'], $user->email);
        $this->assertTrue(Hash::check('password', $user->password));
    }

    public function test_user_can_be_retrieved()
    {
        $attributes = [
            'name' => 'Test Name',
            'email' => '[email protected]',
            'password' => 'password',
        ];
        $user = \App\Models\User::create($attributes);
        $retrieved = \App\Models\User::find($user->id);
        $this->assertNotNull($retrieved);
        $this->assertEquals($attributes['name'], $retrieved->name);
        $this->assertEquals($attributes['email'], $retrieved->email);
        $this->assertTrue(Hash::check('password', $retrieved->password));
    }

    public function test_user_can_be_updated()
    {
        $attributes = [
            'name' => 'Test Name',
            'email' => '[email protected]',
            'password' => 'password',
        ];
        $user = \App\Models\User::create($attributes);
        $updatedAttributes = [
            'name' => 'Updated Test Name',
            'email' => '[email protected]',
            'password' => 'newpassword',
        ];
        $user->update($updatedAttributes);
        $this->assertEquals($updatedAttributes['name'], $user->name);
        $this->assertEquals($updatedAttributes['email'], $user->email);
        $this->assertTrue(Hash::check('newpassword', $user->password));
    }

    public function test_user_can_be_deleted()
    {
        $attributes = [
            'name' => 'Test Name',
            'email' => '[email protected]',
            'password' => 'password',
        ];
        $user = \App\Models\User::create($attributes);
        $user->delete();
        $this->assertDatabaseMissing('users', ['id' => $user->id]);
    }

    public function test_user_email_must_be_unique()
    {
        $attributes = [
            'name' => 'Test Name',
            'email' => '[email protected]',
            'password' => 'password',
        ];
        \App\Models\User::create($attributes);
        $this->assertDatabaseCount('users', 1);
        $this->expectException(\Illuminate\Database\QueryException::class);
        \App\Models\User::create($attributes);
    }
}
bash
php artisan make:test-model ModelName
bash
php artisan make:test-model User
bash
php artisan test