|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tests\Metrics; |
| 4 | + |
| 5 | +use App\Metrics\App\WikiMetrics; |
| 6 | +use App\Wiki; |
| 7 | +use App\WikiDailyMetrics; |
| 8 | +use Carbon\Carbon; |
| 9 | +use Illuminate\Foundation\Testing\RefreshDatabase; |
| 10 | +use Tests\TestCase; |
| 11 | + |
| 12 | +class WikiMetricsTest extends TestCase |
| 13 | +{ |
| 14 | + use RefreshDatabase; |
| 15 | + |
| 16 | + |
| 17 | + public function testSuccessfullyAddRecords() |
| 18 | + { |
| 19 | + $wiki = Wiki::factory()->create([ |
| 20 | + 'domain' => 'thisfake.wikibase.cloud' |
| 21 | + ]); |
| 22 | + |
| 23 | + (new WikiMetrics())->saveMetrics($wiki); |
| 24 | + // Assert the metric is updated in the database |
| 25 | + $this->assertDatabaseHas('wiki_daily_metrics', [ |
| 26 | + 'date' => now()->toDateString() |
| 27 | + ]); |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + public function testDoesNotAddDuplicateRecordsWithOnlyDateChange() |
| 32 | + { |
| 33 | + $wiki = Wiki::factory()->create([ |
| 34 | + 'domain' => 'thisfake.wikibase.cloud' |
| 35 | + ]); |
| 36 | + //Insert an old metric value for a wiki |
| 37 | + WikiDailyMetrics::create([ |
| 38 | + 'id' => $wiki->id. '_'. Carbon::yesterday()->toDateString(), |
| 39 | + 'wiki_id' => $wiki->id, |
| 40 | + 'date' => Carbon::yesterday()->toDateString(), |
| 41 | + 'pages' => 0, |
| 42 | + 'is_deleted' => 0 |
| 43 | + ]); |
| 44 | + (new WikiMetrics())->saveMetrics($wiki); |
| 45 | + |
| 46 | + //Assert No new record was created for today |
| 47 | + $this->assertDatabaseMissing('wiki_daily_metrics', [ |
| 48 | + 'wiki_id' => $wiki->id, |
| 49 | + 'date' => Carbon::today()->toDateString() |
| 50 | + ]); |
| 51 | + } |
| 52 | +} |
| 53 | + |
0 commit comments