hidayetov/auto-testify 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hidayetov/auto-testify

Composer 安装命令:

composer require hidayetov/auto-testify

包简介

Automatic test generator for Laravel models

README 文档

README

AutoTestify is a powerful Laravel package designed to automatically generate comprehensive test files for your Eloquent models. It simplifies the testing process by creating CRUD (Create, Read, Update, Delete) and uniqueness tests, saving you time and ensuring your models are thoroughly tested.

Features

  • Automatic CRUD Tests: Generates tests for creating, retrieving, updating, and deleting model instances.
  • Uniqueness Testing: Detects unique fields from the database schema or model configuration and generates corresponding tests.
  • Database Flexibility: Works with or without a database connection:
    • If migrated and doctrine/dbal is installed, detects unique fields from the database.
    • Otherwise, uses the $unique property defined in your model.
  • Resilient Design: Skips database-dependent tests gracefully if no connection is available.
  • Customizable: Easily extendable for additional test cases or model-specific logic.

Requirements

  • PHP: 8.1 or higher
  • Laravel: 11.0 or higher
  • Optional: doctrine/dbal (for database schema detection)

Installation

Step 1: Install via Composer

Add the package to your Laravel project:

composer require hidayetov/auto-testify

Step 2: (Optional) Enable Database Schema Detection

If you want AutoTestify to detect unique fields from your database schema, install doctrine/dbal:

composer require doctrine/dbal

Without this, the package will rely on the $unique property in your models.

Usage

Generating a Test File

Run the following Artisan command to generate a test file for a specific model:

php artisan make:test-model ModelName

For example, to generate tests for a User model:

php artisan make:test-model User

This will create tests/Feature/UserTest.php with CRUD and uniqueness tests.

Example Model Configuration

Define $fillable and (optionally) $unique properties in your model:

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
}

Generated Tests

For the User model above, the generated UserTest.php might look like this:

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' => 'test@example.com',
            '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' => 'test@example.com',
            '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' => 'test@example.com',
            'password' => 'password',
        ];
        $user = \App\Models\User::create($attributes);
        $updatedAttributes = [
            'name' => 'Updated Test Name',
            'email' => 'test@example.com',
            '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' => 'test@example.com',
            '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' => 'test@example.com',
            'password' => 'password',
        ];
        \App\Models\User::create($attributes);
        $this->assertDatabaseCount('users', 1);
        $this->expectException(\Illuminate\Database\QueryException::class);
        \App\Models\User::create($attributes);
    }
}

Configuration

Model Setup

  • $fillable: Required to specify fields that can be mass-assigned.
  • $unique: Optional; define unique fields if not relying on database schema detection.

Database Detection

Ensure your database is migrated (php artisan migrate) and doctrine/dbal is installed for automatic unique field detection.

Running Tests

After generating the test files, run your tests with:

php artisan test

Example Output

PASS  Tests\Feature\UserTest
✓ user can be created with fillable fields
✓ user can be retrieved
✓ user can be updated
✓ user can be deleted
✓ user email must be unique

Troubleshooting

  • "No connection could be made" Error: Ensure your .env file is configured correctly and your database server is running.
    • Example .env for MySQL:
      DB_CONNECTION=mysql
      DB_HOST=127.0.0.1
      DB_PORT=3306
      DB_DATABASE=your_database
      DB_USERNAME=root
      DB_PASSWORD=
      
    • Or use SQLite:
      DB_CONNECTION=sqlite
      DB_DATABASE=/path/to/database.sqlite
      
  • Unique Tests Not Generated: Define $unique in your model or install doctrine/dbal and migrate your database.

License

This package is open-sourced under the MIT License.

Author

  • Hidayet Hidayetov - GitHub
  • Special thanks to contributors and collaborators!

Happy testing with AutoTestify! 🚀

hidayetov/auto-testify 适用场景与选型建议

hidayetov/auto-testify 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 hidayetov/auto-testify 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hidayetov/auto-testify 我们能提供哪些服务?
定制开发 / 二次开发

基于 hidayetov/auto-testify 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-10