public function find($id)
 {
     $model = ReflectionUtils::createNewInstance(static::$forModel);
     $query = Query::getInstance()->select()->from($model->table())->where(Restrictions::eq("id", ":id"));
     $resultSet = $this->getDatabase()->query($query)->addParameter($id)->uniqueResult();
     return ObjectUtils::toObject($resultSet);
 }
 public function testShouldProduceSelectStatementWithWhere()
 {
     $table = "test";
     $sql = Query::getInstance()->select()->from($table)->where(Restrictions::between("id", 1, 100))->build();
     $expectedSql = "SELECT * FROM {$table} WHERE (id BETWEEN 1 AND 100)";
     $this->assertThat($sql, $this->equalTo($expectedSql));
 }
 public function testShouldCreateSqlDeleteStatementWithParameters()
 {
     $table = "test";
     $delete = new DeleteStatement($table);
     $delete->where(Restrictions::eq("id", ":id"));
     $delete->addParameter(":id", 1001);
     $sql = $delete->toSql();
     $this->assertThat($sql, $this->equalTo("DELETE FROM test WHERE (id = :id)"));
     $this->assertThat($delete->hasParameters(), $this->isTrue());
 }
 public function testShouldExecuteSqlQuery()
 {
     $table = "tests";
     $content = "Test Content 1001";
     $statement = new InsertStatement($table);
     $statement->addParameter("content", $content);
     $database = new Database(self::$phactory->getConnection());
     $lastInsertId = $database->execute($statement);
     $this->assertThat($lastInsertId, $this->logicalNot($this->isNull()));
     $query = Query::getInstance();
     $query->select()->from($table)->where(Restrictions::eq("id", $lastInsertId));
     $test = $database->query($query)->uniqueResult();
     $this->assertThat($test['content'], $this->equalTo($content));
 }
 public function testShouldCreateSqlUpdateStatementWithParameters()
 {
     $table = "test";
     $value = StringUtils::generateRandomAlphaNumeric(10);
     $update = new UpdateStatement($table);
     $update->addParameter("content", $value);
     $update->where(Restrictions::eq("id", ':id'));
     $update->addParameter(":id", 1001, true);
     $sql = $update->toSql();
     $this->assertThat(trim($sql), $this->equalTo("UPDATE {$table} SET content=:content WHERE (id = :id)"));
     $this->assertThat($update->hasParameters(), $this->isTrue());
     $parameters = $update->getParameters();
     $this->assertThat(array_shift($parameters), $this->equalTo($value));
     $this->assertThat(array_pop($parameters), $this->equalTo(1001));
 }