在 Zend Framework 2 中为自己的模块执行 PHPUnit 测试时,我似乎有一点问题。
操作系统:Mac OS 10.8.3
Zend Framework 2.1.4
PHPUnit 版本 3.7.19(通过 pear 安装)
PHP 版本 5.3.15(启用 xdebug,版本 2.1.3)
我按照 Zend 指南的说明创建了模块“Album”(http://framework.zend.com/manual/2.1/en/user-guide/modules.html)
我不想将单元测试放入模块文件夹中,而是希望将它们全部放在一个集中的文件夹中。这是我的应用程序的基本结构:
-config
-data
-module
-public
-tests
--Modules
---Album
----AlbumTest.php
--Bootstrap.php
--phpunit.xml
-vendor
...(license, readme, composers and init_autoloader.php)
-> Module Album 的测试位于“tests”中的“Modules/Album”文件夹中。文件夹“tests”还包含 Bootstrap.php 和 phpunit.xml。
以下是文件的内容:
引导程序.php
<?php
/*
* Set error reporting to the level to which Zend Framework code must comply.
*/
error_reporting( E_ALL | E_STRICT );
/*
* The first run required me to fix some constants
*/
defined('TESTS_ZEND_FORM_RECAPTCHA_PUBLIC_KEY') || define('TESTS_ZEND_FORM_RECAPTCHA_PUBLIC_KEY', 'public key');
defined('TESTS_ZEND_FORM_RECAPTCHA_PRIVATE_KEY') || define('TESTS_ZEND_FORM_RECAPTCHA_PRIVATE_KEY', 'private key');
defined('TESTS_ZEND_LDAP_PRINCIPAL_NAME') || define('TESTS_ZEND_LDAP_PRINCIPAL_NAME', 'someUser@example.com');
defined('TESTS_ZEND_LDAP_ALT_USERNAME') || define('TESTS_ZEND_LDAP_ALT_USERNAME', 'anotherUser');
chdir(dirname(__DIR__));
/*
* autoload the application
*/
include __DIR__ . '/../init_autoloader.php';
Zend\Mvc\Application::init(include 'config/test.config.php');
phpunit.xml
<phpunit bootstrap="./Bootstrap.php" colors="true">
<testsuites>
<testsuite name="Zend Module Tests">
<directory>./Modules</directory>
</testsuite>
</testsuites>
</phpunit>
专辑测试.php
<?php
namespace AlbumTest\Model;
use Album\Model\Album;
use PHPUnit_Framework_TestCase;
/**
* @category Module
* @package Album
* @subpackage UnitTests
* @group Module_Album
*/
class AlbumTest extends PHPUnit_Framework_TestCase {
public function testAlbumInitialState() {
$album = new Album();
$this->assertNull($album->artist, '"artist" should initially be null');
$this->assertNull($album->id, '"id" should initially be null');
$this->assertNull($album->title, '"title" should initially be null');
}
public function testExchangeArraySetsPropertiesCorrectly() {
$album = new Album();
$data = array('artist' => 'some artist',
'id' => 123,
'title' => 'some title');
$album->exchangeArray($data);
$this->assertSame($data['artist'], $album->artist, '"artist" was not set correctly');
$this->assertSame($data['id'], $album->id, '"id" was not set correctly');
$this->assertSame($data['title'], $album->title, '"title" was not set correctly');
}
public function testExchangeArraySetsPropertiesToNullIfKeysAreNotPresent() {
$album = new Album();
$album->exchangeArray(array('artist' => 'some artist',
'id' => 123,
'title' => 'some title'));
$album->exchangeArray(array());
$this->assertNull($album->artist, '"artist" should have defaulted to null');
$this->assertNull($album->id, '"id" should have defaulted to null');
$this->assertNull($album->title, '"title" should have defaulted to null');
}
public function testGetArrayCopyReturnsAnArrayWithPropertyValues() {
$album = new Album();
$data = array('artist' => 'some artist',
'id' => 123,
'title' => 'some title');
$album->exchangeArray($data);
$copyArray = $album->getArrayCopy();
$this->assertSame($data['artist'], $copyArray['artist'], '"artist" was not set correctly');
$this->assertSame($data['id'], $copyArray['id'], '"id" was not set correctly');
$this->assertSame($data['title'], $copyArray['title'], '"title" was not set correctly');
}
public function testInputFiltersAreSetCorrectly() {
$album = new Album();
$inputFilter = $album->getInputFilter();
$this->assertSame(3, $inputFilter->count());
$this->assertTrue($inputFilter->has('artist'));
$this->assertTrue($inputFilter->has('id'));
$this->assertTrue($inputFilter->has('title'));
}
}
NetBeans 知道,在哪里可以找到 phpunit 二进制文件:(我想在这里发布图像,虽然没有声誉 :),我会尝试解释它)在选项中配置的路径 - php - 单元测试
/usr /local/pear/bin/phpunit
和 /usr/local/pear/bin/phpunit-skelgen
在项目的属性中,我设置了“使用 XML 配置”并将路径粘贴到 phpunit.xml。我还检查了“在运行测试之前询问测试组”——我在“Module_Album”组上方进行了测试——这样我确保 PHPUnit 找到了正确的测试。
我右键单击项目并选择“测试”,它显示组“模块_专辑”,我检查它并单击“确定”。它运行一些东西,告诉我它找到了正确的 phpunit.xml(“从 FULL_PATH/tests/phpunit.xml 读取的配置”)。运行后,它告诉我,它没有执行任何测试。
这是 NetBeans 中的完整输出:
PHPUnit 3.7.19 by Sebastian Bergmann.
Configuration read from FULL_PATH/tests/phpunit.xml
Time: 9 seconds, Memory: 348.25Mb
[2KNo tests executed!
[2K
Generating code coverage report in Clover XML format ... done
无论如何,我可以通过 shell(终端)成功地做同样的事情。没关系,如果我直接挂载测试目录并运行phpunit,使用phpunit二进制文件的完整路径(以确保我不使用不同的版本),指定phpunit.xml的完整路径,等等。以下是一些示例:
phpunit
phpunit -c FULL_PATH/tests/phpunit.xml
/usr/local/pear/bin/phpunit -c FULL_PATH/tests/phpunit.xml
所有这些命令都给了我我所期望的:
PHPUnit 3.7.19 by Sebastian Bergmann.
Configuration read from FULL_PATH/tests/phpunit.xml
.....
Time: 0 seconds, Memory: 12.75Mb
OK (5 tests, 16 assertions)
- 我不明白,为什么这在 shell 中有效,但不能通过 NetBeans
- NetBeans 使用 350MB,而在 shell 中仅使用 12.75MB
- 如果我删除“在运行测试之前询问测试组”选项,它似乎会尝试运行所有 ZendTest。不知道这是怎么可能的,从 phpunit.xml 它不应该找到它们。
任何帮助表示赞赏,谢谢!