SchemaTest.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace yiiunit\gii;
  3. use yii\gii\generators\model\Generator as ModelGenerator;
  4. use Yii;
  5. /**
  6. * SchemaTest checks that Gii model generator supports multiple schemas
  7. * @group gii
  8. * @group pgsql
  9. */
  10. class SchemaTest extends GiiTestCase
  11. {
  12. protected $driverName = 'pgsql';
  13. public function testPrefixesGenerator()
  14. {
  15. $generator = new ModelGenerator();
  16. $generator->template = 'default';
  17. $generator->tableName = 'schema1.*';
  18. $generator->generateRelationsFromCurrentSchema = false;
  19. $files = $generator->generate();
  20. if (version_compare(str_replace('-dev', '', Yii::getVersion()), '2.0.4', '<')) {
  21. $this->markTestSkipped('This feature is only available since Yii 2.0.4.');
  22. }
  23. $this->assertEquals(5, count($files));
  24. $this->assertEquals("Schema1Table1", basename($files[3]->path, '.php'));
  25. $this->assertEquals("Schema1Table2", basename($files[4]->path, '.php'));
  26. }
  27. public function relationsProvider()
  28. {
  29. return [
  30. ['default', 'schema1.*', 5, [
  31. 0 => [ // relations from junction1 table
  32. "\$this->hasOne(Schema1Table1::className(), ['id' => 'table1_id']);",
  33. "\$this->hasOne(Schema1MultiPk::className(), ['id1' => 'multi_pk_id1', 'id2' => 'multi_pk_id2']);",
  34. ],
  35. 2 => [ // relations from multi_pk table
  36. "\$this->hasMany(Schema1Table1::className(), ['id' => 'table1_id'])->viaTable('junction1', ['multi_pk_id1' => 'id1', 'multi_pk_id2' => 'id2']);",
  37. "\$this->hasMany(Schema1Junction1::className(), ['multi_pk_id1' => 'id1', 'multi_pk_id2' => 'id2']);",
  38. ],
  39. 3 => [ // relations from table1 table
  40. "\$this->hasMany(Schema2Table1::className(), ['fk1' => 'fk2', 'fk2' => 'fk1']);",
  41. "\$this->hasMany(Schema2Table1::className(), ['fk3' => 'fk4', 'fk4' => 'fk3']);",
  42. "\$this->hasOne(Schema2Table2::className(), ['fk1' => 'fk1', 'fk2' => 'fk2']);",
  43. "\$this->hasMany(Schema1MultiPk::className(), ['id1' => 'multi_pk_id1', 'id2' => 'multi_pk_id2'])->viaTable('junction1', ['table1_id' => 'id']);",
  44. "\$this->hasMany(Schema1Junction1::className(), ['table1_id' => 'id']);",
  45. ],
  46. ]],
  47. ['default', 'schema2.*', 2, [
  48. 0 => [
  49. "\$this->hasOne(Schema1Table1::className(), ['fk2' => 'fk1', 'fk1' => 'fk2']);",
  50. "\$this->hasOne(Schema1Table1::className(), ['fk4' => 'fk3', 'fk3' => 'fk4']);",
  51. "\$this->hasOne(Schema2Table2::className(), ['fk5' => 'fk5', 'fk6' => 'fk6']);",
  52. ],
  53. ]],
  54. ];
  55. }
  56. /**
  57. * @dataProvider relationsProvider
  58. */
  59. public function testRelationsGenerator($template, $tableName, $filesCount, $relationSets)
  60. {
  61. $generator = new ModelGenerator();
  62. $generator->template = $template;
  63. $generator->tableName = $tableName;
  64. $generator->generateRelationsFromCurrentSchema = false;
  65. $files = $generator->generate();
  66. $this->assertEquals($filesCount, count($files));
  67. foreach ($relationSets as $index => $relations) {
  68. $modelCode = $files[$index]->content;
  69. $modelClass = basename($files[$index]->path, '.php');
  70. if (version_compare(str_replace('-dev', '', Yii::getVersion()), '2.0.4', '<')) {
  71. $this->markTestSkipped('This feature is only available since Yii 2.0.4.');
  72. }
  73. foreach ($relations as $relation) {
  74. $this->assertTrue(strpos($modelCode, $relation) !== false,
  75. "Model $modelClass should contain this relation: $relation.\n$modelCode");
  76. }
  77. }
  78. }
  79. }