Skip to content

Commit 1bd0ed9

Browse files
Bug: have to use production databse, TODO: fix this bug, create test success
1 parent 8d83413 commit 1bd0ed9

7 files changed

Lines changed: 46 additions & 13 deletions

File tree

Test/Functional/CrudTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class CrudTest extends TestCase
2222

2323
protected function setUp()
2424
{
25-
$this->queryBuilder = DbQueryBuilderFactory::make('database', 'pdo', ['db_name' => 'bug_app_testing']);
25+
$this->queryBuilder = DbQueryBuilderFactory::make();
2626
//$this->queryBuilder->beginTransaction();
2727
$this->repository = new BugReportRepository($this->queryBuilder);
2828
$this->client = new HttpClient();
@@ -76,7 +76,6 @@ public function testItCanUpdateReportUsingPostRequest(BugReport $bugReport)
7676
$response = $this->client->post("http://localhost/sand_box/bug_tracking_app/src/View/update.php", $postData);
7777

7878
$response = json_decode($response, true);
79-
var_dump($response);die();
8079
self::assertEquals(200, $response['statusCode']);
8180

8281
/** @var BugReport $result */

src/Contracts/RepositoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ interface RepositoryInterface
88
public function find(int $id): ?object;
99
public function findOneBy(string $field, $value): ?object;
1010
public function findBy(array $criteria);
11-
public function findAll(int $id): array;
11+
public function findAll(): array;
1212
public function sql(string $query);
1313
public function create(Entity $entity): object;
1414
public function update(Entity $entity, array $conditions = []): object;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
namespace App\Exception;
5+
6+
class BadRequestException extends BaseException
7+
{
8+
9+
}

src/Helpers/App.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,12 @@ public function isDebugMode() :bool
2222
return $this->config['debug'];
2323
}
2424

25-
public function getEnvironment() : string
25+
public function getEnvironment(): string
2626
{
27-
if(!isset($this->config['env']))
28-
{
27+
if(!isset($this->config['env'])){
2928
return 'production';
3029
}
31-
return $this->config['env'];
30+
return $this->isTestMode() ? 'test' : $this->config['env'];
3231
}
3332

3433
public function getLogPath() :string
@@ -49,4 +48,12 @@ public function getServerTime(): DateTimeInterface
4948
{
5049
return new DateTime('now', new DateTimeZone('Europe/Berlin'));
5150
}
51+
52+
public function isTestMode(): bool
53+
{
54+
if($this->isRunningFromConsole() && defined('PHPUNIT_RUNNING') && PHPUNIT_RUNNING == true) {
55+
return true;
56+
}
57+
return false;
58+
}
5259
}

src/Helpers/DbQueryBuilderFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use App\Exception\DatabaseConnectionException;
1010
use App\Exception\MissingArgumentException;
1111
use App\Exception\NotFoundException;
12+
use phpDocumentor\Reflection\Types\Self_;
1213

1314
class DbQueryBuilderFactory
1415
{

src/Repository/Repository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function findBy (array $criteria)
4444
return $this->queryBuilder->runQuery()->fetchInto(static::$className);
4545
}
4646

47-
public function findAll (int $id): array
47+
public function findAll (): array
4848
{
4949
return $this->queryBuilder->table(static::$table)
5050
->select()->runQuery()->fetchInto(static::$className);

src/View/add.php

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use App\Repository\BugReportRepository;
66
use App\Helpers\DbQueryBuilderFactory;
77
use App\Database\QueryBuilder;
8+
use App\Logger\Logger;
9+
use App\Exception\BadRequestException;
10+
use App\Helpers\App;
811

912
if(isset($_POST, $_POST['add']))
1013
{
@@ -19,9 +22,23 @@
1922
$bugReport->setLink($link);
2023
$bugReport->setMessage($message);
2124

22-
/** @var QueryBuilder $queryBuilder */
23-
$queryBuilder = DbQueryBuilderFactory::make();
24-
/** @var BugReportRepository $repository */
25-
$repository = new BugReportRepository($queryBuilder);
26-
$newReport = $repository->create($bugReport);
25+
$logger = new Logger;
26+
27+
try{
28+
$application = new App();
29+
/** @var QueryBuilder $queryBuilder */
30+
$queryBuilder = DbQueryBuilderFactory::make();
31+
/** @var BugReportRepository $repository */
32+
$repository = new BugReportRepository($queryBuilder);
33+
/** @var BugReport $newReport */
34+
$newReport = $repository->create($bugReport);
35+
}catch (Throwable $exception)
36+
{
37+
$logger->critical($exception->getMessage(), $_POST);
38+
throw new BadRequestException($exception->getMessage(), [$exception], 400);
39+
}
40+
41+
$logger->info('new bug report created', ['id' => $newReport->getId(), 'type' => $newReport->getReportType()]);
42+
43+
//$bugReports = $repository->findAll();
2744
}

0 commit comments

Comments
 (0)