Skip to content

Using HasRelationshipObservables trait in multiple models causes double events to be fired in tests #40

@BoyeMagnus

Description

@BoyeMagnus

When adding the HasRelationshipObservables trait to multiple models, the tests often fail, because of observers receiving the event twice.

For eg. a user and a group model with HasRelationshipObservables. With two observers that subscribe to belongsToManyAttached both.

trait HasRelationshipObservables
{
    /**
     * @var array
     */
    protected static $relationshipObservables = [];

    /**
     * Initialize relationship observables.
     *
     * @return void
     */
    public static function bootHasRelationshipObservables()
    {
         ......gets the methods

        static::mergeRelationshipObservables($methods);
    }

Static properties will most of the time in tests never be reset, because of how PHPUnit works. When booting HasRelationshipObservables, then it merges the $methods with the static $relationshipObservables. This causes the observer to be fire multiple times in tests where the static property is never reset.

A solution would be to set $relationshipObservables to an empty array in the bootHasRelationshipObservables. Anyone expiring the same issues?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions