Skip to content

Commit cd62763

Browse files
author
Jordi Martinez
committed
PHP-Unit-Test Adds unit tests for Chapter 10
Implements unit tests for Chapter 10 Adds Guzzle dependency Updates README file
1 parent e4cc3c3 commit cd62763

File tree

15 files changed

+514
-4
lines changed

15 files changed

+514
-4
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ the Book, you use **Composer**. If you look into the __composer.json__ file incl
2828
- Symfony Dom Crawler: a library you would need to test the post of HTML forms to the web server. (you would not need this until
2929
Chapter 6).
3030
- Symfony CSS Selector: a library to filter the contents of HTML documents using HTML tags and CSS selectors.
31-
- Monolog: a library to handle logging.
31+
- Monolog: a library to handle logging.
32+
- Guzzle: a library that wraps up CURL so that it is easier to handle them.

app/Chapter10/Activity/httpbin.php

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Chapter10\Activity;
4+
5+
use GuzzleHttp\Client;
6+
use Exception;
7+
8+
$client = new Client(['base_uri' => 'http://httpbin.org/']);
9+
10+
try
11+
{
12+
$response = $client->request('POST', '/response-headers', [
13+
'headers' => [
14+
'Accept' => 'application-json'
15+
],
16+
'query' => [
17+
'first' => 'John',
18+
'last' => 'Doe'
19+
]
20+
]);
21+
22+
if ($response->getStatusCode() !== 200) {
23+
throw new Exception("Status code was {$response->getStatusCode()}, not 200");
24+
}
25+
26+
$responseObject = json_decode($response->getBody()->getContents());
27+
28+
echo "The web service responded with {$responseObject->first} {$responseObject->last}" . PHP_EOL;
29+
}
30+
catch (Exception $ex)
31+
{
32+
echo "An error occurred: " . $ex->getMessage() . PHP_EOL;
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
class MailingListRecipient
4+
{
5+
public $email;
6+
public $firstName;
7+
public $lastName;
8+
9+
public function __construct($email, $firstName, $lastName)
10+
{
11+
$this->email = $email;
12+
$this->firstName = $firstName;
13+
$this->lastName = $lastName;
14+
}
15+
}

app/Chapter10/Exercise1/json.php

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
require 'MailingListRecipient.php';
4+
5+
$recipient = new MailingListRecipient('[email protected]','John','Doe');
6+
7+
$requestBody = json_encode($recipient);
8+
echo $requestBody.PHP_EOL;

app/Chapter10/Exercise3/ipify.php

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Chapter10\Exercise3;
4+
5+
use GuzzleHttp\Client;
6+
7+
$client = new Client(['base_uri'=>'https://api.ipify.org']);
8+
9+
$response = $client->request('GET', '/', [
10+
'query' => ['format'=>'json']
11+
]);
12+
13+
$responseObject = json_decode($response->getBody()->getContents());
14+
15+
echo "Your public facing ip address is {$responseObject->ip}" . PHP_EOL;

app/Chapter10/Exercise4/spamcheck.php

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Chapter10\Exercise4;
4+
5+
use GuzzleHttp\Client;
6+
use Exception;
7+
8+
$email = '[email protected]';
9+
10+
$client = new Client(['base_uri' => 'https://spamcheck.postmarkapp.com/']);
11+
12+
$requestBody = json_encode([
13+
'email' => $email,
14+
'options' => 'short'
15+
]);
16+
17+
try
18+
{
19+
$response = $client->request('POST', '/filter', [
20+
'headers' => [
21+
'Accept' => 'application/json',
22+
'Content-Type' => 'application/json'
23+
],
24+
'body' => $requestBody
25+
]);
26+
27+
if ($response->getStatusCode() !== 200) {
28+
throw new Exception("Status code was {$response->getStatusCode()}, not 200");
29+
}
30+
31+
$responseObject = json_decode($response->getBody()->getContents());
32+
33+
if ($responseObject->success !== true) {
34+
throw new Exception("Service returned an unsuccessful response: {$responseObject->message}");
35+
}
36+
37+
echo "The SpamAssassin score for email {$email} is {$responseObject->score}" . PHP_EOL;
38+
}
39+
catch (Exception $ex)
40+
{
41+
echo "An error occurred: " . $ex->getMessage() . PHP_EOL;
42+
}

app/Chapter10/sample.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"property": "value",
3+
"some array": [
4+
{"item 1": "some value"},
5+
{"item 2": "some other value"}
6+
]
7+
}

app/Chapter10/sample.xml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<element>
2+
<propery attr=”some attribute”>value</property>
3+
<items>
4+
<item>some value</item>
5+
<item>some other value</item>
6+
</items>
7+
</element>

composer.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
"require": {
66
"php": "^7.2",
77
"ext-curl": "*",
8+
"ext-json": "*",
89
"monolog/monolog": "^2.0"
910
},
1011
"require-dev": {
1112
"phpunit/phpunit": "^8",
1213
"symfony/dom-crawler": "^4.3",
13-
"symfony/css-selector": "^4.3"
14+
"symfony/css-selector": "^4.3",
15+
"guzzlehttp/guzzle": "^6.3"
1416
},
1517
"autoload": {
1618
"psr-4": {

0 commit comments

Comments
 (0)