Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve publication feature testing #815

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
8cddd6b
Create StaticSiteBuilderPublicationModuleTest.php
caendesilva Jan 8, 2023
fbd9cf5
Merge branch 'publications-feature' into add-additional-tests-for-the…
caendesilva Jan 8, 2023
2788977
Add method stubs
caendesilva Jan 8, 2023
24698f1
Create setUp method to throw on console exceptions during the tests
caendesilva Jan 8, 2023
7317d38
Start sketching up filesystem for test
caendesilva Jan 8, 2023
004cc50
Mark unfinished tests as incomplete
caendesilva Jan 8, 2023
f5bba61
Update publications seeder to seed Markdown body content
caendesilva Jan 8, 2023
417af7a
Create using action instead of through command
caendesilva Jan 8, 2023
b50e9f7
Create method to get test collection for fields covering all types
caendesilva Jan 8, 2023
4c0b7f6
Merge branch 'publications-feature' into add-additional-tests-for-the…
caendesilva Jan 8, 2023
68b0099
Add filesystem assertions
caendesilva Jan 8, 2023
d81075c
Build the site and run baseline assertions
caendesilva Jan 8, 2023
0b085eb
Structure test method
caendesilva Jan 8, 2023
23fe361
Update test class documentation
caendesilva Jan 8, 2023
21ec575
Apply fixes from StyleCI
StyleCIBot Jan 8, 2023
adbceeb
Include index in foreach
caendesilva Jan 8, 2023
044bbf7
Set the tagGroup for tag fields
caendesilva Jan 8, 2023
ff0afdb
Seed publications
caendesilva Jan 8, 2023
163a3f2
Split out count to expression to make it more readable
caendesilva Jan 8, 2023
2293f1d
Test with the seeded files
caendesilva Jan 8, 2023
0f1c44f
Split out the extensive test as we only need to test that once
caendesilva Jan 8, 2023
d27b882
Simplify subsequent test case as it does not need to retest everything
caendesilva Jan 8, 2023
8041cca
Extract method
caendesilva Jan 8, 2023
dd9259a
Use assertSame
caendesilva Jan 8, 2023
5e6f7fa
Apply fixes from StyleCI
StyleCIBot Jan 8, 2023
1c9330b
Reorder test mehods
caendesilva Jan 8, 2023
a0cec1b
Import used functions
caendesilva Jan 8, 2023
77b8c5a
Implement the test
caendesilva Jan 8, 2023
3e14966
Test compiling with paginated publication type
caendesilva Jan 8, 2023
93d63ba
Update incomplete test
caendesilva Jan 8, 2023
43a83fd
Add todo
caendesilva Jan 8, 2023
5bc340f
Merge branch 'publications-feature' into add-additional-tests-for-the…
caendesilva Jan 8, 2023
f8dc263
Apply fixes from StyleCI
StyleCIBot Jan 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function create(): void
$this->generatePublicationData();
$identifier = Str::slug(substr($this->canonicalValue, 0, 64));

$page = new PublicationPage($identifier, $this->matter, '## Write something awesome.', $this->pubType);
$page = new PublicationPage($identifier, $this->matter, "## Write something awesome.\n\n{$this->randomMarkdownLines(rand(0, 16))}\n\n", $this->pubType);
$page->save();
}
}
Expand Down Expand Up @@ -203,4 +203,14 @@ private function fakeUrl(): string
{
return 'https://example.com/'.$this->fakeWord();
}

private function randomMarkdownLines(int $count): string
{
$lines = [];
for ($i = 0; $i < $count; $i++) {
$lines[] = $this->fakeSentence(rand(0, 15));
}

return implode("\n", $lines);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,6 @@ protected function updateSchema(string $type, string $name): void
protected function assertBaseline(MarkdownDocument $publication): void
{
$this->assertCount(2, $publication->matter()->toArray());
$this->assertSame('## Write something awesome.', $publication->markdown()->body());
$this->assertStringStartsWith('## Write something awesome.', $publication->markdown()->body());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

declare(strict_types=1);

namespace Hyde\Framework\Testing\Feature;

use function collect;
use Hyde\Facades\Filesystem;
use Hyde\Framework\Actions\CreatesNewPublicationType;
use Hyde\Framework\Actions\SeedsPublicationFiles;
use Hyde\Framework\Features\Publications\Models\PublicationType;
use Hyde\Framework\Features\Publications\PublicationFieldTypes;
use Hyde\Hyde;
use Hyde\Testing\TestCase;
use Illuminate\Support\Collection;
use function range;

/**
* Tests that publication pages are compiled properly when building the static site.
*
* These tests provide a high level overview of the entire publications feature.
*/
class StaticSiteBuilderPublicationModuleTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

config(['app.throw_on_console_exception' => true]);
}

public function testCompilingWithPublicationTypeWithSeededFilesContainingAllFieldTypes()
{
// Setup

$this->directory('test-publication');

$creator = new CreatesNewPublicationType('Test Publication', $this->getAllFields());
$creator->create();

$this->assertCount(3, Filesystem::files('test-publication'));

$this->assertFileExists('test-publication/schema.json');
$this->assertFileExists('test-publication/detail.blade.php');
$this->assertFileExists('test-publication/list.blade.php');

// Test site build without any publications

$this->artisan('build')->assertSuccessful();

$this->assertCount(1, Filesystem::files('_site/test-publication'));
$this->assertFileExists('_site/test-publication/index.html');

$this->resetSite();

// Test site build with publications

$seeder = new SeedsPublicationFiles(PublicationType::get('test-publication'), 5);
$seeder->create();

$this->assertCount(3 + 5, Filesystem::files('test-publication'));

Hyde::boot(); // Reboot the kernel to discover the new publications

$this->artisan('build')->assertSuccessful();

$this->assertCount(1 + 5, Filesystem::files('_site/test-publication'));

$this->resetSite();
}

public function testCompilingWithPublicationTypeThatUsesThePublishedViews()
{
$this->directory('test-publication');

(new CreatesNewPublicationType('Test Publication', collect([])))->create();
$this->assertCount(3, Filesystem::files('test-publication'));

foreach (range(1, 5) as $i) {
$this->file("test-publication/publication-$i.md", "## Test publication $i");
}

$this->artisan('build')->assertSuccessful();

$this->assertSame([
'index.html',
'publication-1.html',
'publication-2.html',
'publication-3.html',
'publication-4.html',
'publication-5.html',
], $this->getFilenamesInDirectory('_site/test-publication'));

$this->resetSite();
}

public function testCompilingWithPublicationTypeThatUsesTheVendorViews()
{
$this->directory('test-publication');

(new CreatesNewPublicationType('Test Publication', collect([])))->create();
$type = PublicationType::get('test-publication');
$type->detailTemplate = 'hyde::layouts.publication_detail';
$type->listTemplate = 'hyde::layouts.publication_list';
$type->save();

foreach (range(1, 5) as $i) {
$this->file("test-publication/publication-$i.md", "## Test publication $i");
}

$this->artisan('build')->assertSuccessful();

$this->assertSame([
'index.html',
'publication-1.html',
'publication-2.html',
'publication-3.html',
'publication-4.html',
'publication-5.html',
], $this->getFilenamesInDirectory('_site/test-publication'));

$this->resetSite();
}

public function testCompilingWithPublicationTypeThatUsesThePublishedPaginatedViews()
{
$this->directory('test-publication');

(new CreatesNewPublicationType('Test Publication', collect([])))->create();

$type = PublicationType::get('test-publication');
$type->listTemplate = 'hyde::layouts.publication_paginated_list';
$type->pagination->pageSize = 2;
$type->save();

foreach (range(1, 5) as $i) {
$this->file("test-publication/publication-$i.md", "## Test publication $i");
}

$this->artisan('build')->assertSuccessful();

$this->assertSame([
'index.html',
'page-1.html',
'page-2.html',
'page-3.html',
'publication-1.html',
'publication-2.html',
'publication-3.html',
'publication-4.html',
'publication-5.html',
], $this->getFilenamesInDirectory('_site/test-publication'));

// TODO test that the pagination links are correct

$this->resetSite();
}

public function testCompilingWithPublicationTypeThatUsesThePaginatedVendorViews()
{
$this->directory('test-publication');

(new CreatesNewPublicationType('Test Publication', collect([])))->create();
// TODO assert the paginated template was published once we implement that

$this->markTestIncomplete();
}

protected function getAllFields(): Collection
{
$types = PublicationFieldTypes::collect();

$array = [];
foreach ($types as $index => $type) {
$array[$index] = [
'name' => "{$type->name}Field",
'type' => $type->value,
];

if ($type === PublicationFieldTypes::Tag) {
$array[$index]['tagGroup'] = 'myTagGroup';
}
}

return collect($array);
}

protected function getFilenamesInDirectory(string $directory): array
{
return collect(Filesystem::files($directory))->map(fn ($file) => $file->getFilename())->toArray();
}
}