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 9f8502b

Browse files
fixed bugs in Query trait, added body to create, find, findOneBy, all tests are passing
1 parent d9a8aca commit 9f8502b

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

‎Test/Unit/QueryBuilderTest.php‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Test\Unit;
33

44
use App\Database\PDOConnection;
5+
use App\Database\PDOQueryBuilder;
56
use App\Database\QueryBuilder;
67
use App\Helpers\Config;
78
use PHPUnit\Framework\TestCase;
@@ -15,8 +16,9 @@ class QueryBuilderTest extends TestCase
1516

1617
protected function setUp()
1718
{
18-
$pdo = new PDOConnection(Config::get('database', 'pdo'), ['db_name' => 'bug_app_testing']);
19-
$this->queryBuilder = new QueryBuilder(
19+
$credentials = array_merge(Config::get('database', 'pdo'), ['db_name' => 'bug_app_testing']);
20+
$pdo = new PDOConnection($credentials);
21+
$this->queryBuilder = new PDOQueryBuilder(
2022
$pdo->connect()
2123
);
2224
parent::setUp();
@@ -30,28 +32,37 @@ protected function tearDown()
3032

3133
public function testItCanCreateRecords()
3234
{
35+
$data = [
36+
'report_type' => 'Report Type 1',
37+
'message' => 'This is a dummy message',
38+
'email' => 'support@devscreencast',
39+
'link' => 'https://link.com',
40+
'created_at' => date('Y-m-d H:i:s'),
41+
];
3342
$id = $this->queryBuilder->table('reports')->create($data);
3443
self::assertNotNull($id);
3544
}
3645

3746
public function testItCanPerformRawQuery()
3847
{
39-
$result = $this->queryBuilder->raw('SELECT * FROM reports');
48+
$result = $this->queryBuilder->raw('SELECT * FROM reports')->get();
4049
self::assertNotNull($result);
4150
}
4251

4352
public function testItCanPerformSelectQuery()
4453
{
45-
$result = $this->queryBuilder->table('reports')->select('*')->where('id',1);
46-
var_dump($result);exit();
54+
$result = $this->queryBuilder->table('reports')
55+
->select('*')->where('id',1)->first();
56+
4757
self::assertSame(1,(int)$result->id);
4858
self::assertNotNull($result);
4959
}
5060

5161
public function testItCanPerformSelectQueryWithMultipleWhereClause()
5262
{
5363
$results = $this->queryBuilder->table('reports')
54-
->select('*')->where('id',1)->where('report_type','=', 'Report Type 1')->first();
64+
->select('*')->where('id',1)
65+
->where('report_type','=', 'Report Type 1')->first();
5566
self::assertNotNull($results);
5667
self::assertSame(1,(int)$results->id);
5768
self::assertSame('Report Type 1', $results->report_type);

‎src/Database/Query.php‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,29 @@ public function getQuery(string $type)
1111
{
1212
switch ($type)
1313
{
14-
case self::DML_TYPE_SELECT:
14+
case self::DML_TYPE_SELECT:
1515
return sprintf(
1616
"SELECT %s FROM %s WHERE %s",
17-
$this->fields, $this->table, implode(' and ', $this->placeholders));
17+
$this->fields, $this->table, implode(' and ', $this->placeholders)
18+
);
1819
break;
19-
case self::DML_TYPE_INSERT:
20+
case self::DML_TYPE_INSERT:
2021
return sprintf(
21-
"INSERT into %s (%S) VALUES (%s)",
22-
$this->table, $this->fields, implode(', ', $this->placeholders));
22+
"INSERT INTO %s (%s) VALUES (%s)",
23+
$this->table, $this->fields, implode(',', $this->placeholders)
24+
);
2325
break;
24-
case self::DML_TYPE_UPDATE:
26+
case self::DML_TYPE_UPDATE:
2527
return sprintf(
26-
"UPDATE %s SET %s WHERE (%s)",
27-
$this->table, implode(', ',$this->fields), implode(' and ', $this->placeholders));
28+
"UPDATE %s SET %s WHERE %s",
29+
$this->table, implode(', ', $this->fields), implode(' and ', $this->placeholders)
30+
);
2831
break;
29-
case self::DML_TYPE_DELETE:
32+
case self::DML_TYPE_DELETE:
3033
return sprintf(
31-
"DELETE FROM %s WHERE (%s)",
32-
$this->table, implode(' and ', $this->placeholders));
34+
"DELETE FROM %s WHERE %s",
35+
$this->table, implode(' and ', $this->placeholders)
36+
);
3337
break;
3438
default:
3539
throw new InvalidArgumentException('DML type not supported');

‎src/Database/QueryBuilder.php‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
namespace App\Database;
33

44
use App\Contracts\DatabaseConnectionInterface;
5-
use App\Exception\NotFoundException;
65
use http\Exception\InvalidArgumentException;
76
use phpDocumentor\Reflection\Types\Self_;
87

@@ -45,7 +44,7 @@ public function where($column, $operator = self::OPERATORS[0], $value = null)
4544
$value = $operator;
4645
$operator = self::OPERATORS[0];
4746
}else{
48-
throw new NotFoundException('Operator is not valid', ['operator' => $operator]);
47+
throw new \App\Exception\InvalidArgumentException('Operator is not valid', ['operator' => $operator]);
4948
}
5049
}
5150
$this->passWhere([$column => $value], $operator);
@@ -65,7 +64,19 @@ public function select(string $fields = self::COLUMNS)
6564

6665
public function create(array $data)
6766
{
67+
$this->fields = '`' . implode('`, `', array_keys($data)) . '`';
68+
foreach ($data as $value){
69+
$this->placeholders[] = self::PLACEHOLDER;
70+
$this->bindings[] = $value;
71+
}
72+
$query = $this->prepare($this->getQuery(self::DML_TYPE_INSERT));
6873

74+
// var_dump($query);
75+
// exit(); var_dump($query);
76+
// exit();
77+
$this->statement = $this->execute($query);
78+
79+
return $this->lastInsertedId();
6980
}
7081

7182
public function update(array $data)
@@ -80,22 +91,24 @@ public function delete()
8091

8192
public function raw($query)
8293
{
83-
94+
$query = $this->prepare($query);
95+
$this->statement = $this->execute($query);
96+
return $this;
8497
}
8598

8699
public function find($id)
87100
{
88-
101+
return$this->where('id', $id)->first();
89102
}
90103

91104
public function findOneBy(string $field, $value)
92105
{
93-
106+
return$this->where($field, $value)->first();
94107
}
95108

96109
public function first()
97110
{
98-
111+
return$this->count() ? $this->get()[0] : "";
99112
}
100113

101114

0 commit comments

Comments
(0)

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