|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace iamfarhad\LaravelAuditLog\Tests\Unit; |
| 6 | + |
| 7 | +use Mockery; |
| 8 | +use Illuminate\Support\Facades\Event; |
| 9 | +use iamfarhad\LaravelAuditLog\Tests\TestCase; |
| 10 | +use iamfarhad\LaravelAuditLog\Events\ModelAudited; |
| 11 | +use iamfarhad\LaravelAuditLog\Services\AuditBuilder; |
| 12 | + |
| 13 | +final class AuditBuilderTest extends TestCase |
| 14 | +{ |
| 15 | + protected function setUp(): void |
| 16 | + { |
| 17 | + parent::setUp(); |
| 18 | + Event::fake(); |
| 19 | + } |
| 20 | + |
| 21 | + public function test_can_build_and_log_custom_audit_event_with_fluent_api(): void |
| 22 | + { |
| 23 | + // Arrange |
| 24 | + $model = Mockery::mock('Illuminate\Database\Eloquent\Model'); |
| 25 | + $model->shouldReceive('getAuditMetadata')->andReturn(['default' => 'meta']); |
| 26 | + $model->shouldReceive('getAuditableAttributes')->andReturnUsing(function ($attributes) { |
| 27 | + return $attributes; |
| 28 | + }); |
| 29 | + |
| 30 | + // Act |
| 31 | + $builder = new AuditBuilder($model); |
| 32 | + $builder |
| 33 | + ->custom('status_change') |
| 34 | + ->from(['status' => 'pending']) |
| 35 | + ->to(['status' => 'approved']) |
| 36 | + ->withMetadata(['ip' => '127.0.0.1']) |
| 37 | + ->log(); |
| 38 | + |
| 39 | + // Assert |
| 40 | + Event::assertDispatched(ModelAudited::class, function (ModelAudited $event) use ($model) { |
| 41 | + return $event->model === $model |
| 42 | + && $event->action === 'status_change' |
| 43 | + && $event->oldValues === ['status' => 'pending'] |
| 44 | + && $event->newValues === ['status' => 'approved']; |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + public function test_uses_default_action_if_custom_not_specified(): void |
| 49 | + { |
| 50 | + // Arrange |
| 51 | + $model = Mockery::mock('Illuminate\Database\Eloquent\Model'); |
| 52 | + $model->shouldReceive('getAuditMetadata')->andReturn([]); |
| 53 | + $model->shouldReceive('getAuditableAttributes')->andReturnUsing(function ($attributes) { |
| 54 | + return $attributes; |
| 55 | + }); |
| 56 | + |
| 57 | + // Act |
| 58 | + $builder = new AuditBuilder($model); |
| 59 | + $builder |
| 60 | + ->from(['key' => 'old']) |
| 61 | + ->to(['key' => 'new']) |
| 62 | + ->log(); |
| 63 | + |
| 64 | + // Assert |
| 65 | + Event::assertDispatched(ModelAudited::class, function (ModelAudited $event) { |
| 66 | + return $event->action === 'custom'; |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + public function test_merges_model_metadata_with_custom_metadata(): void |
| 71 | + { |
| 72 | + // Arrange |
| 73 | + $model = Mockery::mock('Illuminate\Database\Eloquent\Model'); |
| 74 | + $model->shouldReceive('getAuditMetadata')->andReturn(['default' => 'meta']); |
| 75 | + $model->shouldReceive('getAuditableAttributes')->andReturnUsing(function ($attributes) { |
| 76 | + return $attributes; |
| 77 | + }); |
| 78 | + |
| 79 | + // Act |
| 80 | + $builder = new AuditBuilder($model); |
| 81 | + $builder |
| 82 | + ->custom('test_action') |
| 83 | + ->withMetadata(['custom' => 'data']) |
| 84 | + ->log(); |
| 85 | + |
| 86 | + // Assert |
| 87 | + Event::assertDispatched(ModelAudited::class); |
| 88 | + } |
| 89 | + |
| 90 | + public function test_filters_values_using_get_auditable_attributes_if_available(): void |
| 91 | + { |
| 92 | + // Create a concrete class with getAuditableAttributes method instead of using a mock |
| 93 | + $model = new class extends \Illuminate\Database\Eloquent\Model |
| 94 | + { |
| 95 | + public function getAuditMetadata(): array |
| 96 | + { |
| 97 | + return []; |
| 98 | + } |
| 99 | + |
| 100 | + public function getAuditableAttributes(array $attributes): array |
| 101 | + { |
| 102 | + // Only return the 'allowed' key if it exists in the input |
| 103 | + return isset($attributes['allowed']) ? ['allowed' => $attributes['allowed']] : []; |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + // Directly test AuditBuilder behavior without event faking |
| 108 | + $oldValues = ['allowed' => 'value', 'disallowed' => 'secret']; |
| 109 | + $newValues = ['allowed' => 'new_value', 'disallowed' => 'new_secret']; |
| 110 | + |
| 111 | + // Setup expectation that Event::dispatch will be called with filtered values |
| 112 | + Event::shouldReceive('dispatch') |
| 113 | + ->once() |
| 114 | + ->with(Mockery::on(function ($event) use ($model) { |
| 115 | + return $event instanceof ModelAudited |
| 116 | + && $event->model === $model |
| 117 | + && $event->oldValues === ['allowed' => 'value'] |
| 118 | + && $event->newValues === ['allowed' => 'new_value']; |
| 119 | + })); |
| 120 | + |
| 121 | + // Act |
| 122 | + $builder = new AuditBuilder($model); |
| 123 | + $builder |
| 124 | + ->from($oldValues) |
| 125 | + ->to($newValues) |
| 126 | + ->log(); |
| 127 | + |
| 128 | + // If we get here without Mockery exceptions, the test passes |
| 129 | + $this->assertTrue(true); |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +// Add a mock interface for the test to ensure method_exists works |
| 134 | +interface ModelWithGetAuditableAttributes |
| 135 | +{ |
| 136 | + public function getAuditableAttributes(array $attributes): array; |
| 137 | +} |
0 commit comments