Skip to content
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

Tests setUp() resets Model Events #4975

Closed
NoelDeMartin opened this issue Jul 7, 2014 · 2 comments
Closed

Tests setUp() resets Model Events #4975

NoelDeMartin opened this issue Jul 7, 2014 · 2 comments

Comments

@NoelDeMartin
Copy link
Contributor

Hi,

I've had some issues with my tests after adding some new logic with Model Events. After some investigations, I have found the issue is that whenever the method setUp() is called for the second time in a test, the Model Event listeners are cleared away from models. I don't know the exact reason, but it happens because the method refreshApplication() is called every time which causes the event dispatcher to be resetted. So the execution trace is the following:

0: I don't know what's called before
1: Illuminate\Foundation\Testing\TestCase->refreshApplication()
2: Illuminate\Foundation\Application->boot()
3: Illuminate\Database\DatabaseServiceProvider->boot()
4: Illuminate\Database\Eloquent\Model::setEventDispatcher::setEventDispatcher() // Here event dispatcher is resetted

In case this is not comprehensible enough (probably), here I have a reproducable scenario of what I did to find the bug in a clean laravel installation:

Given the following model:

class Foo extends \Eloquent {

    public $eventWasCalled = false;
    public $guarded = [];

    public static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->eventWasCalled = true;
        });

    }

}

And the following test class:

class ExampleTest extends TestCase {

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testOne()
    {
        $foo = Foo::create(['name' => 'foo']);
        $this->assertTrue($foo->eventWasCalled);
    }

    /**
     * A basic functional test example.
     *
     * @return void
     */
    public function testTwo()
    {
        $foo = Foo::create(['name' => 'foo']);
        $this->assertTrue($foo->eventWasCalled);
    }

}

When phpunit is executed, the second test will fail. I can provide more details if needed, but I think this is enough already. If this is not solved easily, I will try to help doing some more code inspection.

Thanks.

@laurencei
Copy link
Contributor

I think this might be related to #1181 ?

@NoelDeMartin
Copy link
Contributor Author

I see, it seems to be related indeed. Seems to be a recurring issue because a lot of people is affected. I will try to investigate a little more when I can, until then I guess those hacks they mention should work. Thanks for the heads up.

EspadaV8 pushed a commit to EspadaV8/eloquent-versioned that referenced this issue Jun 9, 2015
There are a number of issues related to Laravel not firing model events
when using PHPUnit

laravel/framework#1181
laravel/framework#4975

This package seems to help with that

https://github.com/orchestral/testbench
EspadaV8 pushed a commit to EspadaV8/eloquent-versioned that referenced this issue Jun 9, 2015
There are a number of issues related to Laravel not firing model events
when using PHPUnit

laravel/framework#1181
laravel/framework#4975

This package seems to help with that

https://github.com/orchestral/testbench
EspadaV8 pushed a commit to EspadaV8/eloquent-versioned that referenced this issue Jun 9, 2015
There are a number of issues related to Laravel not firing model events
when using PHPUnit

laravel/framework#1181
laravel/framework#4975

This package seems to help with that

https://github.com/orchestral/testbench
EspadaV8 pushed a commit to EspadaV8/eloquent-versioned that referenced this issue Jun 10, 2015
There are a number of issues related to Laravel not firing model events
when using PHPUnit

laravel/framework#1181
laravel/framework#4975

This package seems to help with that

https://github.com/orchestral/testbench
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants