2010-08-24 159 views
0

这个让我难住。我一直在使用PHPUnit几个月,所以我不是那么绿......但我期待着指出我正在犯的明显错误的方向!如果我从浏览器运行“app”,下面概述的初始化过程工作正常 - 但PHPUnit正在窒息......任何人都可以让我摆脱我的痛苦吗?PHPUnit包含路径问题

我试图测试一个自制的MVC,为研究目的。它遵循典型的ZF布局。 这里的索引页:

include './../library/SKL/Application.php'; 
$SKL_Application = new SKL_Application(); 
$SKL_Application->initialise('./../application/configs/config.ini'); 

这里的应用程序类(初期...)

include 'bootstrap.php'; 

class SKL_Application { 

    /** 
    * initialises the application 
    */ 
    public function initialise($file) { 
     $this->processBootstrap(); 
     //purely to test PHPUnit is working as expected 
     return true; 
    } 

    /** 
    * iterates over bootstrap class and executes 
    * all methods prefixed with "_init" 
    */ 
    private function processBootstrap() { 
     $Bootstrap = new Bootstrap(); 
     $bootstrap_methods = get_class_methods($Bootstrap); 

     foreach ($bootstrap_methods as $method) { 
      if(substr($method,0,5) == '_init'){ 
       $bootstrap->$method(); 
      } 
     } 
     return true; 
    } 
} 

这里的测试:

require_once dirname(__FILE__).'/../../../public/bootstrap.php'; 
require_once dirname(__FILE__).'/../../../library/SKL/Application.php'; 


class SKL_ApplicationTest extends PHPUnit_Framework_TestCase { 
    protected $object; 


    protected function setUp() { 
     $this->object = new SKL_Application(); 
    } 

    /** 
    * Tears down the fixture, for example, closes a network connection. 
    * This method is called after a test is executed. 
    */ 
    protected function tearDown() { 
    } 


    public function testInitialise() { 
     $this->assertType('boolean',$this->object->initialise()); 

    } 

} 

,但我一直在第一障碍绊倒!

PHP Warning: include(bootstrap.php): failed to open stream: 
No such file or directory in path\to\files\SKL\Application.php on line 9 

有什么想法?

回答

0

感谢拉乌尔·杜克给我在正确的方向一推,这里的地方我到至今

1 - 应用程序的根目录添加到包括路径

2 - 让所有列入相对于应用程序根目录的路径

3 - 在您的单元测试中包含执行相同功能的文件,但在包含它时补偿相对位置。我只是在文件的目录位置上使用了realpath()。

我现在面临的问题是,织补事情看不到任何额外的文件,我试图通过它。

所以,我试图测试一个配置类,它将动态地解析各种文件类型。目录结构是这样的:

Application_ConfigTest.php 
config.ini 

第一个测试:

public function testParseFile() { 
    $this->assertType('array',$this->object->parseFile('config.ini')); 
} 

错误:

failed to open stream: No such file or directory 

跆拳道?它在相同的目录测试类...

我通过提供配置绝对(即文件结构)路径解决了这个file.Can任何人向我解释的PHPUnit如何解决它的路径,还是因为测试类本身包含在其他地方,渲染相对路径毫无意义?

1

使用include_once或更好的require_once而不是include将bootstrap.php包含在应用程序类文件中。尽管已经加载了include,但是由于它明显不在包含路径中,因此会出现此错误。

+0

我想通了 - 我必须将所需的文件位置追加到包含路径。应用程序本身只是从相对路径中提取文件,但这在PHPUnit中不起作用 - 所以我只是将它们设置在单独的文件中并将文件包含在测试中。 – sunwukung 2010-08-24 09:52:34