Page MenuHomeIn-Portal Phabricator

globalsTest.php
No OneTemporary

File Metadata

Created
Sun, Oct 5, 3:44 PM

globalsTest.php

<?php
/**
* The class name must match the file name for Phabricator-invoked PHPUnit to run this test.
*
* TODO: Once "globals.php" file is renamed we can rename this class/filename as well.
*/
class globalsTest extends AbstractTestCase // phpcs:ignore Squiz.Classes.ValidClassName.NotCamelCaps
{
/**
* @dataProvider netMatchDataOnlineProvider
*/
public function testNetMatchOnline($network, $expected)
{
static $ip;
if ( !$this->hasInternetConnectivity() ) {
$this->markTestSkipped('No Internet Connection.');
}
if ( $ip === null ) {
$ip = gethostbyname('www.in-portal.org');
}
$this->assertSame($expected, kUtil::netMatch($network, $ip));
}
protected function hasInternetConnectivity()
{
// Use IP of the "www.google.com" hostname to avoid DNS query, that might fail.
$error_code = $error_message = null;
$fp = @fsockopen('142.250.74.36', 443, $error_code, $error_message, 5);
if ( $fp ) {
fclose($fp);
return true;
}
return false;
}
public static function netMatchDataOnlineProvider()
{
return array(
// Hosts.
'matched' => array('www.in-portal.org', true),
'unmatched' => array('www.yahoo.com', false),
);
}
/**
* @dataProvider netMatchDataOfflineProvider
*/
public function testNetMatchOffline($network, $ip, $expected)
{
$this->assertSame($expected, kUtil::netMatch($network, $ip));
}
public static function netMatchDataOfflineProvider()
{
return array(
// IPv4 address vs IPv4 network.
array('1.2.3.4', '1.2.3.4', true),
array('1.2.3.4', '1.2.3.5', false),
array('1.2.3.32-1.2.3.54', '1.2.3.40', true),
array('1.2.3.32-1.2.3.54', '1.2.3.64', false),
array('1.2.3.32/27', '1.2.3.35', true),
array('1.2.3.32/27', '1.2.3.64', false),
array('1.2.3/27', '1.2.3.5', true),
array('1.2.3/27', '1.2.3.33', false),
array('1.2.3.32/255.255.255.224', '1.2.3.35', true),
array('1.2.3.32/255.255.255.224', '1.2.3.64', false),
array('1.2.3.32/27', '1.2.3.4.6', false),
// IPv6 address vs IPv6 network.
array('971a:6ff4:3fe8::7085:f498:fc5a:9893', '971a:6ff4:3fe8::7085:f498:fc5a:9893', true),
array('971a:6ff4:3fe8::7085:f498:fc5a:9893', '971a:6ff4:3fe8::7085:f498:fc5a:9895', false),
array('971a:6ff4:3fe8::9893-971a:6ff4:3fe8::9895', '971a:6ff4:3fe8::9894', true),
array('971a:6ff4:3fe8::9893-971a:6ff4:3fe8::9895', '971a:6ff4:3fe8::9896', false),
array('971a:6ff4:3fe8::9893/64', '971a:6ff4:3fe8::9895', true),
array('971a:6ff4:3fe8::9893/64', '971a:6ff4:3fe9::9895', false),
// IPv6 address vs IPv4 network.
array('1.2.3.4', '971a:6ff4:3fe8::7085:f498:fc5a:9893', false),
array('1.2.3.32-1.2.3.54', '971a:6ff4:3fe8::7085:f498:fc5a:9893', false),
array('1.2.3.32/27', '971a:6ff4:3fe8::7085:f498:fc5a:9893', false),
array('1.2.3/27', '971a:6ff4:3fe8::7085:f498:fc5a:9893', false),
array('1.2.3.32/255.255.255.224', '971a:6ff4:3fe8::7085:f498:fc5a:9893', false),
// IPv4 address vs IPv6 network.
array('971a:6ff4:3fe8::7085:f498:fc5a:9893', '1.2.3.4', false),
array('971a:6ff4:3fe8::9893-971a:6ff4:3fe8::9895', '1.2.3.4', false),
array('971a:6ff4:3fe8::9893/64', '1.2.3.4', false),
);
}
/**
* @dataProvider validKg2PoundsDataProvider
*/
public function testValidKg2Pounds($kg, $pounds_only, $expected_pounds, $expected_ounces)
{
list($pounds, $ounces) = kUtil::Kg2Pounds($kg, $pounds_only);
$this->assertSame($expected_pounds, $pounds);
$this->assertSame($expected_ounces, $ounces);
}
public static function validKg2PoundsDataProvider()
{
return array(
'pounds_only=false' => array(0.68039, false, 1.0, 8.0),
'pounds_only=true' => array(0.68039, true, 1.5, 0),
);
}
public function testExceptionKg2Pounds()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The $kg argument isn\'t a number.');
kUtil::Kg2Pounds(null);
}
/**
* @dataProvider validPounds2KgDataProvider
*/
public function testValidPounds2Kg($pounds, $ounces, $expected)
{
$this->assertSame($expected, kUtil::Pounds2Kg($pounds, $ounces));
}
public static function validPounds2KgDataProvider()
{
return array(
'pounds <> 0, ounces <> 0' => array(1.0, 8.0, 0.68039),
'pounds <> 0, ounces = 0' => array(1.5, 0.0, 0.68039),
'pounds = 0, ounces <> 0' => array(0.0, 8.0, 0.2268),
'pounds = 0, ounces = 0' => array(0.0, 0.0, 0.0),
);
}
/**
* @dataProvider exceptionPounds2KgDataProvider
*/
public function testExceptionPounds2Kg($pounds, $ounces)
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Either the $pounds or $ounces argument isn\'t a number.');
kUtil::Pounds2Kg($pounds, $ounces);
}
public static function exceptionPounds2KgDataProvider()
{
return array(
'pounds non-numeric, ounces numeric' => array(null, 8),
'pounds numeric, ounces non-numeric' => array(1, null),
'both pounds and ounces non-numeric' => array(null, null),
);
}
}

Event Timeline