/**
  * @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 testGenerate_SiteTreeSort_ReturnsIncreasingSort()
 {
     $config = new ConfigParser();
     $field = $config->objectConfig2Field(array('class' => 'SiteTree', 'fields' => array('Title' => 'pageTitle()')));
     $titleField = null;
     foreach ($field->fields as $dbField) {
         if ($dbField->name === 'Title') {
             $titleField = $dbField;
         }
     }
     $this->assertNotNull($titleField);
     $state = new SeederState($field, new SiteTree());
     $provider = new PageTitleProvider();
     $title = $provider->generate($titleField, $state);
     $this->assertEquals('Site Tree', $title[0]);
 }
 /**
  *
  */
 public function testGenerate_SiteTreeSort_ReturnsIncreasingSort()
 {
     $config = new ConfigParser();
     $field = $config->objectConfig2Field(array('class' => 'SiteTree', 'fields' => array('URLSegment' => 'urlSegment()')));
     $urlField = null;
     foreach ($field->fields as $dbField) {
         if ($dbField->name === 'URLSegment') {
             $urlField = $dbField;
         }
     }
     $this->assertNotNull($urlField);
     $state = new SeederState($field, new SiteTree());
     $provider = new URLSegmentProvider();
     $url = $provider->generate($urlField, $state);
     $this->assertEquals('site-tree', $url[0]);
 }
 /**
  *
  */
 public function testGenerate_SiteTreeSort_ReturnsIncreasingSort()
 {
     $config = new ConfigParser();
     $field = $config->objectConfig2Field(array('class' => 'SiteTree', 'fields' => array('Sort' => 'sort()')));
     $sortField = null;
     foreach ($field->fields as $dbField) {
         if ($dbField->name === 'Sort') {
             $sortField = $dbField;
         }
     }
     $this->assertNotNull($sortField);
     $state = new SeederState($field, new SiteTree());
     $provider = new SortProvider();
     $value1 = $provider->generate($sortField, $state);
     $value2 = $provider->generate($sortField, $state);
     $value3 = $provider->generate($sortField, $state);
     $this->assertTrue($value1[0] < $value2[0]);
     $this->assertTrue($value2[0] < $value3[0]);
 }
 /**
  * @param $config
  * @return array
  * @throws \Exception
  */
 public function parse($config)
 {
     if (!is_array($config)) {
         return array();
     }
     $heuristics = array();
     foreach ($config as $name => $heuristicConfig) {
         if (empty($heuristicConfig['conditions']) || !is_array($heuristicConfig['conditions'])) {
             continue;
         }
         $options = isset($heuristicConfig['field']) ? $heuristicConfig['field'] : array();
         if (is_string($options)) {
             $options = $this->configParser->parseProviderOptions($options);
         }
         $heuristic = new Heuristic($name);
         $heuristic->setOptions($options);
         if (array_key_exists('nocache', $heuristicConfig)) {
             $noCache = true;
             if ($heuristicConfig['nocache'] !== null) {
                 $noCache = boolval($heuristicConfig['nocache']);
             }
             $heuristic->setNoCache($noCache);
         }
         if (isset($heuristicConfig['cache'])) {
             $cacheSize = intval($heuristicConfig['cache']);
             $heuristic->setCache($cacheSize);
         }
         if (array_key_exists('ignore', $heuristicConfig)) {
             $ignore = true;
             if ($heuristicConfig['ignore'] !== null) {
                 $ignore = boolval($heuristicConfig['ignore']);
             }
             $heuristic->setIgnore($ignore);
         }
         foreach ($heuristicConfig['conditions'] as $field => $options) {
             $condition = new Condition($field);
             if (!is_array($options)) {
                 $options = array($options);
             }
             foreach ($options as $option) {
                 $matcher = new EqualMatcher($option);
                 preg_match('/^([a-zA-Z0-9_-]+)\\((.+)\\)$/', $option, $matches);
                 if (count($matches) === 3) {
                     $matcherName = strtolower($matches[1]);
                     if ($matcherName === 'like') {
                         $matcher = new LikeMatcher($matches[2]);
                     } else {
                         if ($matcherName === 'gt') {
                             $matcher = new GreaterThanMatcher($matches[2]);
                         } else {
                             if ($matcherName === 'lt') {
                                 $matcher = new LessThanMatcher($matches[2]);
                             } else {
                                 if ($matcherName === 'is_a') {
                                     $matcher = new IsAMatcher($matches[2]);
                                 }
                             }
                         }
                     }
                 }
                 $condition->addMatcher($matcher);
             }
             $heuristic->addCondition($condition);
         }
         $heuristics[] = $heuristic;
     }
     return $heuristics;
 }
 /**
  *
  */
 public function testObjectConfig2Field_ExplicitAndImplicitFields_MarkedCorrectly()
 {
     $config = new ConfigParser(new RecordWriter());
     $field = $config->objectConfig2Field(array('class' => 'Seeder\\Tests\\Human', 'fields' => array('Name' => 'test()')));
     $nameField = $field->fields[0];
     $this->assertTrue($nameField->explicit);
     $ageField = $field->fields[1];
     $this->assertFalse($ageField->explicit);
 }
 /**
  *
  */
 public function testGenerate_PublishedPage_GeneratesPublishedPage()
 {
     $writer = new RecordWriter();
     $configParser = new ConfigParser($writer);
     $field = $configParser->objectConfig2Field(array('class' => 'SiteTree', 'provider' => 'Seeder\\Tests\\TestProvider', 'fields' => array('Title' => 'test()')));
     $provider = $field->provider;
     $pages = $provider->generate($field, new SeederState());
     $writer->finish();
     $this->assertCount(1, $pages);
     $this->assertTrue($pages[0]->isPublished());
     $currentStage = \Versioned::current_stage();
     \Versioned::reading_stage('Stage');
     $this->assertEquals(1, \SiteTree::get()->Count());
     \Versioned::reading_stage('Live');
     $this->assertEquals(1, \SiteTree::get()->Count());
     \Versioned::reading_stage($currentStage);
 }