Skip to content

Commit

Permalink
Add basic test coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseleite committed Dec 15, 2023
1 parent 3ce090c commit bac5c85
Showing 1 changed file with 132 additions and 0 deletions.
132 changes: 132 additions & 0 deletions tests/ReportTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php

namespace Tests;

use Illuminate\Support\Carbon;
use Statamic\Facades\Entry;
use Statamic\Facades\Term;
use Statamic\SeoPro\Reporting\Report;
use Statamic\Support\Str;

class ReportTest extends TestCase
{
public function setUp(): void
{
parent::setUp();

Entry::all()->each->delete();
Term::all()->each->delete();

if ($this->files->exists($path = $this->reportsPath())) {
$this->files->deleteDirectory($path);
}
}

/** @test */
public function it_can_save_pending_report()
{
$this->assertFileNotExists($this->reportsPath());

Carbon::setTestNow($now = now());
Report::create()->save();

$this->assertCount(1, $this->files->directories($this->reportsPath()));
$this->assertFileExists($this->reportsPath('1'));

$expected = <<<"EXPECTED"
date: $now->timestamp
status: pending
score: null
pages_crawled: null
results: null
EXPECTED;

$this->assertCount(1, $this->files->allFiles($this->reportsPath('1')));
$this->assertEquals($expected, $this->files->get($this->reportsPath('1/report.yaml')));
}

/** @test */
public function it_increments_report_folder_numbers()
{
$this->assertFileNotExists($this->reportsPath());

Report::create()->save();
Report::create()->save();
Report::create()->save();

$this->assertCount(3, $this->files->directories($this->reportsPath()));
$this->assertFileExists($this->reportsPath('1'));
$this->assertFileExists($this->reportsPath('2'));
$this->assertFileExists($this->reportsPath('3'));
}

/** @test */
public function it_can_generate_a_report()
{
$this
->generateEntries(5)
->generateTerms(5);

$this->assertFileNotExists($this->reportsPath());

Carbon::setTestNow($now = now());
Report::create()->save()->generate();

$expected = <<<"EXPECTED"
date: $now->timestamp
status: fail
score: 75.0
pages_crawled: 10
results:
SiteName: true
UniqueTitleTag: 0
UniqueMetaDescription: 10
NoUnderscoresInUrl: 0
ThreeSegmentUrls: 0
EXPECTED;

$this->assertCount(1, $this->files->files($this->reportsPath('1')));
$this->assertEquals($expected, $this->files->get($this->reportsPath('1/report.yaml')));

$this->assertFileExists($this->reportsPath('1/pages'));
$this->assertCount(10, $this->files->directories($this->reportsPath('1/pages')));
}

public function reportsPath($path = null)
{
if ($path) {
$path = Str::ensureLeft($path, '/');
}

return storage_path('statamic/seopro/reports').$path;
}

protected function generateEntries($count)
{
collect(range(1, $count))->each(function ($i) {
Entry::make()
->collection('articles')
->blueprint('article')
->slug('test-entry-'.$i)
->set('title', 'Test Entry '.$i)
->save();
});

return $this;
}

protected function generateTerms($count)
{
collect(range(1, $count))->each(function ($i) {
Term::make()
->slug($slug = 'test-term-'.$i)
->taxonomy('topics')
->set('title', 'Test Term '.$i)
->save();
});

return $this;
}
}

0 comments on commit bac5c85

Please sign in to comment.