Permalink
Browse files

Migrating to PHPUnit

git-svn-id: http://svn.php.net/repository/pear/packages/Net_Ping/trunk@299550 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 320aa9e commit 040deec0f48fbd0a6654d35cd255ff4e08c86867 @CloCkWeRX CloCkWeRX committed May 21, 2010
Showing with 153 additions and 151 deletions.
  1. +40 −0 tests/AllTests.php
  2. +113 −0 tests/Net_Ping_ResultTest.php
  3. +0 −151 tests/test_parser.php
View
@@ -0,0 +1,40 @@
+<?php
+if (!defined('PHPUnit_MAIN_METHOD')) {
+ define('PHPUnit_MAIN_METHOD', 'Net_Ping_AllTests::main');
+}
+
+require_once 'PHPUnit/TextUI/TestRunner.php';
+require_once 'Net_Ping_ResultTest.php';
+
+class Net_Ping_AllTests
+{
+ /**
+ * Runs the test suite.
+ *
+ * @return void
+ * @static
+ */
+ public static function main()
+ {
+ PHPUnit_TextUI_TestRunner::run(self::suite());
+ }
+
+ /**
+ * Adds the Net_Ping test suite.
+ *
+ * @return object the PHPUnit_Framework_TestSuite object
+ * @static
+ */
+ public static function suite()
+ {
+ $suite = new PHPUnit_Framework_TestSuite('Net_Ping Test Suite');
+ $suite->addTestSuite('Net_Ping_ResultTest');
+ return $suite;
+ }
+
+}
+
+if (PHPUnit_MAIN_METHOD == 'Net_Ping_AllTests::main') {
+ Net_Ping_AllTests::main();
+}
+?>
@@ -163,4 +163,117 @@ public function testShouldParseResults($os) {
$this->assertSame($expect['targetip'], $ping->getTargetIp());
$this->assertSame($expect['loss'], $ping->getLoss());
}
+
+ public function testShouldParseResults2() {
+ $TPD_DIR = dirname(__FILE__) . '/test_parser_data';
+
+ // there should be a number of "test_data_<N>.php" files in our current
+ // working directory
+ if (!($dh=opendir($TPD_DIR))) {
+ $this->fail("Cannot open '".$TPD_DIR."' to look for test data files", E_USER_ERROR);
+ }
+
+ $saw_none = true;
+ while (false !== ($file=readdir($dh)) ) {
+ // ignore irrelevant nodes in the directory
+ if ( !preg_match('/test_data_\d+\.php/i', $file) ) {
+ continue;
+ }
+
+ // be sure the two arrays the data file will define are unset
+ unset($input);
+ unset($expect);
+
+ ob_start();
+ require $TPD_DIR.'/'.$file;
+ ob_end_clean();
+
+ if ( !isset($input) || !is_array($input) || count($input)<1 ) {
+ $this->fail(" ERROR: file doesn't seem to correctly define the \$input array.\n");
+ continue;
+ }
+ $saw_none = false;
+
+ // if not successful, this function will print messages of its own
+ $this->assertShouldParseResults($input, $expect);
+ }
+ closedir($dh);
+
+ if ($saw_none) {
+ $this->fail("There are no 'test_data_NN.php' files in './test_parser_data/'.");
+ }
+
+ $this->markTestIncomplete("This test needs refactoring.");
+ }
+
+ public function assertShouldParseResults($input, $expect) {
+ // Normally Net_Ping would do all the work of creating a
+ // Net_Ping_Result. In fact, Net_Ping_Result no longer uses the
+ // sysname for *anything*; but we go through the trouble (here) of
+ // creating and invoking the Net_Ping_Result instance exactly like
+ // Net_Ping would do on this system.
+ $OS_Guess = new OS_Guess;
+ $sysname = $OS_Guess->getSysname();
+ $npr = Net_Ping_Result::factory($input, $sysname);
+
+ $this->assertFalse(PEAR::isError($npr));
+
+ // An array of Net_Ping_Result methods (without their leadig "get")
+ // which we'll test against values in the expect array. The keys in
+ // the expect array are these names lowercased.
+ $tests = array(
+ 'TargetIp',
+ 'Min','Max','Avg','Stddev',
+ 'TTL','BytesPerRequest','BytesTotal',
+ 'Transmitted','Received','Loss'
+ );
+
+ // Test all the simple values. If the expect array defines the key,
+ // we compare values.
+ foreach ($tests as $test) {
+ $key = strtolower($test);
+ $method = 'get'.$test;
+
+ if (array_key_exists($key, $expect) && isset($expect[$key])) {
+ if ($expect[$key] != $npr->$method()) {
+ $this->fail(" mismatch for '".$test."'. (expected '".$expect[$key]."' got '".$npr->$method()."')\n");
+
+ }
+ }
+ }
+
+ $icmpseq = $npr->getICMPSequence();
+ // detect ICMP sequence from Net_Ping_Parser, but none defined
+ // in the expect array
+ if (is_array($icmpseq)
+ && count($icmpseq) > 0
+ && (!array_key_exists('icmpseq',$expect) || !is_array($expect['icmpseq']))) {
+ $this->fail(" WARNING: test file doesn't define an 'icmpseq' in \$expect, but Net_Ping_Result has detected successful packets\n");
+
+ }
+
+ // $expect has an array for ICMP sequence; detect variations in
+ // Net_Ping_Result's performance
+ if (array_key_exists('icmpseq', $expect) && is_array($expect['icmpseq'])) {
+
+ // detect expected seqnum/time pairs that are missing or different
+ foreach (array_keys($expect['icmpseq']) as $key) {
+ if (!array_key_exists($key, $icmpseq)) {
+ $this->fail(" ICMP sequence: expected '".$key."', not seen by the Net_Ping_Result parser\n");
+
+ } else if ($expect['icmpseq'][$key] != $icmpseq[$key]) {
+ $this->fail(" ICMP sequence: mismatch for seqnum '".$key."'. Expected '".$expect['icmpseq'][$key]."' got '".$icmpseq[$key]."'\n");
+ }
+ }
+ // detect extraneous pairs from the parser
+ foreach (array_keys($icmpseq) as $key) {
+ if (!array_key_exists($key, $expect['icmpseq'])) {
+ $this->fail(" ICMP sequence: unexpected key/value '".$key."'/'".$icmpseq[$key]."' from the Net_Ping_Result parser\n");
+ }
+ }
+ }
+
+
+ }
}
+
View
@@ -1,151 +0,0 @@
-<?php
-// $Id$
-
-// This program doesn't do anything with the network. It simply uses the
-// Net_Ping_Result class to parse various stored sets of output from
-// various Ping commands against expected results. (See the
-// 'test_parser_data' sub-directory.)
-//
-// Run this PHP program from the directory where it resides. (Because it
-// tests the Ping.php in it's parent directory.) That's most likely
-// accomplished by simply saying:
-//
-// $ php -q ./test_parser.php
-//
-// However, if your command line PHP complains it cannot find PEAR.php,
-// remember that you can set "ini variables" at the command line with:
-//
-// $ php -d include_path=/path/to/pear/install -q ./test_parser.php
-//
-// And as a last attempt to be helpful... if you're wondering where
-// PEAR is installed, look for the value of "PEAR directory" in
-// Pear's configuration:
-//
-// $ pear config-show
-
-
-error_reporting(E_ALL|E_NOTICE);
-
-// This should be run from within the "Net_Ping/tests/" directory. Make
-// sure we get the Ping.php from our parent not the one from the main
-// PEAR installation on this box.
-require_once 'Net/Ping.php';
-
-$TPD_DIR = dirname(__FILE__) . '/test_parser_data';
-
-// there should be a number of "test_data_<N>.php" files in our current
-// working directory
-if ( ! ($dh=opendir($TPD_DIR)) ) {
- trigger_error("Cannot open '".$TPD_DIR."' to look for test data files", E_USER_ERROR);
-}
-$saw_none = true;
-while ( false !== ($file=readdir($dh)) ) {
- // ignore irrelevant nodes in the directory
- if ( !preg_match('/test_data_\d+\.php/i', $file) ) {
- continue;
- }
-
- // be sure the two arrays the data file will define are unset
- unset($input);
- unset($expect);
- print("file '".$file."'...\n");
- ob_start();
- require $TPD_DIR.'/'.$file;
- ob_end_clean();
-
- if ( !isset($input) || !is_array($input) || count($input)<1 ) {
- print(" ERROR: file doesn't seem to correctly define the \$input array.\n");
- continue;
- }
- $saw_none = false;
-
- // if not successful, this function will print messages of its own
- if ( test_net_ping($input, $expect) ) {
- print(" ok!\n");
- }
-}
-closedir($dh);
-
-if ( $saw_none ) {
- trigger_error("There are no 'test_data_NN.php' files in './test_parser_data/'.", E_USER_ERROR);
-}
-
-function test_net_ping($input, $expect) {
-
- $errors = false;
-
- // Normally Net_Ping would do all the work of creating a
- // Net_Ping_Result. In fact, Net_Ping_Result no longer uses the
- // sysname for *anything*; but we go through the trouble (here) of
- // creating and invoking the Net_Ping_Result instance exactly like
- // Net_Ping would do on this system.
- $OS_Guess = new OS_Guess;
- $sysname = $OS_Guess->getSysname();
- $npr = Net_Ping_Result::factory($input, $sysname);
-
- if ( PEAR::isError($npr) ) {
- print(" Net_Ping_Result factory() failed\n");
- return(false);
- }
-
- // An array of Net_Ping_Result methods (without their leadig "get")
- // which we'll test against values in the expect array. The keys in
- // the expect array are these names lowercased.
- $tests = array(
- 'TargetIp',
- 'Min','Max','Avg','Stddev',
- 'TTL','BytesPerRequest','BytesTotal',
- 'Transmitted','Received','Loss'
- );
-
- // Test all the simple values. If the expect array defines the key,
- // we compare values.
- foreach ( $tests as $test ) {
- $key = strtolower($test);
- $method = 'get'.$test;
- if ( array_key_exists($key, $expect) && isset($expect[$key]) ) {
- if ( $expect[$key] != $npr->$method() ) {
- print(" mismatch for '".$test."'. (expected '".$expect[$key]."' got '".$npr->$method()."')\n");
- $errors = true;
- }
- }
- }
-
- $icmpseq = $npr->getICMPSequence();
- // detect ICMP sequence from Net_Ping_Parser, but none defined
- // in the expect array
- if ( is_array($icmpseq)
- && count($icmpseq) > 0
- && ( !array_key_exists('icmpseq',$expect) || !is_array($expect['icmpseq']) )
- ) {
- print(" WARNING: test file doesn't define an 'icmpseq' in \$expect, but Net_Ping_Result has detected successful packets\n");
- $errors = true;
- }
- // $expect has an array for ICMP sequence; detect variations in
- // Net_Ping_Result's performance
- if ( array_key_exists('icmpseq',$expect) && is_array($expect['icmpseq']) ) {
- // detect expected seqnum/time pairs that are missing or different
- foreach ( array_keys($expect['icmpseq']) as $key ) {
- if ( ! array_key_exists($key, $icmpseq) ) {
- print(" ICMP sequence: expected '".$key."', not seen by the Net_Ping_Result parser\n");
- $errors = true;
- }
- else if ( $expect['icmpseq'][$key] != $icmpseq[$key] ) {
- print(" ICMP sequence: mismatch for seqnum '".$key."'. Expected '".$expect['icmpseq'][$key]."' got '".$icmpseq[$key]."'\n");
- $errors = true;
- }
- }
- // detect extraneous pairs from the parser
- foreach ( array_keys($icmpseq) as $key ) {
- if ( ! array_key_exists($key, $expect['icmpseq']) ) {
- print(" ICMP sequence: unexpected key/value '".$key."'/'".$icmpseq[$key]."' from the Net_Ping_Result parser\n");
- $errors = true;
- }
- }
- }
-
- unset($npr);
- return( ($errors?false:true) );
-}
-
-?>

0 comments on commit 040deec

Please sign in to comment.