/** * Elke test_* met een schone database beginnen. * * @param Connection $db */ public function fillDatabase($db) { $db->import(dirname(__FILE__) . '/rebuild_test_database.' . $db->getAttribute(PDO::ATTR_DRIVER_NAME) . '.sql', $error); $repo = new Repository(); $backend = new DatabaseRepositoryBackend(array($this->dbLink)); $repo->registerBackend($backend); Repository::$instances[__CLASS__] = $repo; }
public function test_count() { $db = Connection::instance($this->dbLink); $result = $db->query('SELECT * FROM ducks'); $this->assertInstanceOf(Statement::class, $result); $this->assertSame(count($result), 3); //, 'count() should return the number of rows found'); }
public static function statusbar() { if (defined('SAVEQUERIES') && SAVEQUERIES) { $logger = new Logger(['identifier' => 'wpdb', 'renderer' => [Connection::instance(), 'renderLog'], 'plural' => 'queries', 'singular' => 'query', 'columns' => ['SQL', 'Duration']]); foreach ($GLOBALS['wpdb']->queries as $item) { $logger->append($item[0], ['duration' => $item[1]]); } } render(new Template('sledgehammer/mvc/templates/statusbar.php')); }
/** * Setup the database environment. */ public function setUp() { $db = Connection::instance($this->dbLink); //dump(iterator_to_array($db->query('SHOW DATABASES', null, 'Database'))); if ($this->skipRebuildDatabase == false && $this->dbName) { switch ($db->getAttribute(PDO::ATTR_DRIVER_NAME)) { case 'mysql': $reportWarnings = $db->reportWarnings; $db->reportWarnings = false; $db->query('DROP DATABASE ' . $this->dbName); $db->query('CREATE DATABASE ' . $this->dbName); $db->query('USE ' . $this->dbName); $db->reportWarnings = $reportWarnings; break; case 'sqlite': Connection::$instances[$this->dbLink] = 'CLEAR'; $newDb = new Connection('sqlite::memory:', null, null, array('logIdentifier' => substr($this->dbLink, 9))); foreach ($db as $property => $value) { $newDb->{$property} = $value; } $db = $newDb; Connection::$instances[$this->dbLink] = $newDb; break; } $this->fillDatabase($db); } $this->queryCount = count($db->logger->entries); }
public function test_open_delete_update() { // Remove the refernces Connection::instance($this->dbLink)->query('DELETE FROM orders WHERE customer_id = 1'); Connection::instance($this->dbLink)->query('DELETE FROM memberships WHERE customer_id = 1'); Connection::instance($this->dbLink)->query('DELETE FROM ratings WHERE customer_id = 1'); $record = $this->getCustomer(1); $record->delete(); $this->assertLastQuery('DELETE FROM customers WHERE id = 1'); try { $record->occupation = 'DELETED?'; } catch (\PHPUnit_Framework_Error_Notice $e) { $this->assertEquals($e->getMessage(), 'A deleted Record has no properties'); } try { $record->save(); $this->fail('Expecting an exception'); } catch (Exception $e) { $this->assertEquals($e->getMessage(), SimpleRecord::class . '->save() not allowed on deleted objects'); } $this->assertTableContents('customers', array(array('id' => '2', 'name' => 'James Bond', 'occupation' => 'Spion'))); }
/** * @param array $keys Array containing the primay keys * @param array $config * * @return string */ private function generateWhere($keys, $config, $prepare = false) { $db = Connection::instance($config['dbLink']); $where = []; foreach ($config['primaryKeys'] as $column) { if (isset($keys[$column]) == false) { throw new Exception('Missing key: "' . $column . '"'); // @todo better error } if ($prepare) { $where[] = $db->quoteIdentifier($column) . ' = ?'; } else { $where[] = $db->quoteIdentifier($column) . ' = ' . $this->quote($db, $column, $keys[$column]); } } if (count($where) == 0) { throw new Exception('Invalid config, no "primaryKeys" defined'); } return implode(' AND ', $where); }
public function test_detect_id_truncation() { $repo = new RepositoryTester(); $repo->registerBackend(new DatabaseRepositoryBackend($this->dbLink)); $customer1 = $repo->getCustomer(1); if (Connection::instance($this->dbLink)->getAttribute(PDO::ATTR_DRIVER_NAME) === 'sqlite') { $this->markTestSkipped('SQLite doesn\'t truncate values'); } $this->setExpectedException('Exception', 'The $id parameter doesn\'t match the retrieved data. {1s} != {1}'); $customer1s = $repo->getCustomer('1s'); }
public function test_escaped_where() { $collection = $this->getDatabaseCollection(); $emptyCollection = $collection->where(array('name' => "'")); $this->assertSame(count($emptyCollection->toArray()), 0); if (Connection::instance($this->dbLink)->getAttribute(PDO::ATTR_DRIVER_NAME) === 'sqlite') { $this->assertLastQuery("SELECT * FROM fruits WHERE name = ''''"); } else { $this->assertLastQuery("SELECT * FROM fruits WHERE name = '\\''"); } $this->assertSame((string) $collection->getQuery(), 'SELECT * FROM fruits', 'Collection->where() does not modify the orginal collection'); }
/** * Convert a path to a (escaped) columnname. * * @param string $path */ private function convertPathToColumn($path) { $compiled = PropertyPath::parse($path); if (count($compiled) > 1) { return false; } if (in_array($compiled[0][0], array(PropertyPath::TYPE_ANY, PropertyPath::TYPE_ELEMENT))) { $db = Connection::instance($this->dbLink); return $db->quoteIdentifier($compiled[0][1]); } return false; }