Ejemplo n.º 1
0
 public function setUp()
 {
     $annnotations = $this->getAnnotations();
     $configLoader = ConfigLoader::getInstance();
     $configLoader->loadFromSymbol(true);
     $configLoader->setDefaultDataSourceId($this->getDriverType());
     $connManager = ConnectionManager::getInstance();
     $connManager->init($configLoader);
     try {
         $dbh = $connManager->getConnection($this->getDriverType());
     } catch (PDOException $e) {
         if ($this->allowConnectionFailure) {
             $this->markTestSkipped(sprintf("Can not connect to database by data source '%s' message:'%s' config:'%s'", $this->getDriverType(), $e->getMessage(), var_export($configLoader->getDataSource($this->getDriverType()), true)));
             return;
         } else {
             echo sprintf("Can not connect to database by data source '%s' message:'%s' config:'%s'", $this->getDriverType(), $e->getMessage(), var_export($configLoader->getDataSource($this->getDriverType()), true));
             throw $e;
         }
     }
     $driver = $connManager->getQueryDriver($this->getDriverType());
     $this->assertInstanceOf('SQLBuilder\\Driver\\BaseDriver', $driver, 'QueryDriver object OK');
     // Rebuild means rebuild the database for new tests
     $rebuild = true;
     $basedata = true;
     if (isset($annnotations['method']['rebuild'][0]) && $annnotations['method']['rebuild'][0] == 'false') {
         $rebuild = false;
     }
     if (isset($annnotations['method']['basedata'][0]) && $annnotations['method']['basedata'][0] == 'false') {
         $basedata = false;
     }
     if ($rebuild) {
         $builder = SqlBuilder::create($driver, array('rebuild' => true));
         $this->assertNotNull($builder);
         // $schemas = ClassUtils::schema_classes_to_objects($this->getModels());
         $schemas = ClassUtils::schema_classes_to_objects($this->getModels());
         foreach ($schemas as $schema) {
             $sqls = $builder->build($schema);
             $this->assertNotEmpty($sqls);
             foreach ($sqls as $sql) {
                 $dbh->query($sql);
             }
         }
         if ($basedata) {
             $runner = new SeedBuilder($this->config, $this->logger);
             foreach ($schemas as $schema) {
                 $runner->buildSchemaSeeds($schema);
             }
             if ($scripts = $this->config->getSeedScripts()) {
                 foreach ($scripts as $script) {
                     $runner->buildScriptSeed($script);
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 public function setUp()
 {
     $annnotations = $this->getAnnotations();
     $connManager = ConnectionManager::getInstance();
     $dataSourceConfig = self::createDataSourceConfig($this->driver);
     if ($dataSourceConfig) {
         $connManager->addDataSource('default', $dataSourceConfig);
     } else {
         $this->markTestSkipped("{$this->driver} database configuration is missing.");
     }
     try {
         $dbh = $connManager->getConnection('default');
     } catch (PDOException $e) {
         $this->markTestSkipped('Can not connect to database, test skipped: ' . $e->getMessage());
         return;
     }
     $driver = ConnectionManager::getInstance()->getQueryDriver('default');
     ok($driver, 'QueryDriver object OK');
     // Rebuild means rebuild the database for new tests
     $rebuild = true;
     $basedata = true;
     if (isset($annnotations['method']['rebuild'][0]) && $annnotations['method']['rebuild'][0] == 'false') {
         $rebuild = false;
     }
     if (isset($annnotations['method']['basedata'][0]) && $annnotations['method']['basedata'][0] == 'false') {
         $basedata = false;
     }
     if ($rebuild) {
         $builder = SqlBuilder::create($driver, array('rebuild' => true));
         $this->assertNotNull($builder);
         // $schemas = ClassUtils::schema_classes_to_objects($this->getModels());
         $schemas = ClassUtils::schema_classes_to_objects($this->getModels());
         foreach ($schemas as $schema) {
             $sqls = $builder->build($schema);
             $this->assertNotEmpty($sqls);
             foreach ($sqls as $sql) {
                 $dbh->query($sql);
             }
         }
         if ($basedata) {
             $runner = new SeedBuilder($this->config, $this->logger);
             foreach ($schemas as $schema) {
                 $runner->buildSchemaSeeds($schema);
             }
             if ($scripts = $this->config->getSeedScripts()) {
                 foreach ($scripts as $script) {
                     $runner->buildScriptSeed($script);
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
 /**
  * Returns schema objects.
  *
  * @return array schema objects
  */
 public static function findSchemasByArguments(ConfigLoader $loader, $args, $logger = null)
 {
     if (count($args) && !file_exists($args[0])) {
         $classes = array();
         // it's classnames
         foreach ($args as $class) {
             // call class loader to load
             if (class_exists($class, true)) {
                 $classes[] = $class;
             } else {
                 if ($logger) {
                     $logger->warn("{$class} not found.");
                 } else {
                     echo ">>> {$class} not found.\n";
                 }
             }
         }
         return ClassUtils::schema_classes_to_objects($classes);
     } else {
         $finder = new SchemaFinder();
         if (count($args) && file_exists($args[0])) {
             $finder->paths = $args;
             foreach ($args as $file) {
                 if (is_file($file)) {
                     require_once $file;
                 }
             }
         } elseif ($paths = $loader->getSchemaPaths()) {
             $finder->setPaths($paths);
         }
         $finder->find();
         // load class from class map
         if ($classMap = $loader->getClassMap()) {
             foreach ($classMap as $file => $class) {
                 if (!is_numeric($file) && is_string($file)) {
                     require $file;
                 }
             }
         }
         return SchemaLoader::loadDeclaredSchemas();
     }
 }
Ejemplo n.º 4
0
 public function setUp()
 {
     if ($this->onlyDriver !== null && $this->getDataSource() != $this->onlyDriver) {
         return $this->markTestSkipped("{$this->onlyDriver} only");
     }
     $this->prepareConnection();
     // Ensure that we use the correct default data source ID
     $this->assertEquals($this->getDataSource(), $this->config->getDefaultDataSourceId());
     $this->assertInstanceOf('SQLBuilder\\Driver\\BaseDriver', $this->queryDriver, 'QueryDriver object OK');
     // Rebuild means rebuild the database for new tests
     $annnotations = $this->getAnnotations();
     $rebuild = true;
     $basedata = true;
     if (isset($annnotations['method']['rebuild'][0]) && $annnotations['method']['rebuild'][0] == 'false') {
         $rebuild = false;
     }
     if (isset($annnotations['method']['basedata'][0]) && $annnotations['method']['basedata'][0] == 'false') {
         $basedata = false;
     }
     $schemas = ClassUtils::schema_classes_to_objects($this->getModels());
     if (false === $this->schemaHasBeenBuilt) {
         $g = new SchemaGenerator($this->config);
         $g->setForceUpdate(true);
         $g->generate($schemas);
         $this->schemaHasBeenBuilt = true;
     }
     if ($rebuild === false) {
         $tableParser = TableParser::create($this->conn, $this->queryDriver, $this->config);
         $tables = $tableParser->getTables();
         $schemas = array_filter($schemas, function ($schema) use($tables) {
             return !in_array($schema->getTable(), $tables);
         });
     }
     $this->sqlBuilder = SqlBuilder::create($this->queryDriver, array('rebuild' => $rebuild));
     $this->bootstrap = new Bootstrap($this->conn, $this->sqlBuilder, $this->logger);
     $this->bootstrap->build($schemas);
     if ($rebuild && $basedata) {
         $seeder = new SeedBuilder($this->logger);
         $seeder->build(new SchemaCollection($schemas));
         $seeder->buildConfigSeeds($this->config);
     }
 }
Ejemplo n.º 5
0
 /**
  * Returns schema objects
  *
  * @return array Schema objects
  */
 public function getSchemas()
 {
     return SchemaUtils::expandSchemaClasses(ClassUtils::get_declared_schema_classes());
 }
Ejemplo n.º 6
0
 /**
  * Convert current model name to a class name.
  *
  * @return string table name
  */
 protected function _classnameToTable()
 {
     return ClassUtils::convertClassToTableName($this->getModelName());
 }
Ejemplo n.º 7
0
 public function __construct($name = NULL, array $data = array(), $dataName = '')
 {
     parent::__construct($name, $data, $dataName);
     if (!extension_loaded('pdo')) {
         $this->markTestSkipped('pdo extension is required for model testing');
         return;
     }
     // free and override default connection
     ConnectionManager::getInstance()->free();
     $config = self::createNeutralConfigLoader();
     $this->setConfig($config);
     $this->logger = new Logger();
     $this->logger->setQuiet();
     if (method_exists($this, 'getModels')) {
         $generator = new SchemaGenerator($this->config, $this->logger);
         $schemas = ClassUtils::schema_classes_to_objects($this->getModels());
         $classMap = $generator->generate($schemas);
     }
 }
Ejemplo n.º 8
0
 /**
  * Returns schema objects.
  *
  * @return array schema objects
  */
 public static function findSchemasByArguments(ConfigLoader $loader, array $args, Logger $logger = null)
 {
     $classes = array_filter($args, function ($class) {
         return class_exists($class, true);
     });
     if (!empty($classes)) {
         return ClassUtils::schema_classes_to_objects(array_unique($classes));
     }
     $paths = array_filter($args, 'file_exists');
     if (empty($paths)) {
         $paths = $loader->getSchemaPaths();
     }
     if (!empty($paths)) {
         $finder = new SchemaFinder($paths);
         $finder->find();
     }
     // load class from class map
     if ($classMap = $loader->getClassMap()) {
         foreach ($classMap as $file => $class) {
             if (is_numeric($file)) {
                 continue;
             }
             require_once $file;
         }
     }
     return SchemaLoader::loadDeclaredSchemas();
 }