Skip to content

Commit 475d3c5

Browse files
author
Kurt Friars
committed
Add support for disabling trait specific handlers
1 parent 60030a9 commit 475d3c5

File tree

7 files changed

+139
-12
lines changed

7 files changed

+139
-12
lines changed

src/Concerns/HushesHandlers.php

+24-3
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,34 @@ protected static function filteredHandlers(array $handlers, array $classes)
143143
}
144144

145145
if (is_callable($handler)) {
146-
$refl = new ReflectionFunction($handler);
147-
$class = $refl->getClosureScopeClass();
146+
$class = static::namespacedName($handler);
148147

149-
return $class === null || ! str($class->getName())->contains($classes);
148+
return $class === null
149+
|| ! str($class)->contains($classes);
150150
}
151151

152152
return true;
153153
});
154154
}
155+
156+
/**
157+
* @return class-string
158+
*/
159+
protected static function namespacedName($handler): ?string
160+
{
161+
$refl = new ReflectionFunction($handler);
162+
$namespace = $refl->getNamespaceName();
163+
164+
if ($namespace === null) {
165+
return null;
166+
}
167+
168+
$filename = basename($refl->getFileName());
169+
170+
if ($filename === null) {
171+
return null;
172+
}
173+
174+
return $namespace.'\\'.str($filename)->beforeLast('.');
175+
}
155176
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Plank\LaravelHush\Tests\Database\Factories;
4+
5+
use Illuminate\Database\Eloquent\Factories\Factory;
6+
use Plank\LaravelHush\Tests\Models\Post;
7+
8+
/**
9+
* @extends Factory<Post>
10+
*/
11+
class PostFactory extends Factory
12+
{
13+
protected $model = Post::class;
14+
15+
/**
16+
* Define the model's default state.
17+
*
18+
* @return array<string, mixed>
19+
*/
20+
public function definition(): array
21+
{
22+
return [
23+
'title' => $this->faker->title,
24+
'body' => $this->faker->paragraphs(3, true),
25+
];
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::create('posts', function (Blueprint $table) {
15+
$table->id();
16+
$table->string('title');
17+
$table->text('body');
18+
$table->timestamps();
19+
});
20+
}
21+
};

tests/Feature/DisablesHandlersTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Plank\LaravelHush\Tests\Models\Concerns\HasEventHandlersInTrait;
4+
use Plank\LaravelHush\Tests\Models\Post;
35
use Plank\LaravelHush\Tests\Models\User;
46
use Plank\LaravelHush\Tests\Observers\UserObserver;
57

@@ -31,6 +33,30 @@
3133
expect(User::query()->count())->toBe(0);
3234
});
3335

36+
it('can disable handlers for a specific event and trait and doesnt throw a deleting exception as a result', function () {
37+
$post = Post::withoutEvents(function () {
38+
return Post::factory()->create();
39+
});
40+
41+
expect($post)->not->toBeNull();
42+
43+
Post::withoutHandler('deleting', fn () => $post->delete(), [HasEventHandlersInTrait::class]);
44+
45+
expect(Post::query()->count())->toBe(0);
46+
});
47+
48+
it('throws a deleting exception when a traits handler is not disabled', function () {
49+
$post = Post::withoutEvents(function () {
50+
return Post::factory()->create();
51+
});
52+
53+
expect($post)->not->toBeNull();
54+
55+
$post->delete();
56+
57+
expect(Post::query()->count())->toBe(0);
58+
})->throws('deleting in trait');
59+
3460
it('restores the handlers once the closure has completed executions', function () {
3561
User::withoutHandlers(['saving', 'creating'], function () {
3662
User::factory()->create();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Plank\LaravelHush\Tests\Models\Concerns;
4+
5+
trait HasEventHandlersInTrait
6+
{
7+
public static function bootHasEventHandlersInTrait(): void
8+
{
9+
static::deleting(function () {
10+
throw new \Exception('deleting in trait');
11+
});
12+
}
13+
}

tests/Models/Post.php

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Plank\LaravelHush\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
use Plank\LaravelHush\Concerns\HushesHandlers;
8+
use Plank\LaravelHush\Tests\Database\Factories\PostFactory;
9+
use Plank\LaravelHush\Tests\Models\Concerns\HasEventHandlersInTrait;
10+
11+
class Post extends Model
12+
{
13+
use HasEventHandlersInTrait;
14+
use HasFactory;
15+
use HushesHandlers;
16+
17+
protected $guarded = [];
18+
19+
/**
20+
* Create a new factory instance for the model.
21+
*
22+
* @return \Illuminate\Database\Eloquent\Factories\Factory<static>
23+
*/
24+
protected static function newFactory()
25+
{
26+
return PostFactory::new();
27+
}
28+
}

tests/Models/User.php

-9
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,6 @@ protected static function newFactory()
4444
'password',
4545
];
4646

47-
/**
48-
* The attributes that should be cast.
49-
*
50-
* @var array<string, string>
51-
*/
52-
protected $casts = [
53-
'password' => 'hashed',
54-
];
55-
5647
public static function boot()
5748
{
5849
parent::boot();

0 commit comments

Comments
 (0)