/**
  * Test unloading an initialized AutoLoader
  *
  * NOTE: as the unload function resets the autoloading function
  *       classes needed for phpunit will not be found anymore
  *       so the (default) autoloader for phpunit need to be loaded
  *       before calling this function
  * NOTE: tests are not executed in isolation so every AutoLoader
  *       created in other tests bloats the current include path.
  *       Therefor restore it completely where it is necessary.
  * NOTE: Watch out for the garbage collector at script ends.
  *       It calls the __destruct method so check what is set in it.
  *
  * @small
  */
 public function testUnloading()
 {
     $dip = ".:/usr/share/php:/usr/share/pear";
     set_include_path($dip);
     // restore default include path, see note above
     $dex = ".inc,.php";
     spl_autoload_extensions($dex);
     // restore default extensions
     $p = "src";
     $al = new \core\AutoLoader(true, $p, ",");
     // check extensions set
     $al->expand(".php,.inc.php,.class.php,.interface.php,.test.class.php");
     $this->assertEquals(".php,.inc.php,.class.php,.interface.php,.test.class.php", spl_autoload_extensions());
     // store some values to be checked later, get extension after expansion
     $al_oip = $al->getop();
     // store old include path
     $al_ip = $al->getp();
     // store include path
     $al_oe = $al->getoe();
     // store old extensions
     $al_e = $al->gete();
     // store extensions
     // unload
     $al->unload();
     // check old include path is nulled and current is restored correctly
     // as the include path is set during the phpunit bootstrap execution
     // the include path is not minimal anymore
     $this->assertNotRegExp("/" . preg_replace("/\\//", "\\\\/", $p . "\\\\/") . "/", $al_oip);
     $this->assertNULL($al->getop());
     // check include path is nulled and restored correctly
     $this->assertRegExp("/" . preg_replace("/\\//", "\\\\/", $p . "/") . "/", $al_ip);
     $this->assertNotEquals($al_ip, get_include_path());
     $this->assertNULL($al->getp());
     // check old extensions are nulled and restored correctly
     $this->assertEquals($al_oe, spl_autoload_extensions());
     $this->assertNULL($al->getoe());
     // check extensions are null and restored correctly (default: .inc,.php)
     $this->assertNotEquals($al_e, spl_autoload_extensions());
     $this->assertNULL($al->gete());
     // check the spl_autoload_fuctions() for closures bound to this class
     $rf = null;
     foreach (spl_autoload_functions() as $f) {
         $rf = new ReflectionFunction($f);
         if ($rf->isClosure() && !is_null($rf->getClosureThis())) {
             if (strncmp(gettype($rf->getClosureThis()), "object", "6") == 0) {
                 $this->assertNotEquals(get_class($rf->getClosureThis()), get_class($al));
             }
         }
     }
     $al = null;
 }
Esempio n. 2
0
<?php

// NOTE: no need to load the phpunit autoloader or token-stream directly
//       as the autoloader below is able to load classes represented
//       by their names, e.g. Class_Name masquerades Class/Name.php as
//       phpunit/token-stream does for, e.g. PHPUnit_Framework_TestCase
// auto loading project classes
// when running build script from .. prepend src directory
// AND modify the path the autoloader should search for project classes
//   as it defaults to '.' = 'src' for the build script
require_once "@php-dir-include@/core/autoloader.class.php";
// get an AutoLoader
$al = new core\AutoLoader(false, "@php-dir-include@");
// for phpunit, token, log and test classes
$al->expand(".test.class.php,.interface.php,.php");
// initialize $al manually to expand the suffix list first
$al->load();