public function setUp() { $this->connection = DB::getConnection('testdb'); $this->driver = $this->connection->getDriver(); // Clean up events before every test $this->triggeredEvents = array(); $this->triggeredArguments = array(); $that = $this; foreach ($this->queryEvents as $event) { Event::removeListeners($event); Event::on($event, function () use($event, $that) { $that->triggeredEvents[] = $event; $that->triggeredArguments[] = func_get_args(); }); } }
/** * Prepares, then executes a statement and returns the number of affected * rows. * * The method is useful for updates or deletes, which do * not return anything. * * @param string $query The SQL query to execute. * @param array $arguments The arguments used to substitute params. * @return integer Number of rows affected by the query. */ public function preparedExecute($query, $arguments = array()) { DB::getConnection($this->name); // Handles transactions Event::emit(Event::QUERY_STARTED, array($query, $arguments, $this)); $stmt = $this->pdoPrepare($query, $arguments); $this->pdoExecute($query, $arguments, $stmt); Event::emit(Event::QUERY_COMPLETED, array($query, $arguments, $this, null)); return $stmt->rowCount(); }
/** Performs a prepared query and returns only a single column. */ private function singleColumnQuery($query, $args, $column) { $conn = DB::getConnection($this->meta->database); $pdo = $conn->getPDO(); $stmt = $pdo->prepare($query); $stmt->execute($args); $data = array(); while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $data[] = $row[$column]; } return $data; }
/** * @expectedException PHPUnit_Framework_Error_Warning * @expectedExceptionMessage Attribute PDO::ATTR_ERRMODE is set to something other than PDO::ERRMODE_EXCEPTION for database "db1". This is not allowed because Phormium depends on this setting. Skipping attribute definition. */ public function testAttributesCannotChangeError() { DB::configure(["databases" => ["db1" => ["dsn" => "sqlite:tmp/test.db", "username" => "myuser", "password" => "mypass", "attributes" => [PDO::ATTR_ERRMODE => PDO::ERRMODE_SILENT]]]]); // Test the warning is emitted DB::getConnection("db1"); }
public function testPreparedExecuteTransaction() { $person = new Person(); $person->name = 'Janick Gers'; $person->income = 100; $person->insert(); $id = $person->id; $conn = DB::getConnection('testdb'); DB::begin(); $conn->preparedExecute("UPDATE person SET income = ?", array(200)); DB::rollback(); $this->assertEquals(100, Person::get($id)->income); DB::begin(); $conn->preparedExecute("UPDATE person SET income = ?", array(200)); DB::commit(); $this->assertEquals(200, Person::get($id)->income); }