Skip to content

Commit

Permalink
tests added (ViewRenderer#extractControllerName) (#60)
Browse files Browse the repository at this point in the history
* tests added (ViewRenderer#extractControllerName)

* fix tests

* fix

* .editorconfig

* changelog

* Update CHANGELOG.md

Co-authored-by: Alexander Makarov <[email protected]>

* Update tests/ViewRendererTest.php

* Update tests/ViewRendererTest.php

* Update tests/ViewRendererTest.php

* Update tests/ViewRendererTest.php

Co-authored-by: Sergei Predvoditelev <[email protected]>
Co-authored-by: Sergei Predvoditelev <[email protected]>
Co-authored-by: Alexander Makarov <[email protected]>
Co-authored-by: Alexey Rogachev <[email protected]>
  • Loading branch information
5 people authored Oct 12, 2022
1 parent 999a9e7 commit 61e6ec2
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 25 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ indent_style = space
indent_size = 4
trim_trailing_whitespace = true

[*.php]
ij_php_space_before_short_closure_left_parenthesis = true
ij_php_space_after_type_cast = true

[*.md]
trim_trailing_whitespace = false

Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 5.0.1 under development

- no changes in this release.
- Bug #61: Fixed getting incorrect controller name based on controller instance (@vjik, @kamarton)

## 5.0.0 July 23, 2022

Expand Down
2 changes: 1 addition & 1 deletion src/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ private function extractControllerName(object $controller): string
return $cache[$class];
}

$regexp = '/((?<=controller\\\|s\\\)(?:[\w\\\]+)|(?:[a-z]+))controller/iuU';
$regexp = '/((?<=controller\\\|controllers\\\)(?:[\w\\\]+)|(?:[a-z]+))controller/iuU';
if (!preg_match($regexp, $class, $m) || empty($m[1])) {
throw new RuntimeException('Cannot detect controller name.');
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Support/Controller/SubNamespace/FakeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Tests\Support\Controller\SubNamespace;

final class FakeController
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Tests\Support\Controller\SubNamespace\SubNamespace2;

final class FakeController
{
}
9 changes: 9 additions & 0 deletions tests/Support/Controllers/SubNamespace/FakeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Tests\Support\Controllers\SubNamespace;

final class FakeController
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Tests\Support\Controllers\SubNamespace\SubNamespace2;

final class FakeController
{
}
9 changes: 9 additions & 0 deletions tests/Support/FakeCntrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Tests\Support;

final class FakeCntrl
{
}
9 changes: 9 additions & 0 deletions tests/Support/NotCntrls/SubNamespace/FakeCntrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Tests\Support\NotCntrls\SubNamespace;

final class FakeCntrl
{
}
9 changes: 9 additions & 0 deletions tests/Support/NotCntrls/SubNamespace/FakeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\View\Tests\Support\NotCntrls\SubNamespace;

final class FakeController
{
}
94 changes: 71 additions & 23 deletions tests/ViewRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Yiisoft\View\WebView;
use Yiisoft\Yii\View\Exception\InvalidLinkTagException;
use Yiisoft\Yii\View\Exception\InvalidMetaTagException;
use Yiisoft\Yii\View\Tests\Support\FakeCntrl;
use Yiisoft\Yii\View\Tests\Support\FakeController;
use Yiisoft\Yii\View\Tests\Support\InvalidLinkTagInjection;
use Yiisoft\Yii\View\Tests\Support\InvalidPositionInLinkTagInjection;
Expand Down Expand Up @@ -58,9 +59,12 @@ public function testRenderAndRenderAsString(): void

$this->assertEqualStringsIgnoringLineEndings($expected, (string) $response->getBody());

$this->assertEqualStringsIgnoringLineEndings($expected, $renderer->renderAsString('view', [
'name' => 'donatello',
]));
$this->assertEqualStringsIgnoringLineEndings(
$expected,
$renderer->renderAsString('view', [
'name' => 'donatello',
])
);
}

public function testRenderWithAbsoluteLayoutPath(): void
Expand Down Expand Up @@ -175,7 +179,44 @@ public function testRenderWithLocale(): void
$this->assertSame('<html><body>de_DE locale</body></html>', (string) $response->getBody());
}

public function testWithController(): void
public function dataWithController(): array
{
return [
'controller name, no "controller" / "controllers" namespaces, no subnamespaces' => [new Support\FakeController(), '/fake'],
'controller name, "controller" namespace, 1 subnamespace' => [
new Support\Controller\SubNamespace\FakeController(),
'/sub-namespace/fake',
],
'controller name, "controllers" namespace, 1 subnamespace' => [
new Support\Controllers\SubNamespace\FakeController(),
'/sub-namespace/fake',
],
'controller name, "controller" namespace, 2 subnamespaces' => [
new Support\Controller\SubNamespace\SubNamespace2\FakeController(),
'/sub-namespace/sub-namespace2/fake',
],
'controller name, "controllers" namespace, 2 subnamespaces' => [
new Support\Controllers\SubNamespace\SubNamespace2\FakeController(),
'/sub-namespace/sub-namespace2/fake',
],
'controller name, without "controller" / "controllers" namespaces, subnamespaces' => [
new Support\NotCntrls\SubNamespace\FakeController(),
'/fake',
],
];
}

/**
* @dataProvider dataWithController
*/
public function testWithController(object $controller, string $path): void
{
$renderer = $this->getRenderer()->withController($controller);

$this->assertSame($this->getViewsDir() . $path, $renderer->getViewPath());
}

public function testTwiceWithController(): void
{
$controller = new FakeController();

Expand All @@ -184,16 +225,27 @@ public function testWithController(): void
->withController($controller)
->withController($controller); // twice for test of cache

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

public function testWithIncorrectController(): void
public function dataWithIncorrectController(): array
{
return [
'stdClass' => [new stdClass()],
'withNamespace' => [new FakeCntrl()],
];
}

/**
* @dataProvider dataWithIncorrectController
*/
public function testWithIncorrectController(object $controller): void
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Cannot detect controller name.');
$this
->getRenderer()
->withController(new stdClass());
->withController($controller);
}

public function testWithViewPath(): void
Expand Down Expand Up @@ -221,7 +273,7 @@ public function testWithViewPathWithController(): void
->withViewPath('/dir//')
->withController(new FakeController());

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

public function testInvalidMetaTag(): void
Expand Down Expand Up @@ -278,7 +330,7 @@ public function testCommonParametersInjectionsToNestedViews(): void

$response = $renderer->render('nested/root', ['label' => 'root']);

$this->assertSame('root: leonardo. nested-1: leonardo. nested-2: leonardo.', (string)$response->getBody());
$this->assertSame('root: leonardo. nested-1: leonardo. nested-2: leonardo.', (string) $response->getBody());
}

public function testLayoutParametersInjectionsToNestedViews(): void
Expand All @@ -292,7 +344,7 @@ public function testLayoutParametersInjectionsToNestedViews(): void

$this->assertSame(
'<html><head><title>Hello</title></head><body><h1>Hello</h1></body></html>',
(string)$response->getBody(),
(string) $response->getBody(),
);
}

Expand All @@ -302,8 +354,7 @@ public function testChangeInjectionsAfterCreateProxyAndBeforeRender(): void
->getRenderer()
->withLayout('@views/with-injection/layout')
->withControllerName('with-injection')
->withInjections(new TestInjection())
;
->withInjections(new TestInjection());

$response = $renderer->render('view', [
'name' => 'donatello',
Expand All @@ -326,7 +377,7 @@ public function testChangeInjectionsAfterCreateProxyAndBeforeRender(): void
</html>
EOD;

$this->assertEqualStringsIgnoringLineEndings($expected, (string)$response->getBody());
$this->assertEqualStringsIgnoringLineEndings($expected, (string) $response->getBody());
}

public function testPassingCommonParametersFromContentToLayout(): void
Expand All @@ -342,22 +393,21 @@ public function testPassingCommonParametersFromContentToLayout(): void

$expected = '<html><head><title>TITLE / HELLO</title></head><body><h1>HELLO</h1></body></html>';

$this->assertEqualStringsIgnoringLineEndings($expected, (string)$response->getBody());
$this->assertEqualStringsIgnoringLineEndings($expected, (string) $response->getBody());
}

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

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

$expected = '<html><head><title>COMMON</title></head><body></body></html>';

$this->assertEqualStringsIgnoringLineEndings($expected, (string)$response->getBody());
$this->assertEqualStringsIgnoringLineEndings($expected, (string) $response->getBody());
}

public function testInRenderSetParametersOverrideLayout(): void
Expand All @@ -366,29 +416,27 @@ public function testInRenderSetParametersOverrideLayout(): void
->getRenderer()
->withViewPath('@views/override-layout-parameters')
->withLayout('@views/override-layout-parameters/layout')
->withInjections(new CommonParametersInjection(), new LayoutParametersInjection())
;
->withInjections(new CommonParametersInjection(), new LayoutParametersInjection());

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

$expected = '<html><head><title>RENDER</title></head><body></body></html>';

$this->assertEqualStringsIgnoringLineEndings($expected, (string)$response->getBody());
$this->assertEqualStringsIgnoringLineEndings($expected, (string) $response->getBody());
}

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

$response = $renderer->render('empty', ['seoTitle' => 'custom']);

$expected = '<html><head><title>LAYOUT</title></head><body></body></html>';

$this->assertEqualStringsIgnoringLineEndings($expected, (string)$response->getBody());
$this->assertEqualStringsIgnoringLineEndings($expected, (string) $response->getBody());
}

public function testImmutability(): void
Expand Down

0 comments on commit 61e6ec2

Please sign in to comment.