home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / PEAR_PackageFileManager_File_TestCase_getRegExpableSearchString.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  7.6 KB  |  173 lines

  1. <?php
  2.  
  3. /**
  4.  * API Unit tests for PEAR_PackageFileManager package.
  5.  * 
  6.  * @version    $Id: PEAR_PackageFileManager_File_TestCase_getRegExpableSearchString.php,v 1.2 2003/10/18 18:46:25 cellog Exp $
  7.  * @author     Laurent Laville <pear@laurent-laville.org> portions from HTML_CSS
  8.  * @author     Greg Beaver
  9.  * @package    PEAR_PackageFileManager
  10.  */
  11.  
  12. /**
  13.  * @package PEAR_PackageFileManager
  14.  */
  15.  
  16. class PEAR_PackageFileManager_File_TestCase_getRegExpableSearchString extends PHPUnit_TestCase
  17. {
  18.     /**
  19.      * A PEAR_PackageFileManager object
  20.      * @var        object
  21.      */
  22.     var $packagexml;
  23.  
  24.     function PEAR_PackageFileManager_File_TestCase_getRegExpableSearchString($name)
  25.     {
  26.         $this->PHPUnit_TestCase($name);
  27.     }
  28.  
  29.     function setUp()
  30.     {
  31.         error_reporting(E_ALL);
  32.         $this->errorOccured = false;
  33.         set_error_handler(array(&$this, 'errorHandler'));
  34.  
  35.         $a = false;
  36.         $this->packagexml = new PEAR_PackageFileManager_File($a, array());
  37.         PEAR::pushErrorHandling(PEAR_ERROR_CALLBACK, array(&$this, 'PEARerrorHandler'));
  38.         $this->errorThrown = false;
  39.         $this->_expectedMessage = 'NO ERROR TRIGGERED';
  40.         $this->_expectedCode = -1;
  41.         $this->_testMethod = 'unknown';
  42.     }
  43.  
  44.     function tearDown()
  45.     {
  46.         unset($this->packagexml);
  47.         PEAR::popErrorHandling();
  48.     }
  49.  
  50.     function errorCodeToString($code)
  51.     {
  52.         $codes = array_flip(array(
  53.             'OOPS' => -1,
  54.             'PEAR_PACKAGEFILEMANAGER_NOSTATE' => PEAR_PACKAGEFILEMANAGER_NOSTATE,
  55.             'PEAR_PACKAGEFILEMANAGER_NOVERSION' => PEAR_PACKAGEFILEMANAGER_NOVERSION,
  56.             'PEAR_PACKAGEFILEMANAGER_NOPKGDIR' => PEAR_PACKAGEFILEMANAGER_NOPKGDIR,
  57.             'PEAR_PACKAGEFILEMANAGER_NOBASEDIR' => PEAR_PACKAGEFILEMANAGER_NOBASEDIR,
  58.             'PEAR_PACKAGEFILEMANAGER_GENERATOR_NOTFOUND' => PEAR_PACKAGEFILEMANAGER_GENERATOR_NOTFOUND,
  59.             'PEAR_PACKAGEFILEMANAGER_GENERATOR_NOTFOUND_ANYWHERE' => PEAR_PACKAGEFILEMANAGER_GENERATOR_NOTFOUND_ANYWHERE,
  60.             'PEAR_PACKAGEFILEMANAGER_CANTWRITE_PKGFILE' => PEAR_PACKAGEFILEMANAGER_CANTWRITE_PKGFILE,
  61.             'PEAR_PACKAGEFILEMANAGER_DEST_UNWRITABLE' => PEAR_PACKAGEFILEMANAGER_DEST_UNWRITABLE,
  62.             'PEAR_PACKAGEFILEMANAGER_CANTCOPY_PKGFILE' => PEAR_PACKAGEFILEMANAGER_CANTCOPY_PKGFILE,
  63.             'PEAR_PACKAGEFILEMANAGER_CANTOPEN_TMPPKGFILE' => PEAR_PACKAGEFILEMANAGER_CANTOPEN_TMPPKGFILE,
  64.             'PEAR_PACKAGEFILEMANAGER_PATH_DOESNT_EXIST' => PEAR_PACKAGEFILEMANAGER_PATH_DOESNT_EXIST,
  65.             'PEAR_PACKAGEFILEMANAGER_NOCVSENTRIES' => PEAR_PACKAGEFILEMANAGER_NOCVSENTRIES,
  66.             'PEAR_PACKAGEFILEMANAGER_DIR_DOESNT_EXIST' => PEAR_PACKAGEFILEMANAGER_DIR_DOESNT_EXIST,
  67.             'PEAR_PACKAGEFILEMANAGER_RUN_SETOPTIONS' => PEAR_PACKAGEFILEMANAGER_RUN_SETOPTIONS,
  68.             'PEAR_PACKAGEFILEMANAGER_NOPACKAGE' => PEAR_PACKAGEFILEMANAGER_NOPACKAGE,
  69.             'PEAR_PACKAGEFILEMANAGER_WRONG_MROLE' => PEAR_PACKAGEFILEMANAGER_WRONG_MROLE,
  70.             'PEAR_PACKAGEFILEMANAGER_NOSUMMARY' => PEAR_PACKAGEFILEMANAGER_NOSUMMARY,
  71.             'PEAR_PACKAGEFILEMANAGER_NODESC' => PEAR_PACKAGEFILEMANAGER_NODESC,
  72.             'PEAR_PACKAGEFILEMANAGER_ADD_MAINTAINERS' => PEAR_PACKAGEFILEMANAGER_ADD_MAINTAINERS,
  73.             'PEAR_PACKAGEFILEMANAGER_NO_FILES' => PEAR_PACKAGEFILEMANAGER_NO_FILES,
  74.             'PEAR_PACKAGEFILEMANAGER_IGNORED_EVERYTHING' => PEAR_PACKAGEFILEMANAGER_IGNORED_EVERYTHING,
  75.             'PEAR_PACKAGEFILEMANAGER_INVALID_PACKAGE' => PEAR_PACKAGEFILEMANAGER_INVALID_PACKAGE,
  76.             'PEAR_PACKAGEFILEMANAGER_INVALID_REPLACETYPE' => PEAR_PACKAGEFILEMANAGER_INVALID_REPLACETYPE,
  77.             'PEAR_PACKAGEFILEMANAGER_INVALID_ROLE' => PEAR_PACKAGEFILEMANAGER_INVALID_ROLE,
  78.             'PEAR_PACKAGEFILEMANAGER_PHP_NOT_PACKAGE' => PEAR_PACKAGEFILEMANAGER_PHP_NOT_PACKAGE
  79.         ));
  80.         return $codes[$code];
  81.     }
  82.  
  83.     function _stripWhitespace($str)
  84.     {
  85.         return preg_replace('/\\s+/', '', $str);
  86.     }
  87.  
  88.     function _methodExists($name) 
  89.     {
  90.         if (in_array(strtolower($name), get_class_methods($this->packagexml))) {
  91.             return true;
  92.         }
  93.         $this->assertTrue(false, 'method '. $name . ' not implemented in ' . get_class($this->packagexml));
  94.         return false;
  95.     }
  96.  
  97.     function errorHandler($errno, $errstr, $errfile, $errline) {
  98.         //die("$errstr in $errfile at line $errline: $errstr");
  99.         $this->errorOccured = true;
  100.         $this->assertTrue(false, "$errstr at line $errline, $errfile");
  101.     }
  102.  
  103.     function PEARerrorHandler($error) {
  104.         $this->assertEquals($this->_expectedCode, $error->getCode(),
  105.             $this->_testMethod . ' ' . $this->errorCodeToString($this->_expectedCode)
  106.             . ' actual: ' . $this->errorCodeToString($error->getCode()));
  107.         $this->assertEquals($this->_expectedMessage, $error->getMessage(), $this->_testMethod);
  108.         $this->errorThrown = 'true';
  109.     }
  110.     
  111.     function expectPEARError($method, $msg, $code = null)
  112.     {
  113.         $this->_expectedMessage = $msg;
  114.         $this->_expectedCode = $code;
  115.         $this->_testMethod = $method;
  116.     }
  117.     
  118.     function test_1()
  119.     {
  120.         if (!$this->_methodExists('_getRegExpableSearchString')) {
  121.             return;
  122.         }
  123.         $res1 = $this->packagexml->_getRegExpableSearchString('frog?.a*\\/[]-');
  124.         $this->assertFalse($this->errorThrown, 'error thrown');
  125.         $this->assertEquals('frog.\.a.*\\\\\\\\\\[\\]\\-', $res1, 'wrong regexp 1');
  126.         $res = $this->packagexml->_getRegExpableSearchString('frog?.a*\\/[]-' . DIRECTORY_SEPARATOR);
  127.         $this->assertFalse($this->errorThrown, 'error thrown');
  128.         $y = '\/';
  129.         if (DIRECTORY_SEPARATOR == '\\') {
  130.             $y = '\\\\';
  131.         }
  132.         $res1 .= $y;
  133.         $this->assertEquals("(?:.*$y$res1?.*|$res1.*)", $res, 'wrong regexp 2');
  134.     }
  135.     
  136.     function test_file_regexp()
  137.     {
  138.         if (!$this->_methodExists('_getRegExpableSearchString')) {
  139.             return;
  140.         }
  141.         $res = $this->packagexml->_getRegExpableSearchString('frog?-*.php');
  142.         $this->assertFalse($this->errorThrown, 'error thrown');
  143.         $this->assertEquals('frog.\-.*\.php', $res, 'wrong regexp');
  144.         $this->assertTrue(preg_match("/^$res$/", 'frog1-hairy.php'), 'did not match frog1-hairy.php');
  145.         $this->assertTrue(preg_match("/^$res$/", 'frog1-hairy/thingo.php'), 'did not match frog1-hairy/thingo.php');
  146.         $this->assertTrue(preg_match("/^$res$/", 'frog1-hairy\\thingo.php'), 'did not match frog1-hairy\\thingo.php');
  147.         $this->assertFalse(preg_match("/^$res$/", 'frog11-hairy.php'), 'matched frog11-hairy.php');
  148.     }
  149.     
  150.     function test_dir_regexp()
  151.     {
  152.         if (!$this->_methodExists('_getRegExpableSearchString')) {
  153.             return;
  154.         }
  155.         $res = $this->packagexml->_getRegExpableSearchString('frog/');
  156.         $this->assertFalse($this->errorThrown, 'error thrown');
  157.         $y = '\/';
  158.         if (DIRECTORY_SEPARATOR == '\\') {
  159.             $y = '\\\\';
  160.         }
  161.         $this->assertEquals('(?:.*' . $y . 'frog\\' . DIRECTORY_SEPARATOR .
  162.             '?.*|frog\\' . DIRECTORY_SEPARATOR . '.*)', $res, 'wrong regexp');
  163.         $this->assertTrue(preg_match("/^$res$/", 'frog' . DIRECTORY_SEPARATOR .
  164.             '1-hairy.php'), 'did not match frog//1-hairy.php');
  165.         $this->assertFalse(preg_match("/^$res$/", 'frog1-hairy' . DIRECTORY_SEPARATOR .
  166.             'thingo.php'), 'matched frog1-hairy//thingo.php');
  167.         $this->assertTrue(preg_match("/^$res$/", 'my' . DIRECTORY_SEPARATOR .
  168.             'frog' . DIRECTORY_SEPARATOR . 'thingo.php'), 'did not match my\\frog\\thingo.php');
  169.     }
  170. }
  171.  
  172. ?>
  173.