Download the PHP package bogdanghervan/laravel-dummy-observer without Composer
On this page you can find all versions of the php package bogdanghervan/laravel-dummy-observer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package laravel-dummy-observer
Laravel Dummy Observer
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:
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.
This is how a unit test could look like:
Available assertions
assertSavedAttributes($attributes)
Verify that expected attributes are saved.
It's possible to verify that only a relevant subset of the attributes was saved.
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.
assertSavedTimes($times = 1)
Make an assertion on the number of times a model has been saved.
assertNothingSaved()
Make an assertion the model hasn't been saved.
clear()
Make sure to clear any captured data after every test. A good place to do this from is in the tearDown
method:
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.
All versions of laravel-dummy-observer with dependencies
phpunit/phpunit Version ^9.5
illuminate/database Version >=5.3
dms/phpunit-arraysubset-asserts Version ^0.2.1