Example #1
0
 /**
  * Insert fixtures
  *
  */
 public function executeFixtures()
 {
     $this->_checkProjectDir();
     $this->_initPropel();
     $dir = 'model/fixtures';
     $files = scandir($dir);
     $fixtures = array();
     foreach ($files as $file) {
         if ($file[0] == '.') {
             continue;
         }
         if (!preg_match('|\\.yml$|', $file)) {
             continue;
         }
         $full = $dir . '/' . $file;
         if (!is_file($full)) {
             continue;
         }
         if (!is_readable($full)) {
             continue;
         }
         $fixtures[] = $full;
     }
     $base_r = new ReflectionClass('BaseObject');
     foreach ($fixtures as $fixture_file) {
         $fixture = FWYAML::load($fixture_file);
         $objects = array();
         foreach ($fixture as $class => $rows) {
             // validate class name
             if (!class_exists($class)) {
                 die("Invalid class: {$class}\n");
             }
             $class_r = new ReflectionClass($class);
             if (!$class_r->isSubclassOf($base_r)) {
                 die("{$class} is not a subclass of BaseObject.\n");
             }
             // create objects
             foreach ($rows as $row_name => $row) {
                 if (isset($objects[$row_name])) {
                     die("Duplicate row name: {$row_name}\n");
                 }
                 $inst = new $class();
                 $objects[$row_name] = $inst;
                 foreach ($row as $field_name => $field_value) {
                     $set_method = 'set' . $field_name;
                     if (!method_exists($inst, $set_method)) {
                         die("Invalid field: {$class}->{$field_name}\n");
                     }
                     if (strpos($field_value, '$') === 0) {
                         $ref = substr($field_value, 1);
                         if (!isset($objects[$ref])) {
                             die("Invalid reference: {$ref}\n");
                         }
                         $field_value = $objects[$ref];
                     }
                     call_user_func(array($inst, $set_method), $field_value);
                 }
             }
         }
         // save objects
         foreach ($objects as $object) {
             $object->save();
         }
     }
 }
Example #2
0
 public function testYAML()
 {
     $data_dir = dirname(__FILE__) . '/data/FWYAMLTest';
     $this->assertEqual(FWYAML::load($data_dir . '/test.yml'), array('key1' => 'a', 'key2' => 'b'));
     $this->assertEqual(file_get_contents($data_dir . '/test.yml'), FWYAML::dump(array('key1' => 'a', 'key2' => 'b')));
 }