Skip to content

Commit 41a5be5

Browse files
Created HttpClient, modifie test cases
1 parent bb698dc commit 41a5be5

3 files changed

Lines changed: 55 additions & 5 deletions

File tree

Test/Functional/CrudTest.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
use App\Database\QueryBuilder;
55
use App\Entity\BugReport;
66
use App\Helpers\DbQueryBuilderFactory;
7+
use App\Helpers\HttpClient;
78
use App\Repository\BugReportRepository;
89
use PHPUnit\Framework\TestCase;
10+
use function MongoDB\BSON\fromJSON;
911

1012
class CrudTest extends TestCase
1113
{
@@ -15,6 +17,7 @@ class CrudTest extends TestCase
1517
/** @var QueryBuilder $queryBuilder */
1618
private $queryBuilder;
1719

20+
/** @var HttpClient $client */
1821
private $client;
1922

2023
protected function setUp()
@@ -36,7 +39,10 @@ protected function setUp()
3639
public function testItCanCreateReportUsingPostRequest()
3740
{
3841
$postData = $this->getPostData(['add' => true]);
39-
$this->client->post("http://localhost/bug_tracking_app/src/add.php", $postData);
42+
$response = $this->client->post("http://localhost/bug_tracking_app/src/add.php", $postData);
43+
44+
$response = json_decode($response, true);
45+
self::assertEquals(200, $response['statusCode']);
4046

4147
$result = $this->repository->findBy([
4248
['report_type', '=', 'Audio Issue'],
@@ -68,7 +74,10 @@ public function testItCanUpdateReportUsingPostRequest(BugReport $bugReport)
6874
'link' => 'https://updated.com',
6975
'report_id' => $bugReport->getId()
7076
]);
71-
$this->client->post("http://localhost/bug_tracking_app/src/update.php", $postData);
77+
$response = $this->client->post("http://localhost/bug_tracking_app/src/update.php", $postData);
78+
79+
$response = json_decode($response, true);
80+
self::assertEquals(200, $response['statusCode']);
7281

7382
/** @var BugReport $result */
7483
$result = $this->repository->find($bugReport->getId());
@@ -81,14 +90,20 @@ public function testItCanUpdateReportUsingPostRequest(BugReport $bugReport)
8190
}
8291

8392

84-
93+
/**
94+
* @depends testItCanUpdateReportUsingPostRequest
95+
* @param BugReport $bugReport
96+
*/
8597
public function testItCanDeleteReportUsingPostRequest(BugReport $bugReport)
8698
{
8799
$postData = [
88100
'delete' => true,
89101
'report_id' => $bugReport->getId()
90102
];
91-
$this->client->post("http://localhost/bug_tracking_app/src/delete.php", $postData);
103+
$response = $this->client->post("http://localhost/bug_tracking_app/src/delete.php", $postData);
104+
105+
$response = json_decode($response, true);
106+
self::assertEquals(200, $response['statusCode']);
92107

93108
/** @var BugReport $result */
94109
$result = $this->repository->find($bugReport->getId());

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"ext-json": "*",
55
"ext-pdo": "*",
66
"ext-mysqli": "*",
7-
"ext-http": "*"
7+
"ext-http": "*",
8+
"ext-curl": "*"
89
},
910
"require-dev": {
1011
"phpunit/phpunit": "^7",

src/Helpers/HttpClient.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
namespace App\Helpers;
3+
4+
5+
class HttpClient
6+
{
7+
public function post(string $url, array $data = [])
8+
{
9+
$handler = curl_init();
10+
curl_setopt($handler, CURLOPT_URL, $url);
11+
curl_setopt($handler, CURLOPT_POST, true);
12+
curl_setopt($handler, CURLOPT_POSTFIELDS, http_build_query($data));
13+
14+
$response = curl_exec($handler);
15+
$statusCode = curl_getinfo($handler, CURLINFO_HTTP_CODE);
16+
curl_close($handler);
17+
18+
return json_encode(['statusCode' => $statusCode, 'content' => $response]);
19+
}
20+
21+
22+
public function get(string $url)
23+
{
24+
$handler = curl_init();
25+
curl_setopt($handler, CURLOPT_URL, $url);
26+
curl_setopt($handler, CURLOPT_RETURNTRANSFER, true);
27+
28+
$response = curl_exec($handler);
29+
$statusCode = curl_getinfo($handler, CURLINFO_HTTP_CODE);
30+
curl_close($handler);
31+
32+
return json_encode(['statusCode' => $statusCode, 'content' => $response]);
33+
}
34+
}

0 commit comments

Comments
 (0)