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

  1. <?php
  2.  
  3. /**
  4.  * API Unit tests for PEAR_PackageFileManager package.
  5.  * 
  6.  * @version    $Id: PEAR_PackageFileManager_TestCase_addReplacement.php,v 1.1 2003/10/16 04:15:40 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_TestCase_addReplacement extends PHPUnit_TestCase
  17. {
  18.     /**
  19.      * A PEAR_PackageFileManager object
  20.      * @var        object
  21.      */
  22.     var $packagexml;
  23.  
  24.     function PEAR_PackageFileManager_TestCase_addReplacement($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.         $this->packagexml = new PEAR_PackageFileManager;
  36.         PEAR::pushErrorHandling(PEAR_ERROR_CALLBACK, array(&$this, 'PEARerrorHandler'));
  37.         $this->errorThrown = false;
  38.         $this->_expectedMessage = 'NO ERROR TRIGGERED';
  39.         $this->_expectedCode = -1;
  40.         $this->_testMethod = 'unknown';
  41.     }
  42.  
  43.     function tearDown()
  44.     {
  45.         unset($this->packagexml);
  46.         PEAR::popErrorHandling();
  47.     }
  48.  
  49.     function errorCodeToString($code)
  50.     {
  51.         $codes = array_flip(array(
  52.             'OOPS' => -1,
  53.             'PEAR_PACKAGEFILEMANAGER_NOSTATE' => PEAR_PACKAGEFILEMANAGER_NOSTATE,
  54.             'PEAR_PACKAGEFILEMANAGER_NOVERSION' => PEAR_PACKAGEFILEMANAGER_NOVERSION,
  55.             'PEAR_PACKAGEFILEMANAGER_NOPKGDIR' => PEAR_PACKAGEFILEMANAGER_NOPKGDIR,
  56.             'PEAR_PACKAGEFILEMANAGER_NOBASEDIR' => PEAR_PACKAGEFILEMANAGER_NOBASEDIR,
  57.             'PEAR_PACKAGEFILEMANAGER_GENERATOR_NOTFOUND' => PEAR_PACKAGEFILEMANAGER_GENERATOR_NOTFOUND,
  58.             'PEAR_PACKAGEFILEMANAGER_GENERATOR_NOTFOUND_ANYWHERE' => PEAR_PACKAGEFILEMANAGER_GENERATOR_NOTFOUND_ANYWHERE,
  59.             'PEAR_PACKAGEFILEMANAGER_CANTWRITE_PKGFILE' => PEAR_PACKAGEFILEMANAGER_CANTWRITE_PKGFILE,
  60.             'PEAR_PACKAGEFILEMANAGER_DEST_UNWRITABLE' => PEAR_PACKAGEFILEMANAGER_DEST_UNWRITABLE,
  61.             'PEAR_PACKAGEFILEMANAGER_CANTCOPY_PKGFILE' => PEAR_PACKAGEFILEMANAGER_CANTCOPY_PKGFILE,
  62.             'PEAR_PACKAGEFILEMANAGER_CANTOPEN_TMPPKGFILE' => PEAR_PACKAGEFILEMANAGER_CANTOPEN_TMPPKGFILE,
  63.             'PEAR_PACKAGEFILEMANAGER_PATH_DOESNT_EXIST' => PEAR_PACKAGEFILEMANAGER_PATH_DOESNT_EXIST,
  64.             'PEAR_PACKAGEFILEMANAGER_NOCVSENTRIES' => PEAR_PACKAGEFILEMANAGER_NOCVSENTRIES,
  65.             'PEAR_PACKAGEFILEMANAGER_DIR_DOESNT_EXIST' => PEAR_PACKAGEFILEMANAGER_DIR_DOESNT_EXIST,
  66.             'PEAR_PACKAGEFILEMANAGER_RUN_SETOPTIONS' => PEAR_PACKAGEFILEMANAGER_RUN_SETOPTIONS,
  67.             'PEAR_PACKAGEFILEMANAGER_NOPACKAGE' => PEAR_PACKAGEFILEMANAGER_NOPACKAGE,
  68.             'PEAR_PACKAGEFILEMANAGER_WRONG_MROLE' => PEAR_PACKAGEFILEMANAGER_WRONG_MROLE,
  69.             'PEAR_PACKAGEFILEMANAGER_NOSUMMARY' => PEAR_PACKAGEFILEMANAGER_NOSUMMARY,
  70.             'PEAR_PACKAGEFILEMANAGER_NODESC' => PEAR_PACKAGEFILEMANAGER_NODESC,
  71.             'PEAR_PACKAGEFILEMANAGER_ADD_MAINTAINERS' => PEAR_PACKAGEFILEMANAGER_ADD_MAINTAINERS,
  72.             'PEAR_PACKAGEFILEMANAGER_NO_FILES' => PEAR_PACKAGEFILEMANAGER_NO_FILES,
  73.             'PEAR_PACKAGEFILEMANAGER_IGNORED_EVERYTHING' => PEAR_PACKAGEFILEMANAGER_IGNORED_EVERYTHING,
  74.             'PEAR_PACKAGEFILEMANAGER_INVALID_PACKAGE' => PEAR_PACKAGEFILEMANAGER_INVALID_PACKAGE,
  75.             'PEAR_PACKAGEFILEMANAGER_INVALID_REPLACETYPE' => PEAR_PACKAGEFILEMANAGER_INVALID_REPLACETYPE,
  76.             'PEAR_PACKAGEFILEMANAGER_INVALID_ROLE' => PEAR_PACKAGEFILEMANAGER_INVALID_ROLE,
  77.             'PEAR_PACKAGEFILEMANAGER_PHP_NOT_PACKAGE' => PEAR_PACKAGEFILEMANAGER_PHP_NOT_PACKAGE
  78.         ));
  79.         return $codes[$code];
  80.     }
  81.  
  82.     function _stripWhitespace($str)
  83.     {
  84.         return preg_replace('/\\s+/', '', $str);
  85.     }
  86.  
  87.     function _methodExists($name) 
  88.     {
  89.         if (in_array(strtolower($name), get_class_methods($this->packagexml))) {
  90.             return true;
  91.         }
  92.         $this->assertTrue(false, 'method '. $name . ' not implemented in ' . get_class($this->packagexml));
  93.         return false;
  94.     }
  95.  
  96.     function errorHandler($errno, $errstr, $errfile, $errline) {
  97.         //die("$errstr in $errfile at line $errline: $errstr");
  98.         $this->errorOccured = true;
  99.         $this->assertTrue(false, "$errstr at line $errline, $errfile");
  100.     }
  101.  
  102.     function PEARerrorHandler($error) {
  103.         $this->assertEquals($this->_expectedCode, $error->getCode(),
  104.             $this->_testMethod . ' ' . $this->errorCodeToString($this->_expectedCode)
  105.             . ' actual: ' . $this->errorCodeToString($error->getCode()));
  106.         $this->assertEquals($this->_expectedMessage, $error->getMessage(), $this->_testMethod);
  107.         $this->errorThrown = 'true';
  108.     }
  109.     
  110.     function expectPEARError($method, $msg, $code = null)
  111.     {
  112.         $this->_expectedMessage = $msg;
  113.         $this->_expectedCode = $code;
  114.         $this->_testMethod = $method;
  115.     }
  116.     
  117.     function test_invalid_replacement()
  118.     {
  119.         if (!$this->_methodExists('setOptions')) {
  120.             return;
  121.         }
  122.         if (!$this->_methodExists('addReplacement')) {
  123.             return;
  124.         }
  125.         $this->packagexml->setOptions(array('state' => 'alpha', 'version' => '1.0',
  126.             'packagedirectory' => dirname(__FILE__), 'baseinstalldir' => 'Foo',
  127.             'packagefile' => 'test1_package.xml',
  128.             'filelistgenerator' => 'File'));
  129.         $this->assertFalse($this->errorThrown, 'error thrown');
  130.         $this->expectPEARError('invalid role test',
  131.             'PEAR_PackageFileManager Error: Replacement Type must be one of "' .
  132.             implode(PEAR_Common::getReplacementTypes(), ', ') .
  133.             '", was passed "ribbit"',
  134.             PEAR_PACKAGEFILEMANAGER_INVALID_REPLACETYPE
  135.         );
  136.         $this->packagexml->addReplacement('peeber.php', 'ribbit', 'tadpole', 'frog');
  137.         $this->assertEquals('true', $this->errorThrown, 'error not thrown');
  138.         $this->assertTrue(empty($this->packagexml->_options['replacements']),
  139.             'replacements was set, should not be');
  140.     }
  141.     
  142.     function test_valid_replacement()
  143.     {
  144.         if (!$this->_methodExists('setOptions')) {
  145.             return;
  146.         }
  147.         if (!$this->_methodExists('addReplacement')) {
  148.             return;
  149.         }
  150.         $this->packagexml->setOptions(array('state' => 'alpha', 'version' => '1.0',
  151.             'packagedirectory' => dirname(__FILE__), 'baseinstalldir' => 'Foo',
  152.             'packagefile' => 'test1_package.xml',
  153.             'filelistgenerator' => 'File'));
  154.         $this->assertFalse($this->errorThrown, 'error thrown');
  155.         $this->packagexml->addReplacement('peeber.php', 'package-info', '@version@', 'version');
  156.         $this->assertFalse($this->errorThrown, 'error thrown');
  157.         $this->assertEquals(array(array('type' => 'package-info', 'from' => '@version@', 'to' => 'version')),
  158.             $this->packagexml->_options['replacements']['peeber.php'],
  159.             'extension was not set, should be');
  160.     }
  161. }
  162.  
  163. ?>
  164.