Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test for exception #46

Merged
merged 1 commit into from
May 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Query implements QueryInterface
/**
* @var string
*/
protected string $table;
protected string $table = "";

/**
* @var array
Expand Down Expand Up @@ -185,7 +185,7 @@ public function getQuery(): string
$having = $this->connection->getSqlCondition($this->having);

if (!$this->table) {
throw new DatabaseException("Undefined from fro query");
throw new DatabaseException("Table Not Found From Query");
}

return $driver->createSelectQuery(
Expand Down
45 changes: 45 additions & 0 deletions tests/Src/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Jtrw\DAO\Tests\Src;

use Jtrw\DAO\DataAccessObjectInterface;
use Jtrw\DAO\Exceptions\DatabaseException;
use Jtrw\DAO\Query\Query;
use Jtrw\DAO\Tests\DbConnector;
use PHPUnit\Framework\TestCase;
Expand All @@ -14,12 +15,18 @@ class QueryTest extends TestCase

private DataAccessObjectInterface $db;

/**
* @return void
*/
public function setUp(): void
{
$this->db = DbConnector::getInstance();
parent::setUp(); // TODO: Change the autogenerated stub
}

/**
* @return void
*/
public function testFetch()
{
$query = new Query($this->db);
Expand All @@ -39,6 +46,9 @@ public function testFetch()
Assert::assertEquals($result['caption'], $values['caption']);
}

/**
* @return void
*/
public function testFetchAll()
{
$query = new Query($this->db);
Expand All @@ -61,6 +71,9 @@ public function testFetchAll()
Assert::assertEquals($result[1]['id'], $values[0]['id']);
}

/**
* @return void
*/
public function testLimitFetchAlll()
{
$query = new Query($this->db);
Expand All @@ -84,6 +97,9 @@ public function testLimitFetchAlll()
Assert::assertEquals($result[0]['caption'], $values['caption']);
}

/**
* @return void
*/
public function testJoin()
{
$query = new Query($this->db);
Expand Down Expand Up @@ -113,6 +129,9 @@ public function testJoin()
Assert::assertEquals($result[0]['caption'], $values['caption']);
}

/**
* @return void
*/
public function testJoins()
{
$query = new Query($this->db);
Expand Down Expand Up @@ -142,6 +161,28 @@ public function testJoins()
Assert::assertEquals($result[0]['caption'], $values['caption']);
}

/**
* @return void
*/
public function testTableNotFound()
{
$query = new Query($this->db);
try {
$query->select("caption")->fetch();
$this->fail('DatabaseException was not thrown');
} catch (DatabaseException $exp) {
$this->assertSame(
[
'Table Not Found From Query',
],
[$exp->getMessage()]
);
}
}

/**
* @return array|array[]
*/
private function createItems(): array
{
$values = [
Expand All @@ -163,6 +204,10 @@ private function createItems(): array
return $values;
}

/**
* @param array $values
* @return int
*/
private function createItem(array $values): int
{
$idSetting = $this->db->insert(static::TABLE_SETTINGS, $values);
Expand Down