diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index cf988e78..9c51e58d 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -1,7 +1,7 @@ name: PHPStan on: - push: + pull_request: paths: - '**.php' - 'phpstan.neon.dist' @@ -10,6 +10,9 @@ jobs: phpstan: name: phpstan runs-on: ubuntu-latest + timeout-minutes: 15 + env: + COMPOSER_NO_INTERACTION: 1 steps: - uses: actions/checkout@v4 @@ -24,8 +27,8 @@ jobs: - name: Install larastan run: | - composer require "larastan/larastan" --no-interaction --no-update - composer update --prefer-dist --no-interaction + composer require "larastan/larastan" --no-update + composer update --prefer-dist --no-suggest - name: Run PHPStan run: ./vendor/bin/phpstan --error-format=github diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 00793ebe..e0d373bc 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -2,63 +2,29 @@ name: run-tests on: push: - branches: [master] - pull_request: - branches: [master] + branches: + - '*' jobs: run-tests: runs-on: ubuntu-latest + timeout-minutes: 15 + env: + COMPOSER_NO_INTERACTION: 1 strategy: fail-fast: false matrix: - php: [7.4, 8.0, 8.1, 8.2, 8.3, 8.4] - laravel: [ 11.*, 10.*, 9.*, 8.*, 7.* ] + php: [ 8.2, 8.3, 8.4 ] + laravel: [ 11.*, 12.* ] include: + - laravel: 12.* + testbench: 10.* - laravel: 11.* testbench: 9.* - - laravel: 10.* - testbench: 8.* - - laravel: 9.* - testbench: 7.* - - laravel: 8.* - testbench: 6.* - - laravel: 7.* - testbench: 5.* - exclude: - - laravel: 11.* - php: 8.1 - - laravel: 11.* - php: 8.0 - - laravel: 11.* - php: 7.4 - - laravel: 10.* - php: 7.4 - - laravel: 10.* - php: 8.0 - - laravel: 9.* - php: 7.4 - - laravel: 9.* - php: 8.4 - - laravel: 8.* - php: 8.3 - - laravel: 8.* - php: 8.4 - - laravel: 7.* - php: 8.1 - - laravel: 7.* - php: 8.2 - - laravel: 7.* - php: 8.3 - - laravel: 7.* - php: 8.4 name: PHP${{ matrix.php }} - Laravel${{ matrix.laravel }} - ${{ matrix.dependency-version }} steps: - - name: Update apt - run: sudo apt-get update --fix-missing - - name: Checkout code uses: actions/checkout@v4 @@ -71,11 +37,11 @@ jobs: - name: Install dependencies run: | - composer require "illuminate/console:${{ matrix.laravel }}" "illuminate/database:${{ matrix.laravel }}" "illuminate/filesystem:${{ matrix.laravel }}" "nesbot/carbon:^2.62.1" --no-interaction --no-update - composer require "orchestra/testbench:${{ matrix.testbench }}" --dev --no-interaction --no-update - composer update --prefer-dist --no-interaction --no-suggest + composer require "illuminate/console:${{ matrix.laravel }}" "illuminate/database:${{ matrix.laravel }}" "illuminate/filesystem:${{ matrix.laravel }}" --no-update + composer require "orchestra/testbench:${{ matrix.testbench }}" --dev --no-update + composer update --prefer-dist --no-suggest - name: Execute tests run: | vendor/bin/phpunit - vendor/bin/phpunit tests/Unit/AuditTest.php --group command-line-url-resolver \ No newline at end of file + vendor/bin/phpunit tests/Unit/AuditTest.php --group command-line-url-resolver diff --git a/README.md b/README.md index 95b90bbe..00c10cc5 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Chat

-This package will help you understand changes in your Eloquent models, by providing information about possible discrepancies and anomalies that could indicate business concerns or suspect activities. +This package will help you understand changes in your Eloquent models, by providing information about possible discrepancies and anomalies that could indicate business concerns or suspect activities. Laravel Auditing allows you to keep a history of model changes by simply using a trait. Retrieving the audited data is straightforward, making it possible to display it in various ways. @@ -26,7 +26,8 @@ Thank you for choosing OwenIt\LaravelAuditing! Version | Illuminate | Status | PHP Version :----------|:---------------|:------------------------|:------------ -13.x | 7.x.x - 11.x.x | Active support :rocket: | > = 7.3 \| 8.0 +14.x | 11.x.x - 12.x.x | Active support :rocket: | > = 8.2 +13.x | 7.x.x - 11.x.x | End of life | > = 7.3 \| 8.0 12.x | 6.x.x - 9.x.x | End of life | > = 7.3 \| 8.0 11.x | 5.8.x - 8.x.x | End of life | > = 7.3 10.x | 5.8.x - 7.x.x | End of life | > = 7.2.5 diff --git a/composer.json b/composer.json index f2039acf..867d54e8 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "owen-it/laravel-auditing", - "description": "Audit changes of your Eloquent models in Laravel/Lumen", + "description": "Audit changes of your Eloquent models in Laravel", "keywords": [ "accountability", "audit", @@ -39,17 +39,16 @@ } ], "require": { - "php": "^7.3|^8.0", - "illuminate/console": "^7.0|^8.0|^9.0|^10.0|^11.0", - "illuminate/database": "^7.0|^8.0|^9.0|^10.0|^11.0", - "illuminate/filesystem": "^7.0|^8.0|^9.0|^10.0|^11.0", - "ext-json": "*" + "php": "^8.2", + "ext-json": "*", + "illuminate/console": "^11.0|^12.0", + "illuminate/database": "^11.0|^12.0", + "illuminate/filesystem": "^11.0|^12.0" }, "require-dev": { - "phpunit/phpunit": "^9.6|^10.5|^11.0", - "mockery/mockery": "^1.0", - "orchestra/testbench": "^5.0|^6.0|^7.0|^8.0|^9.0", - "laravel/legacy-factories": "*" + "mockery/mockery": "^1.5.1", + "orchestra/testbench": "^9.0|^10.0", + "phpunit/phpunit": "^11.0" }, "autoload": { "psr-4": { @@ -61,12 +60,9 @@ "OwenIt\\Auditing\\Tests\\": "tests/" } }, - "suggest": { - "irazasyed/larasupport": "Needed to publish the package configuration in Lumen" - }, "extra": { "branch-alias": { - "dev-master": "v13-dev" + "dev-master": "v14-dev" }, "laravel": { "providers": [ @@ -74,6 +70,11 @@ ] } }, + "scripts": { + "test": "phpunit", + "format": "composer require --dev laravel/pint --quiet && pint --config .pint.json && composer remove --dev laravel/pint --no-update", + "analyse": "composer require --dev larastan/larastan --quiet && phpstan analyse && composer remove --dev larastan/larastan --no-update" + }, "minimum-stability": "dev", "prefer-stable": true } diff --git a/database/migrations/audits.stub b/database/migrations/audits.stub index befd47ef..7ce54a6d 100644 --- a/database/migrations/audits.stub +++ b/database/migrations/audits.stub @@ -4,14 +4,12 @@ use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateAuditsTable extends Migration +return new class extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { $connection = config('audit.drivers.database.connection', config('database.default')); $table = config('audit.drivers.database.table', 'audits'); @@ -39,14 +37,12 @@ class CreateAuditsTable extends Migration /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { $connection = config('audit.drivers.database.connection', config('database.default')); $table = config('audit.drivers.database.table', 'audits'); Schema::connection($connection)->drop($table); } -} +}; diff --git a/phpunit.xml b/phpunit.xml index e4003711..c24d10fd 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,21 +1,18 @@ - ./tests + ./tests @@ -27,9 +24,9 @@ command-line-url-resolver - + ./src - + diff --git a/src/Events/AuditCustom.php b/src/Events/AuditCustom.php index 6ef6d780..cd967ebd 100644 --- a/src/Events/AuditCustom.php +++ b/src/Events/AuditCustom.php @@ -6,18 +6,12 @@ class AuditCustom { - /** - * The Auditable model. - * - * @var \OwenIt\Auditing\Contracts\Auditable - */ - public $model; - /** * Create a new Auditing event instance. */ - public function __construct(Auditable $model) - { - $this->model = $model; + public function __construct( + public Auditable $model + ) { + // } } diff --git a/src/Events/Audited.php b/src/Events/Audited.php index aaba043e..d53ee144 100644 --- a/src/Events/Audited.php +++ b/src/Events/Audited.php @@ -8,34 +8,14 @@ class Audited { - /** - * The Auditable model. - * - * @var \OwenIt\Auditing\Contracts\Auditable - */ - public $model; - - /** - * Audit driver. - * - * @var \OwenIt\Auditing\Contracts\AuditDriver - */ - public $driver; - - /** - * The Audit model. - * - * @var \OwenIt\Auditing\Contracts\Audit|null - */ - public $audit; - /** * Create a new Audited event instance. */ - public function __construct(Auditable $model, AuditDriver $driver, ?Audit $audit = null) - { - $this->model = $model; - $this->driver = $driver; - $this->audit = $audit; + public function __construct( + public Auditable $model, + public AuditDriver $driver, + public ?Audit $audit = null + ) { + // } } diff --git a/src/Events/Auditing.php b/src/Events/Auditing.php index e1a7da01..7be451b0 100644 --- a/src/Events/Auditing.php +++ b/src/Events/Auditing.php @@ -7,26 +7,13 @@ class Auditing { - /** - * The Auditable model. - * - * @var \OwenIt\Auditing\Contracts\Auditable - */ - public $model; - - /** - * Audit driver. - * - * @var \OwenIt\Auditing\Contracts\AuditDriver - */ - public $driver; - /** * Create a new Auditing event instance. */ - public function __construct(Auditable $model, AuditDriver $driver) - { - $this->model = $model; - $this->driver = $driver; + public function __construct( + public Auditable $model, + public AuditDriver $driver + ) { + // } } diff --git a/src/Events/DispatchAudit.php b/src/Events/DispatchAudit.php index d9c7dc2a..9ad3f09a 100644 --- a/src/Events/DispatchAudit.php +++ b/src/Events/DispatchAudit.php @@ -7,19 +7,13 @@ class DispatchAudit { - /** - * The Auditable model. - * - * @var Auditable - */ - public $model; - /** * Create a new DispatchAudit event instance. */ - public function __construct(Auditable $model) - { - $this->model = $model; + public function __construct( + public Auditable $model + ) { + // } /** diff --git a/src/Events/DispatchingAudit.php b/src/Events/DispatchingAudit.php index ecfe754d..c989f124 100644 --- a/src/Events/DispatchingAudit.php +++ b/src/Events/DispatchingAudit.php @@ -6,18 +6,12 @@ class DispatchingAudit { - /** - * The Auditable model. - * - * @var Auditable - */ - public $model; - /** * Create a new DispatchingAudit event instance. */ - public function __construct(Auditable $model) - { - $this->model = $model; + public function __construct( + public Auditable $model + ) { + // } } diff --git a/tests/AuditingTestCase.php b/tests/AuditingTestCase.php index f62d1150..f245693b 100644 --- a/tests/AuditingTestCase.php +++ b/tests/AuditingTestCase.php @@ -54,7 +54,6 @@ protected function setUp(): void parent::setUp(); $this->loadMigrationsFrom(__DIR__.'/database/migrations'); - $this->withFactories(__DIR__.'/database/factories'); } /** diff --git a/tests/Functional/AuditingTest.php b/tests/Functional/AuditingTest.php index a7ba96f8..9ac43916 100644 --- a/tests/Functional/AuditingTest.php +++ b/tests/Functional/AuditingTest.php @@ -26,56 +26,44 @@ class AuditingTest extends AuditingTestCase { use WithFaker; - /** - * @test - */ - public function it_will_not_audit_models_when_running_from_the_console() + public function test_it_will_not_audit_models_when_running_from_the_console(): void { $this->app['config']->set('audit.console', false); - factory(User::class)->create(); + User::factory()->create(); $this->assertSame(1, User::query()->count()); $this->assertSame(0, Audit::query()->count()); } - /** - * @test - */ - public function it_will_audit_models_when_running_from_the_console() + public function test_it_will_audit_models_when_running_from_the_console(): void { $this->app['config']->set('audit.console', true); - factory(User::class)->create(); + User::factory()->create(); $this->assertSame(1, User::query()->count()); $this->assertSame(1, Audit::query()->count()); } - /** - * @test - */ - public function it_will_always_audit_models_when_not_running_from_the_console() + public function test_it_will_always_audit_models_when_not_running_from_the_console(): void { App::shouldReceive('runningInConsole') ->andReturn(false); $this->app['config']->set('audit.console', false); - factory(User::class)->create(); + User::factory()->create(); $this->assertSame(1, User::query()->count()); $this->assertSame(1, Audit::query()->count()); } - /** - * @test - */ - public function it_will_not_audit_the_retrieving_event() + public function test_it_will_not_audit_the_retrieving_event(): void { $this->app['config']->set('audit.console', true); - factory(User::class)->create(); + User::factory()->create(); $this->assertSame(1, User::query()->count()); $this->assertSame(1, Audit::query()->count()); @@ -86,10 +74,7 @@ public function it_will_not_audit_the_retrieving_event() $this->assertSame(1, User::query()->count()); } - /** - * @test - */ - public function it_will_audit_the_retrieving_event() + public function test_it_will_audit_the_retrieving_event(): void { $this->app['config']->set('audit.console', true); $this->app['config']->set('audit.events', [ @@ -97,7 +82,7 @@ public function it_will_audit_the_retrieving_event() 'retrieved', ]); - factory(User::class)->create(); + User::factory()->create(); $this->assertSame(1, User::query()->count()); $this->assertSame(1, Audit::query()->count()); @@ -107,16 +92,13 @@ public function it_will_audit_the_retrieving_event() $this->assertSame(2, Audit::query()->count()); } - /** - * @test - */ - public function it_will_audit_the_retrieved_event() + public function test_it_will_audit_the_retrieved_event(): void { $this->app['config']->set('audit.events', [ 'retrieved', ]); - factory(Article::class)->create([ + Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'N/A', 'published_at' => null, @@ -134,16 +116,13 @@ public function it_will_audit_the_retrieved_event() $this->assertEmpty($audit->new_values); } - /** - * @test - */ - public function it_will_audit_the_created_event() + public function test_it_will_audit_the_created_event(): void { $this->app['config']->set('audit.events', [ 'created', ]); - factory(Article::class)->create([ + Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'N/A', 'published_at' => null, @@ -165,16 +144,13 @@ public function it_will_audit_the_created_event() ], $audit->new_values, true); } - /** - * @test - */ - public function it_will_audit_the_updated_event() + public function test_it_will_audit_the_updated_event(): void { $this->app['config']->set('audit.events', [ 'updated', ]); - $article = factory(Article::class)->create([ + $article = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'N/A', 'published_at' => null, @@ -206,16 +182,13 @@ public function it_will_audit_the_updated_event() ], $audit->new_values, true); } - /** - * @test - */ - public function it_will_audit_the_deleted_event() + public function test_it_will_audit_the_deleted_event(): void { $this->app['config']->set('audit.events', [ 'deleted', ]); - $article = factory(Article::class)->create([ + $article = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'N/A', 'published_at' => null, @@ -239,16 +212,13 @@ public function it_will_audit_the_deleted_event() $this->assertEmpty($audit->new_values); } - /** - * @test - */ - public function it_will_audit_the_restored_event() + public function test_it_will_audit_the_restored_event(): void { $this->app['config']->set('audit.events', [ 'restored', ]); - $article = factory(Article::class)->create([ + $article = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'N/A', 'published_at' => null, @@ -273,17 +243,14 @@ public function it_will_audit_the_restored_event() ], $audit->new_values, true); } - /** - * @test - */ - public function it_will_keep_all_audits() + public function test_it_will_keep_all_audits(): void { $this->app['config']->set('audit.threshold', 0); $this->app['config']->set('audit.events', [ 'updated', ]); - $article = factory(Article::class)->create([ + $article = Article::factory()->create([ 'reviewed' => 1, ]); @@ -296,17 +263,14 @@ public function it_will_keep_all_audits() $this->assertSame(100, $article->audits()->count()); } - /** - * @test - */ - public function it_will_remove_older_audits_above_the_threshold() + public function test_it_will_remove_older_audits_above_the_threshold(): void { $this->app['config']->set('audit.threshold', 10); $this->app['config']->set('audit.events', [ 'updated', ]); - $article = factory(Article::class)->create([ + $article = Article::factory()->create([ 'title' => 'Title #0', ]); @@ -326,23 +290,17 @@ public function it_will_remove_older_audits_above_the_threshold() $this->assertSame('Title #20', $audits->last()->new_values['title']); } - /** - * @test - */ - public function it_will_not_audit_due_to_unsupported_driver() + public function test_it_will_not_audit_due_to_unsupported_driver(): void { $this->app['config']->set('audit.driver', 'foo'); $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Driver [foo] not supported.'); - factory(Article::class)->create(); + Article::factory()->create(); } - /** - * @test - */ - public function it_will_not_audit_due_to_class_without_driver_interface() + public function test_it_will_not_audit_due_to_class_without_driver_interface(): void { // We just pass a FQCN that does not implement the AuditDriver interface $this->app['config']->set('audit.driver', Article::class); @@ -350,17 +308,14 @@ public function it_will_not_audit_due_to_class_without_driver_interface() $this->expectException(AuditingException::class); $this->expectExceptionMessage('The driver must implement the AuditDriver contract'); - factory(Article::class)->create(); + Article::factory()->create(); } - /** - * @test - */ - public function it_will_audit_using_the_default_driver() + public function test_it_will_audit_using_the_default_driver(): void { $this->app['config']->set('audit.driver', null); - factory(Article::class)->create([ + Article::factory()->create([ 'title' => 'How To Audit Using The Fallback Driver', 'content' => 'N/A', 'published_at' => null, @@ -382,29 +337,23 @@ public function it_will_audit_using_the_default_driver() ], $audit->new_values, true); } - /** - * @test - */ - public function it_will_cancel_the_audit_from_an_event_listener() + public function test_it_will_cancel_the_audit_from_an_event_listener(): void { Event::listen(Auditing::class, function () { return false; }); - factory(Article::class)->create(); + Article::factory()->create(); $this->assertNull(Audit::first()); } - /** - * @test - */ - public function it_disables_and_enables_auditing_back_again() + public function test_it_disables_and_enables_auditing_back_again(): void { // Auditing is enabled by default $this->assertFalse(Article::$auditingDisabled); - factory(Article::class)->create(); + Article::factory()->create(); $this->assertSame(1, Article::count()); $this->assertSame(1, Audit::count()); @@ -413,7 +362,7 @@ public function it_disables_and_enables_auditing_back_again() Article::disableAuditing(); $this->assertTrue(Article::$auditingDisabled); - factory(Article::class)->create(); + Article::factory()->create(); $this->assertSame(2, Article::count()); $this->assertSame(1, Audit::count()); @@ -422,23 +371,20 @@ public function it_disables_and_enables_auditing_back_again() Article::enableAuditing(); $this->assertFalse(Article::$auditingDisabled); - factory(Article::class)->create(); + Article::factory()->create(); $this->assertSame(2, Audit::count()); $this->assertSame(3, Article::count()); } - /** - * @test - */ - public function it_disables_and_enables_auditing_back_again_via_facade() + public function test_it_disables_and_enables_auditing_back_again_via_facade(): void { // Auditing is enabled by default $this->assertFalse(Article::$auditingDisabled); Article::disableAuditing(); - factory(Article::class)->create(); + Article::factory()->create(); $this->assertSame(1, Article::count()); $this->assertSame(0, Audit::count()); @@ -447,22 +393,19 @@ public function it_disables_and_enables_auditing_back_again_via_facade() Article::enableAuditing(); $this->assertFalse(Article::$auditingDisabled); - factory(Article::class)->create(); + Article::factory()->create(); $this->assertSame(2, Article::count()); $this->assertSame(1, Audit::count()); } - /** - * @test - */ - public function it_disables_and_enables_auditing_back_again_via_without_auditing_method() + public function test_it_disables_and_enables_auditing_back_again_via_without_auditing_method(): void { // Auditing is enabled by default $this->assertFalse(Article::$auditingDisabled); Article::withoutAuditing(function () { - factory(Article::class)->create(); + Article::factory()->create(); }); $this->assertSame(1, Article::count()); @@ -470,20 +413,15 @@ public function it_disables_and_enables_auditing_back_again_via_without_auditing $this->assertFalse(Article::$auditingDisabled); - factory(Article::class)->create(); + Article::factory()->create(); $this->assertSame(2, Article::count()); $this->assertSame(1, Audit::count()); } - /** - * @test - * - * @return void - */ - public function it_handles_json_columns_correctly() + public function test_it_handles_json_columns_correctly(): void { - $article = factory(Article::class)->create(['config' => ['articleIsGood' => true, 'authorsJob' => 'vampire']]); + $article = Article::factory()->create(['config' => ['articleIsGood' => true, 'authorsJob' => 'vampire']]); $article->refresh(); $article->config = ['articleIsGood' => false, 'authorsJob' => 'vampire']; @@ -497,17 +435,12 @@ public function it_handles_json_columns_correctly() $this->assertTrue($audit->getModified()['config']['old']['articleIsGood']); } - /** - * @return void - * - * @test - */ - public function can_add_additional_resolver() + public function test_can_add_additional_resolver(): void { // added new resolver $this->app['config']->set('audit.resolvers.tenant_id', TenantResolver::class); - $article = factory(Article::class)->create(); + $article = Article::factory()->create(); $audit = $article->audits()->first(); @@ -516,17 +449,12 @@ public function can_add_additional_resolver() $this->assertSame(1, (int) $audit->tenant_id); } - /** - * @return void - * - * @test - */ - public function can_disable_resolver() + public function test_can_disable_resolver(): void { // added new resolver $this->app['config']->set('audit.resolvers.ip_address', null); - $article = factory(Article::class)->create(); + $article = Article::factory()->create(); $audit = $article->audits()->first(); @@ -535,12 +463,7 @@ public function can_disable_resolver() $this->assertEmpty($audit->ip_address); } - /** - * @test - * - * @return void - */ - public function it_will_exclude_if_global_exclude_is_set() + public function test_it_will_exclude_if_global_exclude_is_set(): void { $this->app['config']->set('audit.exclude', ['content']); @@ -553,12 +476,7 @@ public function it_will_exclude_if_global_exclude_is_set() $this->assertArrayNotHasKey('content', $article->audits()->first()->getModified()); } - /** - * @test - * - * @return void - */ - public function local_exclude_overrides_global_exclude() + public function test_local_exclude_overrides_global_exclude(): void { $this->app['config']->set('audit.exclude', ['content']); @@ -572,10 +490,7 @@ public function local_exclude_overrides_global_exclude() $this->assertArrayNotHasKey('title', $article->audits()->first()->getModified()); } - /** - * @test - */ - public function it_will_not_audit_models_when_values_are_empty() + public function test_it_will_not_audit_models_when_values_are_empty(): void { $this->app['config']->set('audit.empty_values', false); @@ -598,12 +513,7 @@ public function it_will_not_audit_models_when_values_are_empty() $this->assertSame(1, Audit::query()->count()); } - /** - * @return void - * - * @test - */ - public function it_will_audit_retrieved_event_even_if_audit_empty_is_disabled() + public function test_it_will_audit_retrieved_event_even_if_audit_empty_is_disabled(): void { $this->app['config']->set('audit.empty_values', false); $this->app['config']->set('audit.allowed_empty_values', ['retrieved']); @@ -615,19 +525,16 @@ public function it_will_audit_retrieved_event_even_if_audit_empty_is_disabled() $this->app['config']->set('audit.empty_values', false); /** @var Article $model */ - factory(Article::class)->create(); + Article::factory()->create(); Article::find(1); $this->assertSame(2, Audit::query()->count()); } - /** - * @test - */ - public function it_will_audit_models_when_values_are_empty() + public function test_it_will_audit_models_when_values_are_empty(): void { - $model = factory(Article::class)->create([ + $model = Article::factory()->create([ 'reviewed' => 0, ]); @@ -638,16 +545,11 @@ public function it_will_audit_models_when_values_are_empty() $this->assertSame(2, Audit::query()->count()); } - /** - * @test - * - * @return void - */ - public function it_will_audit_attach() + public function test_it_will_audit_attach(): void { - $firstCategory = factory(Category::class)->create(); - $secondCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $secondCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->auditAttach('categories', $firstCategory); $article->auditAttach('categories', $secondCategory); @@ -659,31 +561,21 @@ public function it_will_audit_attach() $this->assertSame($secondCategory->name, $lastArticleAudit['new'][0]['name']); } - /** - * @test - * - * @return void - */ - public function it_will_not_audit_attach_by_invalid_relation_name() + public function test_it_will_not_audit_attach_by_invalid_relation_name(): void { - $firstCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $article = Article::factory()->create(); $this->expectExceptionMessage('Relationship invalidRelation was not found or does not support method attach'); $article->auditAttach('invalidRelation', $firstCategory); } - /** - * @test - * - * @return void - */ - public function it_will_audit_sync() + public function test_it_will_audit_sync(): void { - $firstCategory = factory(Category::class)->create(); - $secondCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $secondCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach($firstCategory); @@ -701,17 +593,12 @@ public function it_will_audit_sync() $this->assertGreaterThan($no_of_audits_before, $no_of_audits_after); } - /** - * @test - * - * @return void - */ - public function it_will_audit_sync_individually() + public function test_it_will_audit_sync_individually(): void { Article::disableAuditing(); - $user = factory(User::class)->create(); - $category = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $user = User::factory()->create(); + $category = Category::factory()->create(); + $article = Article::factory()->create(); Article::enableAuditing(); $no_of_audits_before = Audit::where('auditable_type', Article::class)->count(); @@ -736,20 +623,15 @@ public function it_will_audit_sync_individually() $this->assertArrayNotHasKey('users', $auditLast->old_values); } - /** - * @test - * - * @return void - */ - public function it_will_audit_sync_with_pivot_values() + public function test_it_will_audit_sync_with_pivot_values(): void { if (version_compare($this->app->version(), '8.0.0', '<')) { $this->markTestSkipped('This test is only for Laravel 8.0.0+'); } - $firstCategory = factory(Category::class)->create(); - $secondCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $secondCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach([$firstCategory->getKey() => ['pivot_type' => 'PIVOT_1']]); @@ -780,17 +662,12 @@ public function it_will_audit_sync_with_pivot_values() ); } - /** - * @test - * - * @return void - */ - public function it_will_audit_sync_by_closure() + public function test_it_will_audit_sync_by_closure(): void { - $firstCategory = factory(Category::class)->create(); - $secondCategory = factory(Category::class)->create(); - $thirdCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $secondCategory = Category::factory()->create(); + $thirdCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach([$firstCategory->getKey() => ['pivot_type' => 'PIVOT_1']]); $article->categories()->attach([$secondCategory->getKey() => ['pivot_type' => 'PIVOT_2']]); @@ -831,16 +708,11 @@ function ($categories) { ); } - /** - * @test - * - * @return void - */ - public function it_will_not_audit_sync_by_invalid_closure() + public function test_it_will_not_audit_sync_by_invalid_closure(): void { - $firstCategory = factory(Category::class)->create(); - $secondCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $secondCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach($firstCategory); @@ -857,16 +729,11 @@ function ($categories) { ); } - /** - * @test - * - * @return void - */ - public function it_will_audit_detach() + public function test_it_will_audit_detach(): void { - $firstCategory = factory(Category::class)->create(); - $secondCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $secondCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach($firstCategory); $article->categories()->attach($secondCategory); @@ -885,17 +752,12 @@ public function it_will_audit_detach() $this->assertGreaterThan($no_of_audits_before, $no_of_audits_after); } - /** - * @test - * - * @return void - */ - public function it_will_audit_detach_by_closure() + public function test_it_will_audit_detach_by_closure(): void { - $firstCategory = factory(Category::class)->create(); - $secondCategory = factory(Category::class)->create(); - $thirdCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $secondCategory = Category::factory()->create(); + $thirdCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach([$firstCategory->getKey() => ['pivot_type' => 'PIVOT_1']]); $article->categories()->attach([$secondCategory->getKey() => ['pivot_type' => 'PIVOT_2']]); @@ -928,15 +790,10 @@ function ($categories) { ); } - /** - * @test - * - * @return void - */ - public function it_will_not_audit_detach_by_invalid_closure() + public function test_it_will_not_audit_detach_by_invalid_closure(): void { - $firstCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach($firstCategory); @@ -953,15 +810,10 @@ function ($categories) { ); } - /** - * @test - * - * @return void - */ - public function it_will_audit_sync_without_changes() + public function test_it_will_audit_sync_without_changes(): void { - $firstCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach($firstCategory); @@ -979,18 +831,13 @@ public function it_will_audit_sync_without_changes() $this->assertGreaterThan($no_of_audits_before, $no_of_audits_after); } - /** - * @test - * - * @return void - */ - public function it_will_audit_sync_when_skipping_empty_values() + public function test_it_will_audit_sync_when_skipping_empty_values(): void { $this->app['config']->set('audit.empty_values', false); - $firstCategory = factory(Category::class)->create(); - $secondCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $secondCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach($firstCategory); @@ -1008,17 +855,12 @@ public function it_will_audit_sync_when_skipping_empty_values() $this->assertGreaterThan($no_of_audits_before, $no_of_audits_after); } - /** - * @test - * - * @return void - */ - public function it_will_not_audit_sync_when_skipping_empty_values_and_no_changes_made() + public function test_it_will_not_audit_sync_when_skipping_empty_values_and_no_changes_made(): void { $this->app['config']->set('audit.empty_values', false); - $firstCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach($firstCategory); @@ -1036,17 +878,12 @@ public function it_will_not_audit_sync_when_skipping_empty_values_and_no_changes $this->assertSame($no_of_audits_before, $no_of_audits_after); } - /** - * @test - * - * @return void - */ - public function it_will_not_audit_attach_when_skipping_empty_values_and_no_changes_made() + public function test_it_will_not_audit_attach_when_skipping_empty_values_and_no_changes_made(): void { $this->app['config']->set('audit.empty_values', false); - $firstCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach($firstCategory); @@ -1064,18 +901,13 @@ public function it_will_not_audit_attach_when_skipping_empty_values_and_no_chang $this->assertSame($no_of_audits_before, $no_of_audits_after); } - /** - * @test - * - * @return void - */ - public function it_will_not_audit_detach_when_skipping_empty_values_and_no_changes_made() + public function test_it_will_not_audit_detach_when_skipping_empty_values_and_no_changes_made(): void { $this->app['config']->set('audit.empty_values', false); - $firstCategory = factory(Category::class)->create(); - $secondCategory = factory(Category::class)->create(); - $article = factory(Article::class)->create(); + $firstCategory = Category::factory()->create(); + $secondCategory = Category::factory()->create(); + $article = Article::factory()->create(); $article->categories()->attach($firstCategory); @@ -1093,14 +925,9 @@ public function it_will_not_audit_detach_when_skipping_empty_values_and_no_chang $this->assertSame($no_of_audits_before, $no_of_audits_after); } - /** - * @test - * - * @return void - */ - public function can_audit_any_custom_event() + public function test_can_audit_any_custom_event(): void { - $article = factory(Article::class)->create(); + $article = Article::factory()->create(); $article->auditEvent = 'whateverYouWant'; $article->isCustomEvent = true; $article->auditCustomOld = [ @@ -1120,12 +947,7 @@ public function can_audit_any_custom_event() ]); } - /** - * @test - * - * @return void - */ - public function can_audit_custom_audit_model_implementation() + public function test_can_audit_custom_audit_model_implementation(): void { $audit = null; Event::listen(Audited::class, function ($event) use (&$audit) { diff --git a/tests/Functional/CommandTest.php b/tests/Functional/CommandTest.php index 7cf39aa6..c304b23f 100644 --- a/tests/Functional/CommandTest.php +++ b/tests/Functional/CommandTest.php @@ -6,10 +6,7 @@ class CommandTest extends AuditingTestCase { - /** - * @test - */ - public function it_will_generate_the_audit_driver() + public function test_it_will_generate_the_audit_driver(): void { $driverFilePath = sprintf( '%s/AuditDrivers/TestDriver.php', diff --git a/tests/Models/ApiModel.php b/tests/Models/ApiModel.php index 530ca074..7dd2d208 100644 --- a/tests/Models/ApiModel.php +++ b/tests/Models/ApiModel.php @@ -2,15 +2,20 @@ namespace OwenIt\Auditing\Tests\Models; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use OwenIt\Auditing\Contracts\Auditable; +use OwenIt\Auditing\Tests\database\factories\ApiModelFactory; class ApiModel extends Model implements Auditable { + use HasFactory; use \OwenIt\Auditing\Auditable; use SoftDeletes; + protected static string $factory = ApiModelFactory::class; + /** * @var string UUID key */ diff --git a/tests/Models/Article.php b/tests/Models/Article.php index b45aff00..aa51e5a0 100644 --- a/tests/Models/Article.php +++ b/tests/Models/Article.php @@ -3,16 +3,21 @@ namespace OwenIt\Auditing\Tests\Models; use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use OwenIt\Auditing\Contracts\Auditable; use OwenIt\Auditing\Tests\Casts\Money; +use OwenIt\Auditing\Tests\database\factories\ArticleFactory; class Article extends Model implements Auditable { + use HasFactory; use \OwenIt\Auditing\Auditable; use SoftDeletes; + protected static string $factory = ArticleFactory::class; + protected $laravel_version; /** diff --git a/tests/Models/Category.php b/tests/Models/Category.php index 178c70b6..d1be9ce3 100644 --- a/tests/Models/Category.php +++ b/tests/Models/Category.php @@ -2,4 +2,11 @@ namespace OwenIt\Auditing\Tests\Models; -class Category extends \Illuminate\Database\Eloquent\Model {} +use Illuminate\Database\Eloquent\Factories\HasFactory; +use OwenIt\Auditing\Tests\database\factories\CategoryFactory; + +class Category extends \Illuminate\Database\Eloquent\Model { + use HasFactory; + + protected static string $factory = CategoryFactory::class; +} diff --git a/tests/Models/User.php b/tests/Models/User.php index c005ab40..ba246bef 100644 --- a/tests/Models/User.php +++ b/tests/Models/User.php @@ -3,14 +3,19 @@ namespace OwenIt\Auditing\Tests\Models; use Illuminate\Contracts\Auth\Authenticatable; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use OwenIt\Auditing\Contracts\Auditable; +use OwenIt\Auditing\Tests\database\factories\UserFactory; class User extends Model implements Auditable, Authenticatable { + use HasFactory; use \Illuminate\Auth\Authenticatable; use \OwenIt\Auditing\Auditable; + protected static string $factory = UserFactory::class; + /** * {@inheritdoc} */ diff --git a/tests/Unit/AuditTest.php b/tests/Unit/AuditTest.php index 629bc306..b2130c56 100644 --- a/tests/Unit/AuditTest.php +++ b/tests/Unit/AuditTest.php @@ -13,19 +13,16 @@ use OwenIt\Auditing\Tests\Models\Article; use OwenIt\Auditing\Tests\Models\Money; use OwenIt\Auditing\Tests\Models\User; +use PHPUnit\Framework\Attributes\Group; class AuditTest extends AuditingTestCase { - /** - * @group Audit::resolveData - * - * @test - */ - public function it_resolves_audit_data() + #[Group('Audit::resolveData')] + public function test_it_resolves_audit_data(): void { $now = Carbon::now(); - $article = factory(Article::class)->create([ + $article = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -57,16 +54,12 @@ public function it_resolves_audit_data() ], $resolvedData, true); } - /** - * @group Audit::resolveData - * - * @test - */ - public function it_resolves_audit_data_including_user_attributes() + #[Group('Audit::resolveData')] + public function test_it_resolves_audit_data_including_user_attributes(): void { $now = Carbon::now(); - $user = factory(User::class)->create([ + $user = User::factory()->create([ 'is_admin' => 1, 'first_name' => 'rick', 'last_name' => 'Sanchez', @@ -75,7 +68,7 @@ public function it_resolves_audit_data_including_user_attributes() $this->actingAs($user); - $article = factory(Article::class)->create([ + $article = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -111,15 +104,11 @@ public function it_resolves_audit_data_including_user_attributes() ], $resolvedData, true); } - /** - * @group Audit::resolveData - * @group Audit::getDataValue - * - * @test - */ - public function it_returns_the_appropriate_auditable_data_values() + #[Group('Audit::resolveData')] + #[Group('Audit::getDataValue')] + public function test_it_returns_the_appropriate_auditable_data_values(): void { - $user = factory(User::class)->create([ + $user = User::factory()->create([ 'is_admin' => 1, 'first_name' => 'rick', 'last_name' => 'Sanchez', @@ -128,7 +117,7 @@ public function it_returns_the_appropriate_auditable_data_values() $this->actingAs($user); - $audit = factory(Article::class)->create([ + $audit = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -160,15 +149,11 @@ public function it_returns_the_appropriate_auditable_data_values() $this->assertNull($audit->getDataValue('invalid_key')); } - /** - * @group Audit::resolveData - * @group Audit::getDataValue - * - * @test - */ - public function it_returns_the_appropriate_auditable_data_values_with_custom_cast_value_object() + #[Group('Audit::resolveData')] + #[Group('Audit::getDataValue')] + public function test_it_returns_the_appropriate_auditable_data_values_with_custom_cast_value_object(): void { - $user = factory(User::class)->create([ + $user = User::factory()->create([ 'is_admin' => 1, 'first_name' => 'rick', 'last_name' => 'Sanchez', @@ -177,7 +162,7 @@ public function it_returns_the_appropriate_auditable_data_values_with_custom_cas $this->actingAs($user); - $article = factory(Article::class)->create([ + $article = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -196,14 +181,10 @@ public function it_returns_the_appropriate_auditable_data_values_with_custom_cas $this->assertEquals(new Money('12.45', 'USD'), $lastAudit->getModified()['price']['old']); } - /** - * @group Audit::getMetadata - * - * @test - */ - public function it_returns_audit_metadata_as_array() + #[Group('Audit::getMetadata')] + public function test_it_returns_audit_metadata_as_array(): void { - $audit = factory(Article::class)->create()->audits()->first(); + $audit = Article::factory()->create()->audits()->first(); $this->assertNotNull($audit); @@ -223,30 +204,23 @@ public function it_returns_audit_metadata_as_array() ], $metadata, true); } + #[Group('command-line-url-resolver')] /** * This test is meant to be run with specific command line "vendor/bin/phpunit tests/Unit/AuditTest.php --group command-line-url-resolver" - * - * @group command-line-url-resolver - * - * @test */ - public function it_returns_proper_command_line_in_url_audit_metadata() + public function test_it_returns_proper_command_line_in_url_audit_metadata(): void { - $audit = factory(Article::class)->create()->audits()->first(); + $audit = Article::factory()->create()->audits()->first(); $this->assertNotNull($audit); Assert::assertEquals($audit->getMetadata()['audit_url'], 'vendor/bin/phpunit tests/Unit/AuditTest.php --group command-line-url-resolver'); } - /** - * @group Audit::getMetadata - * - * @test - */ - public function it_returns_audit_metadata_including_user_attributes_as_array() + #[Group('Audit::getMetadata')] + public function test_it_returns_audit_metadata_including_user_attributes_as_array(): void { - $user = factory(User::class)->create([ + $user = User::factory()->create([ 'is_admin' => 1, 'first_name' => 'rick', 'last_name' => 'Sanchez', @@ -255,7 +229,7 @@ public function it_returns_audit_metadata_including_user_attributes_as_array() $this->actingAs($user); - $audit = factory(Article::class)->create()->audits()->first(); + $audit = Article::factory()->create()->audits()->first(); $this->assertNotNull($audit); @@ -281,14 +255,10 @@ public function it_returns_audit_metadata_including_user_attributes_as_array() ], $metadata, true); } - /** - * @group Audit::getMetadata - * - * @test - */ - public function it_returns_audit_metadata_as_json_string() + #[Group('Audit::getMetadata')] + public function test_it_returns_audit_metadata_as_json_string(): void { - $audit = factory(Article::class)->create()->audits()->first(); + $audit = Article::factory()->create()->audits()->first(); $this->assertNotNull($audit); @@ -312,14 +282,10 @@ public function it_returns_audit_metadata_as_json_string() $this->assertSame($expected, json_decode($metadata, true)); } - /** - * @group Audit::getMetadata - * - * @test - */ - public function it_returns_audit_metadata_including_user_attributes_as_json_string() + #[Group('Audit::getMetadata')] + public function test_it_returns_audit_metadata_including_user_attributes_as_json_string(): void { - $user = factory(User::class)->create([ + $user = User::factory()->create([ 'is_admin' => 1, 'first_name' => 'rick', 'last_name' => 'Sanchez', @@ -328,7 +294,7 @@ public function it_returns_audit_metadata_including_user_attributes_as_json_stri $this->actingAs($user); - $audit = factory(Article::class)->create()->audits()->first(); + $audit = Article::factory()->create()->audits()->first(); $this->assertNotNull($audit); @@ -360,16 +326,12 @@ public function it_returns_audit_metadata_including_user_attributes_as_json_stri $this->assertSame($expected, json_decode($metadata, true)); } - /** - * @group Audit::getModified - * - * @test - */ - public function it_returns_auditable_modified_attributes_as_array() + #[Group('Audit::getModified')] + public function test_it_returns_auditable_modified_attributes_as_array(): void { $now = Carbon::now()->second(0)->microsecond(0); - $audit = factory(Article::class)->create([ + $audit = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -399,16 +361,12 @@ public function it_returns_auditable_modified_attributes_as_array() ], $modified, true); } - /** - * @group Audit::getModified - * - * @test - */ - public function it_returns_auditable_modified_attributes_as_json_string() + #[Group('Audit::getModified')] + public function test_it_returns_auditable_modified_attributes_as_json_string(): void { $now = Carbon::now()->second(0)->microsecond(0); - $audit = factory(Article::class)->create([ + $audit = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -441,14 +399,10 @@ public function it_returns_auditable_modified_attributes_as_json_string() $this->assertSame($expected, json_decode($modified, true)); } - /** - * @group Audit::getModified - * - * @test - */ - public function it_returns_decoded_auditable_attributes() + #[Group('Audit::getModified')] + public function test_it_returns_decoded_auditable_attributes(): void { - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); $this->assertTrue(itReturnsDecodedAuditableAttributesArticle::first()->is($model)); @@ -487,14 +441,10 @@ public function it_returns_decoded_auditable_attributes() ], $modified, true); } - /** - * @group Audit::getTags - * - * @test - */ - public function it_returns_tags() + #[Group('Audit::getTags')] + public function test_it_returns_tags(): void { - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); $audit = Audit::create([ 'event' => 'updated', @@ -511,14 +461,10 @@ public function it_returns_tags() ], $audit->getTags(), true); } - /** - * @group Audit::getTags - * - * @test - */ - public function it_returns_empty_tags() + #[Group('Audit::getTags')] + public function test_it_returns_empty_tags(): void { - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); $audit = Audit::create([ 'event' => 'updated', diff --git a/tests/Unit/AuditableObserverTest.php b/tests/Unit/AuditableObserverTest.php index 5816af6a..eb7512b1 100644 --- a/tests/Unit/AuditableObserverTest.php +++ b/tests/Unit/AuditableObserverTest.php @@ -9,15 +9,13 @@ use OwenIt\Auditing\Models\Audit; use OwenIt\Auditing\Tests\AuditingTestCase; use OwenIt\Auditing\Tests\Models\Article; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; class AuditableObserverTest extends AuditingTestCase { - /** - * @test - * - * @dataProvider auditableObserverDispatchTestProvider - */ - public function it_will_cancel_the_audit_dispatching_from_an_event_listener($eventMethod) + #[DataProvider('auditableObserverDispatchTestProvider')] + public function test_it_will_cancel_the_audit_dispatching_from_an_event_listener($eventMethod): void { Event::fake( [ @@ -30,7 +28,7 @@ public function it_will_cancel_the_audit_dispatching_from_an_event_listener($eve }); $observer = new AuditableObserver; - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); $observer->$eventMethod($model); @@ -39,17 +37,13 @@ public function it_will_cancel_the_audit_dispatching_from_an_event_listener($eve Event::assertNotDispatched(DispatchAudit::class); } - /** - * @test - * - * @dataProvider auditableObserverDispatchTestProvider - */ - public function it_dispatches_the_correct_events(string $eventMethod) + #[DataProvider('auditableObserverDispatchTestProvider')] + public function test_it_dispatches_the_correct_events(string $eventMethod): void { Event::fake(); $observer = new AuditableObserver; - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); $observer->$eventMethod($model); @@ -62,22 +56,17 @@ public function it_dispatches_the_correct_events(string $eventMethod) }); } - /** - * @group AuditableObserver::retrieved - * @group AuditableObserver::created - * @group AuditableObserver::updated - * @group AuditableObserver::deleted - * @group AuditableObserver::restoring - * @group AuditableObserver::restored - * - * @test - * - * @dataProvider auditableObserverTestProvider - */ - public function it_executes_the_auditor_successfully(string $eventMethod, bool $expectedBefore, bool $expectedAfter) + #[Group('AuditableObserver::retrieved')] + #[Group('AuditableObserver::created')] + #[Group('AuditableObserver::updated')] + #[Group('AuditableObserver::deleted')] + #[Group('AuditableObserver::restoring')] + #[Group('AuditableObserver::restored')] + #[DataProvider('auditableObserverTestProvider')] + public function test_it_executes_the_auditor_successfully(string $eventMethod, bool $expectedBefore, bool $expectedAfter): void { $observer = new AuditableObserver; - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); $this->assertSame($expectedBefore, $observer::$restoring); diff --git a/tests/Unit/AuditableTest.php b/tests/Unit/AuditableTest.php index 41d7a04f..0885a7b8 100644 --- a/tests/Unit/AuditableTest.php +++ b/tests/Unit/AuditableTest.php @@ -21,6 +21,8 @@ use OwenIt\Auditing\Tests\Models\Article; use OwenIt\Auditing\Tests\Models\ArticleExcludes; use OwenIt\Auditing\Tests\Models\User; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Group; use ReflectionClass; class AuditableTest extends AuditingTestCase @@ -38,12 +40,8 @@ protected function setUp(): void Relation::morphMap([], false); } - /** - * @group Auditable::withoutAuditing - * - * @test - */ - public function it_will_run_callback_with_model_auditing_disabled() + #[Group('Auditable::withoutAuditing')] + public function test_it_will_run_callback_with_model_auditing_disabled(): void { $this->assertFalse(Article::$auditingDisabled); @@ -58,12 +56,8 @@ public function it_will_run_callback_with_model_auditing_disabled() $this->assertSame('result', $result); } - /** - * @group Auditable::withoutAuditing - * - * @test - */ - public function it_will_run_callback_with_auditing_disabled() + #[Group('Auditable::withoutAuditing')] + public function test_it_will_run_callback_with_auditing_disabled(): void { $this->assertFalse(Article::$auditingDisabled); @@ -78,12 +72,8 @@ public function it_will_run_callback_with_auditing_disabled() $this->assertSame('result', $result); } - /** - * @group Auditable::withoutAuditing - * - * @test - */ - public function it_will_run_callback_then_restore_auditing_disabled() + #[Group('Auditable::withoutAuditing')] + public function test_it_will_run_callback_then_restore_auditing_disabled(): void { Article::$auditingDisabled = true; @@ -94,36 +84,24 @@ public function it_will_run_callback_then_restore_auditing_disabled() $this->assertTrue(Article::$auditingDisabled); } - /** - * @group Auditable::isAuditingEnabled - * - * @test - */ - public function it_will_not_audit_models_when_running_from_the_console() + #[Group('Auditable::isAuditingEnabled')] + public function test_it_will_not_audit_models_when_running_from_the_console(): void { $this->app['config']->set('audit.console', false); $this->assertFalse(Article::isAuditingEnabled()); } - /** - * @group Auditable::isAuditingEnabled - * - * @test - */ - public function it_will_audit_models_when_running_from_the_console() + #[Group('Auditable::isAuditingEnabled')] + public function test_it_will_audit_models_when_running_from_the_console(): void { $this->app['config']->set('audit.console', true); $this->assertTrue(Article::isAuditingEnabled()); } - /** - * @group Auditable::isAuditingEnabled - * - * @test - */ - public function it_will_always_audit_models_when_not_running_from_the_console() + #[Group('Auditable::isAuditingEnabled')] + public function test_it_will_always_audit_models_when_not_running_from_the_console(): void { App::shouldReceive('runningInConsole') ->andReturn(false); @@ -133,12 +111,8 @@ public function it_will_always_audit_models_when_not_running_from_the_console() $this->assertTrue(Article::isAuditingEnabled()); } - /** - * @group Auditable::bootAuditable - * - * @test - */ - public function it_will_boot_trait_when_static_flag_is_set() + #[Group('Auditable::bootAuditable')] + public function test_it_will_boot_trait_when_static_flag_is_set(): void { App::spy(); @@ -152,24 +126,16 @@ public function it_will_boot_trait_when_static_flag_is_set() Article::$auditingDisabled = false; } - /** - * @group Auditable::getAuditEvent - * - * @test - */ - public function it_returns_null_when_the_audit_event_is_not_set() + #[Group('Auditable::getAuditEvent')] + public function test_it_returns_null_when_the_audit_event_is_not_set(): void { $model = new Article; $this->assertNull($model->getAuditEvent()); } - /** - * @group Auditable::getAuditEvent - * - * @test - */ - public function it_returns_the_audit_event_that_has_been_set() + #[Group('Auditable::getAuditEvent')] + public function test_it_returns_the_audit_event_that_has_been_set(): void { $model = new Article; $model->setAuditEvent('created'); @@ -177,12 +143,8 @@ public function it_returns_the_audit_event_that_has_been_set() $this->assertSame('created', $model->getAuditEvent()); } - /** - * @group Auditable::getAuditEvents - * - * @test - */ - public function it_returns_the_default_audit_events() + #[Group('Auditable::getAuditEvents')] + public function test_it_returns_the_default_audit_events(): void { $model = new Article; @@ -194,12 +156,8 @@ public function it_returns_the_default_audit_events() ], $model->getAuditEvents(), true); } - /** - * @group Auditable::getAuditEvents - * - * @test - */ - public function it_returns_the_custom_audit_events_from_attribute() + #[Group('Auditable::getAuditEvents')] + public function test_it_returns_the_custom_audit_events_from_attribute(): void { $model = new Article; @@ -214,12 +172,8 @@ public function it_returns_the_custom_audit_events_from_attribute() ], $model->getAuditEvents(), true); } - /** - * @group Auditable::getAuditEvents - * - * @test - */ - public function it_returns_the_custom_audit_events_from_config() + #[Group('Auditable::getAuditEvents')] + public function test_it_returns_the_custom_audit_events_from_config(): void { $this->app['config']->set('audit.events', [ 'published' => 'getPublishedEventAttributes', @@ -234,13 +188,9 @@ public function it_returns_the_custom_audit_events_from_config() ], $model->getAuditEvents(), true); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::readyForAuditing - * - * @test - */ - public function it_is_not_ready_for_auditing_with_custom_event() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::readyForAuditing')] + public function test_it_is_not_ready_for_auditing_with_custom_event(): void { $model = new Article; @@ -248,13 +198,9 @@ public function it_is_not_ready_for_auditing_with_custom_event() $this->assertFalse($model->readyForAuditing()); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::readyForAuditing - * - * @test - */ - public function it_is_ready_for_auditing_with_custom_events() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::readyForAuditing')] + public function test_it_is_ready_for_auditing_with_custom_events(): void { $model = new Article; @@ -274,13 +220,9 @@ public function it_is_ready_for_auditing_with_custom_events() $this->assertTrue($model->readyForAuditing()); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::readyForAuditing - * - * @test - */ - public function it_is_ready_for_auditing_with_regular_events() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::readyForAuditing')] + public function test_it_is_ready_for_auditing_with_regular_events(): void { $model = new Article; @@ -297,13 +239,9 @@ public function it_is_ready_for_auditing_with_regular_events() $this->assertTrue($model->readyForAuditing()); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - */ - public function it_fails_when_an_invalid_audit_event_is_set() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + public function test_it_fails_when_an_invalid_audit_event_is_set(): void { $this->expectException(AuditingException::class); $this->expectExceptionMessage('A valid audit event has not been set'); @@ -315,19 +253,14 @@ public function it_fails_when_an_invalid_audit_event_is_set() $model->toAudit(); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - * - * @dataProvider auditCustomAttributeGetterFailTestProvider - */ - public function it_fails_when_the_custom_attribute_getters_are_missing( + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + #[DataProvider('auditCustomAttributeGetterFailTestProvider')] + public function test_it_fails_when_the_custom_attribute_getters_are_missing( string $event, array $auditEvents, string $exceptionMessage - ) { + ): void { $this->expectException(AuditingException::class); $this->expectExceptionMessage($exceptionMessage); @@ -374,13 +307,9 @@ public static function auditCustomAttributeGetterFailTestProvider(): array ]; } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - */ - public function it_fails_when_the_ip_address_resolver_implementation_is_invalid() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + public function test_it_fails_when_the_ip_address_resolver_implementation_is_invalid(): void { $this->expectException(AuditingException::class); $this->expectExceptionMessage('Invalid Resolver implementation for: ip_address'); @@ -394,13 +323,9 @@ public function it_fails_when_the_ip_address_resolver_implementation_is_invalid( $model->toAudit(); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - */ - public function it_fails_when_the_url_resolver_implementation_is_invalid() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + public function test_it_fails_when_the_url_resolver_implementation_is_invalid(): void { $this->expectException(AuditingException::class); $this->expectExceptionMessage('Invalid Resolver implementation for: url'); @@ -414,13 +339,9 @@ public function it_fails_when_the_url_resolver_implementation_is_invalid() $model->toAudit(); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - */ - public function it_fails_when_the_user_agent_resolver_implementation_is_invalid() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + public function test_it_fails_when_the_user_agent_resolver_implementation_is_invalid(): void { $this->expectException(AuditingException::class); $this->expectExceptionMessage('Invalid Resolver implementation for: user_agent'); @@ -434,13 +355,9 @@ public function it_fails_when_the_user_agent_resolver_implementation_is_invalid( $model->toAudit(); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - */ - public function it_fails_when_the_user_resolver_implementation_is_invalid() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + public function test_it_fails_when_the_user_resolver_implementation_is_invalid(): void { $this->expectException(AuditingException::class); $this->expectExceptionMessage('Invalid UserResolver implementation'); @@ -454,17 +371,13 @@ public function it_fails_when_the_user_resolver_implementation_is_invalid() $model->toAudit(); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - */ - public function it_returns_the_audit_data() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + public function test_it_returns_the_audit_data(): void { $now = Carbon::now(); - $model = factory(Article::class)->make([ + $model = Article::factory()->make([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -496,31 +409,26 @@ public function it_returns_the_audit_data() ], $auditData, true); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - * - * @dataProvider userResolverProvider - */ - public function it_returns_the_audit_data_including_user_attributes( + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + #[DataProvider('userResolverProvider')] + public function test_it_returns_the_audit_data_including_user_attributes( string $guard, string $driver, ?int $id = null, ?string $type = null - ) { + ): void { $this->app['config']->set('audit.user.guards', [ $guard, ]); - $user = factory(User::class)->create(); + $user = User::factory()->create(); $this->actingAs($user, $driver); $now = Carbon::now(); - $model = factory(Article::class)->make([ + $model = Article::factory()->make([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -582,17 +490,13 @@ public static function userResolverProvider(): array ]; } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - */ - public function it_excludes_attributes_from_the_audit_data_when_in_strict_mode() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + public function test_it_excludes_attributes_from_the_audit_data_when_in_strict_mode(): void { $this->app['config']->set('audit.strict', true); - $model = factory(Article::class)->make([ + $model = Article::factory()->make([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -631,18 +535,14 @@ public function it_excludes_attributes_from_the_audit_data_when_in_strict_mode() ], $auditData, true); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - */ - public function it_fails_when_the_attribute_modifier_implementation_is_invalid() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + public function test_it_fails_when_the_attribute_modifier_implementation_is_invalid(): void { $this->expectException(AuditingException::class); $this->expectExceptionMessage('Invalid AttributeModifier implementation: invalidAttributeRedactorOrEncoder'); - $model = factory(Article::class)->make(); + $model = Article::factory()->make(); $model->attributeModifiers = [ 'title' => 'invalidAttributeRedactorOrEncoder', @@ -653,15 +553,11 @@ public function it_fails_when_the_attribute_modifier_implementation_is_invalid() $model->toAudit(); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::toAudit - * - * @test - */ - public function it_modifies_the_audit_attributes_successfully() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::toAudit')] + public function test_it_modifies_the_audit_attributes_successfully(): void { - $model = factory(Article::class)->make([ + $model = Article::factory()->make([ 'title' => 'How To Audit Models', 'content' => 'N/A', 'reviewed' => 0, @@ -701,14 +597,10 @@ public function it_modifies_the_audit_attributes_successfully() ], $model->toAudit(), true); } - /** - * @group Auditable::setAuditEvent - * @group Auditable::transformAudit - * @group Auditable::toAudit - * - * @test - */ - public function it_transforms_the_audit_data() + #[Group('Auditable::setAuditEvent')] + #[Group('Auditable::transformAudit')] + #[Group('Auditable::toAudit')] + public function test_it_transforms_the_audit_data(): void { $model = new class extends Article { @@ -742,24 +634,16 @@ public function transformAudit(array $data): array ], $auditData, true); } - /** - * @group Auditable::getAuditInclude - * - * @test - */ - public function it_returns_the_default_attributes_to_be_included_in_the_audit() + #[Group('Auditable::getAuditInclude')] + public function test_it_returns_the_default_attributes_to_be_included_in_the_audit(): void { $model = new Article; Assert::assertArraySubset([], $model->getAuditInclude(), true); } - /** - * @group Auditable::getAuditInclude - * - * @test - */ - public function it_returns_the_custom_attributes_to_be_included_in_the_audit() + #[Group('Auditable::getAuditInclude')] + public function test_it_returns_the_custom_attributes_to_be_included_in_the_audit(): void { $model = new Article; @@ -774,24 +658,16 @@ public function it_returns_the_custom_attributes_to_be_included_in_the_audit() ], $model->getAuditInclude(), true); } - /** - * @group Auditable::getAuditExclude - * - * @test - */ - public function it_returns_the_default_attributes_to_be_excluded_from_the_audit() + #[Group('Auditable::getAuditExclude')] + public function test_it_returns_the_default_attributes_to_be_excluded_from_the_audit(): void { $model = new Article; Assert::assertArraySubset([], $model->getAuditExclude(), true); } - /** - * @group Auditable::getAuditExclude - * - * @test - */ - public function it_returns_the_custom_attributes_to_be_excluded_from_the_audit() + #[Group('Auditable::getAuditExclude')] + public function test_it_returns_the_custom_attributes_to_be_excluded_from_the_audit(): void { $model = new Article; @@ -804,12 +680,7 @@ public function it_returns_the_custom_attributes_to_be_excluded_from_the_audit() ], $model->getAuditExclude(), true); } - /** - * @test - * - * @return void - */ - public function it_excludes_attributes_from_exclude() + public function test_it_excludes_attributes_from_exclude(): void { $model = new ArticleExcludes; @@ -825,24 +696,16 @@ public function it_excludes_attributes_from_exclude() $this->assertArrayNotHasKey('title', $audit->getModified()); } - /** - * @group Auditable::getAuditStrict - * - * @test - */ - public function it_returns_the_default_audit_strict_value() + #[Group('Auditable::getAuditStrict')] + public function test_it_returns_the_default_audit_strict_value(): void { $model = new Article; $this->assertFalse($model->getAuditStrict()); } - /** - * @group Auditable::getAuditStrict - * - * @test - */ - public function it_returns_the_custom_audit_strict_value_from_attribute() + #[Group('Auditable::getAuditStrict')] + public function test_it_returns_the_custom_audit_strict_value_from_attribute(): void { $model = new Article; @@ -851,12 +714,8 @@ public function it_returns_the_custom_audit_strict_value_from_attribute() $this->assertTrue($model->getAuditStrict()); } - /** - * @group Auditable::getAuditStrict - * - * @test - */ - public function it_returns_the_custom_audit_strict_value_from_config() + #[Group('Auditable::getAuditStrict')] + public function test_it_returns_the_custom_audit_strict_value_from_config(): void { $this->app['config']->set('audit.strict', true); @@ -865,24 +724,16 @@ public function it_returns_the_custom_audit_strict_value_from_config() $this->assertTrue($model->getAuditStrict()); } - /** - * @group Auditable::getAuditTimestamps - * - * @test - */ - public function it_returns_the_default_audit_timestamps_value() + #[Group('Auditable::getAuditTimestamps')] + public function test_it_returns_the_default_audit_timestamps_value(): void { $model = new Article; $this->assertFalse($model->getAuditTimestamps()); } - /** - * @group Auditable::getAuditTimestamps - * - * @test - */ - public function it_returns_the_custom_audit_timestamps_value_from_attribute() + #[Group('Auditable::getAuditTimestamps')] + public function test_it_returns_the_custom_audit_timestamps_value_from_attribute(): void { $model = new Article; @@ -891,12 +742,8 @@ public function it_returns_the_custom_audit_timestamps_value_from_attribute() $this->assertTrue($model->getAuditTimestamps()); } - /** - * @group Auditable::getAuditTimestamps - * - * @test - */ - public function it_returns_the_custom_audit_timestamps_value_from_config() + #[Group('Auditable::getAuditTimestamps')] + public function test_it_returns_the_custom_audit_timestamps_value_from_config(): void { $this->app['config']->set('audit.timestamps', true); @@ -905,24 +752,16 @@ public function it_returns_the_custom_audit_timestamps_value_from_config() $this->assertTrue($model->getAuditTimestamps()); } - /** - * @group Auditable::getAuditDriver - * - * @test - */ - public function it_returns_the_default_audit_driver_value() + #[Group('Auditable::getAuditDriver')] + public function test_it_returns_the_default_audit_driver_value(): void { $model = new Article; $this->assertSame('database', $model->getAuditDriver()); } - /** - * @group Auditable::getAuditDriver - * - * @test - */ - public function it_returns_the_custom_audit_driver_value_from_attribute() + #[Group('Auditable::getAuditDriver')] + public function test_it_returns_the_custom_audit_driver_value_from_attribute(): void { $model = new Article; @@ -931,12 +770,8 @@ public function it_returns_the_custom_audit_driver_value_from_attribute() $this->assertSame('RedisDriver', $model->getAuditDriver()); } - /** - * @group Auditable::getAuditDriver - * - * @test - */ - public function it_returns_the_custom_audit_driver_value_from_config() + #[Group('Auditable::getAuditDriver')] + public function test_it_returns_the_custom_audit_driver_value_from_config(): void { $this->app['config']->set('audit.driver', 'RedisDriver'); @@ -945,24 +780,16 @@ public function it_returns_the_custom_audit_driver_value_from_config() $this->assertSame('RedisDriver', $model->getAuditDriver()); } - /** - * @group Auditable::getAuditThreshold - * - * @test - */ - public function it_returns_the_default_audit_threshold_value() + #[Group('Auditable::getAuditThreshold')] + public function test_it_returns_the_default_audit_threshold_value(): void { $model = new Article; $this->assertSame(0, $model->getAuditThreshold()); } - /** - * @group Auditable::getAuditThreshold - * - * @test - */ - public function it_returns_the_custom_audit_threshold_value_from_attribute() + #[Group('Auditable::getAuditThreshold')] + public function test_it_returns_the_custom_audit_threshold_value_from_attribute(): void { $model = new Article; @@ -971,12 +798,8 @@ public function it_returns_the_custom_audit_threshold_value_from_attribute() $this->assertSame(10, $model->getAuditThreshold()); } - /** - * @group Auditable::getAuditThreshold - * - * @test - */ - public function it_returns_the_custom_audit_threshold_value_from_config() + #[Group('Auditable::getAuditThreshold')] + public function test_it_returns_the_custom_audit_threshold_value_from_config(): void { $this->app['config']->set('audit.threshold', 200); @@ -985,24 +808,16 @@ public function it_returns_the_custom_audit_threshold_value_from_config() $this->assertSame(200, $model->getAuditThreshold()); } - /** - * @group Auditable::generateTags - * - * @test - */ - public function it_returns_the_default_generated_audit_tags() + #[Group('Auditable::generateTags')] + public function test_it_returns_the_default_generated_audit_tags(): void { $model = new Article; Assert::assertArraySubset([], $model->generateTags(), true); } - /** - * @group Auditable::generateTags - * - * @test - */ - public function it_returns_the_custom_generated_audit_tags() + #[Group('Auditable::generateTags')] + public function test_it_returns_the_custom_generated_audit_tags(): void { $model = new class extends Article { @@ -1021,12 +836,8 @@ public function generateTags(): array ], $model->generateTags(), true); } - /** - * @group Auditable::transitionTo - * - * @test - */ - public function it_fails_to_transition_when_the_audit_auditable_type_does_not_match_the_model_type() + #[Group('Auditable::transitionTo')] + public function test_it_fails_to_transition_when_the_audit_auditable_type_does_not_match_the_model_type(): void { $this->expectException(AuditableTransitionException::class); $this->expectExceptionMessage('Expected Auditable type OwenIt\Auditing\Tests\Models\Article, got OwenIt\Auditing\Tests\Models\User instead'); @@ -1040,19 +851,15 @@ public function it_fails_to_transition_when_the_audit_auditable_type_does_not_ma $model->transitionTo($audit); } - /** - * @group Auditable::transitionTo - * - * @test - */ - public function it_works_on_times_restored_correctly() + #[Group('Auditable::transitionTo')] + public function test_it_works_on_times_restored_correctly(): void { config(['app.timezone' => 'America/New_York']); date_default_timezone_set('America/New_York'); $originalStart = new Carbon('2022-01-01 12:00:00'); - $article = factory(Article::class)->create([ + $article = Article::factory()->create([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -1075,12 +882,8 @@ public function it_works_on_times_restored_correctly() $this->assertEquals($model->published_at, $originalStart); } - /** - * @group Auditable::transitionTo - * - * @test - */ - public function it_fails_to_transition_when_the_audit_auditable_type_does_not_match_the_morph_map_value() + #[Group('Auditable::transitionTo')] + public function test_it_fails_to_transition_when_the_audit_auditable_type_does_not_match_the_morph_map_value(): void { $this->expectException(AuditableTransitionException::class); $this->expectExceptionMessage('Expected Auditable type articles, got users instead'); @@ -1098,38 +901,30 @@ public function it_fails_to_transition_when_the_audit_auditable_type_does_not_ma $model->transitionTo($audit); } - /** - * @group Auditable::transitionTo - * - * @test - */ - public function it_fails_to_transition_when_the_audit_auditable_id_does_not_match_the_model_id() + #[Group('Auditable::transitionTo')] + public function test_it_fails_to_transition_when_the_audit_auditable_id_does_not_match_the_model_id(): void { $this->expectException(AuditableTransitionException::class); $this->expectExceptionMessage('Expected Auditable id (integer)2, got (integer)1 instead'); - $firstModel = factory(Article::class)->create(); + $firstModel = Article::factory()->create(); $firstAudit = $firstModel->audits()->first(); $this->assertNotNull($firstAudit); $firstAudit->auditable_id = $firstModel->id; - $secondModel = factory(Article::class)->create(); + $secondModel = Article::factory()->create(); $secondModel->transitionTo($firstAudit); } - /** - * @group Auditable::transitionTo - * - * @test - */ - public function it_fails_to_transition_when_the_audit_auditable_id_type_does_not_match_the_model_id_type() + #[Group('Auditable::transitionTo')] + public function test_it_fails_to_transition_when_the_audit_auditable_id_type_does_not_match_the_model_id_type(): void { $this->expectException(AuditableTransitionException::class); $this->expectExceptionMessage('Expected Auditable id (integer)1, got (string)1 instead'); - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); $audit = Audit::create([ 'event' => 'updated', @@ -1150,14 +945,10 @@ public function it_fails_to_transition_when_the_audit_auditable_id_type_does_not $model->transitionTo($audit); } - /** - * @group Auditable::transitionTo - * - * @test - */ - public function it_transitions_when_the_audit_auditable_id_type_does_not_match_the_model_id_type() + #[Group('Auditable::transitionTo')] + public function test_it_transitions_when_the_audit_auditable_id_type_does_not_match_the_model_id_type(): void { - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); // Key depends on type if ($model->getKeyType() == 'string') { @@ -1175,17 +966,13 @@ public function it_transitions_when_the_audit_auditable_id_type_does_not_match_t $this->assertInstanceOf(Auditable::class, $model->transitionTo($audit)); } - /** - * @group Auditable::transitionTo - * - * @test - */ - public function it_fails_to_transition_when_an_attribute_redactor_is_set() + #[Group('Auditable::transitionTo')] + public function test_it_fails_to_transition_when_an_attribute_redactor_is_set(): void { $this->expectException(AuditableTransitionException::class); $this->expectExceptionMessage('Cannot transition states when an AttributeRedactor is set'); - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); $model->attributeModifiers = [ 'title' => RightRedactor::class, @@ -1200,14 +987,10 @@ public function it_fails_to_transition_when_an_attribute_redactor_is_set() $model->transitionTo($audit); } - /** - * @group Auditable::transitionTo - * - * @test - */ - public function it_fails_to_transition_when_the_auditable_attribute_compatibility_is_not_met() + #[Group('Auditable::transitionTo')] + public function test_it_fails_to_transition_when_the_auditable_attribute_compatibility_is_not_met(): void { - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); $incompatibleAudit = Audit::create([ 'event' => 'created', @@ -1241,21 +1024,16 @@ public function it_fails_to_transition_when_the_auditable_attribute_compatibilit $this->assertTrue($exceptionWasThrown); } - /** - * @group Auditable::transitionTo - * - * @test - * - * @dataProvider auditableTransitionTestProvider - */ - public function it_transitions_to_another_model_state( + #[Group('Auditable::transitionTo')] + #[DataProvider('auditableTransitionTestProvider')] + public function test_it_transitions_to_another_model_state( bool $morphMap, array $oldValues, array $newValues, array $oldExpectation, array $newExpectation - ) { - $models = factory(Article::class, 2)->create([ + ): void { + $models = Article::factory()->count(2)->create([ 'title' => 'Facilis voluptas qui impedit deserunt vitae quidem.', 'content' => 'Consectetur distinctio nihil eveniet cum. Expedita dolores animi dolorum eos repellat rerum.', ]); @@ -1287,12 +1065,9 @@ public function it_transitions_to_another_model_state( $this->assertSame($newExpectation, $models[1]->getDirty()); } - /** - * @test - */ - public function it_works_with_string_key_models() + public function test_it_works_with_string_key_models(): void { - $model = factory(ApiModel::class)->create(); + $model = ApiModel::factory()->create(); $model->save(); $model->refresh(); @@ -1414,14 +1189,11 @@ public static function auditableTransitionTestProvider(): array ]; } - /** - * @test - */ - public function it_works_when_config_allowed_array_value_is_true() + public function test_it_works_when_config_allowed_array_value_is_true(): void { $this->app['config']->set('audit.allowed_array_values', true); - $model = factory(Article::class)->make([ + $model = Article::factory()->make([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, @@ -1459,14 +1231,11 @@ public function it_works_when_config_allowed_array_value_is_true() ], $auditData, true); } - /** - * @test - */ - public function it_works_when_config_allowed_array_value_is_false() + public function test_it_works_when_config_allowed_array_value_is_false(): void { $this->app['config']->set('audit.allowed_array_values', false); - $model = factory(Article::class)->make([ + $model = Article::factory()->make([ 'title' => 'How To Audit Eloquent Models', 'content' => 'First step: install the laravel-auditing package.', 'reviewed' => 1, diff --git a/tests/Unit/ProcessDispatchAuditTest.php b/tests/Unit/ProcessDispatchAuditTest.php index 988cc47a..8c3b6673 100644 --- a/tests/Unit/ProcessDispatchAuditTest.php +++ b/tests/Unit/ProcessDispatchAuditTest.php @@ -12,10 +12,7 @@ class ProcessDispatchAuditTest extends AuditingTestCase { - /** - * @test - */ - public function it_is_listening_to_the_correct_event() + public function test_it_is_listening_to_the_correct_event(): void { if (version_compare($this->app->version(), '8.0.0', '<')) { $this->markTestSkipped('This test is only for Laravel 8.0.0+'); @@ -29,14 +26,11 @@ public function it_is_listening_to_the_correct_event() ); } - /** - * @test - */ - public function it_gets_properly_queued() + public function test_it_gets_properly_queued(): void { Queue::fake(); - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); app()->make('events')->dispatch(new DispatchAudit($model)); @@ -47,10 +41,7 @@ public function it_gets_properly_queued() }); } - /** - * @test - */ - public function it_can_have_connection_and_queue_set() + public function test_it_can_have_connection_and_queue_set(): void { $this->app['config']->set('audit.queue.connection', 'redis'); $this->app['config']->set('audit.queue.queue', 'audits'); @@ -58,7 +49,7 @@ public function it_can_have_connection_and_queue_set() Queue::fake(); - $model = factory(Article::class)->create(); + $model = Article::factory()->create(); app()->make('events')->dispatch(new DispatchAudit($model)); diff --git a/tests/database/factories/ApiModelFactory.php b/tests/database/factories/ApiModelFactory.php index 667cd101..f1dfffb5 100644 --- a/tests/database/factories/ApiModelFactory.php +++ b/tests/database/factories/ApiModelFactory.php @@ -1,20 +1,20 @@ define(ApiModel::class, function (Faker $faker) { - return [ - 'api_model_id' => Uuid::uuid4(), - 'content' => $faker->unique()->paragraph(6), - 'published_at' => null, - ]; -}); + public function definition() + { + return [ + 'api_model_id' => fake()->uuid(), + 'content' => fake()->unique()->paragraph(6), + 'published_at' => null, + ]; + } +} diff --git a/tests/database/factories/ArticleFactory.php b/tests/database/factories/ArticleFactory.php index 83f3c6ef..ff3f2d31 100644 --- a/tests/database/factories/ArticleFactory.php +++ b/tests/database/factories/ArticleFactory.php @@ -1,20 +1,21 @@ define(Article::class, function (Faker $faker) { - return [ - 'title' => $faker->unique()->sentence, - 'content' => $faker->unique()->paragraph(6), - 'published_at' => null, - 'reviewed' => $faker->randomElement([0, 1]), - ]; -}); + public function definition() + { + return [ + 'title' => fake()->unique()->sentence, + 'content' => fake()->unique()->paragraph(6), + 'published_at' => null, + 'reviewed' => fake()->randomElement([0, 1]), + ]; + } +} diff --git a/tests/database/factories/CategoryFactory.php b/tests/database/factories/CategoryFactory.php index 8b8b4386..02bc187e 100644 --- a/tests/database/factories/CategoryFactory.php +++ b/tests/database/factories/CategoryFactory.php @@ -1,16 +1,18 @@ define(\OwenIt\Auditing\Tests\Models\Category::class, function (Faker $faker) { - return [ - 'name' => $faker->unique()->colorName(), - ]; -}); +class CategoryFactory extends Factory +{ + protected $model = Category::class; + + public function definition() + { + return [ + 'name' => fake()->unique()->colorName(), + ]; + } +} diff --git a/tests/database/factories/UserFactory.php b/tests/database/factories/UserFactory.php index 4cbd8cc7..6dd01a7d 100644 --- a/tests/database/factories/UserFactory.php +++ b/tests/database/factories/UserFactory.php @@ -1,20 +1,21 @@ define(User::class, function (Faker $faker) { - return [ - 'is_admin' => $faker->randomElement([0, 1]), - 'first_name' => $faker->firstName, - 'last_name' => $faker->lastName, - 'email' => $faker->unique()->safeEmail, - ]; -}); + public function definition() + { + return [ + 'is_admin' => fake()->randomElement([0, 1]), + 'first_name' => fake()->firstName, + 'last_name' => fake()->lastName, + 'email' => fake()->unique()->safeEmail, + ]; + } +} diff --git a/tests/database/migrations/0000_00_00_000000_create_audits_test_table.php b/tests/database/migrations/0000_00_00_000000_create_audits_test_table.php new file mode 100644 index 00000000..c36dab42 --- /dev/null +++ b/tests/database/migrations/0000_00_00_000000_create_audits_test_table.php @@ -0,0 +1,3 @@ +unsignedInteger('tenant_id')->nullable(); }); } -} + + /** + * Reverse the migrations. + */ + public function down(): void + { + } +}; diff --git a/tests/database/migrations/0000_00_00_000002_create_users_test_table.php b/tests/database/migrations/0000_00_00_000002_create_users_test_table.php index 99f3fc12..f42b713a 100644 --- a/tests/database/migrations/0000_00_00_000002_create_users_test_table.php +++ b/tests/database/migrations/0000_00_00_000002_create_users_test_table.php @@ -4,14 +4,12 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateUsersTestTable extends Migration +return new class extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { Schema::create('users', function (Blueprint $table) { $table->increments('id'); @@ -32,12 +30,10 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::drop('model_has_users'); Schema::drop('users'); } -} +}; diff --git a/tests/database/migrations/0000_00_00_000003_create_api_models_test_table.php b/tests/database/migrations/0000_00_00_000003_create_api_models_test_table.php index 09f60b17..74685c76 100644 --- a/tests/database/migrations/0000_00_00_000003_create_api_models_test_table.php +++ b/tests/database/migrations/0000_00_00_000003_create_api_models_test_table.php @@ -4,14 +4,12 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateApiModelsTestTable extends Migration +return new class extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { Schema::create('api_models', function (Blueprint $table) { $table->uuid('api_model_id'); @@ -24,11 +22,9 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::drop('api_models'); } -} +}; diff --git a/tests/database/migrations/0000_00_00_000003_create_articles_test_table.php b/tests/database/migrations/0000_00_00_000003_create_articles_test_table.php index 35d29cc8..14eba27a 100644 --- a/tests/database/migrations/0000_00_00_000003_create_articles_test_table.php +++ b/tests/database/migrations/0000_00_00_000003_create_articles_test_table.php @@ -4,14 +4,12 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateArticlesTestTable extends Migration +return new class extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { Schema::create('articles', function (Blueprint $table) { $table->increments('id'); @@ -29,11 +27,9 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::drop('articles'); } -} +}; diff --git a/tests/database/migrations/0000_00_00_000003_create_categories_test_table.php b/tests/database/migrations/0000_00_00_000003_create_categories_test_table.php index 79d450ca..e8470b2f 100644 --- a/tests/database/migrations/0000_00_00_000003_create_categories_test_table.php +++ b/tests/database/migrations/0000_00_00_000003_create_categories_test_table.php @@ -4,14 +4,12 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -class CreateCategoriesTestTable extends Migration +return new class extends Migration { /** * Run the migrations. - * - * @return void */ - public function up() + public function up(): void { Schema::create('categories', function (Blueprint $table) { $table->increments('id'); @@ -30,12 +28,10 @@ public function up() /** * Reverse the migrations. - * - * @return void */ - public function down() + public function down(): void { Schema::drop('model_has_categories'); Schema::drop('categories'); } -} +};