Skip to content

Commit 6beffae

Browse files
authored
Merge pull request #9 from TappNetwork/update_tests
Add test stubs
2 parents 30127e8 + 4ecfe16 commit 6beffae

21 files changed

+592
-70
lines changed

.github/workflows/run-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ jobs:
4949
COMPOSER_AUTH: ${{ secrets.COMPOSER_AUTH }}
5050
run: |
5151
composer require "laravel/framework:${{ matrix.laravel }}" "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
52-
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
52+
composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-progress
5353
5454
- name: List Installed Dependencies
5555
run: composer show -D

PUBLISH_TESTS.md

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
# Test Integration Guide
2+
3+
This guide explains how the test stubs integrate with your Laravel application.
4+
5+
## How It Works
6+
7+
When you run `php artisan filament-forum:install-tests`, the command copies test files from the plugin's `stubs/tests` directory to your application's `tests/Feature` directory.
8+
9+
### Configuration Integration
10+
11+
The tests automatically read from your application's configuration:
12+
13+
```php
14+
// In the test files
15+
$userModel = config('filament-forum.user.model');
16+
$tenantModel = config('filament-forum.tenancy.model');
17+
```
18+
19+
This means the tests will use **your** User and Tenant models, not the plugin's internal test models.
20+
21+
## Test Structure
22+
23+
### FilamentForumTest.php
24+
25+
Tests basic forum functionality that works with or without tenancy:
26+
27+
```php
28+
it('can create a forum', function () {
29+
$user = $this->userModel::factory()->create();
30+
$this->actingAs($user);
31+
32+
$forum = Forum::factory()->create([
33+
'name' => 'Test Forum',
34+
]);
35+
36+
expect($forum->name)->toBe('Test Forum');
37+
});
38+
```
39+
40+
### FilamentForumTenancyTest.php
41+
42+
Tests multi-tenancy features and automatically skips if tenancy is disabled:
43+
44+
```php
45+
beforeEach(function () {
46+
if (! config('filament-forum.tenancy.enabled')) {
47+
$this->markTestSkipped('Tenancy is not enabled');
48+
}
49+
// ...
50+
});
51+
```
52+
53+
## Dynamic Tenant Column Names
54+
55+
The tests use the plugin's helper methods to get the correct tenant relationship and column names:
56+
57+
```php
58+
// Get the tenant relationship name (e.g., 'team', 'organization', 'company')
59+
$tenantRelationship = Forum::getTenantRelationshipName();
60+
61+
// Get the tenant column name (e.g., 'team_id', 'organization_id', 'company_id')
62+
$tenantColumn = Forum::getTenantRelationshipName() . '_id';
63+
```
64+
65+
This ensures tests work regardless of what you name your tenant model.
66+
67+
## Example Scenarios
68+
69+
### Scenario 1: Using Team Model
70+
71+
```php
72+
// config/filament-forum.php
73+
'tenancy' => [
74+
'enabled' => true,
75+
'model' => App\Models\Team::class,
76+
],
77+
```
78+
79+
Tests will:
80+
- Use `App\Models\Team::factory()` to create tenants
81+
- Look for `team_id` column in tables
82+
- Use `team()` relationship method
83+
84+
### Scenario 2: Using Organization Model
85+
86+
```php
87+
// config/filament-forum.php
88+
'tenancy' => [
89+
'enabled' => true,
90+
'model' => App\Models\Organization::class,
91+
],
92+
```
93+
94+
Tests will:
95+
- Use `App\Models\Organization::factory()` to create tenants
96+
- Look for `organization_id` column in tables
97+
- Use `organization()` relationship method
98+
99+
### Scenario 3: No Tenancy
100+
101+
```php
102+
// config/filament-forum.php
103+
'tenancy' => [
104+
'enabled' => false,
105+
],
106+
```
107+
108+
Tests will:
109+
- Run all basic tests from `FilamentForumTest.php`
110+
- Skip all tests in `FilamentForumTenancyTest.php`
111+
112+
## Customizing Tests
113+
114+
The published tests are meant to be modified for your needs. Here are some common customizations:
115+
116+
### Adding Custom Fields
117+
118+
```php
119+
it('can create a forum with custom fields', function () {
120+
$user = $this->userModel::factory()->create();
121+
$this->actingAs($user);
122+
123+
$forum = Forum::factory()->create([
124+
'name' => 'Test Forum',
125+
'slug' => 'test-forum',
126+
'custom_field' => 'custom value', // Your custom field
127+
]);
128+
129+
expect($forum->custom_field)->toBe('custom value');
130+
});
131+
```
132+
133+
### Testing Custom Permissions
134+
135+
```php
136+
it('respects forum permissions', function () {
137+
$user = $this->userModel::factory()->create();
138+
$admin = $this->userModel::factory()->create(['role' => 'admin']);
139+
140+
$this->actingAs($user);
141+
142+
$this->assertDatabaseMissing('forums', ['name' => 'Admin Forum']);
143+
144+
$this->actingAs($admin);
145+
146+
$forum = Forum::factory()->create(['name' => 'Admin Forum']);
147+
148+
expect($forum)->toBeInstanceOf(Forum::class);
149+
});
150+
```
151+
152+
### Testing with Multiple Tenants
153+
154+
```php
155+
it('can handle multiple tenant memberships', function () {
156+
$user = $this->userModel::factory()->create();
157+
$tenant1 = $this->tenantModel::factory()->create();
158+
$tenant2 = $this->tenantModel::factory()->create();
159+
160+
// Add user to both tenants (implement based on your membership logic)
161+
$user->teams()->attach([$tenant1->id, $tenant2->id]);
162+
163+
$this->actingAs($user);
164+
165+
// Test tenant switching
166+
Filament::setTenant($tenant1);
167+
expect(Filament::getTenant()->id)->toBe($tenant1->id);
168+
169+
Filament::setTenant($tenant2);
170+
expect(Filament::getTenant()->id)->toBe($tenant2->id);
171+
});
172+
```
173+
174+
## Best Practices
175+
176+
1. **Run tests after publishing**: Immediately after running `filament-forum:install-tests`, run the tests to verify they work with your setup
177+
2. **Customize for your needs**: The stubs are a starting point - modify them to match your application's requirements
178+
3. **Keep tests in sync**: When you upgrade the plugin, review the test stubs for any changes
179+
4. **Use factories**: Ensure your User and Tenant models have proper factories for reliable testing
180+
5. **Test edge cases**: Add tests for your specific business logic and edge cases
181+
182+
## Troubleshooting
183+
184+
### Tests fail with "Factory not found"
185+
186+
Create factories for your models:
187+
188+
```bash
189+
php artisan make:factory UserFactory --model=User
190+
php artisan make:factory TeamFactory --model=Team
191+
```
192+
193+
### Tests fail with "Table not found"
194+
195+
Run migrations in your test environment:
196+
197+
```bash
198+
php artisan migrate --env=testing
199+
```
200+
201+
Or configure in-memory SQLite in `phpunit.xml`:
202+
203+
```xml
204+
<env name="DB_CONNECTION" value="sqlite"/>
205+
<env name="DB_DATABASE" value=":memory:"/>
206+
```
207+
208+
Or add your test database in `phpunit.xml`:
209+
210+
```xml
211+
<env name="APP_ENV" value="testing"/>
212+
<env name="DB_DATABASE" value="myapp_test"/>
213+
```
214+
215+
### Tenancy tests always skip
216+
217+
Verify configuration:
218+
219+
```bash
220+
php artisan config:clear
221+
php artisan tinker
222+
>>> config('filament-forum.tenancy.enabled')
223+
=> true
224+
```

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,35 @@ class UserMentionedNotification extends Notification
514514
}
515515
```
516516

517+
## Testing
518+
519+
### Publishing Tests to Your Application
520+
521+
You can publish ready-to-use test files to your application to test the Filament Forum functionality:
522+
523+
```bash
524+
php artisan filament-forum:install-tests
525+
```
526+
527+
This will copy test files to your `tests/Feature` directory:
528+
- `FilamentForumTest.php` - Basic forum and post functionality tests
529+
- `FilamentForumTenancyTest.php` - Multi-tenancy specific tests (automatically skipped if tenancy is disabled)
530+
531+
The tests are written using [Pest](https://pestphp.com/) and automatically use your configured User and Tenant models from `config/filament-forum.php`.
532+
533+
**Requirements:**
534+
- Pest testing framework: `composer require pestphp/pest --dev`
535+
- Model factories for your User model and Tenant model (if using tenancy)
536+
537+
**Run the tests:**
538+
```bash
539+
php artisan test --filter=FilamentForum
540+
```
541+
542+
See the published tests for more examples of how to test the plugin in your application.
543+
544+
Read more about publishing tests [here](PUBLISH_TESTS.md).
545+
517546
## Changelog
518547

519548
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

database/migrations/create_favorite_forum_post_table.php.stub renamed to database/migrations/create_favorite_forum_post_table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,3 @@ public function down(): void
3636
Schema::dropIfExists('favorite_forum_post');
3737
}
3838
};
39-

database/migrations/create_forum_comment_reactions_table.php.stub renamed to database/migrations/create_forum_comment_reactions_table.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ public function up(): void
2121

2222
$table->foreignId('forum_comment_id')->constrained()->cascadeOnDelete();
2323
$table->morphs('reactor');
24-
24+
2525
if (config('database.default') === 'mysql') {
2626
$table->string('type', 50)->collation('utf8mb4_bin');
2727
} else {
2828
$table->string('type', 50);
2929
}
30-
30+
3131
$table->timestamps();
3232

3333
$table->unique(['forum_comment_id', 'reactor_type', 'reactor_id', 'type'], 'unique_comment_reaction');

database/migrations/create_forum_comments_table.php.stub renamed to database/migrations/create_forum_comments_table.php

File renamed without changes.

database/migrations/create_forum_post_views_table.php.stub renamed to database/migrations/create_forum_post_views_table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,3 @@ public function down(): void
3333
Schema::dropIfExists('forum_post_views');
3434
}
3535
};
36-
File renamed without changes.

database/migrations/create_forums_table.php.stub renamed to database/migrations/create_forums_table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,3 @@ public function down(): void
3737
Schema::dropIfExists('forums');
3838
}
3939
};
40-
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace Tapp\FilamentForum\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Filesystem\Filesystem;
7+
8+
class InstallTestsCommand extends Command
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*/
13+
protected $signature = 'filament-forum:install-tests
14+
{--force : Overwrite existing test files}';
15+
16+
/**
17+
* The console command description.
18+
*/
19+
protected $description = 'Install Filament Forum test files into your application';
20+
21+
/**
22+
* Execute the console command.
23+
*/
24+
public function handle(): int
25+
{
26+
$filesystem = new Filesystem;
27+
28+
// Ensure tests directory exists
29+
$filesystem->ensureDirectoryExists(base_path('tests/Feature'));
30+
31+
// Copy test files
32+
$testFiles = [
33+
'FilamentForumTest.php',
34+
'FilamentForumTenancyTest.php',
35+
];
36+
37+
foreach ($testFiles as $testFile) {
38+
$source = __DIR__.'/../../stubs/tests/Feature/'.$testFile;
39+
$destination = base_path('tests/Feature/'.$testFile);
40+
41+
if (file_exists($destination) && ! $this->option('force')) {
42+
if (! $this->confirm("The file {$testFile} already exists. Do you want to overwrite it?")) {
43+
$this->components->info("Skipped: {$testFile}");
44+
45+
continue;
46+
}
47+
}
48+
49+
$filesystem->copy($source, $destination);
50+
$this->components->info("Published: {$testFile}");
51+
}
52+
53+
$this->newLine();
54+
$this->components->info('Filament Forum tests published successfully!');
55+
$this->newLine();
56+
57+
// Show next steps
58+
$this->line('Next steps:');
59+
$this->line('1. Make sure you have Pest installed: composer require pestphp/pest --dev');
60+
$this->line('2. Run the tests: php artisan test --filter=FilamentForum');
61+
$this->newLine();
62+
63+
// Check if tenancy is enabled and show relevant message
64+
if (config('filament-forum.tenancy.enabled')) {
65+
$tenantModel = config('filament-forum.tenancy.model');
66+
$this->components->info("Tenancy is enabled with model: {$tenantModel}");
67+
$this->line('Make sure your tenant model has a factory for the tenancy tests to work.');
68+
} else {
69+
$this->components->warn('Tenancy is not enabled. Tenancy tests will be skipped.');
70+
$this->line('To enable tenancy, set filament-forum.tenancy.enabled to true in your config.');
71+
}
72+
73+
$this->newLine();
74+
75+
return self::SUCCESS;
76+
}
77+
}

0 commit comments

Comments
 (0)