Skip to content

Commit

Permalink
Add support for disabling trait specific handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurt Friars committed Jan 31, 2025
1 parent bcbecf1 commit 7cc4f7d
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 12 deletions.
27 changes: 24 additions & 3 deletions src/Concerns/HushesHandlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,34 @@ protected static function filteredHandlers(array $handlers, array $classes)
}

if (is_callable($handler)) {
$refl = new ReflectionFunction($handler);
$class = $refl->getClosureScopeClass();
$class = static::namespacedName($handler);

return $class === null || ! str($class->getName())->contains($classes);
return $class === null
|| ! str($class)->contains($classes);
}

return true;
});
}

/**
* @return class-string
*/
protected static function namespacedName($handler): ?string
{
$refl = new ReflectionFunction($handler);
$namespace = $refl->getNamespaceName();

if ($namespace === null) {
return null;
}

$filename = basename($refl->getFileName());

if ($filename === null) {
return null;
}

return $namespace.'\\'.str($filename)->beforeLast('.');
}
}
27 changes: 27 additions & 0 deletions tests/Database/Factories/PostFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Plank\LaravelHush\Tests\Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;
use Plank\LaravelHush\Tests\Models\Post;

/**
* @extends Factory<Post>
*/
class PostFactory extends Factory
{
protected $model = Post::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition(): array
{
return [
'title' => $this->faker->title,
'body' => $this->faker->paragraphs(3, true),
];
}
}
21 changes: 21 additions & 0 deletions tests/Database/Migrations/create_posts_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('body');
$table->timestamps();
});
}
};
26 changes: 26 additions & 0 deletions tests/Feature/DisablesHandlersTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Plank\LaravelHush\Tests\Models\Concerns\HasEventHandlersInTrait;
use Plank\LaravelHush\Tests\Models\Post;
use Plank\LaravelHush\Tests\Models\User;
use Plank\LaravelHush\Tests\Observers\UserObserver;

Expand Down Expand Up @@ -31,6 +33,30 @@
expect(User::query()->count())->toBe(0);
});

it('can disable handlers for a specific event and trait and doesnt throw a deleting exception as a result', function () {
$post = Post::withoutEvents(function () {
return Post::factory()->create();
});

expect($post)->not->toBeNull();

Post::withoutHandler('deleting', fn () => $post->delete(), [HasEventHandlersInTrait::class]);

expect(Post::query()->count())->toBe(0);
});

it('throws a deleting exception when a traits handler is not disabled', function () {
$post = Post::withoutEvents(function () {
return Post::factory()->create();
});

expect($post)->not->toBeNull();

$post->delete();

expect(Post::query()->count())->toBe(0);
})->throws('deleting in trait');

it('restores the handlers once the closure has completed executions', function () {
User::withoutHandlers(['saving', 'creating'], function () {
User::factory()->create();
Expand Down
13 changes: 13 additions & 0 deletions tests/Models/Concerns/HasEventHandlersInTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Plank\LaravelHush\Tests\Models\Concerns;

trait HasEventHandlersInTrait
{
public static function bootHasEventHandlersInTrait(): void
{
static::deleting(function () {
throw new \Exception('deleting in trait');
});
}
}
28 changes: 28 additions & 0 deletions tests/Models/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Plank\LaravelHush\Tests\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Plank\LaravelHush\Concerns\HushesHandlers;
use Plank\LaravelHush\Tests\Database\Factories\PostFactory;
use Plank\LaravelHush\Tests\Models\Concerns\HasEventHandlersInTrait;

class Post extends Model
{
use HasEventHandlersInTrait;
use HasFactory;
use HushesHandlers;

protected $guarded = [];

/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory<static>
*/
protected static function newFactory()
{
return PostFactory::new();
}
}
9 changes: 0 additions & 9 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ protected static function newFactory()
'password',
];

/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'password' => 'hashed',
];

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

0 comments on commit 7cc4f7d

Please sign in to comment.