-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[5.3] Add support for testing eloquent model events #14374
Conversation
Can you give a quick example of what was broken before this PR so I can do a quick before / after test? |
Please see:
Essentially |
I think it would be a better API if I could say:
|
Oh, I like that idea! I'll update the PR in the next hour or two. |
Updated. How does that look? |
One thing I did change was I made |
That will break observers for people using expectsEvents. That's why I kept them as separate methods. See @GrahamCampbell's comments in the previous PR: #12904 |
Hmm, it's a little hard for me to follow what Graham means there. Do you have a simple example you could give me of how it would break things? |
Yeah, I'll write one up later today. |
Any testing code that is mocking events but requires observers to fire would break (see the test case below). This could be one of the behavioral changes for 5.3 if you feel that the change in functionality is intended. If it isn't an intended change, I already have a branch ready to open a PR with the test case to ensure this does not regress in the future. /** @test */
public function observers_do_fire_when_mocking_normal_events()
{
$this->withoutEvents();
EloquentTestModel::observe(EloquentTestModelSucceedingObserver::class);
EloquentTestModel::create(['field' => 1])->delete();
$this->assertEquals(4, EloquentTestModelSucceedingObserver::$eventsFired);
EloquentTestModelSucceedingObserver::reset();
} class EloquentTestModelSucceedingObserver {
public static $eventsFired = 0;
public function saved() { static::$eventsFired += 1; }
public function saving() { static::$eventsFired += 1; }
public function deleted() { static::$eventsFired += 1; }
public function deleting() { static::$eventsFired += 1; }
public static function reset() { static::$eventsFired = 0; }
} |
Take 2. I've added a separate method for mocking model events so it shouldn't completely break code using observers (sorry!).
Notes:
Illuminate\Foundation\Testing\TestCase
to treat it as an integration test to be sure it worked within the context of using that class.