Exemplo n.º 1
0
 public function testInitializeRelations()
 {
     $blueprint = new Blueprint('foo');
     $relations = array('bar' => 'bar');
     $relation = Mockery::mock('Skovachev\\Fakefactory\\Model\\Blueprint\\Relation');
     $relation->shouldReceive('getName')->once()->andReturn('foo');
     $blueprint->mergeRelations(array($relation));
     $blueprint->initializeRelations($relations);
 }
Exemplo n.º 2
0
 /**
  * Creates a Blueprint for a class
  * 
  * @param string $class 
  * @param boolean $asRelationship if set the factory will not explore further nested relations
  * @return Skovachev\Fakefactory\Model\Blueprint\Blueprint
  */
 public function makeBlueprint($class, $asRelationship = false)
 {
     $overrides = $this->getBuildOption('override_attributes', array());
     $excludeAttributes = $this->getBuildOption('exclude_attributes', array());
     $overrideRules = $this->getBuildOption('override_rules', array());
     $skipRelatedModels = $this->getBuildOption('skip_related_models', false);
     if ($this->getBuildOption('generate_id') === false) {
         $excludeAttributes[] = 'id';
     }
     // get faker class
     $faker = $this->getClassFaker($class, $overrideRules);
     $blueprint = new Blueprint($class);
     // if is model class -> try to extract information from model / database
     if ($this->model->isModelClass($class)) {
         // extract fields
         $attributes = $this->model->getAttributesForClass($class);
         // exclude attributes from generation
         $attributes = array_filter($attributes, function ($attribute) use($excludeAttributes) {
             return !in_array($attribute->getName(), $excludeAttributes);
         });
         // merge fields from model
         $blueprint->mergeAttributes($attributes);
         // load relationship fields only if obj being built is not part of a relation
         if (!$asRelationship) {
             // extract relationships from model / db
             $mandatoryRelations = array_merge($faker->getMandatoryRelations(), $this->getBuildOption('with'));
             $relations = $this->model->getRelationsForClass($class, $faker->getRelatedTo(), $skipRelatedModels ? array() : $mandatoryRelations);
             // add relationship data to faker
             $blueprint->mergeRelations($relations);
         }
     }
     $faker->setClassBlueprint($blueprint);
     // generate fake attributes
     $attributes = $faker->fakeAttributes();
     // generate fake relations
     $relations = $asRelationship ? array() : $faker->fakeRelations($this->extractRelationOverrides($overrides));
     $blueprint->initializeAttributes($attributes);
     $blueprint->initializeRelations($relations);
     // apply overrides for fields
     if (!empty($overrides)) {
         $attributeOverrides = $this->extractAttributeOverrides($overrides);
         $blueprint->initializeAttributes($attributeOverrides);
     }
     return $blueprint;
 }
Exemplo n.º 3
0
 /**
  * Create a fake model instance from Blueprint
  * 
  * @param Skovachev\Fakefactory\Model\Blueprint\Blueprint $blueprint 
  * @return object
  */
 protected function makeFromBlueprint($blueprint)
 {
     $modelClass = $blueprint->getClass();
     $model = $this->reflector->instantiate($modelClass);
     foreach ($blueprint->getAttributes() as $attribute) {
         $this->model->setAttribute($model, $attribute);
     }
     foreach ($blueprint->getRelations() as $relation) {
         $relation->applyToModelAndContainedValue($model);
         $this->model->setRelation($model, $relation);
     }
     $this->clearBuildOptions();
     return $model;
 }