diff --git a/.gitignore b/.gitignore index 49ce3c1..61ead86 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1 @@ -/vendor \ No newline at end of file +/vendor diff --git a/composer.json b/composer.json index 78e8538..85db354 100644 --- a/composer.json +++ b/composer.json @@ -13,5 +13,7 @@ "psr-4": { "NovaTesting\\": "src/" } - } + }, + "minimum-stability": "dev", + "prefer-stable": true } diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..5851acd --- /dev/null +++ b/composer.lock @@ -0,0 +1,18 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", + "This file is @generated automatically" + ], + "content-hash": "4dcfba802a5250bbb0bb6d9ac207fa7a", + "packages": [], + "packages-dev": [], + "aliases": [], + "minimum-stability": "dev", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [], + "plugin-api-version": "2.2.0" +} diff --git a/src/Assert/AssertPolicies.php b/src/Assert/AssertPolicies.php index 788a4e9..12867d2 100644 --- a/src/Assert/AssertPolicies.php +++ b/src/Assert/AssertPolicies.php @@ -2,97 +2,126 @@ namespace NovaTesting\Assert; -use Laravel\Nova\Nova; use Illuminate\Support\Arr; use Illuminate\Testing\Assert as PHPUnit; +use Laravel\Nova\Nova; trait AssertPolicies { + public function assertCanView() + { + $model = $this->resolveNovaModel(); + + PHPUnit::assertTrue($model->authorizedToView); + } + + public function assertCannotView() + { + $model = $this->resolveNovaModel(); + + PHPUnit::assertFalse($model->authorizedToView); + } + public function assertCanCreate() { - PHPUnit::assertTrue( - collect(Nova::jsonVariables(request())['resources']) - ->where('uriKey', $this->novaParameters['resource']) - ->first()['authorizedToCreate'] - ); + $model = $this->resolveNovaModel(); + + PHPUnit::assertTrue($model->authorizedToCreate); } public function assertCannotCreate() { - PHPUnit::assertFalse( - collect(Nova::jsonVariables(request())['resources']) - ->where('uriKey', $this->novaParameters['resource']) - ->first()['authorizedToCreate'] - ); + $model = $this->resolveNovaModel(); + + PHPUnit::assertFalse($model->authorizedToCreate); } public function assertCanDelete() { - return $this->assertJsonFragment([ - 'authorizedToDelete' => true - ]); + $model = $this->resolveNovaModel(); + + PHPUnit::assertTrue($model->authorizedToDelete); } public function assertCannotDelete() { - return $this->assertJsonFragment([ - 'authorizedToDelete' => false - ]); + $model = $this->resolveNovaModel(); + + PHPUnit::assertFalse($model->authorizedToDelete); } public function assertCanForceDelete() { - return $this->assertJsonFragment([ - 'authorizedToForceDelete' => true - ]); + $model = $this->resolveNovaModel(); + + PHPUnit::assertTrue($model->authorizedToForceDelete); } public function assertCannotForceDelete() { - return $this->assertJsonFragment([ - 'authorizedToForceDelete' => false - ]); + $model = $this->resolveNovaModel(); + + PHPUnit::assertFalse($model->authorizedToForceDelete); } public function assertCanRestore() { - return $this->assertJsonFragment([ - 'authorizedToRestore' => true - ]); + $model = $this->resolveNovaModel(); + + PHPUnit::assertTrue($model->authorizedToRestore); } public function assertCannotRestore() { - return $this->assertJsonFragment([ - 'authorizedToRestore' => false - ]); + $model = $this->resolveNovaModel(); + + PHPUnit::assertFalse($model->authorizedToRestore); } public function assertCanUpdate() { - return $this->assertJsonFragment([ - 'authorizedToUpdate' => true - ]); + $model = $this->resolveNovaModel(); + + PHPUnit::assertTrue($model->authorizedToUpdate); } public function assertCannotUpdate() { - return $this->assertJsonFragment([ - 'authorizedToUpdate' => false - ]); + $model = $this->resolveNovaModel(); + + PHPUnit::assertFalse($model->authorizedToUpdate); } - public function assertCanView() + public function assertCanReplicate() { - return $this->assertJsonFragment([ - 'authorizedToView' => true - ]); + $model = $this->resolveNovaModel(); + + PHPUnit::assertTrue($model->authorizedToReplicate); } - public function assertCannotView() + public function assertCannotReplicate() + { + $model = $this->resolveNovaModel(); + + PHPUnit::assertFalse($model->authorizedToReplicate); + } + + public function assertCanImpersonate() + { + $model = $this->resolveNovaModel(); + + PHPUnit::assertTrue($model->authorizedToImpersonate); + } + + public function assertCannotImpersonate() + { + $model = $this->resolveNovaModel(); + + PHPUnit::assertFalse($model->authorizedToImpersonate); + } + + private function resolveNovaModel() { - return $this->assertJsonFragment([ - 'authorizedToView' => false - ]); + return reset($this->originalResponse()->baseResponse->getData()->resources); } } diff --git a/src/NovaResponse.php b/src/NovaResponse.php index db733d3..494ddb6 100644 --- a/src/NovaResponse.php +++ b/src/NovaResponse.php @@ -2,11 +2,12 @@ namespace NovaTesting; +use Illuminate\Database\Eloquent\Model; +use NovaTesting\Assert\AssertActions; use NovaTesting\Assert\AssertCards; use NovaTesting\Assert\AssertFields; -use NovaTesting\Assert\AssertLenses; -use NovaTesting\Assert\AssertActions; use NovaTesting\Assert\AssertFilters; +use NovaTesting\Assert\AssertLenses; use NovaTesting\Assert\AssertPolicies; use NovaTesting\Assert\AssertRelations; use NovaTesting\Assert\AssertResources;