public function testDriver() { TestUtils::clearDatabase(); $driver = Database::driver(); $this->assertInstanceOf("\\Arvici\\Heart\\Database\\Driver\\MySQL\\Driver", $driver); $this->assertInstanceOf("\\Arvici\\Heart\\Database\\Driver", $driver); }
public function testInit() { TestUtils::clearDatabase(); $conn = Database::connection(); $query = $conn->build(); $this->assertInstanceOf(QueryBuilder::class, $query); }
public function testTypeDetection() { $null = null; $int = 0; $bool = true; $string = 'string'; $this->assertEquals(Database::TYPE_NULL, Database::typeOfValue($null)); $this->assertEquals(Database::TYPE_INT, Database::typeOfValue($int)); $this->assertEquals(Database::TYPE_BOOL, Database::typeOfValue($bool)); $this->assertEquals(Database::TYPE_STR, Database::typeOfValue($string)); }
public static function resetDatabase($connection = 'default') { self::clearDatabase(); /** @var Connection $connection */ $connection = Database::connection($connection); $connection->truncate('posts'); // Add test posts $posts = [['id' => 1, 'title' => 'First Post', 'author' => 1, 'content' => 'Content', 'publishdate' => null], ['id' => 2, 'title' => 'Second Post', 'author' => 1, 'content' => 'Content', 'publishdate' => null], ['id' => 3, 'title' => 'Hello Post', 'author' => 1, 'content' => 'Content', 'publishdate' => null], ['id' => 4, 'title' => 'Mister Post', 'author' => 1, 'content' => 'Content', 'publishdate' => null], ['id' => 5, 'title' => 'Test Post', 'author' => 1, 'content' => 'Content', 'publishdate' => null], ['id' => 6, 'title' => 'Is Post', 'author' => 1, 'content' => 'Content', 'publishdate' => null], ['id' => 7, 'title' => 'Here Post', 'author' => 1, 'content' => 'Content', 'publishdate' => date('c', time() - 5000)]]; foreach ($posts as $post) { $connection->insert('posts', $post); } self::clearDatabase(); }
public function testFetchClass() { $connection = Database::connection(); $all = $connection->select("SELECT * FROM posts", array(), Database::FETCH_CLASS, FetchTestPosts::class); foreach ($all as $post) { $this->assertInstanceOf(FetchTestPosts::class, $post); $this->assertNotNull($post->id); } $all = $connection->raw("SELECT * FROM posts", true, Database::FETCH_CLASS, FetchTestPosts::class); foreach ($all as $post) { $this->assertInstanceOf(FetchTestPosts::class, $post); $this->assertNotNull($post->id); } }
/** * Get driver instance for this connection. * * @return \Arvici\Heart\Database\Driver */ public function getDriver() { return Database::driver(null, 'MySQL'); }
/** * Execute query. Warning! No escaping! * * @param string $query * @param bool $return Return something back? (Fetch) * @param int $fetchMode Fetch mode when returning something. * @param string $fetchClass Class to fetch * * @return mixed * * @throws \Exception */ public function raw($query, $return = false, $fetchMode = null, $fetchClass = null) { if (!$return) { return $this->exec($query); } $fetchMode = Database::normalizeFetchType($fetchMode); $statement = $this->query($query); if ($fetchMode === Database::FETCH_CLASS) { $statement->setFetchMode($fetchMode, $fetchClass); } else { $statement->setFetchMode($fetchMode); } return $statement->fetchAll(); }
/** * Get connection. * * @param string $connectionName * * @return Connection * * @codeCoverageIgnore */ protected function database($connectionName = 'default') { return Database::connection($connectionName); }
/** * Execute the query, fetch if provided details to do so. * * @param bool $fetch Fetch on or off? * @param null $fetchMode Fetch mode (null for default) * @param null $fetchClass Fetch class (null for none) * * @throws QueryBuilderParseException * @throws QueryBuilderException * @throws QueryException * @throws DatabaseException * * @throws \Exception * * @return bool Boolean when fetching is off. * @return array|object|null Result when fetching is on. */ private function doExecute($fetch = false, $fetchMode = null, $fetchClass = null) { if ($fetch) { $fetchMode = Database::normalizeFetchType($fetchMode); } // Parse SQL and Bind $sql = $this->query->getSQL(); $bind = $this->query->getBind(); // Connection and execute $statement = $this->connection->prepare($sql); foreach ($bind as $idx => $value) { $statement->bindValue($idx + 1, $value, Database::typeOfValue($value)); } // Fetch if ($fetch) { if ($fetchMode === Database::FETCH_CLASS) { // Verify class new \ReflectionClass($fetchClass); $statement->setFetchMode($fetchMode, $fetchClass); } else { $statement->setFetchMode($fetchMode); } } // Execute $success = $statement->execute(); if (!$success) { return false; } if ($fetch && $fetch === 'single') { return $statement->fetch(); } if ($fetch) { return $statement->fetchAll(); } return true; }