定制 imanghafoori/eloquent-mockery 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

imanghafoori/eloquent-mockery

最新稳定版本:v1.0.40

Composer 安装命令:

composer require --dev imanghafoori/eloquent-mockery

包简介

Allows you to design your tests in an independent manner.

README 文档

README

Mock your eloquent queries without the repository pattern.

Required Laravel Version Required PHP Version tests Imports Software License

Why this package was invented?

  • It solves the problem of "slow tests" by removing the interactions with a real database.
  • It simplifies the process of writing and running tests since your tests will be "DB Independent".

Installation

You can install the package via Composer:

composer require imanghafoori/eloquent-mockery --dev dev-main

Usage:

First, you have to define a new connection in your config/database.php and set the driver to 'arrayDB'.

<?php

return [
  
   ...
  
  'connections' => [
     'my_test_connection' => [
         'driver' => 'arrayDB',
         'database' => '',
     ],
     
     ...
  ],
  ...
]

Then you can:

public function test_basic()
{
    config()->set('database.default', 'my_test_connection');

    # ::Arrange:: (Setup Sample Data)
    FakeDB::addRow('users', ['id' => 1, 'username' => 'faky', 'password' => '...']);
    FakeDB::addRow('users', ['id' => 1, 'username' => 'maky', 'password' => '...']);

    # ::Act:: (This query resides in your controller)
    $user = User::where('username', 'faky')->first();   # <=== This does NOT connect to a real DB.

    # ::Assert::
    $this->assert($user->id === 1);
    $this->assert($user->username === 'faky');
}

Mocking a create query:

public function test_basic()
{
    # In setUp:
    FakeDB::mockEloquentBuilder();

    # ::Act::
    $this->post('/create-url', ['some' => 'data' ])

    # ::Assert::
    $user = User::first();
    $this->assertEquals('iman', $user->username);

    # In tearDown
    FakeDB::dontMockEloquentBuilder();
}
  • For more examples take a look at the tests directory.

License

The MIT License (MIT). Please see License File for more information.

🙋 Contributing

If you find an issue or have a better way to do something, feel free to open an issue, or a pull request.

❗ Security

If you discover any security-related issues, please email imanghafoori1@gmail.com instead of using the issue tracker.

统计信息

  • 总下载量: 599.13k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 127
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 127
  • Watchers: 2
  • Forks: 18
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-21