2013-02-10 122 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>