/** * Cria o objeto DAO * * @param \MyFrameWork\DataBase\DataBase $db */ public function __construct($db = null) { if ($db) { $this->db = $db; } else { $this->db = Factory::database(); } $this->setParams(); }
protected function createDatabaseItems($folder) { $db = Factory::database(); foreach (glob($folder . '*.sql') as $sqlfile) { echo "<li>{$sqlfile}</li>"; $items = explode(';', file_get_contents($sqlfile)); foreach ($items as $object) { //echo $object . '<br>'; if (!$db->execute($object)) { //echo "<pre>" . $object . "</pre><hr>"; } } } }
public function testDatabase() { LoggerApp::clear(); $this->assertFalse(LoggerApp::hasError(), LoggerApp::getLastError()); $defaultDb = Factory::database(); $this->assertNotNull($defaultDb); $this->assertFalse(LoggerApp::hasError(), LoggerApp::getLastError()); $types = array('pgsql' => 'MyFrameWork\\DataBase\\PgDataBase', 'mysql' => 'MyFrameWork\\DataBase\\MyDataBase'); $this->assertTrue(is_a($defaultDb, $types[DATABASE_DRIVER]), 'Invalid class'); $this->assertEquals($defaultDb, Factory::database()); $this->assertSame($defaultDb, Factory::database()); $this->assertNull(Factory::database(array('dbname' => 'testxyz'))); $this->assertTrue(LoggerApp::hasError(), 'Conexão inválida não gerou um erro'); $this->assertNull(Factory::database(array('driver' => 'testxyz'))); $this->assertNull(Factory::database(array('driver' => 'testxyz', 'dbname' => 'test'))); $this->assertNull(Factory::database(array('driver' => 'testxyz', 'dbname' => 'test', 'user' => 'test'))); foreach ($types as $driver => $database) { $db = Factory::database(array('driver' => $driver, 'dbname' => 'xyz', 'user' => 'xyz')); $this->assertNull($db); } }
protected function setUp() { LoggerApp::clear(); $this->db = Factory::database(); $this->assertFalse(LoggerApp::hasError(), LoggerApp::getLastError()); $this->assertNotNull($this->db, 'Invalid connection'); $sql = <<<'SQL' CREATE TABLE IF NOT EXISTS test ( id serial PRIMARY KEY, name varchar(40) NOT NULL CHECK (name <> '') ) SQL; try { $this->db->exec($sql); $this->db->exec('DELETE FROM test'); $this->assertTrue($this->db->exec("INSERT INTO test (name) VALUES ('value 1')") > 0); $this->assertTrue($this->db->exec("INSERT INTO test (name) VALUES ('value 2')") > 0); $this->assertTrue($this->db->exec("INSERT INTO test (name) VALUES ('value 3')") > 0); } catch (Exception $e) { $this->fail($e->getMessage()); } }
public function testParams() { $this->assertEquals('test', $this->dao->getTableName()); $this->assertEquals('id', $this->dao->getPKFieldName()); $this->dao->changeToTable2(); $this->assertEquals('test2', $this->dao->getTableName()); $this->assertEquals(array('id1', 'id2'), $this->dao->getPKFieldName()); $this->assertEquals(Factory::database(), $this->dao->getDatabase()); //Invalid database object - Dont do this $dao = new MyDAO(Factory::log()); $this->assertEquals(Factory::log(), $dao->getDatabase()); }