Skip to content

Commit

Permalink
#61 tests added and fix more bugs (ViewRenderer#extractControllerName) (
Browse files Browse the repository at this point in the history
  • Loading branch information
kamarton committed Oct 13, 2022
1 parent 64e92e9 commit 83b7c7c
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/ViewRenderer.php
Expand Up @@ -529,7 +529,7 @@ private function extractControllerName(object $controller): string
return $cache[$class];
}

$regexp = '/((?<=controller\\\|controllers\\\)(?:[\w\\\]+)|(?:[a-z]+))controller/iuU';
$regexp = '/((?<=controller\\\|controllers\\\)(?:[\w\\\]+)|(?:[a-z\d]+))controller$/iU';
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/Sub8Namespace/FakeController.php
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

final class FakeController
{
}
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

final class FakeCntrl
{
}
9 changes: 9 additions & 0 deletions tests/Support/Controllers/FakeCntrl.php
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

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

final class FakeCntrl
{
}
2 changes: 1 addition & 1 deletion tests/Support/FakeCntrl.php
Expand Up @@ -4,6 +4,6 @@

namespace Yiisoft\Yii\View\Tests\Support;

final class FakeCntrl
class FakeCntrl
{
}
20 changes: 18 additions & 2 deletions tests/ViewRendererTest.php
Expand Up @@ -203,6 +203,18 @@ public function dataWithController(): array
new Support\NotCntrls\SubNamespace\FakeController(),
'/fake',
],
'controller name with root namespace' => [
$this->getMockBuilder('\stdClass')->setMockClassName('FakeController')->getMock(),
'/fake',
],
'controller class contains number' => [
$this->getMockBuilder('\stdClass')->setMockClassName('Fake8Controller')->getMock(),
'/fake8',
],
'namespace contains number' => [
new Support\Controller\Sub8Namespace\FakeController(),
'/sub8namespace/fake',
],
];
}

Expand Down Expand Up @@ -231,8 +243,12 @@ public function testTwiceWithController(): void
public function dataWithIncorrectController(): array
{
return [
'stdClass' => [new stdClass()],
'withNamespace' => [new FakeCntrl()],
'root namespace' => [new stdClass()],
'with namespace' => [new FakeCntrl()],
'with controllers namespace' => [new Support\Controllers\FakeCntrl()],
'with controller namespace and subnamespace, classname not ends with "Controller"' => [
new Support\Controller\SubNamespace\SubController\FakeCntrl(),
],
];
}

Expand Down

0 comments on commit 83b7c7c

Please sign in to comment.