Skip to content

Commit

Permalink
Support 8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Nov 6, 2024
1 parent 57d8084 commit 503b8a4
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,9 @@ jobs:
composer update --prefer-dist --no-interaction --no-progress
- name: Execute tests
if: ${{ matrix.php == 8.1 }}
run: vendor/bin/phpunit --testsuite="Feature"

- name: Execute tests
if: ${{ matrix.php != 8.1 }}
run: vendor/bin/phpunit
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="82-up">
<file>./tests/IntersectionTypeTest.php</file>
</testsuite>
</testsuites>
<source>
<include>
Expand Down
11 changes: 3 additions & 8 deletions tests/Feature/DatabaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1585,9 +1585,6 @@ public function testCanRetrieveAllFeaturesForDifferingScopeTypes(): void
Feature::define('array', fn (array $t) => 7);
Feature::define('string', fn (string $str) => 8);
Feature::define('team-or-user', fn (Team|User $model) => 9);
if ($supportsIntersectionTypes = version_compare(PHP_VERSION, '8.1', '>')) {
Feature::define('team-or-user-intersection', fn (Team|(Authenticatable&Authorizable) $model) => 10);
}

$features = Feature::for(new User)->all();
$this->assertSame([
Expand All @@ -1596,10 +1593,9 @@ public function testCanRetrieveAllFeaturesForDifferingScopeTypes(): void
'mixed' => 5,
'none' => 6,
'team-or-user' => 9,
...$supportsIntersectionTypes ? ['team-or-user-intersection' => 10] : [],
], $features);
$this->assertCount(2, DB::getQueryLog());
$this->assertCount(6, DB::table('features')->get()); // query
$this->assertCount(5, DB::table('features')->get()); // query

$features = Feature::for(new Team)->all();
$this->assertSame([
Expand All @@ -1608,10 +1604,9 @@ public function testCanRetrieveAllFeaturesForDifferingScopeTypes(): void
'mixed' => 5,
'none' => 6,
'team-or-user' => 9,
...$supportsIntersectionTypes ? ['team-or-user-intersection' => 10] : [],
], $features);
$this->assertCount(5, DB::getQueryLog());
$this->assertCount(12, DB::table('features')->get()); // query
$this->assertCount(10, DB::table('features')->get()); // query

$features = Feature::for('scope')->all();
$this->assertSame([
Expand All @@ -1620,7 +1615,7 @@ public function testCanRetrieveAllFeaturesForDifferingScopeTypes(): void
'string' => 8,
], $features);
$this->assertCount(8, DB::getQueryLog());
$this->assertCount(15, DB::table('features')->get());
$this->assertCount(13, DB::table('features')->get());
}
}

Expand Down
74 changes: 74 additions & 0 deletions tests/IntersectionTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Tests;

use Illuminate\Contracts\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Laravel\Pennant\Feature;
use Tests\TestCase;
use Workbench\App\Models\Team;
use Workbench\App\Models\User;

class IntersectionTypeTest extends TestCase
{
use RefreshDatabase;

protected function setUp(): void
{
parent::setUp();

Config:&:set('pennant.default', 'database');

DB::enableQueryLog();
}

public function testCanRetrieveAllFeaturesForDifferingScopeTypes(): void
{
Feature::define('user', fn (User $user) => 1);
Feature::define('nullable-user', fn (?User $user) => 2);
Feature::define('team', fn (Team $team) => 3);
Feature::define('nullable-team', fn (?Team $team) => 4);
Feature::define('mixed', fn (mixed $v) => 5);
Feature::define('none', fn ($v) => 6);
Feature::define('array', fn (array $t) => 7);
Feature::define('string', fn (string $str) => 8);
Feature::define('team-or-user', fn (Team|User $model) => 9);
Feature::define('team-or-user-intersection', fn (Team|(Authenticatable&Authorizable) $model) => 10);

$features = Feature::for(new User)->all();
$this->assertSame([
'user' => 1,
'nullable-user' => 2,
'mixed' => 5,
'none' => 6,
'team-or-user' => 9,
'team-or-user-intersection' => 10,
], $features);
$this->assertCount(2, DB::getQueryLog());
$this->assertCount(6, DB::table('features')->get()); // query

$features = Feature::for(new Team)->all();
$this->assertSame([
'team' => 3,
'nullable-team' => 4,
'mixed' => 5,
'none' => 6,
'team-or-user' => 9,
'team-or-user-intersection' => 10,
], $features);
$this->assertCount(5, DB::getQueryLog());
$this->assertCount(12, DB::table('features')->get()); // query

$features = Feature::for('scope')->all();
$this->assertSame([
'mixed' => 5,
'none' => 6,
'string' => 8,
], $features);
$this->assertCount(8, DB::getQueryLog());
$this->assertCount(15, DB::table('features')->get()); // query
}
}

0 comments on commit 503b8a4

Please sign in to comment.