A testing

This commit is contained in:
a6a2f5842 2025-06-17 17:53:26 +02:00
parent 578b735f4f
commit 318e6a90ac
4 changed files with 113 additions and 3 deletions

View File

@ -39,8 +39,10 @@
"illuminate/support": "^8.12|^9.0|^10.0|^11.0|^12.0" "illuminate/support": "^8.12|^9.0|^10.0|^11.0|^12.0"
}, },
"require-dev": { "require-dev": {
"laravel/framework": "*",
"laravel/pint": "^1.22", "laravel/pint": "^1.22",
"laravel/framework": "*" "orchestra/testbench": "^10.4",
"phpunit/phpunit": "^12.2"
}, },
"extra": { "extra": {
"laravel": { "laravel": {
@ -50,5 +52,29 @@
} }
}, },
"minimum-stability": "dev", "minimum-stability": "dev",
"prefer-stable": true "prefer-stable": true,
} "autoload-dev": {
"psr-4": {
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/",
"Workbench\\Database\\Seeders\\": "workbench/database/seeders/"
}
},
"scripts": {
"post-autoload-dump": [
"@clear",
"@prepare"
],
"clear": "@php vendor/bin/testbench package:purge-skeleton --ansi",
"prepare": "@php vendor/bin/testbench package:discover --ansi",
"build": "@php vendor/bin/testbench workbench:build --ansi",
"serve": [
"Composer\\Config::disableProcessTimeout",
"@build",
"@php vendor/bin/testbench serve --ansi"
],
"lint": [
"@php vendor/bin/pint --ansi"
]
}
}

23
phpunit.xml.dist Normal file
View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
bootstrap="vendor/autoload.php"
colors="true"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd"
>
<source>
<include>
<directory suffix=".php">src/</directory>
</include>
</source>
<testsuites>
<testsuite name="Permissions Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<php>
<env name="CACHE_DRIVER" value="array"/>
<!-- APP_KEY required for Passport client -->
<env name="APP_KEY" value="base64:W99w+5JYz8SVGf5sx17gmPR6uoNCtWiEVc+9qu8iGEg="/>
<ini name="memory_limit" value="512M"/>
</php>
</phpunit>

33
testbench.yaml Normal file
View File

@ -0,0 +1,33 @@
laravel: '@testbench'
providers:
# - Workbench\App\Providers\WorkbenchServiceProvider
migrations:
- workbench/database/migrations
seeders:
- Workbench\Database\Seeders\DatabaseSeeder
workbench:
start: '/'
install: true
health: false
discovers:
web: true
api: true
commands: true
components: false
factories: true
views: false
build:
- asset-publish
- create-sqlite-db
- db-wipe
- migrate-fresh
assets:
- laravel-assets
sync:
- from: storage
to: workbench/storage
reverse: true

View File

@ -0,0 +1,28 @@
<?php
namespace Blax\Roles\Tests\Unit;
use Orchestra\Testbench\PHPUnit\TestCase;
class PermissionTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();
$this->loadLaravelMigrations(['--database' => 'testbench']);
$this->artisan('migrate', ['--database' => 'testbench']);
}
public function testHasPermission()
{
// Assuming you have a User model with the HasPermissions trait
$user = \App\Models\User::factory()->create();
// Add a permission to the user
$user->permissions()->attach(1, ['context' => 'test']);
// Check if the user has the permission
$this->assertTrue($user->hasPermission('view_posts', ['context' => 'test']));
$this->assertFalse($user->hasPermission('edit_posts', ['context' => 'test']));
}
}