bogdanghervan/laravel-dummy-observer
Composer 安装命令:
composer require --dev bogdanghervan/laravel-dummy-observer
包简介
Mock Eloquent model save and make assertions on saved data
README 文档
README
A purpose-built model observer that can be registered with an Eloquent model to intercept all attempted saves and perform assertions on the data. The data being saved never reaches the database.
Installation
Requirements
- PHP ≥ 7.3
- PHPUnit ≥ 9.0
- Laravel Eloquent ≥ 5.3
Installation
Install it via Composer:
composer require --dev bogdanghervan/laravel-dummy-observer
Usage
Let's assume we'd like to test a method named landed on a model called Flight. This method would update the flight's status by invoking save internally.
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; class Flight extends Model { protected $fillable = [ 'departure', 'destination', 'status' ]; public function landed() { $this->status = 'landed'; $this->save(); } }
This is how a unit test could look like:
<?php namespace Tests\Unit; use Tests\TestCase; use App\Models\Flight; use BogdanGhervan\DummyObserver; class FlightTest extends TestCase { protected function setUp(): void { parent::setUp(); // Prevent model from being saved Flight::observe(DummyObserver::class); } protected function tearDown(): void { DummyObserver::clear(); } public function testLanded(): void { $flight = new Flight([ 'departure' => 'Bucharest', 'destination' => 'New York', ]); $flight->landed(); DummyObserver::assertSavedAttributes([ 'departure' => 'Bucharest', 'destination' => 'New York', 'status' => 'landed' ]); } }
Available assertions
assertSavedAttributes($attributes)
Verify that expected attributes are saved.
Flight::observe(DummyObserver::class); $flight = Flight::create([ 'passenger' => 'John Smith' ]); DummyObserver::assertSavedAttributes([ 'passenger' => 'John Smith' ]);
It's possible to verify that only a relevant subset of the attributes was saved.
Flight::observe(DummyObserver::class); $flight = Flight::create([ 'passenger' => 'John Smith', 'departure' => 'Bucharest', 'destination' => 'New York', 'status' => 'boarded' ]); DummyObserver::assertSavedAttributes([ 'passenger' => 'John Smith', 'status' => 'boarded' ]);
We can make multiple assertions if consecutive saves are being made in the code being tested. Just make sure to specify them in the same order.
Flight::observe(DummyObserver::class); $flight = Flight::create([ 'departure' => 'Bucharest', 'destination' => 'New York' ]); $flight->update([ 'status' => 'boarded', 'gate' => 'A1' ]); DummyObserver::assertSavedAttributes([ 'destination' => 'New York' ]); DummyObserver::assertSavedAttributes([ 'status' => 'boarded', 'gate' => 'A1' ]);
assertSavedTimes($times = 1)
Make an assertion on the number of times a model has been saved.
Flight::observe(DummyObserver::class); $flight = Flight::create([ 'departure' => 'Bucharest', 'destination' => 'New York' ]); $flight->update([ 'status' => 'boarded', 'gate' => 'A1' ]); DummyObserver::assertSavedTimes(2);
assertNothingSaved()
Make an assertion the model hasn't been saved.
Flight::observe(DummyObserver::class); $flight = new Flight(); DummyObserver::assertNothingSaved();
clear()
Make sure to clear any captured data after every test. A good place to do this from is in the tearDown method:
protected function tearDown(): void { DummyObserver::clear(); }
Limitations
When working with multiple models, it is not possible to assert a save against the model where the save originated. See issue #1 for more details.
Support
Has this just helped you in a pinch when you tried to mock the Eloquent save method and nothing was working? Consider leaving me a note and buying me a coffee by clicking the button below.
Have you found a problem? Submit an issue
I myself have been inspired by the work done by @timacdonald on timacdonald/log-fake whom I'd like to thank!
Contributing
Pull requests are welcome. All contributions should follow the PSR-2 coding standard and should be accompanied by passing tests.
License
This package is available under the MIT License.
bogdanghervan/laravel-dummy-observer 适用场景与选型建议
bogdanghervan/laravel-dummy-observer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 691 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 02 月 03 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「testing」 「phpunit」 「mock」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 bogdanghervan/laravel-dummy-observer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bogdanghervan/laravel-dummy-observer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 bogdanghervan/laravel-dummy-observer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
HiDev plugin for PHPUnit
Testing Suite For Lumen like Laravel does.
A cli tool which generates unit tests.
The PHP SDK for Checkmango
Mock PSR-18 HTTP client
统计信息
- 总下载量: 691
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 2
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-02-03