Skip to content

monooso/unobserve

Repository files navigation

Unobserve

Lint and Test Status Latest Stable Version License

About Unobserve

When testing Laravel applications, we frequently need to "silence" events, so as not to trigger additional side-effects. Laravel's Event::fake method is useful, but muting a specific model observer is still problematic.

Unobserve takes care of that, making it easy to mute and unmute an observer at will.

Requirements and installation

Unobserve supports only the latest major version of Laravel. See composer.json for the exact version requirements.

Install it using Composer:

composer require monooso/unobserve

Usage

First, add the CanMute trait to your observer class.

<?php

namespace App\Observers;

use Monooso\Unobserve\CanMute;

class UserObserver
{
    use CanMute;
}

You can now mute and unmute your observer as needed:

UserObserver::mute();
UserObserver::unmute();

Mute options

Mute all observer events:

UserObserver::mute();

Mute specific observer events:

UserObserver::mute('creating');
UserObserver::mute(['creating', 'created']);

Local development

Unobserve is very stable at this point. If you have any feature ideas please open an issue before doing any work.

Development and testing happen entirely inside Podman containers, driven by the ./dev script. You do not need to install PHP, Composer, or any project dependencies on your machine.

The dev helper script builds a disposable PHP + Composer image for the PHP version you name, mounts your working tree into it, and runs your command. Any changes to composer.json and composer.lock are written back to your disk so they can be committed.

Prerequisites

Podman 5.x or later.

Common tasks

The dev script always accepts the target PHP version as the first argument. The first time you run a command with a given PHP version, it builds the image automatically.

Command Purpose
./dev 8.3 Open a shell on PHP 8.3
./dev 8.3 test Run the test suite
./dev 8.3 lint Run Laravel Pint
./dev 8.3 composer install Install dependencies from the lock file
./dev 8.3 composer update Re-resolve dependencies (rewrites composer.lock)
./dev 8.3 composer outdated List outdated packages
./dev 8.3 php -v Run any command in the container
./dev build 8.3 Rebuild the image for a version

Run ./dev --help for the full reference.

Upgrading PHP or Laravel

  1. Create a branch.

  2. Edit composer.json to widen the relevant constraints, e.g. "php": "^8.3" and "illuminate/support": "^13.0".

  3. Re-resolve dependencies against the target PHP version:

    ./dev 8.3 composer update
  4. In the event of a conflict, investigate and iterate:

    ./dev 8.3 composer why-not laravel/framework 13.0.0
    ./dev 8.3 composer require illuminate/support:"^13.0" --no-update
    ./dev 8.3 composer update
  5. Run the tests and linter:

    ./dev 8.3 test
    ./dev 8.3 lint
  6. Commit composer.json and composer.lock.

Each PHP version keeps its own dependency cache, so several versions can be tested side by side (e.g. ./dev 8.2 test, ./dev 8.3 test) without interfering with each other.

License

Unobserve is open source software, released under the MIT license.

About

Mute and unmute Laravel observers at will.

Topics

Resources

License

Stars

83 stars

Watchers

2 watching

Forks

Packages

 
 
 

Contributors