Unittests
[ class tree: Unittests ] [ index: Unittests ] [ all elements ]

Source for file LoaderTest.php

Documentation is available at LoaderTest.php

  1. <?php
  2. /**
  3.  * Console Library
  4.  *
  5.  * LICENSE
  6.  *
  7.  * "THE BEER-WARE LICENSE" (Revision 42):
  8.  * "Sven Strittmatter" <ausserirdisch@sven-space.de> wrote this file.
  9.  * As long as you retain this notice you can do whatever you want with
  10.  * this stuff. If we meet some day, and you think this stuff is worth it,
  11.  * you can buy me a beer in return.
  12.  *
  13.  * $Author$
  14.  * $Revisio$
  15.  *
  16.  * @category    Console
  17.  * @package     Unittests
  18.  * @copyright   Copyright (c) 2009 Sven Strittmatter
  19.  */
  20.  
  21. // Call Console_LoaderTest::main() if this source file is executed directly.
  22. if (!defined('PHPUnit_MAIN_METHOD')) {
  23.     define('PHPUnit_MAIN_METHOD''Console_LoaderTest::main');
  24. }
  25.  
  26. /**
  27.  * @see TestHelper
  28.  */
  29. require_once(dirname(__FILE__'/../TestHelper.php');
  30.  
  31. /**
  32.  * @see Console_Loader
  33.  */
  34. require_once('Console/Loader.php');
  35.  
  36.  
  37. /**
  38.  * @category   Console
  39.  * @package    Unittests
  40.  * @copyright  Copyright (c) 2009 Sven Strittmatter
  41.  */
  42. class Console_LoaderTest extends PHPUnit_Framework_TestCase {
  43.     private $includePath;
  44.     private $error;
  45.     private $errorHandler;
  46.  
  47.     /**
  48.      * Runs the test methods of this class.
  49.      *
  50.      * @return void 
  51.      */
  52.     public static function main({
  53.         require_once('PHPUnit/TextUI/TestRunner.php');
  54.         $suite  new PHPUnit_Framework_TestSuite("Console_LoaderTest");
  55.         $result PHPUnit_TextUI_TestRunner::run($suite);
  56.     }
  57.  
  58.     public function setUp({
  59.         // Store original include_path
  60.         $this->includePath  get_include_path();
  61.         $this->error        null;
  62.         $this->errorHandler null;
  63.     }
  64.  
  65.     public function tearDown({
  66.         if ($this->errorHandler !== null{
  67.             restore_error_handler();
  68.         }
  69.  
  70.         // Retore original include_path
  71.         set_include_path($this->includePath);
  72.     }
  73.  
  74.     /**
  75.      * Tests that a class can be loaded from a well-formed PHP file
  76.      */
  77.     public function testLoadClass({
  78.         try {
  79.             Console_Loader::loadClass('Console_Application');
  80.  
  81.             if (!class_exists('Console_Application'false)) {
  82.                 $this->file('Loading of class should not fail!');
  83.             }
  84.         catch(Exception $e{
  85.             echo $e;
  86.             $this->fail('Loading of existent class should not throw exception!');
  87.         }
  88.     }
  89.  
  90.     public function testLoadInterface({
  91.         try {
  92.             Console_Loader::loadClass('Console_Application_Interface');
  93.  
  94.             if (!interface_exists('Console_Application_Interface'false)) {
  95.                 $this->file('Loading of class should not fail!');
  96.             }
  97.         catch(Exception $e{
  98.             $this->fail('Loading of existent class should not throw exception!');
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Tests that the security filter catches illegal characters.
  104.      */
  105.     public function testThrowsExceptionOnLoadIllegalFilename({
  106.         try {
  107.             Console_Loader::loadClass('/path/:to/@danger');
  108.             $this->fail('Console_Exception was expected but never thrown.');
  109.         catch (Console_Exception $e{
  110.             $this->assertRegExp('/Illegal character in filename: (.*)/i'$e->getMessage());
  111.         }
  112.     }
  113.  
  114.     /**
  115.      * Tests that isReadable works
  116.      */
  117.     public function testIsFileReadable({
  118.         $this->assertTrue(Console_Loader::isReadable(__FILE__));
  119.         $this->assertFalse(Console_Loader::isReadable(__FILE__ . '.foobaar'));
  120.         // test that a file in include_path gets loaded, see ZF-2985
  121.         $this->assertTrue(Console_Loader::isReadable('Console/Application/Interface.php'));
  122.     }
  123.  
  124.     public function testThrowsExceptionOnLoadNotReadableFile({
  125.         $this->setExpectedException('Console_Exception');
  126.         Console_Loader::loadClass('nonExistingFile');
  127.     }
  128.  
  129.     /**
  130.      * With type either class or interface is ment!
  131.      */
  132.         try {
  133.             Console_Loader::loadClass('Fixtures_BadLoadableClass');
  134.             $this->fail('Console_Exception was expected but never thrown.');
  135.         catch (Console_Exception $e{
  136.             $this->assertRegExp('/Class or interface \'(.*)\' was not found in the file \'(.*)\'/i'$e->getMessage());
  137.         }
  138.     }
  139.  
  140.     public function testFileNameIsValid({
  141.         $this->assertTrue(Console_Loader::isFileNameValid('this/is/a/valid/name'));
  142.         $this->assertTrue(Console_Loader::isFileNameValid('This_Should_Be_Valid_2'));
  143.         $this->assertFalse(Console_Loader::isFileNameValid(' # this is not $ valid'));
  144.     }
  145. }
  146.  
  147. // Call Console_LoaderTest::main() if this source file is executed directly.
  148. if (PHPUnit_MAIN_METHOD === 'Console_LoaderTest::main'{
  149. }

Documentation generated on Mon, 17 Aug 2009 15:52:50 +0200 by phpDocumentor 1.4.2