Permalink
Browse files

Add unit test for HTTP Search class

  • Loading branch information...
Ricardo Quintanilha
Ricardo Quintanilha committed Sep 5, 2017
1 parent b7d7308 commit 3de49c8f3c53d3f57ae6c3d4490118a485b0252f
Showing with 119 additions and 9 deletions.
  1. +26 −9 phplib/Search/HTTP.php
  2. +93 −0 tests/unit/Search/HTTPTest.php
View
@@ -10,6 +10,8 @@
class HTTP_Search extends Search {
public static $TYPE = 'http';
private $httpClient;
protected function constructQuery() {
return $this->obj['query_data'];
}
@@ -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 [];
}
@@ -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;
}
}
@@ -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.