Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hason committed Feb 21, 2024
1 parent a37859d commit b85d1d1
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/Pug/PugCommentFrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
final class PugCommentFrontMatter
{
/**
* @codeCoverageIgnore
*/
private function __construct()
{
// prevent any instantiation
Expand Down
1 change: 1 addition & 0 deletions src/Twig/DataToTwigConvertor.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public static function vars(bool $force = true): self
$content = '';
foreach ($data as $key => $value) {
if (is_int($key)) {
//TODO add log
continue;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Twig/TwigCommentFrontMatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
*/
final class TwigCommentFrontMatter
{
/**
* @codeCoverageIgnore
*/
private function __construct()
{
// prevent any instantiation
Expand Down
6 changes: 6 additions & 0 deletions tests/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ public function testReturnContent(): void
self::assertEquals($content, $document->getContent());
}

public function testToString(): void
{
$document = new Document($content = 'content');
self::assertEquals($content, (string) $document);
}

public function testReturnData(): void
{
$document = new Document('content', $data = ['foo' => 'bar']);
Expand Down
21 changes: 13 additions & 8 deletions tests/FrontMatterChainTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use ArrayObject;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Webuni\FrontMatter\Document;
use Webuni\FrontMatter\FrontMatter;
use Webuni\FrontMatter\FrontMatterChain;
use Webuni\FrontMatter\FrontMatterInterface;
Expand All @@ -25,10 +26,7 @@ final class FrontMatterChainTest extends TestCase

protected function setUp(): void
{
$this->chain = new FrontMatterChain([
FrontMatter::createYaml(),
FrontMatter::createJson(),
]);
$this->chain = FrontMatterChain::create();
}

public function testEmptyAdapters(): void
Expand All @@ -52,9 +50,9 @@ public function testInvalidArrayOfAdapters(): void
/**
* @dataProvider getFrontMatter
*/
public function testExists(string $source): void
public function testExists(string $source, array $data, string $content, bool $exists): void
{
self::assertTrue($this->chain->exists($source));
self::assertSame($exists, $this->chain->exists($source));
}

/**
Expand All @@ -67,11 +65,18 @@ public function testParse(string $source, array $data, string $content): void
self::assertEquals($content, $document->getContent());
}

public function testDumpViaFirstAdapter(): void
{
$source = $this->chain->dump(new Document('Content', ['foo' => 'bar']));
$this->assertSame("---\nfoo: bar\n---\nContent", $source);
}

public static function getFrontMatter(): array
{
return [
["---\nfoo: bar\n---\nContent", ['foo' => 'bar'], 'Content'],
["{\n \"foo\": \"bar\"\n}\nContent", ['foo' => 'bar'], 'Content'],
["Content", [], 'Content', false],
["---\nfoo: bar\n---\nContent", ['foo' => 'bar'], 'Content', true],
["{\n \"foo\": \"bar\"\n}\nContent", ['foo' => 'bar'], 'Content', true],
];
}
}
16 changes: 16 additions & 0 deletions tests/FrontMatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ public function testYaml(string $string, array $data, string $content, bool $has
self::assertEquals($string, $frontMatter->dump($document));
}

public function testIndentedYaml(): void
{
$frontMatter = new FrontMatter();
$document = $frontMatter->parse("---\n\n\n \n foo: bar\n text: |\n text\n\n---\nContent");

self::assertDocument(['foo' => 'bar', 'text' => 'text'], 'Content', $document);
}

public function testEmptyYaml(): void
{
$frontMatter = new FrontMatter();
$document = $frontMatter->parse("---\n\n\n \n \n\n---\nContent");

self::assertDocument([], 'Content', $document);
}

/**
* @dataProvider getSeparator
*/
Expand Down
46 changes: 46 additions & 0 deletions tests/Processor/ProcessorDecoratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

/*
* This is part of the webuni/front-matter package.
*
* (c) Martin Hasoň <martin.hason@gmail.com>
* (c) Webuni s.r.o. <info@webuni.cz>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Processor;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Webuni\FrontMatter\Processor\ProcessorDecorator;
use Webuni\FrontMatter\Processor\ProcessorInterface;

final class ProcessorDecoratorTest extends TestCase
{
private MockObject $wrapped;
private DummyProcessorDecorator $processor;

protected function setUp(): void
{
$this->wrapped = $this->createMock(ProcessorInterface::class);
$this->processor = new DummyProcessorDecorator($this->wrapped);
}

public function testParse(): void
{
$this->wrapped->expects($this->once())->method('parse')->with('{}');
$this->processor->parse('{}');
}

public function testDump(): void
{
$this->wrapped->expects($this->once())->method('dump')->with(['foo' => 'bar']);
$this->processor->dump(['foo' => 'bar']);
}
}

final class DummyProcessorDecorator extends ProcessorDecorator
{
}
1 change: 1 addition & 0 deletions tests/Twig/data.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
foo: bar
1: number
number: 1234
pi: 3.14159
date: 2016-05-27
Expand Down
2 changes: 1 addition & 1 deletion tests/Twig/templates/var.twig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{% set parameters = {foo: "bar", number: 1234, pi: 3.14159, date: (1464307200|date_modify('0sec')), empty: null, multiline: "Multiple\nLine\nString\n", object: {key: "value", datetime: (1605185652|date_modify('0sec')), values: {0: "one", 1: "two", }, }, } %}
{% set parameters = {foo: "bar", 1: "number", number: 1234, pi: 3.14159, date: (1464307200|date_modify('0sec')), empty: null, multiline: "Multiple\nLine\nString\n", object: {key: "value", datetime: (1605185652|date_modify('0sec')), values: {0: "one", 1: "two", }, }, } %}

0 comments on commit b85d1d1

Please sign in to comment.