Beispiel #1
0
 /**
  * @covers Foote\Ginny\Map\BaseBundle::__construct
  * @covers Foote\Ginny\Map\BaseBundle::addModel
  */
 public function test()
 {
     $bundle = new BaseBundle('BigThing');
     $bundle->addModel(new BaseModel('LittleThing'));
     $bundle->addModel(new BaseModel('OtherLittleThing'));
     $map = new BaseMap('Admin');
     $map->addBundle($bundle);
     $this->assertEquals($bundle->name, 'BigThing');
     $this->assertEquals($bundle->single, 'BigThing');
     $this->assertEquals($bundle->plural, 'BigThings');
     $this->assertInstanceOf('\\Doctrine\\Common\\Collections\\ArrayCollection', $bundle->models);
     $this->assertFalse($bundle->models->isEmpty());
 }
Beispiel #2
0
 /**
  * @covers \Foote\Ginny\Generator\BaseGenerator::generate
  */
 public function testgenerate()
 {
     // where bundle=system
     $input = $this->getInput();
     $input->setOption('bundle', 'System');
     $generator = new BaseGenerator($input, new ConsoleOutput(ConsoleOutput::VERBOSITY_QUIET));
     $map = new BaseMap('Admin');
     $map->addBundle(new BaseBundle('System'));
     $map->addBundle(new BaseBundle('Content'));
     $generator->map = $map;
     //does nothing
     $generator->generate();
 }
Beispiel #3
0
 /**
  * @covers Foote\Ginny\Map\BaseMap::validate
  *
  * @expectedException \Foote\Ginny\Exception\GinnyMapException
  * @expectedExceptionCode 301
  */
 public function testInvalidModel()
 {
     $model = new BaseModel('Thing');
     $bundle = new BaseBundle('Bundle');
     $bundle->addModel($model);
     $map = new BaseMap('Admin');
     $map->addBundle($bundle);
     //additional model validations tested inside BaseBundleTest
     $map->validate();
 }
Beispiel #4
0
 public function map()
 {
     foreach ($this->schema->bundles as $_bundle) {
         $bundle = new BaseBundle($_bundle->name);
         $this->map->addBundle($bundle);
         # add models to bundle
         foreach ($_bundle->models as $_model) {
             if (strpos($_model->name, ':') != false) {
                 continue;
             }
             $model = new BaseModel($_model->name);
             $bundle->addModel($model);
             $model->description = $_model->description;
             $model->table = $model->bundle->snake . '_' . $model->snakes;
             if (!empty($_model->ormAttributes)) {
                 foreach ($_model->ormAttributes as $key => $val) {
                     if ('table' == $key) {
                         $model->table = $val;
                     }
                 }
             }
             # add fields to model
             foreach ($_model->fields as $_field) {
                 $field = new BaseField($_field->name);
                 $model->addField($field);
                 $field->type = $_field->type;
                 $field->size = $_field->size;
                 $field->default = $_field->default;
                 $field->required = $_field->required;
                 $field->unique = $_field->unique;
                 $field->primary = $_field->primary;
                 $field->autoIncrement = $_field->autoIncrement;
             }
         }
         # add relationships to models
         foreach ($_bundle->models as $_model) {
             if (strpos($_model->name, ':') != false) {
                 continue;
             }
             $model = $bundle->models->get($_model->name);
             foreach ($_bundle->associations as $_association) {
                 # belongsTo (ex. User belongsTo Site)
                 if ($model->name == $_association->from) {
                     $parent = $bundle->models->get($_association->to);
                     new BaseAssociation($parent->name, ['owner' => $model, 'ownerKey' => $_association->field->from, 'type' => 'belongsTo', 'target' => $parent, 'targetKey' => $_association->field->to]);
                 }
                 # hasOne and hasMany (ex. User hasOne Profile, User hasMany Comment)
                 if ($model->name == $_association->to) {
                     $child = $bundle->models->get($_association->from);
                     new BaseAssociation($child->name, ['owner' => $model, 'ownerKey' => $_association->field->to, 'type' => 'hasMany', 'target' => $child, 'targetKey' => $_association->field->from]);
                     /**
                      * for User hasOne Profile, if Profile.user is set to unique, the association
                      * will change to "hasOne" on $association->update()
                      */
                 }
             }
             # manyToMany
             foreach ($_bundle->manyToManys as $_manyToMany) {
                 if ($model->name == $_manyToMany->name) {
                     /**
                      * example: User <--> UserRole <--> Role
                      */
                     // identify UserRole as a manyToMany table
                     $model->manyToMany = true;
                     // ex: Role
                     $manyModel1 = $bundle->models->get($_manyToMany->owner->name);
                     //Role
                     // ex: User
                     $manyModel2 = $bundle->models->get($_manyToMany->inverse->name);
                     //User
                     new BaseAssociation($manyModel2->name, ['owner' => $model, 'ownerKey' => $_manyToMany->inverse->field->from, 'type' => 'belongsTo', 'target' => $manyModel2, 'targetKey' => $_manyToMany->owner->field->to]);
                     new BaseAssociation($manyModel1->name, ['owner' => $model, 'ownerKey' => $_manyToMany->owner->field->from, 'type' => 'belongsTo', 'target' => $manyModel1, 'targetKey' => $_manyToMany->inverse->field->to]);
                     new BaseAssociation($manyModel1->name, ['owner' => $manyModel2, 'ownerKey' => $_manyToMany->owner->field->to, 'type' => 'belongsToMany', 'target' => $manyModel1, 'targetKey' => $_manyToMany->inverse->field->to, 'pivot' => $model]);
                     new BaseAssociation($manyModel2->name, ['owner' => $manyModel1, 'ownerKey' => $_manyToMany->inverse->field->to, 'type' => 'belongsToMany', 'target' => $manyModel2, 'targetKey' => $_manyToMany->owner->field->to, 'pivot' => $model]);
                 }
             }
         }
     }
     $this->map->update();
     $this->map->validate();
     //s($this->map->dump());
     return $this->map;
 }