/** * @param string $table * @param string|array $idColumn * @param string $class * @param \PDO|null $dbh */ public function __construct($table, $idColumn, $class = 'stdClass', PDO $dbh = null) { parent::__construct($class, $dbh); $this->table = $table; $this->idColumn = is_array($idColumn) ? $idColumn : array($idColumn); $this->quoteIdentifier = Rorm::getIdentifierQuoter($this->dbh); }
/** * @depends testDbDriver */ public function testQuote() { $dbh = Rorm::getDatabase(); $this->assertEquals('TRUE', Rorm::quote($dbh, true)); $this->assertEquals('FALSE', Rorm::quote($dbh, false)); $this->assertEquals('NULL', Rorm::quote($dbh, null)); $this->assertEquals(17, Rorm::quote($dbh, 17)); $this->assertEquals(28.75, Rorm::quote($dbh, 28.75)); $this->assertInternalType('integer', Rorm::quote($dbh, 10)); $this->assertInternalType('float', Rorm::quote($dbh, 10.6)); $this->assertEquals("'lorem'", Rorm::quote($dbh, 'lorem')); // todo test object with __toString }
<?php use Rorm\Rorm; /** * database connection * * we do it inside a function to prevent leaking the $dbh instance to the global namespace * if the instance is in the global namespace phpunit fails because it tries to serialize it */ $setupDatabaseMySQL = function () { $dbh = new PDO('mysql:host=localhost;dbname=rorm', 'rorm', 'secret'); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Rorm::setDatabase($dbh); // init database $dbh->exec('DROP TABLE IF EXISTS test_basic;'); $dbh->exec('DROP TABLE IF EXISTS rormtest_test_compound;'); $dbh->exec('CREATE TABLE test_basic ( id INT UNSIGNED AUTO_INCREMENT, name VARCHAR(255), email VARCHAR(255) UNIQUE, number DECIMAL(10,2), modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, active TINYINT(1) DEFAULT 0, deleted TINYINT(1) DEFAULT 0, PRIMARY KEY(id) );'); $dbh->exec('CREATE TABLE rormtest_test_compound ( foo_id INT UNSIGNED, bar_id INT UNSIGNED, name VARCHAR(255), rank INT UNSIGNED,
/** * @param string $class * @param PDO|null $dbh if null the default database connection is used */ public function __construct($class = 'stdClass', PDO $dbh = null) { $this->class = $class; $this->classIsOrmModel = is_subclass_of($this->class, '\\Rorm\\Model'); $this->dbh = $dbh ? $dbh : Rorm::getDatabase(); }
/** * @depends testModels */ public function testQuoteIdentifier() { $quoter = Rorm::getIdentifierQuoter(Rorm::getDatabase('sqlite')); $this->assertEquals('"sqlite"', $quoter('sqlite')); }
<?php use Rorm\Rorm; /** * database connection * * we do it inside a function to prevent leaking the $dbh instance to the global namespace * if the instance is in the global namespace phpunit fails because it tries to serialize it */ $setupDatabaseSQLite = function () { // create sqlite database $dbh = new PDO('sqlite::memory:'); $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Rorm::setDatabase($dbh, 'sqlite'); // setup database $dbh->exec('DROP TABLE IF EXISTS modelsqlite'); $dbh->exec('CREATE TABLE modelsqlite ( rowid INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, email TEXT UNIQUE, number REAL, active INTEGER, deleted INTEGER );'); $dbh->exec('DROP TABLE IF EXISTS modelsqlitecompound'); $dbh->exec('CREATE TABLE modelsqlitecompound ( foo_id INTEGER, bar_id INTEGER, name TEST, rank INTEGER, PRIMARY KEY(foo_id, bar_id)
/** * @return bool */ public function delete() { $dbh = static::getDatabase(); $quoteIdentifier = Rorm::getIdentifierQuoter($dbh); $idColumns = static::$_idColumn; if (!is_array($idColumns)) { $idColumns = array($idColumns); } $where = array(); foreach ($idColumns as $columnName) { $where[] = $quoteIdentifier($columnName) . ' = ' . Rorm::quote($dbh, $this->{$columnName}); } $sql = 'DELETE FROM ' . $quoteIdentifier(static::getTable()) . ' WHERE ' . implode(' AND ', $where); return $dbh->exec($sql) > 0; }
/** * @param $value * @param $expected * * @dataProvider providerQuoteIdentifier * @fixme this test requires the sqlite connection */ public function testQuoteIdentifier($value, $expected) { $quoter = Rorm::getIdentifierQuoter(Rorm::getDatabase('sqlite')); $this->assertEquals($expected, $quoter($value)); }