Skip to content

Commit

Permalink
Add unit test for HTTP Search class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricardo Quintanilha committed Sep 5, 2017
1 parent b7d7308 commit 3de49c8
Show file tree
Hide file tree
Showing 2 changed files with 119 additions and 9 deletions.
35 changes: 26 additions & 9 deletions phplib/Search/HTTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
class HTTP_Search extends Search {
public static $TYPE = 'http';

private $httpClient;

protected function constructQuery() {
return $this->obj['query_data'];
}
Expand All @@ -33,16 +35,17 @@ public function validateData(array $data) {
}

protected function _execute($date, $constructed_qdata) {
$curl = new Curl;
$curl = $this->getHttpClient();
$curl->get($constructed_qdata['url']);
$this->obj['last_status'] = sprintf(
'Code = %d, Message = %s',
$curl->httpStatusCode,
$curl->httpErrorMessage
);

if ($this->isStatusCodeValid($constructed_qdata['code'], $curl->httpStatusCode) &&
$this->isContentValid($constructed_qdata['content_match'], $curl->rawResponse)) {
if ($this->isStatusCodeValid($constructed_qdata, $curl) &&
$this->isContentValid($constructed_qdata, $curl)
) {
return [];
}

Expand All @@ -51,24 +54,38 @@ protected function _execute($date, $constructed_qdata) {
$alert['content'] = [
'status' => $curl->httpStatusCode,
'url' => $constructed_qdata['url'],
'content length' => strlen($curl->rawResponse)
'content_length' => strlen($curl->rawResponse)
];

return [$alert];
}

private function isStatusCodeValid($expected, $current)
private function isStatusCodeValid($expectedData, $curl)
{
return $expected === $current;
return $expectedData['code'] === $curl->httpStatusCode;
}

private function isContentValid($regex, $response)
private function isContentValid($expectedData, $curl)
{
if (empty($regex)) {
if (!isset($expectedData['content_match']) && !empty($expectedData['content_match'])) {
return true;
}

return (bool)preg_match("/" . $regex . "/", $response);
return preg_match("/" . $expectedData['content_match'] . "/", $curl->rawResponse) === 1;
}

private function getHttpClient()
{
if ($this->httpClient === null) {
$this->httpClient = new Curl;
}

return $this->httpClient;
}

public function setHttpClient(Curl $httpClient)
{
$this->httpClient = $httpClient;
}
}

93 changes: 93 additions & 0 deletions tests/unit/Search/HTTPTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

use FOO\Alert;
use FOO\Curl;
use FOO\HTTP_Search;

class HTTP_SearchTest extends TestCase {
public function testShouldGenerateAnAlertForInvalidStatusCode()
{
$httpClient = $this->getHttpClientMock();
$httpClient->httpStatusCode = 500;
$httpClient->rawResponse = 'Internal Server Error';

$search = new HTTP_Search();

$search->setId(1);
$search->setHttpClient($httpClient);

$search['query_data'] = [
'url' => 'http://unittest.com/alerts',
'code' => 200
];

$expectedAlert = new Alert();
$expectedAlert['content'] = [
'status' => 500,
'url' => 'http://unittest.com/alerts',
'content_length' => 21
];

$alert = $search->execute(0)[0];

$this->assertEquals($expectedAlert['alert_date'], $alert['alert_date']);
$this->assertEquals($expectedAlert['content'], $alert['content']);
}

public function testShouldGenerateAnAlertForInvalidContentMatch()
{
$httpClient = $this->getHttpClientMock();
$httpClient->httpStatusCode = 200;
$httpClient->rawResponse = 'Content doesn\'t match.';

$search = new HTTP_Search();

$search->setId(1);
$search->setHttpClient($httpClient);

$search['query_data'] = [
'url' => 'http://unittest.com/alerts',
'code' => 200,
'content_match' => 'Content match.'
];

$expectedAlert = new Alert();
$expectedAlert['content'] = [
'status' => 200,
'url' => 'http://unittest.com/alerts',
'content_length' => 22
];

$alert = $search->execute(0)[0];

$this->assertEquals($expectedAlert['alert_date'], $alert['alert_date']);
$this->assertEquals($expectedAlert['content'], $alert['content']);
}

public function testShouldNotGenerateAnAlert()
{
$httpClient = $this->getHttpClientMock();
$httpClient->httpStatusCode = 200;
$httpClient->rawResponse = 'Content match.';

$search = new HTTP_Search();

$search->setId(1);
$search->setHttpClient($httpClient);

$search['query_data'] = [
'url' => 'http://unittest.com/alerts',
'code' => 200,
'content_match' => 'Content match.'
];

$this->assertEmpty($search->execute(0));
}

private function getHttpClientMock()
{
return $this->getMockBuilder(Curl::class)
->disableOriginalConstructor()
->getMock();
}
}

0 comments on commit 3de49c8

Please sign in to comment.