Skip to content

Add JekyllPageGenerator #10

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

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
36 changes: 36 additions & 0 deletions src/Generator/JekyllPageGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
declare(strict_types=1);

namespace App\Generator;

use App\Value\Sniff;

class JekyllPageGenerator extends MarkdownGenerator implements Generator
Copy link
Collaborator

@afilina afilina Mar 18, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Classes should be final and we should not extend implementations. It's okay to copy an existing to make the necessary changes. We can always extract truly reusable elements and inject them as dependencies instead. The test for the new class should encompass the common elements as well, otherwise we're not truly testing them, and any unintended regressions would go unnoticed. Not a deal breaker and I'm happy to address the issue in a subsequent commit.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's defer it and create a ticket to improve later.

{
public function createSniffDoc(Sniff $sniff): string
{
$sniffDoc = $this->getFrontMatter($sniff) . "\n";
$sniffDoc .= parent::createSniffDoc($sniff);

return $sniffDoc;
}

private function getFrontMatter(Sniff $sniff): string
{
$sniffName = $sniff->getSniffName();
if ($sniffName === '') {
return <<<'MD'
---
---

MD;
}

return <<<MD
---
title: {$sniffName}
---

MD;
}
}
26 changes: 26 additions & 0 deletions src/Value/Sniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
class Sniff
{
private string $code;
private string $standardName;
private string $categoryName;
private string $sniffName;
private string $docblock;
/**
* @var Property[]
Expand Down Expand Up @@ -42,6 +45,11 @@ public function __construct(
Assert::that($code)
->notBlank();

$sniffNameParts = explode('.', $code);
Assert::that($sniffNameParts)
->isArray()
->count(3);

Assert::thatAll($properties)
->isInstanceOf(Property::class);

Expand All @@ -52,6 +60,9 @@ public function __construct(
->isInstanceOf(Violation::class);

$this->code = $code;
$this->standardName = $sniffNameParts[0];
$this->categoryName = $sniffNameParts[1];
$this->sniffName = $sniffNameParts[2];
$this->docblock = $docblock;
$this->properties = array_values($properties);
$this->urls = $urls;
Expand All @@ -65,6 +76,21 @@ public function getCode(): string
return $this->code;
}

public function getStandardName(): string
{
return $this->standardName;
}

public function getCategoryName(): string
{
return $this->categoryName;
}

public function getSniffName(): string
{
return $this->sniffName;
}

public function getDocblock(): string
{
return $this->docblock;
Expand Down
46 changes: 46 additions & 0 deletions tests/Generator/JekyllPageGeneratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
declare(strict_types=1);

namespace App\Tests\Generator;

use App\Generator\JekyllPageGenerator;
use App\Value\Sniff;
use App\Value\UrlList;
use PHPUnit\Framework\TestCase;

/** @covers \App\Generator\JekyllPage */
class JekyllPageTest extends TestCase
{
private JekyllPageGenerator $generator;

/** @test */
public function fromSniff_WithMinimalData_WriteMinimalDetails()
{
$doc = new Sniff(
'Standard.Category.My',
'',
[],
new UrlList([]),
'',
[],
[]
);

self::assertSame(
<<<MD
---
title: My
---

# Standard.Category.My

MD,
$this->generator->createSniffDoc($doc)
);
}

protected function setUp(): void
{
$this->generator = new JekyllPageGenerator();
}
}
30 changes: 30 additions & 0 deletions tests/Value/SniffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
class SniffTest extends TestCase
{
const CODE = 'Standard.Category.Code';
const STANDARD = 'Standard';
const CATEGORY = 'Category';
const SNIFFNAME = 'Code';
const DOCBLOCK = 'Docblock';
const DESCRIPTION = 'Description';
/**
Expand Down Expand Up @@ -128,6 +131,33 @@ public function getCode()
);
}

/** @test */
public function getStandardName()
{
self::assertSame(
self::STANDARD,
$this->createSniff()->getStandardName()
);
}

/** @test */
public function getCategoryName()
{
self::assertSame(
self::CATEGORY,
$this->createSniff()->getCategoryName()
);
}

/** @test */
public function getSniffName()
{
self::assertSame(
self::SNIFFNAME,
$this->createSniff()->getSniffName()
);
}

protected function setUp(): void
{
$this->PROPERTIES = [
Expand Down