예제 #1
0
 /**
  * @param null $onlyClassName
  * @param null $key
  * @throws \Exception
  */
 public function seed($onlyClassName = null, $key = null)
 {
     // seed random to get different results each run
     srand();
     $this->outputFormatter->beginSeed();
     $dataObjects = $this->config()->create;
     $configParser = new ConfigParser($this->writer);
     $heuristics = array();
     $config = \Config::inst()->get('Seeder', 'heuristics');
     if ($config) {
         $heuristicParser = new HeuristicParser();
         $heuristics = $heuristicParser->parse($config);
     }
     if (is_array($dataObjects)) {
         foreach ($dataObjects as $index => $option) {
             $className = $index;
             if (isset($option['class'])) {
                 $className = $option['class'];
             }
             if (empty($option['key'])) {
                 $option['key'] = $className;
             }
             if (class_exists($className) && (!$onlyClassName || $onlyClassName === $className) && (!$key || $key === $option['key'])) {
                 $option['class'] = $className;
                 $field = $configParser->objectConfig2Field($option);
                 $field->name = $option['class'];
                 // has_many will generate the number passed in count
                 $field->fieldName = '';
                 $field->methodName = '';
                 $field->count = $this->getCount($field);
                 if (!$field->count) {
                     $this->outputFormatter->alreadySeeded($option['class'], $option['key']);
                 } else {
                     $this->outputFormatter->creatingDataObject($option['class'], $option['key']);
                     $this->applyHeuristics($field, $heuristics);
                     $state = new SeederState();
                     $objects = $field->provider->generate($field, $state);
                     $this->writer->finish();
                     $this->outputFormatter->dataObjectsCreated($option['class'], count($objects));
                 }
             } else {
                 error_log('Class not found ' . $className);
             }
         }
     } else {
         throw new \Exception('\'create\' must be an array');
     }
 }
 /**
  *
  */
 public function testMatch_ManyConditions_MatchesSuccessfully()
 {
     $parser = new HeuristicParser();
     $heuristics = $parser->parse(array('MenuTitle' => array('conditions' => array('name' => 'MenuTitle', 'fieldType' => 'db', 'dataType' => 'like(varchar%)', 'parent' => 'is_a(SiteTree)'), 'field' => '{$Title}')));
     $heuristic = $heuristics[0];
     $field = new Field();
     $field->name = 'Magic';
     $field->dataType = 'SiteTree';
     $titleField = new Field();
     $titleField->name = 'MenuTitle';
     $titleField->dataType = 'Varchar';
     $titleField->fieldType = Field::FT_FIELD;
     $titleField->parent = $field;
     $field->fields[] = $titleField;
     $this->assertTrue($heuristic->match($titleField));
     $heuristic->apply($titleField, new RecordWriter());
     $this->assertInstanceOf('Seeder\\ValueProvider', $titleField->provider);
 }
 /**
  *
  */
 public function testParse_ParentHeuristics_HeuristicParsedCorrectly()
 {
     $parser = new HeuristicParser();
     $heuristics = $parser->parse(array('Test' => array('conditions' => array('parent.fieldType' => array('has_many', 'many_many')))));
     $heuristic = $heuristics[0];
     $field = new Field();
     $field->fieldType = 'has_many';
     $childField = new Field();
     $childField->parent = $field;
     $childField->fieldType = 'has_many';
     $this->assertTrue($heuristic->match($childField));
     $field->fieldType = 'has_one';
     $this->assertFalse($heuristic->match($childField));
 }