Refactor to move the network configuration capture to a separate method

This commit is contained in:
Ben Ramsey
2013-12-27 08:53:45 -06:00
parent 7bebd01e31
commit 6430a37adc
4 changed files with 18 additions and 86 deletions
+18 -10
View File
@@ -1060,6 +1060,23 @@ final class Uuid
);
}
/**
* Returns the network interface configuration for the system
*
* @todo Needs evaluation and possibly modification to ensure this works
* well across multiple platforms.
* @codeCoverageIgnore
*/
protected static function getIfconfig()
{
// If we're on Windows, use ipconfig; otherwise use ifconfig
if (strtoupper(substr(php_uname('a'), 0, 3)) == 'WIN') {
return `ipconfig /all 2>&1`;
}
return `ifconfig 2>&1`;
}
/**
* Get the hardware address as a 48-bit positive integer. If all attempts to
* obtain the hardware address fail, we choose a random 48-bit number with
@@ -1069,25 +1086,16 @@ final class Uuid
* returned.
*
* @return string
* @todo Needs evaluation and possibly modification to ensure this works
* well across multiple platforms.
*/
protected static function getNodeFromSystem()
{
// If we're on Windows, use ipconfig; otherwise use ifconfig
if (strtoupper(substr(php_uname('a'), 0, 3)) == 'WIN') {
$ifconfig = `ipconfig /all 2>&1`;
} else {
$ifconfig = `ifconfig 2>&1`;
}
$node = null;
$pattern = '/[^:]([0-9A-Fa-f]{2}([:-])[0-9A-Fa-f]{2}(\2[0-9A-Fa-f]{2}){4})[^:]/';
$matches = array();
// Search the ifconfig output for all MAC addresses and return
// the first one found
if (preg_match_all($pattern, $ifconfig, $matches, PREG_PATTERN_ORDER)) {
if (preg_match_all($pattern, self::getIfconfig(), $matches, PREG_PATTERN_ORDER)) {
$node = $matches[1][0];
$node = str_replace(':', '', $node);
$node = str_replace('-', '', $node);
-22
View File
@@ -1,8 +1,6 @@
<?php
namespace Rhumsaa\Uuid;
require_once 'functions.php';
/**
* This is a backwards-compatibility test to ensure that Rhumsaa\Uuid
* maintains backwards compatibility with tag 1.1.2.
@@ -382,26 +380,6 @@ class UuidBcTag1_1_2Test extends \PHPUnit_Framework_TestCase
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', sprintf('%s', $uuid));
}
/**
* This calls php_uname() in getNodeFromSystem. The first time it is
* called, it returns "WIN." Each additional times, it returns the
* normal system php_uname().
*
* See the bottom of this test file to see where we are overriding
* php_uname() for the purpose of this test.
*
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @covers Rhumsaa\Uuid\Uuid::getNodeFromSystem
*/
public function testUuid1CoverageForWindows()
{
$uuid = Uuid::uuid1();
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(1, $uuid->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @covers Rhumsaa\Uuid\Uuid::getNodeFromSystem
-22
View File
@@ -1,8 +1,6 @@
<?php
namespace Rhumsaa\Uuid;
require_once 'functions.php';
class UuidTest extends \PHPUnit_Framework_TestCase
{
protected function setUp()
@@ -630,26 +628,6 @@ class UuidTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('0901e600-0154-1000-9b21-0800200c9a66', sprintf('%s', $uuid));
}
/**
* This calls php_uname() in getNodeFromSystem. The first time it is
* called, it returns "WIN." Each additional times, it returns the
* normal system php_uname().
*
* See the bottom of this test file to see where we are overriding
* php_uname() for the purpose of this test.
*
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @covers Rhumsaa\Uuid\Uuid::getNodeFromSystem
*/
public function testUuid1CoverageForWindows()
{
$uuid = Uuid::uuid1();
$this->assertInstanceOf('\Rhumsaa\Uuid\Uuid', $uuid);
$this->assertInstanceOf('\DateTime', $uuid->getDateTime());
$this->assertEquals(2, $uuid->getVariant());
$this->assertEquals(1, $uuid->getVersion());
}
/**
* @covers Rhumsaa\Uuid\Uuid::uuid1
* @covers Rhumsaa\Uuid\Uuid::getNodeFromSystem
-32
View File
@@ -1,32 +0,0 @@
<?php
namespace Rhumsaa\Uuid;
/**
* Overriding the default php_uname() for the purpose of this test so we can
* have full code coverage on all the lines.
*
* This should ensure that we have full code coverage whether the tests are run
* on Windows or a *NIX environment. This does not mean that the code has been
* tested for both environments if it is only tested in one environment, so
* the tests must still be run in both environments to ensure they pass.
*/
function php_uname($v)
{
static $run = 0;
if ($run == 0 && strtoupper(substr(\php_uname('a'), 0, 3)) != 'WIN') {
// If this is the first run and the SUT is not Windows, return "Windows"
$ret = 'Windows';
} elseif ($run == 0 && strtoupper(substr(\php_uname('a'), 0, 3)) == 'WIN') {
// If this is the first run and the SUT is Windows, return "Darwin"
$ret = 'Darwin';
} else {
// If this isn't the first run, then use the system php_uname()
$ret = \php_uname($v);
}
$run++;
return $ret;
}