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

Allow multiple docs contexts (for example: including legacy versions) #69

Merged
merged 11 commits into from
May 6, 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
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@
"symfony/phpunit-bridge": "^4.4|^5.1",
"symplify/easy-coding-standard": "^9.0"
},
"conflict": {
"symplify/easy-coding-standard": "9.3.*"
},
"config": {
"preferred-install": "dist",
"sort-packages": true
Expand Down
34 changes: 34 additions & 0 deletions docs/how-to-add-docs-contexts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# How to add docs contexts

You can add several contexts of the documentation.

For example, you can add a context to browse the legacy versions of your documentation.

```yaml
# config/packages/mobizel_markdown_docs.yaml

mobizel_markdown_docs:
contexts:
legacy:
path: /{version}
docs_dir: '/path/to/your/legacy_docs/{versions}'
requirements:
version: '(\d+)\.(\d+)'
```

Another example is to add a context for your packages.

```yaml
mobizel_markdown_docs:
contexts:
packages:
path: /packages/{package}/{version}
docs_dir: '/path/to/your/packages_docs/{package}/{version}'
requirements:
package: '.+'
version: '(\d+)\.(\d+)'
```

`Warning`
Remember the first context to match the route path will be used.
So the order of your contexts can be important.
7 changes: 5 additions & 2 deletions docs/how-to-use.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

Markdown templates are on `docs` directory.

You can customize the docs directory:
You can customize your main docs directory:

```yaml
# config/packages/mobizel_markdown_docs.yaml

mobizel_markdown_docs:
docs_dir: '/path/to/your/docs'
contexts:
main:
path: /docs
docs_dir: '/path/to/your/docs'
```

Your files are already available on your server.
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@

* [Setup](setup.md)
* [How to use](how-to-use.md)
* [How to add docs contexts](how-to-add-docs-contexts.md)
* [How to add warning and notes](how-to-add-warning-and-notes.md)
* [How to draw charts](how-to-draw-charts)
9 changes: 6 additions & 3 deletions docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@ composer config extra.symfony.allow-contrib true
composer require mobizel/markdown-docs-bundle
```

If you don't use Symfony Flex, add the required routes on `config/routes.yaml`:
If you don't use Symfony Flex, add the required configuration on `config/packages/mobizel_markdown_docs.yaml`:

```yaml
_mobizel_markdowns:
resource: '@MobizelMarkdownDocsBundle/Resources/config/routes.xml'
mobizel_markdown_docs:
contexts:
main:
path: /docs
docs_dir: '%kernel.project_dir%/docs'
```
51 changes: 39 additions & 12 deletions spec/DataProvider/PageCollectionDataProviderSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,48 @@

namespace spec\Mobizel\Bundle\MarkdownDocsBundle\DataProvider;

use Mobizel\Bundle\MarkdownDocsBundle\Context\ReaderContextInterface;
use Mobizel\Bundle\MarkdownDocsBundle\DataProvider\PageCollectionDataProvider;
use Mobizel\Bundle\MarkdownDocsBundle\Docs\ContextInterface;
use PhpSpec\ObjectBehavior;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class PageCollectionDataProviderSpec extends ObjectBehavior
{
function let(): void
function let(ReaderContextInterface $readerContext, RequestStack $requestStack, Request $request): void
{
$docsDir = realpath(dirname(__FILE__).'/../../tests/docs');

$this->beConstructedWith($docsDir);
$requestStack->getCurrentRequest()->willReturn($request);
$this->beConstructedWith($readerContext, $requestStack);
}

function it_is_initializable(): void
{
$this->shouldHaveType(PageCollectionDataProvider::class);
}

function it_returns_root_pages(): void
{
function it_returns_root_pages(
ReaderContextInterface $readerContext,
ContextInterface $context,
Request $request
): void {
$readerContext->getContext()->willReturn($context);
$context->getDocsDir($request)->willReturn(realpath(dirname(__FILE__).'/../../tests/docs'));

$rootPages = $this->getRootPages();
$rootPages->shouldHaveCount(7);
$rootPages[0]->slug->shouldReturn('index');
$rootPages[0]->title->shouldReturn('Documentation');
}

function it_returns_children_pages(): void
{
function it_returns_children_pages(
ReaderContextInterface $readerContext,
ContextInterface $context,
Request $request
): void {
$readerContext->getContext()->willReturn($context);
$context->getDocsDir($request)->willReturn(realpath(dirname(__FILE__).'/../../tests/docs'));

$childrenPages = $this->getChildrenPages('products');
$childrenPages->shouldHaveCount(2);
$childrenPages[0]->slug->shouldReturn('products/board-games');
Expand All @@ -37,8 +52,14 @@ function it_returns_children_pages(): void
$childrenPages[1]->title->shouldReturn('Book homepage');
}

function it_returns_pages_map(): void
{
function it_returns_pages_map(
ReaderContextInterface $readerContext,
ContextInterface $context,
Request $request
): void {
$readerContext->getContext()->willReturn($context);
$context->getDocsDir($request)->willReturn(realpath(dirname(__FILE__).'/../../tests/docs'));

$this->getPagesMap()->shouldReturn([
'index' => 'Documentation',
'bar' => 'Bar',
Expand All @@ -57,8 +78,14 @@ function it_returns_pages_map(): void
]);
}

function it_returns_pases_as_tree(): void
{
function it_returns_pages_as_tree(
ReaderContextInterface $readerContext,
ContextInterface $context,
Request $request
): void {
$readerContext->getContext()->willReturn($context);
$context->getDocsDir($request)->willReturn(realpath(dirname(__FILE__).'/../../tests/docs'));

$tree = $this->getPagesAsTree();

$tree->shouldHaveCount(7);
Expand Down
38 changes: 38 additions & 0 deletions spec/Docs/ContextRegistrySpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace spec\Mobizel\Bundle\MarkdownDocsBundle\Docs;

use Mobizel\Bundle\MarkdownDocsBundle\Docs\ContextInterface;
use Mobizel\Bundle\MarkdownDocsBundle\Docs\ContextRegistry;
use PhpSpec\ObjectBehavior;

class ContextRegistrySpec extends ObjectBehavior
{
function let(ContextInterface $firstContext, ContextInterface $secondContext): void
{
$firstContext->getName()->willReturn('first');
$secondContext->getName()->willReturn('second');
$this->beConstructedWith([$firstContext, $secondContext]);
}

function it_is_initializable(): void
{
$this->shouldHaveType(ContextRegistry::class);
}

function it_adds_contexts(ContextInterface $context): void
{
$context->getName()->willReturn('third');

$this->add($context);

$this->getAll()->shouldContain($context);
$this->getAll()->shouldHaveCount(3);
}

function it_returns_all_contexts(ContextInterface $firstContext, ContextInterface $secondContext): void
{
$this->getAll()->shouldContain($firstContext);
$this->getAll()->shouldContain($secondContext);
}
}
45 changes: 45 additions & 0 deletions spec/Docs/ContextResolverSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace spec\Mobizel\Bundle\MarkdownDocsBundle\Docs;

use http\Client\Request;
use Mobizel\Bundle\MarkdownDocsBundle\Docs\ContextInterface;
use Mobizel\Bundle\MarkdownDocsBundle\Docs\ContextRegistryInterface;
use Mobizel\Bundle\MarkdownDocsBundle\Docs\ContextResolver;
use PhpSpec\ObjectBehavior;

class ContextResolverSpec extends ObjectBehavior
{
function let(ContextRegistryInterface $contextRegistry): void
{
$this->beConstructedWith($contextRegistry);
}

function it_is_initializable(): void
{
$this->shouldHaveType(ContextResolver::class);
}

function it_does_not_returns_any_context_when_registry_is_empty(
ContextRegistryInterface $contextRegistry
): void {
$contextRegistry->getAll()->willReturn([]);

$this->resolve('/docs')->shouldReturn(null);
}

function it_resolves_the_path_to_return_the_right_context(
ContextRegistryInterface $contextRegistry,
ContextInterface $currentVersionContext,
ContextInterface $legacyVersionContext
): void {
$contextRegistry->getAll()->willReturn([$currentVersionContext->getWrappedObject(), $legacyVersionContext->getWrappedObject()]);
$currentVersionContext->getPath()->willReturn('/current');
$currentVersionContext->getPattern()->willReturn('/\/current/');
$legacyVersionContext->getPath()->willReturn('/{version}');
$legacyVersionContext->getPattern()->willReturn('/(\d+).(\d+)/');

$this->resolve('/current/setup/requirements')->shouldReturn($currentVersionContext);
$this->resolve('/1.2/setup/requirements')->shouldReturn($legacyVersionContext);
}
}
57 changes: 57 additions & 0 deletions spec/Docs/ContextSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace spec\Mobizel\Bundle\MarkdownDocsBundle\Docs;

use Mobizel\Bundle\MarkdownDocsBundle\Docs\Context;
use PhpSpec\ObjectBehavior;
use Symfony\Component\HttpFoundation\Request;

class ContextSpec extends ObjectBehavior
{
function let(): void
{
$this->beConstructedWith('default', '/docs', './docs', []);
}

function it_is_initializable()
{
$this->shouldHaveType(Context::class);
}

public function it_can_get_path(): void
{
$this->getPath()->shouldReturn('/docs');
}

public function it_can_get_pattern(): void
{
$this->beConstructedWith('legacy', '/{project}/{version}', './legacy_docs/{project}/{version}', [
'project' => '(\w+)',
'version' => '(\d+).(\d+)'
]);

$this->getPattern()->shouldReturn('/\/(\w+)\/(\d+).(\d+)/');
}

public function it_can_get_docs_dir(Request $request): void
{
$request->get('_route_params')->willReturn([]);

$this->getDocsDir($request)->shouldReturn('./docs');
}

public function it_replaces_params_in_docs_dir(Request $request): void
{
$this->beConstructedWith('legacy', '/{project}/{version}', './docs/{project}/{version}', [
'project' => '(\w+)',
'version' => '(\d+).(\d+)'
]);

$request->get('_route_params')->willReturn([
'project' => 'my-project',
'version' => '1.2',
]);

$this->getDocsDir($request)->shouldReturn('./docs/my-project/1.2');
}
}
7 changes: 5 additions & 2 deletions spec/Helper/PageHelperSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

namespace spec\Mobizel\Bundle\MarkdownDocsBundle\Helper;

use Mobizel\Bundle\MarkdownDocsBundle\DataProvider\PageCollectionDataProviderInterface;
use Mobizel\Bundle\MarkdownDocsBundle\Helper\PageHelper;
use PhpSpec\ObjectBehavior;

class PageHelperSpec extends ObjectBehavior
{
function let(): void
function let(PageCollectionDataProviderInterface $pageCollectionDataProvider): void
{
$this->beConstructedWith([
$pageCollectionDataProvider->getPagesMap()->willReturn([
'first-page' => 'First page',
'foo' => 'Foo fighters',
'last-page' => 'Last page',
]);

$this->beConstructedWith($pageCollectionDataProvider);
}

function it_is_initializable(): void
Expand Down
5 changes: 2 additions & 3 deletions spec/Page/PageInfoSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace spec\Mobizel\Bundle\MarkdownDocsBundle\Page;

use Mobizel\Bundle\MarkdownDocsBundle\Template\TemplateHandlerInterface;
use PhpSpec\ObjectBehavior;

class PageInfoSpec extends ObjectBehavior
Expand All @@ -13,14 +12,14 @@ function it_can_get_page_title(): void
$this->getTitle()->shouldReturn('Foo fighters');
}

function it_return_default_title_when_no_title_has_been_found(TemplateHandlerInterface $templateHandler): void
function it_return_default_title_when_no_title_has_been_found(): void
{
$this->beConstructedWith('tests/docs/bar.md', '', '');

$this->getTitle()->shouldReturn('Bar');
}

function it_return_default_title_when_file_is_empty(TemplateHandlerInterface $templateHandler): void
function it_return_default_title_when_file_is_empty(): void
{
$this->beConstructedWith('tests/docs/empty.md', '', '');

Expand Down
Loading