From 56f7a835fc34ae273dce2a0a3b8361daf64e433f Mon Sep 17 00:00:00 2001 From: Nate Nolting Date: Thu, 1 Jun 2017 08:21:21 -0500 Subject: [PATCH] Fixed Utility::lookUpHostIp by mocking functions --- src/Bandolier/Type/Utility.php | 10 +++--- tests/Type/Utility/LookUpHostIpTest.php | 47 ++++++++++++++++++++++--- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/src/Bandolier/Type/Utility.php b/src/Bandolier/Type/Utility.php index 0b56fd2..69068ba 100644 --- a/src/Bandolier/Type/Utility.php +++ b/src/Bandolier/Type/Utility.php @@ -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; diff --git a/tests/Type/Utility/LookUpHostIpTest.php b/tests/Type/Utility/LookUpHostIpTest.php index e3d4f22..6296a06 100644 --- a/tests/Type/Utility/LookUpHostIpTest.php +++ b/tests/Type/Utility/LookUpHostIpTest.php @@ -12,7 +12,27 @@ * @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 @@ -20,24 +40,29 @@ 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(); } /** @@ -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"); } @@ -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); + } +} \ No newline at end of file