Skip to content

Commit

Permalink
Remove usage of request get method
Browse files Browse the repository at this point in the history
  • Loading branch information
loic425 committed Nov 17, 2021
1 parent bcd0099 commit 0d269ad
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 13 deletions.
28 changes: 24 additions & 4 deletions spec/Docs/ContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

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

class ContextSpec extends ObjectBehavior
Expand Down Expand Up @@ -44,25 +45,44 @@ public function it_can_get_pattern(): void
$this->getPattern()->shouldReturn('/\/(\w+)\/(\d+).(\d+)/');
}

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

$request->attributes = $attributesBag;

$this->beConstructedWith('legacy', '/{project}/{version}', './legacy_docs/{project}/{version}', [], [
'title' => '{package} documentation',
]);

$this->getMetadata($request)->shouldReturn([
'title' => '{package} documentation',
]);
}

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

$request->attributes = $attributesBag;

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

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

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

$request->attributes = $attributesBag;

$this->getDocsDir($request)->shouldReturn('./docs/my-project/1.2');
}
}
11 changes: 9 additions & 2 deletions spec/Template/TemplateResolverSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Mobizel\Bundle\MarkdownDocsBundle\Docs\ContextInterface;
use Mobizel\Bundle\MarkdownDocsBundle\Template\TemplateResolver;
use PhpSpec\ObjectBehavior;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;

class TemplateResolverSpec extends ObjectBehavior
Expand All @@ -33,25 +34,31 @@ function it_is_initializable(): void

function it_resolves_the_context_to_use(
Request $request,
ParameterBag $attributesBag,
ReaderContextInterface $readerContext,
ContextInterface $context
): void {
$request->get('slug')->willReturn('cookbook/bdd');
$attributesBag->get('slug')->willReturn('cookbook/bdd');
$readerContext->getContext()->willReturn($context);
$context->getDocsDir($request)->willReturn(dirname(__FILE__).'/../../tests/docs');

$request->attributes = $attributesBag;

$this->resolve($request)->shouldNotBeNull();
}

function it_returns_null_when_template_was_not_found(
Request $request,
ParameterBag $attributesBag,
ReaderContextInterface $readerContext,
ContextInterface $context
): void {
$request->get('slug')->willReturn('not-found');
$attributesBag->get('slug')->willReturn('not-found');
$readerContext->getContext()->willReturn($context);
$context->getDocsDir($request)->willReturn(dirname(__FILE__).'/../../tests/docs');

$request->attributes = $attributesBag;

$this->resolve($request)->shouldBeNull();
}
}
2 changes: 1 addition & 1 deletion src/Controller/SearchAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function __construct(ReaderContextInterface $readerContext)

public function __invoke(Request $request): Response
{
$query = $request->get('query', '');
$query = $request->query->get('query', '');

$finder = new Finder();
$finder->files()->in($this->readerContext->getContext()->getDocsDir($request))->contains('/'.$query.'/i');
Expand Down
2 changes: 1 addition & 1 deletion src/Docs/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ private function replaceMatchingParametersWithRouteParameters(string $data, arra

private function getRouteParameters(Request $request): array
{
$routeParameters = $request->get('_route_params');
$routeParameters = $request->attributes->get('_route_params');

Assert::isArray($routeParameters, 'Route params must be an array. Got: %s');

Expand Down
5 changes: 2 additions & 3 deletions src/Template/TemplateResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

final class TemplateResolver implements TemplateResolverInterface
{
/** @var ReaderContextInterface */
private $readerContext;
private ReaderContextInterface $readerContext;

public function __construct(ReaderContextInterface $readerContext)
{
Expand All @@ -29,7 +28,7 @@ public function __construct(ReaderContextInterface $readerContext)

public function resolve(Request $request): ?string
{
$slug = $request->get('slug');
$slug = $request->attributes->get('slug');

Assert::string($slug, 'Slug must be a string. Got: %s');

Expand Down
4 changes: 2 additions & 2 deletions tests/app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class AppKernel extends Kernel
{
public function registerBundles()
public function registerBundles(): iterable
{
return [
new FrameworkBundle(),
Expand All @@ -32,7 +32,7 @@ public function registerBundles()
];
}

public function registerContainerConfiguration(LoaderInterface $loader)
public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__.'/config/config.yaml');
}
Expand Down

0 comments on commit 0d269ad

Please sign in to comment.