/** * 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; }
public function testGetAttributesForClass() { $databaseManager = Mockery::mock('Skovachev\\Fakefactory\\Model\\DatabaseManager'); $databaseManager->shouldReceive('registerTypeMapping')->once()->with('enum', 'string'); $databaseManager->shouldReceive('listTableColumnsAsArray')->once()->with('table')->andReturn(array('foo' => 'bar')); $model = Mockery::mock(); $model->shouldReceive('getTable')->andReturn('table'); $reflector = Mockery::mock('Skovachev\\Fakefactory\\Model\\Reflector'); $reflector->shouldReceive('instantiate')->with('foo')->andReturn($model); $manager = new ModelManager($databaseManager, $reflector); $manager->clearCachedFieldData(); $attributes = $manager->getAttributesForClass('foo'); $this->assertCount(1, $attributes); $this->assertInstanceOf('Skovachev\\Fakefactory\\Model\\Blueprint\\Attribute', $attributes[0]); $this->assertEquals($attributes[0]->getName(), 'foo'); $this->assertEquals($attributes[0]->getType(), 'bar'); }