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

Fix getControllerId() auto-generated id #398

Merged
merged 7 commits into from
Feb 17, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ before_script:
fi

script:
- phpunit $PHPUNIT_FLAGS
- ./vendor/bin/phpunit $PHPUNIT_FLAGS

after_script:
- |
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Yii Framework 2 gii extension Change Log
2.1.0 under development
-----------------------

- Bug #398, #397: Use strict mode when generating view folder name (machour)
- Enh #390, Bug #260: Create (bootstrap)-independent version (simialbi)
- Enh #395: Made `yii\gii\CodeFile` indepdendent of controller context, do not apply `$newDirMode` and `$newFileMode` if module is not available (CeBe)

Expand Down
7 changes: 6 additions & 1 deletion src/generators/crud/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class Generator extends \yii\gii\Generator
* @since 2.0.5
*/
public $enablePjax = false;
/**
* @var bool whether to use strict inflection for controller IDs (insert a separator between two consecutive uppercase chars)
* @since 2.1.0
*/
public $strictInflector = true;


/**
Expand Down Expand Up @@ -195,7 +200,7 @@ public function getControllerID()
$pos = strrpos($this->controllerClass, '\\');
$class = substr(substr($this->controllerClass, $pos + 1), 0, -10);

return Inflector::camel2id($class);
return Inflector::camel2id($class, '-', $this->strictInflector);
}

/**
Expand Down
30 changes: 29 additions & 1 deletion tests/generators/CrudGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,32 @@ public function testGenerateColumnFormat()
$c = new ColumnSchema(['phpType' => 'string', 'type' => 'string', 'name' => 'url_lalala']);
$this->assertEquals('url', $g->generateColumnFormat($c));
}
}

public function testGeneratedControllerId()
{
$g = new Generator();
$g->controllerClass = '\app\controllers\TestController';
$this->assertEquals('test', $g->getControllerID());

$g = new Generator();
$g->controllerClass = '\app\controllers\SomeTestController';
$this->assertEquals('some-test', $g->getControllerID());

$g = new Generator();
$g->controllerClass = '\app\controllers\ATestController';
$this->assertEquals('a-test', $g->getControllerID());

$g = new Generator();
$g->controllerClass = '\app\controllers\YoTestController';
$this->assertEquals('yo-test', $g->getControllerID());

$g = new Generator();
$g->controllerClass = '\app\controllers\ABCTestController';
$this->assertEquals('a-b-c-test', $g->getControllerID());

$g = new Generator();
$g->controllerClass = '\app\controllers\XYTestController';
$this->assertEquals('x-y-test', $g->getControllerID());
}

}