Skip to content

Commit

Permalink
Test Improvements (#73)
Browse files Browse the repository at this point in the history
* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Fix code styling

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* Fix code styling

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

* wip

Signed-off-by: Mior Muhammad Zaki <[email protected]>

---------

Signed-off-by: Mior Muhammad Zaki <[email protected]>
Co-authored-by: crynobone <[email protected]>
  • Loading branch information
crynobone and crynobone authored Sep 19, 2023
1 parent 1fe9705 commit 8fb268f
Show file tree
Hide file tree
Showing 14 changed files with 71 additions and 55 deletions.
8 changes: 7 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
"Tests\\": "tests/",
"Workbench\\App\\": "workbench/app/",
"Workbench\\Database\\Factories\\": "workbench/database/factories/"
}
},
"extra": {
Expand All @@ -59,6 +61,10 @@
"config": {
"sort-packages": true
},
"scripts": {
"post-autoload-dump": "@prepare",
"prepare": "@php vendor/bin/testbench package:discover --ansi"
},
"minimum-stability": "dev",
"prefer-stable": true
}
8 changes: 8 additions & 0 deletions testbench.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
providers:
- Laravel\Pennant\PennantServiceProvider

migrations:
- database/migrations

workbench:
install: true
7 changes: 5 additions & 2 deletions tests/Feature/ArrayDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
use Laravel\Pennant\Feature;
use RuntimeException;
use Tests\TestCase;
use Workbench\App\Models\User;

use function Orchestra\Testbench\workbench_path;

class ArrayDriverTest extends TestCase
{
Expand Down Expand Up @@ -857,13 +860,13 @@ public function test_it_can_set_for_all()
public function test_it_can_auto_register_feature_classes()
{
Feature::define('marketing-design', 'marketing-design-value');
Feature::discover('Tests\\FeatureClasses', __DIR__.'/../FeatureClasses');
Feature::discover('Workbench\\App\\Features', workbench_path('app/Features'));

$all = Feature::all();

$this->assertSame([
'marketing-design' => 'marketing-design-value',
'Tests\\FeatureClasses\\NewApi' => 'new-api-value',
'Workbench\\App\\Features\\NewApi' => 'new-api-value',
], $all);
}

Expand Down
17 changes: 8 additions & 9 deletions tests/Feature/DatabaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema;
use Laravel\Pennant\Contracts\FeatureScopeable;
use Laravel\Pennant\Events\AllFeaturesPurged;
use Laravel\Pennant\Events\DynamicallyRegisteringFeatureClass;
Expand All @@ -20,6 +19,10 @@
use Laravel\Pennant\Events\UnknownFeatureResolved;
use Laravel\Pennant\Feature;
use Tests\TestCase;
use Workbench\App\Models\User;
use Workbench\Database\Factories\UserFactory;

use function Orchestra\Testbench\workbench_path;

class DatabaseDriverTest extends TestCase
{
Expand Down Expand Up @@ -365,15 +368,11 @@ public function toFeatureIdentifier($driver): mixed

public function test_it_serializes_eloquent_models()
{
Schema::create('users', function ($table) {
$table->id();
$table->timestamps();
});
Feature::for(User::create())->activate('foo');
Feature::for(UserFactory::new()->create())->activate('foo');

$scope = DB::table('features')->value('scope');

$this->assertStringContainsString('Tests\Feature\User|1', $scope);
$this->assertStringContainsString('Workbench\App\Models\User|1', $scope);
}

public function test_it_can_load_feature_state_into_memory()
Expand Down Expand Up @@ -1047,13 +1046,13 @@ public function test_it_can_set_for_all()
public function test_it_can_auto_register_feature_classes()
{
Feature::define('marketing-design', 'marketing-design-value');
Feature::discover('Tests\\FeatureClasses', __DIR__.'/../FeatureClasses');
Feature::discover('Workbench\\App\\Features', workbench_path('app/Features'));

$all = Feature::all();

$this->assertSame([
'marketing-design' => 'marketing-design-value',
'Tests\\FeatureClasses\\NewApi' => 'new-api-value',
'Workbench\\App\\Features\\NewApi' => 'new-api-value',
], $all);
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/FeatureDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\Config;
use Laravel\Pennant\Feature;
use Tests\TestCase;

class FeatureDirectiveTest extends TestCase
{
use RefreshDatabase;
use LazilyRefreshDatabase;

protected function setUp(): void
{
Expand Down
5 changes: 3 additions & 2 deletions tests/Feature/FeatureManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Lottery;
use Laravel\Pennant\Feature;
use Tests\TestCase;
use Workbench\App\Models\User;

class FeatureManagerTest extends TestCase
{
use RefreshDatabase;
use LazilyRefreshDatabase;

public function test_it_can_chain_scope_additions()
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/FeatureMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Http\Request;
use Laravel\Pennant\Feature;
use Laravel\Pennant\Middleware\EnsureFeaturesAreActive;
Expand All @@ -13,7 +13,7 @@

class FeatureMiddlewareTest extends TestCase
{
use RefreshDatabase;
use LazilyRefreshDatabase;

private EnsureFeaturesAreActive $middleware;

Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/PurgeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use Laravel\Pennant\Feature;
use Tests\TestCase;

class PurgeCommandTest extends TestCase
{
use RefreshDatabase;
use LazilyRefreshDatabase;

public function test_it_can_purge_flags()
{
Expand Down
10 changes: 0 additions & 10 deletions tests/Feature/User.php

This file was deleted.

25 changes: 2 additions & 23 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,12 @@
namespace Tests;

use Laravel\Pennant\FeatureManager;
use Laravel\Pennant\PennantServiceProvider;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase as OrchestraTestCase;

abstract class TestCase extends OrchestraTestCase
{
/**
* Define database migrations.
*
* @return void
*/
protected function defineDatabaseMigrations()
{
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
}

/**
* Get package providers.
*
* @param \Illuminate\Foundation\Application $app
* @return array<int, class-string<\Illuminate\Support\ServiceProvider>>
*/
protected function getPackageProviders($app)
{
return [
PennantServiceProvider::class,
];
}
use WithWorkbench;

/**
* Create an instance of the manager.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\FeatureClasses;
namespace Workbench\App\Features;

class NewApi
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/Team.php → workbench/app/Models/Team.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Tests\Feature;
namespace Workbench\App\Models;

use Illuminate\Database\Eloquent\Model;

Expand Down
10 changes: 10 additions & 0 deletions workbench/app/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Workbench\App\Models;

use Illuminate\Foundation\Auth\User as Authenticable;

class User extends Authenticable
{
protected $guarded = [];
}
20 changes: 20 additions & 0 deletions workbench/database/factories/UserFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Workbench\Database\Factories;

use Workbench\App\Models\User;

/**
* @template TModel of \Workbench\App\Models\User
*
* @extends \Illuminate\Database\Eloquent\Factories\Factory<TModel>
*/
class UserFactory extends \Orchestra\Testbench\Factories\UserFactory
{
/**
* The name of the factory's corresponding model.
*
* @var class-string<TModel>
*/
protected $model = User::class;
}

0 comments on commit 8fb268f

Please sign in to comment.