Exemple #1
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;
 }
 protected function runGetRelationsForClassScenario($choices = array())
 {
     $externalRelationsAdded = array_get($choices, 'external_relations_added', false);
     $excludeRelations = array_get($choices, 'excluding_relations', false);
     $relationName = 'relation';
     $relationForeignKey = 'foreignKey';
     $relatedClassName = 'Testing\\DummyModelClass';
     $relationClass = 'Illuminate\\Database\\Eloquent\\Relations\\BelongsTo';
     $relationType = 'BelongsTo';
     $externalRelationName = 'bar';
     $externalRelationForeignKey = 'externalForeignKey';
     $externalRelatedClassName = 'Testing\\DummyModelClass2';
     $externalRelationClass = 'Illuminate\\Database\\Eloquent\\Relations\\BelongsTo';
     $externalRelationType = 'BelongsTo';
     $allowedRelations = $excludeRelations ? array($externalRelationName) : array($relationName, $externalRelationName);
     $relatedTo = $externalRelationsAdded ? array($externalRelationName) : array();
     $databaseManager = Mockery::mock('Skovachev\\Fakefactory\\Model\\DatabaseManager');
     $databaseManager->shouldReceive('registerTypeMapping')->once()->with('enum', 'string');
     $databaseManager->shouldReceive('listTableColumnsAsArray')->once()->with('table')->andReturn(array($relationName . '_id' => 'bar'));
     $model = Mockery::mock();
     $model->shouldReceive('getTable')->andReturn('table');
     $reflector = Mockery::mock('Skovachev\\Fakefactory\\Model\\Reflector');
     $reflector->shouldReceive('instantiate')->with('className')->andReturn($model);
     if (!$excludeRelations) {
         $relationRelatedObject = Mockery::mock();
         $relationObject = Mockery::mock();
         $relationObject->shouldReceive('getRelated')->once()->andReturn($relationRelatedObject);
         $relationObject->shouldReceive('getForeignKey')->once()->andReturn($relationForeignKey);
         $model->shouldReceive($relationName)->andReturn($relationObject);
         $reflector->shouldReceive('methodExists')->andReturn(true);
         $reflector->shouldReceive('isRelation')->andReturn(true);
         $reflector->shouldReceive('getClass')->with($relationObject)->andReturn($relationClass);
         $reflector->shouldReceive('getClass')->with($relationRelatedObject)->andReturn($relatedClassName);
         if ($externalRelationsAdded) {
             $externalRelationRelatedObject = Mockery::mock();
             $externalRelationObject = Mockery::mock();
             $externalRelationObject->shouldReceive('getRelated')->once()->andReturn($externalRelationRelatedObject);
             $externalRelationObject->shouldReceive('getForeignKey')->once()->andReturn($externalRelationForeignKey);
             $model->shouldReceive($externalRelationName)->andReturn($externalRelationObject);
             $reflector->shouldReceive('getClass')->with($externalRelationObject)->andReturn($externalRelationClass);
             $reflector->shouldReceive('getClass')->with($externalRelationRelatedObject)->andReturn($externalRelatedClassName);
         }
     }
     $manager = new ModelManager($databaseManager, $reflector);
     $manager->clearCachedFieldData();
     $relations = $manager->getRelationsForClass('className', $relatedTo, $allowedRelations);
     $expectedCount = 1;
     if ($excludeRelations) {
         $expectedCount = 0;
     } else {
         if ($externalRelationsAdded) {
             $expectedCount = 2;
         }
     }
     $this->assertCount($expectedCount, $relations);
     if (!$excludeRelations) {
         $relation = $relations[0];
         $this->assertInstanceOf('Skovachev\\Fakefactory\\Model\\Blueprint\\Relation', $relation);
         $this->assertEquals($relation->getName(), $relationName);
         $this->assertEquals($relation->getType(), $relationType);
         $this->assertEquals($relation->getRelatedClassName(), $relatedClassName);
         $this->assertEquals($relation->getForeignKey(), $relationForeignKey);
         if ($externalRelationsAdded) {
             $this->assertInstanceOf('Skovachev\\Fakefactory\\Model\\Blueprint\\Relation', $relations[1]);
             $this->assertEquals($relations[1]->getName(), $externalRelationName);
             $this->assertEquals($relations[1]->getType(), $externalRelationType);
             $this->assertEquals($relations[1]->getRelatedClassName(), $externalRelatedClassName);
             $this->assertEquals($relations[1]->getForeignKey(), $externalRelationForeignKey);
         }
     }
 }