/** * Test auth service */ public function testAuth() { $config = array('services' => array('session' => '\\Duality\\Service\\Session\\Dummy'), 'db' => array('dsn' => DB_DSN, 'user' => DB_USER, 'pass' => DB_PASS), 'auth' => array('table' => 'dummy', 'user' => 'email', 'pass' => 'pass')); $app = new \Duality\App(dirname(__FILE__), $config); $auth = $app->call('auth'); $this->assertInstanceOf('\\Duality\\Service\\Auth\\Database', $auth); $db = $app->call('db'); $db->setName('duality'); $table = new Table($db); $table->setName('dummy'); $schema = array('id' => 'auto', 'email' => 'varchar(80)', 'pass' => 'varchar(80)'); $db->getPDO()->exec($db->getDropTable($table)); $db->getPDO()->exec($db->getCreateTable($table, $schema)); $table->setColumns($schema); $table->setPrimaryKey('id'); $table->add(1, array('email' => 'dummy', 'pass' => 'dummy')); $expected = true; $result = $auth->login('dummy', 'dummy'); $this->assertEquals($expected, $result); $table->remove(1); $expected = false; $result = $auth->login('dummy', 'dummy'); $this->assertEquals($expected, $result); $db->getPDO()->exec($db->getDropTable($table)); $this->assertEquals(true, $auth->isLogged()); $this->assertEquals('dummy', $auth->whoAmI()); $auth->logout(); $this->assertEquals(false, $auth->isLogged()); $this->assertEquals(null, $auth->whoAmI()); $this->assertNull($auth->terminate()); }
/** * Test MySql database methods * * @requires extension pdo */ public function testMethods() { $config = array('db' => array('dsn' => DB_DSN, 'user' => DB_USER, 'pass' => DB_PASS)); $app = $this->getMockBuilder('\\Duality\\App')->setConstructorArgs(array(dirname(__FILE__), $config))->getMock(); $db = new MySql($app); $table = new Table($db); $table->setName('dummy'); $table->setColumns(array('dummy' => 'integer')); $property = new Property('dummy'); $expected = 'SELECT * FROM dummy;'; $result = $db->getSelect('*', (string) $table); $this->assertEquals($expected, $result); $expected = 'SELECT * FROM dummy WHERE dummy = ? LIMIT 10 OFFSET 0;'; $result = $db->getSelect('*', (string) $table, 'dummy = ?', '', 10, 0); $this->assertEquals($expected, $result); $expected = 'CREATE TABLE IF NOT EXISTS dummy (id INTEGER NOT NULL AUTO_INCREMENT PRIMARY KEY, dummy integer);'; $result = $db->getCreateTable($table, array('id' => 'auto', 'dummy' => 'integer')); $this->assertEquals($expected, $result); $expected = 'DROP TABLE IF EXISTS dummy;'; $result = $db->getDropTable($table); $this->assertEquals($expected, $result); $expected = 'ALTER TABLE dummy ADD COLUMN dummy integer;'; $result = $db->getAddColumn($table, $property, 'integer'); $this->assertEquals($expected, $result); $expected = 'ALTER TABLE dummy DROP COLUMN dummy;'; $result = $db->getDropColumn($table, $property); $this->assertEquals($expected, $result); $expected = 'ALTER TABLE dummy MODIFY COLUMN dummy integer;'; $result = $db->getModifyColumn($table, $property, 'integer'); $this->assertEquals($expected, $result); $expected = 'INSERT INTO dummy (dummy) VALUES (?);'; $result = $db->getInsert($table, array('dummy' => 'value')); $this->assertEquals($expected, $result); $expected = 'UPDATE dummy SET dummy = ?;'; $result = $db->getUpdate($table, array('dummy' => 'value')); $this->assertEquals($expected, $result); $expected = 'DELETE FROM dummy WHERE dummy = ?;'; $result = $db->getDelete($table, array('dummy' => 'value')); $this->assertEquals($expected, $result); $expected = "SELECT COLUMN_NAME, "; $expected .= "COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS "; $expected .= "WHERE TABLE_SCHEMA = '' "; $expected .= "AND TABLE_NAME = 'dummy';"; $result = $db->getColumns($table, $property); $this->assertEquals($expected, $result); $expected = 'TRUNCATE dummy;'; $result = $db->getTruncate($table); $this->assertEquals($expected, $result); $db->terminate(); }
/** * Creates a new table for this database * * @param string $name The table name * * @return \Duality\Core\Table The resulting table */ public function makeTable($name) { $table = new Table($this); $table->setName($name); return $table; }
/** * Returns an UPDATE statement * * @param \Duality\Structure\Table $table The database table * @param string $item The item as array * * @return string Returns the SQL statement */ public function getUpdate(Table $table, $item = array()) { $sql = "UPDATE " . strtolower((string) $table) . " SET "; $values = array(); foreach ($item as $field => $value) { $values[] = $this->parseValue($value); $sql .= $field . " = ?,"; } $sql = rtrim($sql, ','); $sql .= " WHERE " . $table->getPrimaryKey() . ' = ?'; $sql .= ";"; return $sql; }
/** * Test database table * * @requires extension pdo */ public function testTable() { $config = array('db' => array('dsn' => DB_DSN, 'user' => DB_USER, 'pass' => DB_PASS)); $app = new \Duality\App(dirname(__FILE__), $config); $db = $app->call('db'); $db->setName('duality'); $table = new Table($db); $table->setName('dummy'); $schema = array('id' => 'auto', 'email' => 'varchar(80)'); $sql = $db->getDropTable($table); $db->getPDO()->exec($sql); $sql = $db->getCreateTable($table, $schema); $db->getPDO()->exec($sql); $expected = array(); $table->setColumns($schema); $result = $table->toArray(); $this->assertEquals($expected, $result); $table->setPrimaryKey('id'); $expected = array(1 => array('id' => 1, 'email' => 'dummy1')); $table->add(1, array('email' => 'dummy1')); $result = $table->find()->toArray(); $this->assertEquals($expected, $result); $expected = array(1 => array('id' => 1, 'email' => 'dummy2')); $table->set(1, array('id' => 1, 'email' => 'dummy2')); $result = $table->find()->toArray(); $this->assertEquals($expected, $result); $expected = array(1 => array('id' => 1, 'email' => 'dummy2')); $table->find(0, 10, 'id = ?', array(1)); $result = $table->toArray(); $this->assertEquals($expected, $result); $expected = array(1 => array('id' => 1, 'email' => 'dummy2')); $filter = new Filter($table); $filter->columns('id,email')->where('id = ?', array(1))->group('id')->limit(0, 10); $table->filter($filter); $result = $table->toArray(); $this->assertEquals($expected, $result); $expected = array('id' => 1, 'email' => 'dummy2'); $result = $table->get(1); $this->assertEquals($expected, $result); $result = $table->has(1); $this->assertTrue($result); $expected = array(1 => array('id' => 1, 'email' => 'dummy2')); $result = $table->find()->toArray(); $this->assertEquals($expected, $result); $expected = array(1 => array('id' => 1, 'email' => 'dummy3'), 2 => array('id' => 2, 'email' => 'dummy4')); $data = array(1 => array('email' => 'dummy3'), 2 => array('email' => 'dummy4')); $table->importArray($data); $result = $table->find()->toArray(); $this->assertEquals($expected, $result); $expected = $expected = array(2 => array('id' => 2, 'email' => 'dummy4')); $table->remove(1); $result = $table->find()->toArray(); $this->assertEquals($expected, $result); $expected = array(); $table->reset(); $result = $table->find()->toArray(); $this->assertEquals($expected, $result); $result = $db->getTable('dummy'); $this->assertInstanceOf('\\Duality\\Structure\\Database\\Table', $result); $sql = $db->getDropTable($table); $db->getPDO()->exec($sql); $result = $db->getTable('dummy'); $this->assertFalse($result); // Clean up $this->cleanUp(); }