Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c263ae3

Browse files
created QueryBuilder test, created Query builder to write common funcatonality for PDO and MySQLi
1 parent ac2921a commit c263ae3

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
lines changed

‎Test/Unit/QueryBuilderTest.php‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
namespace Test\Unit;
3+
4+
use PHPUnit\Framework\TestCase;
5+
6+
class QueryBuilderTest extends TestCase
7+
{
8+
private $queryBuilder;
9+
10+
protected function setUp()
11+
{
12+
$this->queryBuilder = new QueryBuilder();
13+
parent::setUp();
14+
}
15+
16+
protected function tearDown()
17+
{
18+
unset($this->queryBuilder);
19+
}
20+
21+
public function testItCanCreateRecords()
22+
{
23+
$id = $this->queryBuilder->table('reports')->create($data);
24+
self::assertNotNull($id);
25+
}
26+
27+
public function testItCanPerformRawQuery()
28+
{
29+
$result = $this->queryBuilder->raw('SELECT * FROM reports');
30+
self::assertNotNull($result);
31+
}
32+
33+
public function testItCanPerformSelectQuery()
34+
{
35+
$result = $this->queryBuilder->table('reports')->select('*')->where('id',1)->first();
36+
self::assertSame(1,(int)$result->id);
37+
self::assertNotNull($result);
38+
}
39+
40+
public function testItCanPerformSelectQueryWithMultipleWhereClause()
41+
{
42+
$results = $this->queryBuilder->table('reports')
43+
->select('*')->where('id',1)->where('report_type','=', 'Report Type 1')->first();
44+
self::assertNotNull($results);
45+
self::assertSame(1,(int)$results->id);
46+
self::assertSame('Report Type 1', $results->report_type);
47+
}
48+
}

‎index.php‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,3 @@
44

55
require_once __DIR__.DIRECTORY_SEPARATOR.'src'.DIRECTORY_SEPARATOR.'Exception'.DIRECTORY_SEPARATOR.'exception.php';
66

7-
$test = new \App\Database\PDOConnection( [
8-
'driver' => 'mysql',
9-
'host' => 'localhost',
10-
'db_name' => 'bug_app',
11-
'db_username' => 'root',
12-
'db_user_password' => '',
13-
'default_fetch' => PDO::FETCH_OBJ
14-
]);
15-
var_dump($test->connect());

‎src/Database/QueryBuilder.php‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace App\Database;
3+
4+
use App\Contracts\DatabaseConnectionInterface;
5+
use App\Exception\NotFoundException;
6+
use phpDocumentor\Reflection\Types\Self_;
7+
8+
abstract class QueryBuilder
9+
{
10+
protected $conn; // pdo or mysqli
11+
protected $table;
12+
protected $statement;
13+
protected $fields;
14+
protected $placeholders;
15+
protected $bindings;
16+
protected $operation; //dml statement like SELECT, UPDATE, DELETE etc
17+
18+
const OPERATORS = ['=', '>=', '>', '<=', '<', '<>'];
19+
const PLACEHOLDER = '?';
20+
const COLUMNS = '*';
21+
const DML_TYPE_SELECT = 'SELECT';
22+
const DML_TYPE_INSERT = 'INSERT';
23+
const DML_TYPE_UPDATE = 'UPDATE';
24+
const DML_TYPE_DELETE = 'DELETE';
25+
26+
public function __construct(DatabaseConnectionInterface $connection)
27+
{
28+
$this->conn = $connection->getConnection();
29+
}
30+
31+
public function table($table)
32+
{
33+
$this->table = $table;
34+
return $this;
35+
}
36+
37+
public function where($column, $operator = self::OPERATORS[0], $value = null)
38+
{
39+
if(!in_array($operator, self::OPERATORS)){
40+
if($value === null){
41+
$value = $operator;
42+
$operator = self::OPERATORS[0];
43+
}else{
44+
throw new NotFoundException('Operator is not valid', ['operator' => $operator]);
45+
}
46+
}
47+
$this->passWhere([$column => $value], $operator);
48+
return $this;
49+
}
50+
51+
52+
//----------------------------------------------- Internals Methods -----------------------------------//
53+
protected function passWhere(array $conditions, string $operator)
54+
{
55+
56+
}
57+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /