Skip to content

Commit

Permalink
Separate chain calls (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
sankaest committed Jun 2, 2022
1 parent 85f7c77 commit a472cc5
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 35 deletions.
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -227,8 +227,12 @@ final class MyTagsInjection implements
public function getMetaTags(): array
{
return [
Html::meta()->name('http-equiv')->content('public'),
'noindex' => Html::meta()->name('robots')->content('noindex'),
Html::meta()
->name('http-equiv')
->content('public'),
'noindex' => Html::meta()
->name('robots')
->content('noindex'),
[
'name' => 'description',
'content' => 'This website is about funny raccoons.',
Expand Down
4 changes: 3 additions & 1 deletion config/params.php
Expand Up @@ -11,7 +11,9 @@
// Reference::to(CsrfViewInjection::class),
// or
// DynamicReference::to(function (ContainerInterface $container) {
// return $container->get(CsrfViewInjection::class)->withParameter('mycsrf');
// return $container
// ->get(CsrfViewInjection::class)
// ->withParameter('mycsrf');
// }),
],
],
Expand Down
11 changes: 9 additions & 2 deletions src/Exception/InvalidMetaTagException.php
Expand Up @@ -42,8 +42,13 @@ public function getSolution(): ?string
The meta tag can be define in the following ways:
- as array of attributes: `['name' => 'keywords', 'content' => 'yii,framework']`,
- as instance of `Yiisoft\Html\Tag\Meta`: `Html::meta()->name('keywords')->content('yii,framework')`.
- as instance of `Yiisoft\Html\Tag\Meta`:
```php
Html::meta()
->name('keywords')
->content('yii,framework');
```
Optionally, you may use string keys of array as identifies the meta tag.
Example:
Expand All @@ -56,7 +61,9 @@ public function getMetaTags(): array
'name' => 'keywords',
'content' => 'yii,framework',
],
Html::meta()->name('description')->content('Yii is a fast, secure, and efficient PHP framework.'),
Html::meta()
->name('description')
->content('Yii is a fast, secure, and efficient PHP framework.'),
];
}
```
Expand Down
8 changes: 6 additions & 2 deletions src/MetaTagsInjectionInterface.php
Expand Up @@ -19,8 +19,12 @@ interface MetaTagsInjectionInterface
*
* ```php
* [
* Html::meta()->name('http-equiv')->content('public'),
* 'noindex' => Html::meta()->name('robots')->content('noindex'),
* Html::meta()
* ->name('http-equiv')
* ->content('public'),
* 'noindex' => Html::meta()
* ->name('robots')
* ->content('noindex'),
* [
* 'name' => 'description',
* 'content' => 'This website is about funny raccoons.',
Expand Down
8 changes: 6 additions & 2 deletions src/ViewRenderer.php
Expand Up @@ -145,7 +145,9 @@ public function renderPartial(string $view, array $parameters = []): DataRespons
return $this->render($view, $parameters);
}

return $this->withLayout(null)->render($view, $parameters);
return $this
->withLayout(null)
->render($view, $parameters);
}

/**
Expand Down Expand Up @@ -196,7 +198,9 @@ public function renderPartialAsString(string $view, array $parameters = []): str
return $this->renderAsString($view, $parameters);
}

return $this->withLayout(null)->renderAsString($view, $parameters);
return $this
->withLayout(null)
->renderAsString($view, $parameters);
}

/**
Expand Down
15 changes: 11 additions & 4 deletions tests/CsrfViewInjectionTest.php
Expand Up @@ -18,7 +18,9 @@ final class CsrfViewInjectionTest extends TestCase
{
public function testGetCommonParameters(): void
{
$parameters = $this->getInjection('123', 'p-csrf', 'h-csrf')->getCommonParameters();
$parameters = $this
->getInjection('123', 'p-csrf', 'h-csrf')
->getCommonParameters();

$this->assertCount(1, $parameters);
$this->assertSame('csrf', key($parameters));
Expand All @@ -43,7 +45,9 @@ public function testGetCommonParameters(): void

public function testGetMetaTags(): void
{
$metaTags = $this->getInjection('123')->getMetaTags();
$metaTags = $this
->getInjection('123')
->getMetaTags();

$this->assertSame(
[
Expand All @@ -58,7 +62,9 @@ public function testGetMetaTags(): void

public function testWithParameterName(): void
{
$injection = $this->getInjection('123')->withParameterName('kitty');
$injection = $this
->getInjection('123')
->withParameterName('kitty');

$commonParameters = $injection->getCommonParameters();

Expand All @@ -67,7 +73,8 @@ public function testWithParameterName(): void

public function testWithMetaAttributeName(): void
{
$metaTags = $this->getInjection('123')
$metaTags = $this
->getInjection('123')
->withMetaAttributeName('kitty')
->getMetaTags();

Expand Down
79 changes: 57 additions & 22 deletions tests/ViewRendererTest.php
Expand Up @@ -33,7 +33,8 @@ final class ViewRendererTest extends TestCase

public function testRenderAndRenderAsString(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withLayout('@views/with-injection/layout')
->withControllerName('with-injection')
->withInjections(new TestInjection());
Expand Down Expand Up @@ -64,7 +65,9 @@ public function testRenderAndRenderAsString(): void

public function testRenderWithAbsoluteLayoutPath(): void
{
$renderer = $this->getRenderer()->withLayout($this->getViewsDir() . '/layout.php');
$renderer = $this
->getRenderer()
->withLayout($this->getViewsDir() . '/layout.php');

$response = $renderer->render('simple', [
'name' => 'donatello',
Expand All @@ -75,7 +78,9 @@ public function testRenderWithAbsoluteLayoutPath(): void

public function testRenderAsStringWithAbsoluteLayoutPath(): void
{
$renderer = $this->getRenderer()->withLayout($this->getViewsDir() . '/layout.php');
$renderer = $this
->getRenderer()
->withLayout($this->getViewsDir() . '/layout.php');

$result = $renderer->renderAsString('simple', [
'name' => 'donatello',
Expand All @@ -86,7 +91,10 @@ public function testRenderAsStringWithAbsoluteLayoutPath(): void

public function testRenderWithoutLayout(): void
{
$renderer = $this->getRenderer()->withLayout(null)->withInjections(new TestInjection());
$renderer = $this
->getRenderer()
->withLayout(null)
->withInjections(new TestInjection());

$response = $renderer->render('simple');

Expand All @@ -101,7 +109,10 @@ public function testRenderWithoutLayout(): void

public function testRenderAsStringWithoutLayout(): void
{
$renderer = $this->getRenderer()->withLayout(null)->withInjections(new TestInjection());
$renderer = $this
->getRenderer()
->withLayout(null)
->withInjections(new TestInjection());

$result = $renderer->renderAsString('simple');

Expand All @@ -116,7 +127,9 @@ public function testRenderAsStringWithoutLayout(): void

public function testRenderPartial(): void
{
$renderer = $this->getRenderer()->withInjections(new TestInjection());
$renderer = $this
->getRenderer()
->withInjections(new TestInjection());

$response = $renderer->renderPartial('simple');

Expand All @@ -133,7 +146,9 @@ public function testRenderPartial(): void

public function testRenderPartialAsString(): void
{
$renderer = $this->getRenderer()->withInjections(new TestInjection());
$renderer = $this
->getRenderer()
->withInjections(new TestInjection());

$result = $renderer->renderPartialAsString('simple');

Expand All @@ -152,7 +167,8 @@ public function testWithController(): void
{
$controller = new FakeController();

$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withController($controller)
->withController($controller); // twice for test of cache

Expand All @@ -163,33 +179,43 @@ public function testWithIncorrectController(): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cannot detect controller name.');
$this->getRenderer()->withController(new stdClass());
$this
->getRenderer()
->withController(new stdClass());
}

public function testWithViewPath(): void
{
$renderer = $this->getRenderer()->withViewPath('/dir/');
$renderer = $this
->getRenderer()
->withViewPath('/dir/');

$this->assertSame('/dir', $renderer->getViewPath());
}

public function testWithViewPathWithAlias(): void
{
$renderer = $this->getRenderer()->withViewPath('@views/dir');
$renderer = $this
->getRenderer()
->withViewPath('@views/dir');

$this->assertSame($this->getViewsDir() . '/dir', $renderer->getViewPath());
}

public function testWithViewPathWithController(): void
{
$renderer = $this->getRenderer()->withViewPath('/dir//')->withController(new FakeController());
$renderer = $this
->getRenderer()
->withViewPath('/dir//')
->withController(new FakeController());

$this->assertSame('/dir/support/fake', $renderer->getViewPath());
}

public function testInvalidMetaTag(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withInjections(new InvalidMetaTagInjection());

$response = $renderer->render('empty');
Expand All @@ -203,7 +229,8 @@ public function testInvalidMetaTag(): void

public function testInvalidLinkTag(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withInjections(new InvalidLinkTagInjection());

$response = $renderer->render('empty');
Expand All @@ -217,7 +244,8 @@ public function testInvalidLinkTag(): void

public function testInvalidPositionInLinkTag(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withInjections(new InvalidPositionInLinkTagInjection());

$response = $renderer->render('empty');
Expand All @@ -231,7 +259,8 @@ public function testInvalidPositionInLinkTag(): void

public function testCommonParametersInjectionsToNestedViews(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withLayout(null)
->withInjections(new TestInjection());

Expand All @@ -242,7 +271,8 @@ public function testCommonParametersInjectionsToNestedViews(): void

public function testLayoutParametersInjectionsToNestedViews(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withLayout('@views/nested-layout/layout')
->withInjections(new TitleInjection());

Expand All @@ -256,7 +286,8 @@ public function testLayoutParametersInjectionsToNestedViews(): void

public function testChangeInjectionsAfterCreateProxyAndBeforeRender(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withLayout('@views/with-injection/layout')
->withControllerName('with-injection')
->withInjections(new TestInjection())
Expand Down Expand Up @@ -288,7 +319,8 @@ public function testChangeInjectionsAfterCreateProxyAndBeforeRender(): void

public function testPassingCommonParametersFromContentToLayout(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withViewPath('@views/passing-parameters-to-layout')
->withLayout('@views/passing-parameters-to-layout/layout');

Expand All @@ -303,7 +335,8 @@ public function testPassingCommonParametersFromContentToLayout(): void

public function testCommonParametersOverrideLayout(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withLayout('@views/override-layout-parameters/layout')
->withInjections(new CommonParametersInjection())
;
Expand All @@ -317,7 +350,8 @@ public function testCommonParametersOverrideLayout(): void

public function testInRenderSetParametersOverrideLayout(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withViewPath('@views/override-layout-parameters')
->withLayout('@views/override-layout-parameters/layout')
->withInjections(new CommonParametersInjection(), new LayoutParametersInjection())
Expand All @@ -332,7 +366,8 @@ public function testInRenderSetParametersOverrideLayout(): void

public function testRenderParametersNotOverrideLayout(): void
{
$renderer = $this->getRenderer()
$renderer = $this
->getRenderer()
->withLayout('@views/override-layout-parameters/layout')
->withInjections(new LayoutParametersInjection())
;
Expand Down

0 comments on commit a472cc5

Please sign in to comment.