Skip to content

Commit

Permalink
Fixed Utility::lookUpHostIp by mocking functions
Browse files Browse the repository at this point in the history
  • Loading branch information
natenolting committed Jun 1, 2017
1 parent d65ac6c commit 56f7a83
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/Bandolier/Type/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ public static function lookUpHostIp($address)
{

$ipAddress = null;
$records = dns_get_record(trim($address));
foreach ($records as $record) {
if ($record['type'] == 'A') {
$ipAddress = $record['ip'];
if (function_exists('dns_get_record')) {
$records = dns_get_record(trim($address));
foreach ($records as $record) {
if ($record['type'] == 'A') {
$ipAddress = $record['ip'];
}
}
}
return $ipAddress;
Expand Down
47 changes: 43 additions & 4 deletions tests/Type/Utility/LookUpHostIpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,57 @@
* @package ${NAMESPACE}
*/

use \Mockery as m;

/**
* Mocked function_exists
* @param $function
* @return mixed
*/
function function_exists($function)
{
return LookUpHostIpTest::$functions->function_exists($function);
}

/**
* Mocked dns_get_record
* @param $function
* @return mixed
*/
function dns_get_record($hostname)
{
return LookUpHostIpTest::$functions->dns_get_record($hostname);
}
/**
* Class UtilityTest
* @package Pbc\Bandolier\Type
*/
class LookUpHostIpTest extends \PHPUnit_Framework_TestCase
{

/** @var m::mock $functions*/
public static $functions;

/** @var \Faker\Factory */
protected static $faker;

/**
*
* Setup test case
*/
protected function setUp()
{
parent::setUp();
self::$faker = \Faker\Factory::create();
self::$functions = m::mock();
}

/**
*
* tear down test case
*/
protected function tearDown()
{
parent::tearDown();
m::close();
}

/**
Expand All @@ -46,6 +71,8 @@ protected function tearDown()
public function testLookUpHostIpReturnsAnIpAddress()
{
$address = "paulbunyan.net";
self::$functions->shouldReceive('function_exists')->once()->andReturn(true);
self::$functions->shouldReceive('dns_get_record')->once()->andReturn([['type' => 'A', 'ip' => '127.0.0.1']]);
$lookUp = Utility::lookUpHostIp($address);
$this->assertNotFalse(filter_var($lookUp, FILTER_VALIDATE_IP), "$lookUp is an ip address");
}
Expand All @@ -55,9 +82,21 @@ public function testLookUpHostIpReturnsAnIpAddress()
*/
public function testLookUpHostIpReturnsNullIfNoneFound()
{
$address = self::$faker->sha256.".com";
$address = "paulbunyan.net";
self::$functions->shouldReceive('function_exists')->once()->andReturn(true);
self::$functions->shouldReceive('dns_get_record')->once()->andReturn([['type' => 'MX', 'ip' => '127.0.0.1']]);
$lookUp = Utility::lookUpHostIp($address);
$this->assertNull($lookUp);

}
}
/**
* check that a not ip is returned if the dns_get_record does not exist
*/
public function testLookUpHostIpReturnsNullIfFunctionDoesNotExist()
{
$address = "paulbunyan.net";
self::$functions->shouldReceive('function_exists')->once()->andReturn(false);
$lookUp = Utility::lookUpHostIp($address);
$this->assertNull($lookUp);
}
}

0 comments on commit 56f7a83

Please sign in to comment.