Skip to content

Commit

Permalink
test: use refreshdatabase trait
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Jul 3, 2023
1 parent fd20ae3 commit 9c18466
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions tests/Jobs/PlatformStatsSummaryJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,54 @@

namespace Tests\Jobs;

use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\User;
use App\Wiki;
use App\WikiManager;
use App\WikiDb;
use App\Jobs\ProvisionWikiDbJob;
use Illuminate\Support\Facades\DB;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Database\DatabaseManager;
use Carbon\Carbon;
use App\Jobs\PlatformStatsSummaryJob;

class PlatformStatsSummaryJobTest extends TestCase
{
use DatabaseTransactions;
use RefreshDatabase;

private $numWikis = 5;
private $wikis = [];

private DatabaseManager $manager;

private $connectionsToTransact = ["mysql", "mw"];

private $db_prefix = "somecoolprefix";
private $db_name = "some_cool_db_name";

protected function setUp(): void {
parent::setUp();
for($n = 0; $n < $this->numWikis; $n++ ) {
DB::connection('mysql')->getPdo()->exec("DROP DATABASE IF EXISTS {$this->db_name}{$n};");
}
$this->seedWikis();
$this->manager = $this->app->make('db');
}

protected function tearDown(): void {
foreach($this->wikis as $wiki) {
$wiki['wiki']->wikiDb()->forceDelete();
$wiki['wiki']->forceDelete();
for($n = 0; $n < $this->numWikis; $n++ ) {
$this->manager->connection('mysql')->getPdo()->exec("DROP DATABASE IF EXISTS {$this->db_name}{$n};");
}
parent::tearDown();
}

private function seedWikis() {
$manager = $this->app->make('db');
for($n = 0; $n < $this->numWikis; $n++ ) {

$user = User::factory()->create(['verified' => true]);
$wiki = Wiki::factory()->create( [ 'deleted_at' => null ] );
WikiManager::factory()->create(['wiki_id' => $wiki->id, 'user_id' => $user->id]);

$job = new ProvisionWikiDbJob($this->db_prefix . $n, $this->db_name . $n, null);
$job->handle($manager);
$job->handle($this->manager);

$wikiDb = WikiDb::whereName($this->db_name.$n)->first();
$wikiDb->update( ['wiki_id' => $wiki->id] );
Expand All @@ -64,15 +63,13 @@ private function seedWikis() {
}
public function testQueryGetsStats()
{
$manager = $this->app->make('db');

$mockJob = $this->createMock(Job::class);
$mockJob->expects($this->never())->method('fail');

$job = new PlatformStatsSummaryJob();
$job->setJob($mockJob);

$job->handle($manager);
$job->handle($this->manager);
}

public function testGroupings()
Expand All @@ -82,7 +79,7 @@ public function testGroupings()

$job = new PlatformStatsSummaryJob();
$job->setJob($mockJob);

$testWikis = [
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki1.com' ] ),
Wiki::factory()->create( [ 'deleted_at' => null, 'domain' => 'wiki2.com' ] ),
Expand All @@ -92,14 +89,14 @@ public function testGroupings()
];

foreach($testWikis as $wiki) {
$wikiDB = WikiDb::create([
WikiDb::create([
'name' => 'mwdb_asdasfasfasf' . $wiki->id,
'user' => 'asdasd',
'password' => 'asdasfasfasf',
'version' => 'asdasdasdas',
'prefix' => 'asdasd',
'wiki_id' => $wiki->id
]);
]);
}
$stats = [
[ // inactive
Expand Down Expand Up @@ -145,10 +142,10 @@ public function testGroupings()
"platform_summary_version" => "v1"
],
];


$groups = $job->prepareStats($stats, $testWikis);

$this->assertEquals(
[
"total" => 4,
Expand All @@ -162,7 +159,7 @@ public function testGroupings()
"total_non_deleted_edits" => 1,
"platform_summary_version" => "v1"
],
$groups,
$groups,
);
}

Expand Down

0 comments on commit 9c18466

Please sign in to comment.