Skip to content

Commit

Permalink
Always create when running test instead
Browse files Browse the repository at this point in the history
  • Loading branch information
nanaya committed Nov 6, 2023
1 parent 1a70ec0 commit 867d3bd
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 65 deletions.
50 changes: 1 addition & 49 deletions app/Libraries/Groups.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

use App\Models\Group;
use App\Traits\Memoizes;
use Ds\Set;
use Exception;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;

Expand Down Expand Up @@ -61,56 +59,10 @@ public function byIdOrFail(int|string|null $id): Group

/**
* Get a group by its identifier (e.g. "admin").
*
* If the requested group doesn't exist and it's one of the privilege
* related groups, a new is created.
*/
public function byIdentifier(string $id): ?Group
{
$group = $this->allByIdentifier()->get($id);

if ($group === null) {
static $privGroups = new Set([
'admin',
'alumni',
'announce',
'bng',
'bng_limited',
'bot',
'default',
'dev',
'gmt',
'loved',
'nat',
'no_profile',
]);

if (!$privGroups->contains($id)) {
return null;
}

try {
$group = Group::create([
'group_desc' => '',
'group_name' => $id,
'group_type' => 2,
'identifier' => $id,
'short_name' => $id,
])->fresh();
} catch (Exception $ex) {
if (!is_sql_unique_exception($ex)) {
throw $ex;
}
$group = Group::firstWhere(['identifier' => $id]);
}

// TODO: This shouldn't have to be called here, since it's already
// called by `Group::afterCommit`, but `Group::afterCommit` isn't
// running in tests when creating/saving `Group`s.
$this->resetMemoized();
}

return $group;
return $this->allByIdentifier()->get($id);
}

protected function fetch(): Collection
Expand Down
17 changes: 17 additions & 0 deletions app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,23 @@
*/
class Group extends Model implements AfterCommit
{
// Identifier of groups which involved in permission checks
// and assumed to always exist in database.
const PRIV_GROUPS = [
'admin',
'alumni',
'announce',
'bng',
'bng_limited',
'bot',
'default',
'dev',
'gmt',
'loved',
'nat',
'no_profile',
];

public $timestamps = false;

protected $casts = [
Expand Down
3 changes: 3 additions & 0 deletions phpunit.dusk.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<extensions>
<extension class="Tests\SeederExtension" />
</extensions>
<testsuites>
<testsuite name="Browser Test Suite">
<directory suffix="Test.php">./tests/Browser</directory>
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
bootstrap="vendor/autoload.php"
colors="true"
>
<extensions>
<extension class="Tests\SeederExtension" />
</extensions>
<testsuites>
<testsuite name="Application Test Suite">
<directory suffix="Test.php">./tests/</directory>
Expand Down
5 changes: 1 addition & 4 deletions tests/Browser/SanityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ private static function cleanup()
Authorize::truncate();
TopicTrack::truncate();
Genre::truncate();
Group::truncate();
Language::truncate();
LoginAttempt::truncate();
NewsPost::truncate();
Expand All @@ -111,8 +110,6 @@ private static function cleanup()
UserNotification::truncate();
UserProfileCustomization::truncate();
UserStatistics\Osu::truncate();

app('groups')->resetMemoized();
}

private static function createScaffolding()
Expand Down Expand Up @@ -232,7 +229,7 @@ private static function createScaffolding()
]);

// factory for /g/*
self::$scaffolding['group'] = Group::factory()->create();
self::$scaffolding['group'] = Group::first();

// factory for comments
self::$scaffolding['comment'] = Comment::factory()->create([
Expand Down
1 change: 0 additions & 1 deletion tests/Controllers/BeatmapsControllerSoloScoresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ public static function tearDownAfterClass(): void
Beatmapset::truncate();
Country::truncate();
Genre::truncate();
Group::truncate();
Language::truncate();
SoloScore::truncate();
User::truncate();
Expand Down
1 change: 0 additions & 1 deletion tests/Jobs/RemoveBeatmapsetSoloScoresTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ public function testHandle()
Beatmapset::truncate();
Country::truncate();
Genre::truncate();
Group::truncate();
Language::truncate();
Score::truncate();
ScorePerformance::truncate();
Expand Down
1 change: 0 additions & 1 deletion tests/Models/Solo/ScoreEsIndexTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ public static function tearDownAfterClass(): void
Beatmapset::truncate();
Country::truncate();
Genre::truncate();
Group::truncate();
Language::truncate();
Score::truncate();
User::truncate();
Expand Down
38 changes: 38 additions & 0 deletions tests/SeederExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the GNU Affero General Public License v3.0.
// See the LICENCE file in the repository root for full licence text.

declare(strict_types=1);

namespace Tests;

use App\Models\Group;
use PHPUnit\Runner\AfterLastTestHook;
use PHPUnit\Runner\BeforeFirstTestHook;

class SeederExtension implements AfterLastTestHook, BeforeFirstTestHook
{
public function executeAfterLastTest(): void
{
TestCase::withDbAccess(function () {
Group::truncate();
});
}

public function executeBeforeFirstTest(): void
{
TestCase::withDbAccess(function () {
Group::truncate();
foreach (Group::PRIV_GROUPS as $identifier) {
Group::create([
'group_desc' => '',
'group_name' => $identifier,
'group_type' => 2,
'identifier' => $identifier,
'short_name' => $identifier,
]);
}
});
}
}
18 changes: 9 additions & 9 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ class TestCase extends BaseTestCase
{
use ArraySubsetAsserts, CreatesApplication, DatabaseTransactions;

public static function withDbAccess(callable $callback): void
{
$db = (new static())->createApplication()->make('db');

$callback();

static::resetAppDb($db);
}

protected static function reindexScores()
{
$search = new ScoreSearch();
Expand All @@ -54,15 +63,6 @@ protected static function resetAppDb(DatabaseManager $database): void
}
}

protected static function withDbAccess(callable $callback): void
{
$db = (new static())->createApplication()->make('db');

$callback();

static::resetAppDb($db);
}

protected $connectionsToTransact = [
'mysql',
'mysql-chat',
Expand Down

0 comments on commit 867d3bd

Please sign in to comment.