/** * Class constructor. * * The Database argument MUST be connected before passing it to the * constructor. * * @since 1.0.0 * @access public * * @param Freyja\Database\Database $database */ public function __construct(Database $database) { $this->database = $database; $this->filename = getcwd() . '/db/schema.yml'; if (file_exists($this->filename)) { // Put file content into an array. $schema = Yaml::parse(file_get_contents($this->filename)); // Check if $database exists in the array and put its schema into the // object property. $this->schema = isset($schema[$database->getName()]) ? $schema[$database->getName()] : array(); if (!isset($this->schema['tables'])) { $this->schema['tables'] = array(); } // At this point $schema has this structure: // `array('tables'=>array())` // and the internal array may or may not contain some tables. // $schema is the schema of $database, therefore it must be dumped by Yaml // in this way: `array($database->getName() => $schema)`. } }
/** * Update schema. * * Update object property and yaml configuration file as well. * * @since 1.0.0 * @access private * * @param */ private function updateSchema() { // Retrieve current schema. $schema = array(); if (file_exists($this->filename)) { $schema = Yaml::parse(file_get_contents($this->filename)); } // Replace database schema with the updated one. // Create it if not already there. $schema[$this->database->getName()] = $this->schema; // Rewrite yaml file. $yaml_string = Yaml::dump($schema); if (!file_exists(getcwd() . '/db')) { mkdir(getcwd() . '/db'); } file_put_contents($this->filename, $yaml_string); }
/** * Test for `Schema::hasTable()`. * * @since 1.0.0 * @access public * * @requires function Freyja\Database\Schema\Schema::__construct * @requires function Freyja\Database\Database::__construct * @requires function Freyja\Database\Database::connect * @requires function Freyja\Database\Driver\MySqlDriver::connect * @requires function Freyja\Database\Schema\Schema::hasTable * * @expectedException Freyja\Exceptions\InvalidArgumentException * @expectedExceptionMessage Wrong type for argument table. String or Freyja\Database\Schema\Table expected, array given instead. */ public function testHasTableWithInvalidArgument() { $db = new Database(new MySqlDriver()); $schema = new Schema($db->connect('localhost', 'test', 'travis', '')); $schema->hasTable(array()); }
/** * Test for `Database::get()`. * * @since 1.0.0 * @access public * * @requires function Freyja\Database\Database::__construct * @requires function Freyja\Database\Database::get * * @expectedException Freyja\Exceptions\RuntimeException * @expectedExceptionMessage A query must be executed before retrieving the results. */ public function testGetWithoutExecutingAnyQuery() { $driver = new MySqlDriver(); $db = new Database($driver); $db->connect('localhost', 'test', 'travis', '')->get(); }