Skip to content

Commit

Permalink
Add coverage for HTML generator
Browse files Browse the repository at this point in the history
  • Loading branch information
onigoetz committed Dec 22, 2022
1 parent a146392 commit bd10bc7
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 44 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
"phpunit/phpunit": "^9.5"
},
"scripts": {
"test": "php vendor/bin/phpunit",
"test:coverage": "XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-clover=coverage.xml --log-junit=test-report.xml",
"test:coverage-html": "XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html=build/coverage",
"test": "APP_ENV=test php vendor/bin/phpunit",
"test:coverage": "APP_ENV=test XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-clover=coverage.xml --log-junit=test-report.xml",
"test:coverage-html": "APP_ENV=test XDEBUG_MODE=coverage php vendor/bin/phpunit --coverage-html=build/coverage",
"lint": "php build/php-cs-fixer fix --config=.php-cs-fixer.php --dry-run -v",
"lint:fix": "php build/php-cs-fixer fix --config=.php-cs-fixer.php"
}
Expand Down
8 changes: 6 additions & 2 deletions libs/BaseConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ public function hasValue($key)
return array_key_exists($key, (array) $this);
}

public function getValue($key)
public function getValue($key, $default = null)
{
return $this[$key];
if ($this->hasValue($key)) {
return $this[$key];
}

return $default;
}

public function isTruthy($key)
Expand Down
6 changes: 1 addition & 5 deletions libs/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,7 @@ public function setValidContentExtensions(array $value)

public function canCache()
{
if ($this->hasValue('cache')) {
return $this->getValue('cache');
}

return false;
return $this->getValue('cache', false);
}

public function getCacheKey()
Expand Down
4 changes: 3 additions & 1 deletion libs/DauxHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,9 @@ protected static function getTheme(Config $config, string $currentUrl)
}
}

$cache[$cacheKey] = $theme;
if (!getenv('APP_ENV') || getenv('APP_ENV') != 'test') {
$cache[$cacheKey] = $theme;
}

return $theme;
}
Expand Down
16 changes: 4 additions & 12 deletions libs/Format/Confluence/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,17 @@ class Config extends BaseConfig
{
public function shouldAutoDeleteOrphanedPages()
{
if ($this->hasValue('delete')) {
return $this->getValue('delete');
}

return false;
return $this->getValue('delete', false);
}

public function shouldPrintDiff()
{
if ($this->hasValue('print_diff')) {
return $this->getValue('print_diff');
}

return false;
return $this->getValue('print_diff', false);
}

public function getUpdateThreshold()
{
return $this->hasValue('update_threshold') ? $this->getValue('update_threshold') : 2;
return $this->getValue('update_threshold', 2);
}

public function getPrefix()
Expand Down Expand Up @@ -89,6 +81,6 @@ public function getHeader()

public function createRootIfMissing()
{
return $this->hasValue('create_root_if_missing') ? $this->getValue('create_root_if_missing') : false;
return $this->getValue('create_root_if_missing', false);
}
}
24 changes: 4 additions & 20 deletions libs/Format/HTML/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,12 @@ public function showDateModified()

public function showPreviousNextLinks()
{
if ($this->hasValue('jump_buttons')) {
return $this->getValue('jump_buttons');
}

return true;
return $this->getValue('jump_buttons', true);
}

public function showCodeToggle()
{
if ($this->hasValue('toggle_code')) {
return $this->getValue('toggle_code');
}

return true;
return $this->getValue('toggle_code', true);
}

public function hasAutomaticTableOfContents(): bool
Expand Down Expand Up @@ -153,20 +145,12 @@ public function getButtons()

public function hasLandingPage()
{
if ($this->hasValue('auto_landing')) {
return $this->getValue('auto_landing');
}

return true;
return $this->getValue('auto_landing', true);
}

public function hasBreadcrumbs()
{
if ($this->hasValue('breadcrumbs')) {
return $this->getValue('breadcrumbs');
}

return true;
return $this->getValue('breadcrumbs', true);
}

public function getBreadcrumbsSeparator()
Expand Down
2 changes: 1 addition & 1 deletion libs/Format/HTML/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private function renderNavigation($entries)
$link = '<a href="' . $entry['href'] . '">' . $entry['title'] . '</a>';
}

$nav .= "<li class='Nav__item $entry[class]'>$link</li>";
$nav .= "\n<li class='Nav__item $entry[class]'>$link</li>";
}

return "<ul class='Nav'>$nav</ul>";
Expand Down
236 changes: 236 additions & 0 deletions tests/Format/HTML/GeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
<?php
namespace Todaymade\Daux\Format\HTML;

use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Output\NullOutput;
use Todaymade\Daux\ConfigBuilder;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
use Todaymade\Daux\Format\Base\Page;

class GeneratorTest extends TestCase
{
protected function getDaux($moreConfig = [])
{
$root = vfsStream::setup('root', null, [
'index.md' => 'Homepage, welcome!',
'Content' => [
'Page.md' => 'some text content',
],
'Widgets' => [
'index.md' => 'Widget Folder',
'Button.md' => 'Button',
'image.svg' => '<xml...>',
],
]);

$config = ConfigBuilder::withMode()
->withDocumentationDirectory($root->url())
->withValidContentExtensions(['md'])
->with($moreConfig)
->build();

$output = new NullOutput();
$daux = new Daux($config, $output);
$daux->generateTree();

return $daux;
}

public function testGenerateSinglePage()
{
$daux = $this->getDaux();
$generator = new Generator($daux);

$config = $daux->getConfig();
$baseUrl = '../';

DauxHelper::rebaseConfiguration($config, $baseUrl);

$daux->tree->setActiveNode($daux->tree['Widgets']['Button.html']);
$generated = $generator->generateOne($daux->tree['Widgets']['Button.html'], $config);

$this->assertInstanceOf(ContentPage::class, $generated);

$content = $generated->getContent();

// Assert previous page is present
$this->assertStringContainsString('<li class=Pager--prev><a href="../Widgets/index.html">Previous</a></li>', $content);

// Assert breadcrumb presence
$this->assertStringContainsString('<a href="../Widgets/index.html">Widgets</a> <svg ', $content);
$this->assertStringContainsString('</svg> <a href="../Widgets/Button.html">Button</a>', $content);

// Assert search elements presence
$this->assertStringContainsString('class="Search__field"', $content);
$this->assertStringContainsString('<script type="text/javascript" src="../daux_libraries/search.min.js"', $content);
$this->assertStringContainsString("window.search({'base_url': '../'})", $content);
$this->assertStringContainsString("<link href='../daux_libraries/search.css' rel='stylesheet' type='text/css'>", $content);

// Assert nav is correct
$this->assertStringContainsString('class=\'Nav__item Nav__item--open has-children\'><a href="../Widgets/index.html"', $content);
$this->assertStringContainsString('<li class=\'Nav__item Nav__item--active\'><a href="../Widgets/Button.html">', $content);
}

public function testGenerateAnalytics()
{
$daux = $this->getDaux([
'html' => [
'google_analytics' => 'ua_45734258',
'plausible_domain' => 'daux.io',
'piwik_analytics' => 'piwik.com',
'piwik_analytics_id' => 12345,
],
]);
$generator = new Generator($daux);

$config = $daux->getConfig();
$baseUrl = '../';

DauxHelper::rebaseConfiguration($config, $baseUrl);

$daux->tree->setActiveNode($daux->tree['Widgets']['Button.html']);
$generated = $generator->generateOne($daux->tree['Widgets']['Button.html'], $config);

$this->assertInstanceOf(ContentPage::class, $generated);

$content = $generated->getContent();

// Google Analytics
$this->assertStringContainsString("ga('create', 'ua_45734258', '');", $content);

// Piwik
$this->assertStringContainsString('://piwik.com/', $content);
$this->assertStringContainsString('["setSiteId", 12345]', $content);

// Plausible.io
$this->assertStringContainsString('<script async defer data-domain="daux.io" src="https://plausible.io/js/plausible.js"></script>', $content);
}

public function testGenerateSinglePageWithoutSearch()
{
$daux = $this->getDaux(['html' => ['search' => false]]);
$generator = new Generator($daux);

$config = $daux->getConfig();
$baseUrl = '../';

DauxHelper::rebaseConfiguration($config, $baseUrl);

$daux->tree->setActiveNode($daux->tree['Widgets']['Button.html']);
$generated = $generator->generateOne($daux->tree['Widgets']['Button.html'], $config);

$this->assertInstanceOf(ContentPage::class, $generated);

$content = $generated->getContent();

// Assert previous page is present
$this->assertStringContainsString('<li class=Pager--prev><a href="../Widgets/index.html">Previous</a></li>', $content);

// Assert breadcrumb presence
$this->assertStringContainsString('<a href="../Widgets/index.html">Widgets</a> <svg ', $content);
$this->assertStringContainsString('</svg> <a href="../Widgets/Button.html">Button</a>', $content);

// Assert search elements presence
$this->assertStringNotContainsString('class="Search__field"', $content);
$this->assertStringNotContainsString('<script type="text/javascript" src="../daux_libraries/search.min.js"', $content);
$this->assertStringNotContainsString("window.search({'base_url': '../'})", $content);
$this->assertStringNotContainsString("<link href='../daux_libraries/search.css' rel='stylesheet' type='text/css'>", $content);

// Assert nav is correct
$this->assertStringContainsString('class=\'Nav__item Nav__item--open has-children\'><a href="../Widgets/index.html"', $content);
$this->assertStringContainsString('<li class=\'Nav__item Nav__item--active\'><a href="../Widgets/Button.html">', $content);
}

public function testGenerateSidebarLinks()
{
$daux = $this->getDaux(['html' => [
'search' => false,
'links' => [
'Bing' => 'https://bing.com',
'Google' => 'https://google.com',
],
'twitter' => ['onigoetz'],
'powered_by' => 'Powered by Daux.io',
]]);
$generator = new Generator($daux);

$config = $daux->getConfig();
$baseUrl = '../';

DauxHelper::rebaseConfiguration($config, $baseUrl);

$daux->tree->setActiveNode($daux->tree['Widgets']['Button.html']);
$generated = $generator->generateOne($daux->tree['Widgets']['Button.html'], $config);

$this->assertInstanceOf(ContentPage::class, $generated);

$content = $generated->getContent();

// Twitter link
$this->assertStringContainsString('<span class="Twitter__button__label">Follow @onigoetz</span>', $content);

// Other links
$this->assertStringContainsString('<a href="https://bing.com" target="_blank" rel="noopener noreferrer">Bing<', $content);
$this->assertStringContainsString('<a href="https://google.com" target="_blank" rel="noopener noreferrer">Google<', $content);

$this->assertStringContainsString('Powered by Daux.io', $content);
}

public function testGenerateLandingPage()
{
$daux = $this->getDaux(['html' => [
'search' => false,
'repo' => 'dauxio/daux.io',
'links' => [
'Bing' => 'https://bing.com',
'Google' => 'https://google.com',
],
'buttons' => [
'Gitlab' => 'https://gitlab.com',
],
]]);
$generator = new Generator($daux);

$config = $daux->getConfig();
$baseUrl = '';

DauxHelper::rebaseConfiguration($config, $baseUrl);

$daux->tree->setActiveNode($daux->tree['index.html']);
$generated = $generator->generateOne($daux->tree['index.html'], $config);

$this->assertInstanceOf(ContentPage::class, $generated);

$content = $generated->getContent();

// Twitter link
$this->assertStringContainsString('Homepage, welcome!', $content);

// Extra buttons
$this->assertStringContainsString('<a href="https://gitlab.com" class="Button Button--secondary Button--hero">Gitlab</a>', $content);

// Other links
$this->assertStringContainsString('<a href="Content/Page.html" class="Button Button--primary Button--hero">View Documentation', $content);
$this->assertStringContainsString('<a href="https://github.com/dauxio/daux.io" class="Button Button--secondary Button--hero"', $content);
}

public function testGenerateRawPage()
{
$daux = $this->getDaux();
$generator = new Generator($daux);

$config = $daux->getConfig();
$baseUrl = '../';

DauxHelper::rebaseConfiguration($config, $baseUrl);

$daux->tree->setActiveNode($daux->tree['Widgets']['image.svg']);
$generated = $generator->generateOne($daux->tree['Widgets']['image.svg'], $config);

$this->assertInstanceOf(RawPage::class, $generated);

$this->assertEquals('<xml...>', file_get_contents($generated->getFile()));
}
}

0 comments on commit bd10bc7

Please sign in to comment.