2013-02-10 121 views
1

首先,我對Zend Framework非常陌生。實際上在範圍內只有大約1天的時間。我對PHPUnit也絕對愚蠢。我不認爲沒有單元測試就使用Zend Framework是可行的,並且需要在短時間內瞭解如何使用。如果你能幫我解決這個問題,我將不勝感激。這就是我所做的。Zend Framework 2 - PHPUnit - 單元測試 - 未執行任何測試以及想法

我正在閱讀文檔,我發現很難遵循。我不知道它是否僅僅是我或者它是否是未處理的文檔。我覺得有些東西不見了。

我也安裝了Composer。

這是我做的,安裝PEAR後我安裝了PHPUnit。現在,當我把phpunit一些地方給出了一個選項列表。所以我認爲它做得很好。

我從GitHub獲得了Zend Skeleton Application。得到了Zend FrameworkComposer

我經歷了文檔。我覺得我的頭會打擊!我的問題是特別是單元測試。問題在於http://framework.zend.com/manual/2.1/en/user-guide/unit-testing.html的內容。

<?xml version="1.0" encoding="UTF-8"?> 
<phpunit bootstrap="Bootstrap.php"> 
    <testsuites> 
     <testsuite name="zf2tutorial"> 
      <directory>./ApplicationTest</directory> 
     </testsuite> 
    </testsuites> 
</phpunit> 

此文件位於zf2-tutorial/module/Application/test。我的問題是,是不是Bootstrap.php文件位於zf2-tutorial/module/Application/test

該文件說創建一個Bootstrap.php,但在{Application Folder}/test。有了它,當我運行phpunitzf2-tutorial/module/Application/test它說文件zf2-tutorial/module/Application/test/Bootstrap.php無法打開。

然後,我將XML中的Bootstrap.php更改爲/../../../test/Bootstrap.php(正如由XML文件創建的文檔所建議的),從而解決了錯誤。但測試計數爲零:No tests executed!

下面是測試類:

<?php 
namespace ApplicationTest\Controller; 

use Zend\Test\PHPUnit\Controller\AbstractHttpControllerTestCase; 
//use PHPUnit_Framework_TestCase; 
//I also tried PHPUnit_Framework_TestCase instead of AbstractHttpControllerTestCase 
class IndexControllerTest extends AbstractHttpControllerTestCase 
{ 
    public function setUp() 
    { 
     $this->setApplicationConfig(
      //include '/path/to/application/config/test/application.config.php' 
      include __DIR__ . '/../../../../config/application.config.php' 
     ); 

     parent::setUp(); 
    } 
    /** 
    * @test 
    */ 
    public function testIndexActionCanBeAccessed(){ 
     $this->dispatch('/'); 
     $this->assertResponseStatusCode(200); 
     $this->assertModule('application'); 
     $this->assertControllerName('application_index'); 
     $this->assertControllerClass('IndexController'); 
     $this->assertMatchedRouteName('home'); 
    } 
} 

include '/path/to/application/config/test/application.config.php' ???這條奇特的道路我改名爲include __DIR__ . '/../../../../config/application.config.php'

該文件位於測試文件夾zf-tutorial/module/Application/test/ApplicationTest/Controller內。

在最後的事情,No tests executed!是PHP單位不得不說的。

我做錯了什麼?我能做些什麼才能解決問題。

我也想在這個主題上有一個關於碰撞的課程(鏈接到博客或類似的東西),我知道亞馬遜甚至沒有一本關於ZF2的書。如果你能幫我一把,我會很高興。這真的很緊急!

預先感謝您!

增加:: 我錯過了測試文件夾中的Bootstrap.php的製作。

新的XML是這樣

<?xml version="1.0" encoding="UTF-8"?> 
<phpunit bootstrap="Bootstrap.php"> 
    <testsuites> 
     <testsuite name="zf2tutorial"> 
      <directory>./ApplicationTest</directory> 
     </testsuite> 
    </testsuites> 
</phpunit> 

的bootstrap.php中看起來像這樣

<?php 

chdir(dirname(__DIR__)); 
include __DIR__ . '/../../../init_autoloader.php'; 

init_autoloader.php進行了修改,找到zendframework庫。

但還是PHPUnit的說:No tests executed!

+0

我有這個完全相同的問題。使用我的Mac上的PHPUnit版本,測試運行。隨着Linux上的版本,我得到上述錯誤。看起來真的不清楚究竟是什麼區別,以及爲什麼一個操作系統運行它而另一個不運行。 – 2013-09-05 04:51:08

回答

3

變化埃文Coury寫的phpunit.xml

<?xml version="1.0" encoding="UTF-8"?> 
<phpunit bootstrap="Bootstrap.php"> 
    <testsuites> 
     <testsuite name="zf2tutorial"> 
      <directory>./</directory> 
     </testsuite> 
    </testsuites> 
</phpunit> 
0

使用的bootstrap.php設在User Guide

<?php 
namespace HelloworldTest; //Change this namespace for your test 

use Zend\Loader\AutoloaderFactory; 
use Zend\Mvc\Service\ServiceManagerConfig; 
use Zend\ServiceManager\ServiceManager; 
use Zend\Stdlib\ArrayUtils; 
use RuntimeException; 

error_reporting(E_ALL | E_STRICT); 
chdir(__DIR__); 

class Bootstrap 
{ 
    protected static $serviceManager; 
    protected static $config; 
    protected static $bootstrap; 

    public static function init() 
    { 
     // Load the user-defined test configuration file, if it exists; otherwise, load 
     if (is_readable(__DIR__ . '/TestConfig.php')) { 
      $testConfig = include __DIR__ . '/TestConfig.php'; 
     } else { 
      $testConfig = include __DIR__ . '/TestConfig.php.dist'; 
     } 

     $zf2ModulePaths = array(); 

     if (isset($testConfig['module_listener_options']['module_paths'])) { 
      $modulePaths = $testConfig['module_listener_options']['module_paths']; 
      foreach ($modulePaths as $modulePath) { 
       if (($path = static::findParentPath($modulePath))) { 
        $zf2ModulePaths[] = $path; 
       } 
      } 
     } 

     $zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR; 
     $zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : ''); 

     static::initAutoloader(); 

     // use ModuleManager to load this module and it's dependencies 
     $baseConfig = array(
      'module_listener_options' => array(
       'module_paths' => explode(PATH_SEPARATOR, $zf2ModulePaths), 
      ), 
     ); 

     $config = ArrayUtils::merge($baseConfig, $testConfig); 

     $serviceManager = new ServiceManager(new ServiceManagerConfig()); 
     $serviceManager->setService('ApplicationConfig', $config); 
     $serviceManager->get('ModuleManager')->loadModules(); 

     static::$serviceManager = $serviceManager; 
     static::$config = $config; 
    } 

    public static function getServiceManager() 
    { 
     return static::$serviceManager; 
    } 

    public static function getConfig() 
    { 
     return static::$config; 
    } 

    protected static function initAutoloader() 
    { 
     $vendorPath = static::findParentPath('vendor'); 

     if (is_readable($vendorPath . '/autoload.php')) { 
      $loader = include $vendorPath . '/autoload.php'; 
     } else { 
      $zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false)); 

      if (!$zf2Path) { 
       throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.'); 
      } 

      include $zf2Path . '/Zend/Loader/AutoloaderFactory.php'; 

     } 

     AutoloaderFactory::factory(array(
      'Zend\Loader\StandardAutoloader' => array(
       'autoregister_zf' => true, 
       'namespaces' => array(
        __NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__, 
       ), 
      ), 
     )); 
    } 

    protected static function findParentPath($path) 
    { 
     $dir = __DIR__; 
     $previousDir = '.'; 
     while (!is_dir($dir . '/' . $path)) { 
      $dir = dirname($dir); 
      if ($previousDir === $dir) return false; 
      $previousDir = $dir; 
     } 
     return $dir . '/' . $path; 
    } 
} 

Bootstrap::init(); 

記住,命名空間中定義必須在phpunit.xml.dist上提供Bootstrap.php:

<?xml version="1.0" encoding="UTF-8"?> 

<phpunit bootstrap="Bootstrap.php"> 
    <testsuites> 
     <testsuite name="zf2tutorial"> 
     <directory>./HelloworldTest</directory> 
     </testsuite> 
    </testsuites> 
</phpunit>