Skip to content

Commit

Permalink
Guides: Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mvriel committed Mar 7, 2021
1 parent 6071cd6 commit 4cf1648
Show file tree
Hide file tree
Showing 11 changed files with 215 additions and 9 deletions.
3 changes: 3 additions & 0 deletions src/Guides/Environment.php
Expand Up @@ -224,6 +224,9 @@ public function setVariable(string $variable, $value) : void
$this->variables[$variable] = $value;
}

/**
* @todo is this used?
*/
public function createTitle(int $level) : string
{
for ($currentLevel = 0; $currentLevel < 16; $currentLevel++) {
Expand Down
4 changes: 1 addition & 3 deletions src/Guides/Markdown/Parser.php
Expand Up @@ -26,7 +26,6 @@
use phpDocumentor\Guides\Nodes\TitleNode;
use phpDocumentor\Guides\Parser as ParserInterface;
use function get_class;
use function md5;

final class Parser implements ParserInterface
{
Expand Down Expand Up @@ -95,8 +94,7 @@ public function parseDocument(NodeWalker $walker) : DocumentNode
$content = $node->getStringContent();
$title = new TitleNode(
new SpanNode($this->environment, $content),
$node->getLevel(),
md5($content)
$node->getLevel()
);
$document->addNode($title);
continue;
Expand Down
3 changes: 1 addition & 2 deletions src/Guides/Nodes/TitleNode.php
Expand Up @@ -32,12 +32,11 @@ class TitleNode extends Node
/** @var string */
protected $target = '';

public function __construct(Node $value, int $level, string $token)
public function __construct(Node $value, int $level)
{
parent::__construct($value);

$this->level = $level;
$this->token = $token;
$this->id = (new AsciiSlugger())->slug($this->value->getValue())->lower()->toString();
}

Expand Down
5 changes: 1 addition & 4 deletions src/Guides/RestructuredText/Parser/DocumentParser.php
Expand Up @@ -410,12 +410,9 @@ private function flush() : void
$level = $this->environment->getLevel((string) $this->specialLetter);
$level = $this->environment->getInitialHeaderLevel() + $level - 1;

$token = $this->environment->createTitle($level);

$node = new TitleNode(
new SpanNode($this->environment, $data),
$level,
$token
$level
);

if ($this->lastTitleNode !== null) {
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/Guides/Nodes/ListNodeTest.php
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\Nodes;

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

use PHPUnit\Framework\TestCase;

final class ListNodeTest extends TestCase
{
public function test_it_can_be_return_information_on_each_line_in_a_list() : void
{
$list = [
[
'text' => 'the line text for line 1',
'depth' => 0,
'prefix' => '*',
'ordered' => false,
],
];

$node = new ListNode();
$node->addLine($list[0]);

self::assertSame($list, $node->getLines());
}
}
27 changes: 27 additions & 0 deletions tests/unit/Guides/Nodes/MainNodeTest.php
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\Nodes;

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

use PHPUnit\Framework\TestCase;

final class MainNodeTest extends TestCase
{
public function test_it_can_be_created_with_a_value() : void
{
$node = new MainNode('id');

self::assertSame('id', $node->getValue());
self::assertSame('id', $node->getValueString());
}
}
27 changes: 27 additions & 0 deletions tests/unit/Guides/Nodes/MetaNodeTest.php
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\Nodes;

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

use PHPUnit\Framework\TestCase;

final class MetaNodeTest extends TestCase
{
public function test_it_can_be_created_with_a_key_and_value() : void
{
$node = new MetaNode('key', 'value');

self::assertSame('key', $node->getKey());
self::assertSame('value', $node->getValue());
}
}
28 changes: 28 additions & 0 deletions tests/unit/Guides/Nodes/ParagraphNodeTest.php
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\Nodes;

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

use PHPUnit\Framework\TestCase;

final class ParagraphNodeTest extends TestCase
{
public function test_it_can_be_created_with_a_value_even_another_node() : void
{
$imageNode = new ImageNode();

$node = new ParagraphNode($imageNode);

self::assertSame($imageNode, $node->getValue());
}
}
28 changes: 28 additions & 0 deletions tests/unit/Guides/Nodes/QuoteNodeTest.php
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\Nodes;

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

use PHPUnit\Framework\TestCase;

final class QuoteNodeTest extends TestCase
{
public function test_it_can_be_created_with_a_value_even_another_node() : void
{
$imageNode = new ImageNode();

$node = new QuoteNode($imageNode);

self::assertSame($imageNode, $node->getValue());
}
}
26 changes: 26 additions & 0 deletions tests/unit/Guides/Nodes/RawNodeTest.php
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\Nodes;

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

use PHPUnit\Framework\TestCase;

final class RawNodeTest extends TestCase
{
public function test_it_can_be_created_with_a_value() : void
{
$node = new RawNode('raw string');

self::assertSame('raw string', $node->getValue());
}
}
37 changes: 37 additions & 0 deletions tests/unit/Guides/Nodes/TitleNodeTest.php
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace phpDocumentor\Guides\Nodes;

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

use Mockery as m;
use Mockery\Adapter\Phpunit\MockeryTestCase;
use phpDocumentor\Guides\Environment;

final class TitleNodeTest extends MockeryTestCase
{
public function test_it_can_be_created_with_a_title_slug_and_depth() : void
{
$environment = m::mock(Environment::class);
$environment->shouldReceive('getTitleLetters')->andReturn(['a']);
$environment->shouldReceive('resetAnonymousStack');

$titleNode = new SpanNode($environment, 'Raw String');
$node = new TitleNode($titleNode, 1);
$node->setTarget('target');

self::assertSame('raw-string', $node->getId());
self::assertSame($titleNode, $node->getValue());
self::assertSame(1, $node->getLevel());
self::assertSame('target', $node->getTarget());
}
}

0 comments on commit 4cf1648

Please sign in to comment.