CrudGeneratorTest.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace yiiunit\gii\generators;
  3. use yii\db\ColumnSchema;
  4. use yii\gii\generators\crud\Generator;
  5. use yiiunit\gii\TestCase;
  6. class CrudGeneratorTest extends TestCase
  7. {
  8. public function testGenerateColumnFormat()
  9. {
  10. $g = new Generator();
  11. $c = new ColumnSchema(['phpType' => 'boolean', 'type' => 'boolean', 'name' => 'is_enabled']);
  12. $this->assertEquals('boolean', $g->generateColumnFormat($c));
  13. $c = new ColumnSchema(['phpType' => 'string', 'type' => 'text', 'name' => 'description']);
  14. $this->assertEquals('ntext', $g->generateColumnFormat($c));
  15. $c = new ColumnSchema(['phpType' => 'integer', 'type' => 'integer', 'name' => 'create_time']);
  16. $this->assertEquals('datetime', $g->generateColumnFormat($c));
  17. $c = new ColumnSchema(['phpType' => 'string', 'type' => 'string', 'name' => 'email_address']);
  18. $this->assertEquals('email', $g->generateColumnFormat($c));
  19. $c = new ColumnSchema(['phpType' => 'string', 'type' => 'string', 'name' => 'email_address']);
  20. $this->assertEquals('email', $g->generateColumnFormat($c));
  21. // url type and false positive checks for URL
  22. $c = new ColumnSchema(['phpType' => 'string', 'type' => 'string', 'name' => 'hourly']);
  23. $this->assertEquals('text', $g->generateColumnFormat($c));
  24. $c = new ColumnSchema(['phpType' => 'string', 'type' => 'string', 'name' => 'some_hourly_check']);
  25. $this->assertEquals('text', $g->generateColumnFormat($c));
  26. $c = new ColumnSchema(['phpType' => 'string', 'type' => 'string', 'name' => 'some_url']);
  27. $this->assertEquals('url', $g->generateColumnFormat($c));
  28. $c = new ColumnSchema(['phpType' => 'string', 'type' => 'string', 'name' => 'my_url_string']);
  29. $this->assertEquals('url', $g->generateColumnFormat($c));
  30. $c = new ColumnSchema(['phpType' => 'string', 'type' => 'string', 'name' => 'url_lalala']);
  31. $this->assertEquals('url', $g->generateColumnFormat($c));
  32. }
  33. public function testGeneratedControllerId()
  34. {
  35. $g = new Generator();
  36. $g->controllerClass = '\app\controllers\TestController';
  37. $this->assertEquals('test', $g->getControllerID());
  38. $g = new Generator();
  39. $g->controllerClass = '\app\controllers\SomeTestController';
  40. $this->assertEquals('some-test', $g->getControllerID());
  41. $g = new Generator();
  42. $g->controllerClass = '\app\controllers\ATestController';
  43. $this->assertEquals('a-test', $g->getControllerID());
  44. $g = new Generator();
  45. $g->controllerClass = '\app\controllers\YoTestController';
  46. $this->assertEquals('yo-test', $g->getControllerID());
  47. $g = new Generator();
  48. $g->controllerClass = '\app\controllers\ABCTestController';
  49. $this->assertEquals('a-b-c-test', $g->getControllerID());
  50. $g = new Generator();
  51. $g->controllerClass = '\app\controllers\XYTestController';
  52. $this->assertEquals('x-y-test', $g->getControllerID());
  53. }
  54. }