我有一个用Zend Framework编写的PHP应用程序.它使用Phing构建系统,使用PHPUnit进行单元测试.所有这些部件都有配置设置.Zend使用application.xml,Phing使用build.xml和可选的一些build.properties,以及PHPUnit使用phpunit.xml.
但是,我在哪里存储所有三个组件所需的信息?想想数据库配置(例如密码).
在我的例子中,application.xml有各种部分(开发,登台,生产)都具有不同的数据库配置.我最近在我的应用程序中集成了一个ORM,现在我想对我的模型进行单元测试.所以我有第四个数据库(单元测试),由PHPUnit使用.
PHPUnit可以处理夹具数据,但不能处理数据库架构.所以,我在想我会写一个Phing构建目标,它将数据库模式从生产或登台复制到unittest数据库.这样我就可以获得额外的好处,甚至可以对我的数据库迁移脚本进行单元测试.但为了做到这一点,Phing需要同时访问多个数据库.
我的第一个直觉就是将所有四个数据库的所有配置都放入其中build.properties并让Phing简单地生成application.xml和phpunit.xml.但是,这感觉吧,肮脏的有一个生成系统生成的配置文件.
什么是最好的解决方案?或者我应该简单地复制配置细节而不用担心太多?
思考
我可以简单地复制它们.它只是一些设置,它们不应经常更改.但我敢打赌,当他们改变时,我会忘记重复(因为它不经常发生).共享参数包括:
我正在尝试为我的Yii项目构建单元测试.
问题:MySQL数据库.我不希望每次运行测试时都运行MySQL数据库,因为它很慢,不可靠,也许某些团队成员没有设置等等.
似乎有一种方法可以在内存中使用SQLite DB并使用它,但Yii生成的SQL似乎不像SQL上那样在MySQL上运行.我收到很多错误.
简而言之:我想在内存中模拟一个MySQL数据库.
我怎样才能做到这一点?
获取代码覆盖率报告时,白名单无法正常工作.我正在使用Zend Framework,我的Zend文件也在项目快速入门中的库目录中.
我正在从Netbeans 7.0.1运行PHPUnit 3.6
我只是想看看课程的覆盖范围MyLib.
<whitelist>
<directory suffix=".php">../../library/MyLib</directory>
</whitelist>
Run Code Online (Sandbox Code Playgroud)
但在报告中,我似乎得到了几个Zend文件的百分比值,例如.Zend_Controller_Front,Zend_Loader_Autoloader等等.
显然我没有为这些课程编写任何测试,因此我有0%的覆盖率也就不足为奇了.
我错过了什么?
这是我正在编写测试套件的类的构造函数(它扩展了mysqli):
function __construct(Config $c)
{
// store config file
$this->config = $c;
// do mysqli constructor
parent::__construct(
$this->config['db_host'],
$this->config['db_user'],
$this->config['db_pass'],
$this->config['db_dbname']
);
}
Run Code Online (Sandbox Code Playgroud)
Config传递给构造函数的类实现了arrayaccessphp内置的接口:
class Config implements arrayaccess{...}
Run Code Online (Sandbox Code Playgroud)
我如何模拟/存根Config对象?我应该使用哪个以及为什么?
提前致谢!
我正在尝试使用vagrant和默认的ubuntu 10.04 64位计算机将我的开发环境(symfony2应用程序)从我的Windows 7 localhost移动到虚拟机.一切都已建立,几乎可以运作,但有一件事困扰着我:
当我运行ant并执行phpunit时,我在执行自制引导时遇到以下错误:
stty: standard input: Invalid argument
Run Code Online (Sandbox Code Playgroud)
我可以将问题缩小到以下代码行,执行symfony cache:warmup命令:
executeCommand($application, "cache:warmup");
Run Code Online (Sandbox Code Playgroud)
这将执行以下命令:
php app/console -e test -q cache:warmup
Run Code Online (Sandbox Code Playgroud)
没有ant的运行phpunit工作正常,没有executeCommand行运行ant也是如此.
我读了一些关于这个stty的错误,抬头~/.bashrc,~./profile,/etc/bash.bashrc,/etc/profile以及/root/.bashrc和/root/.profile没有找到像TTY或stty的东西.所以我不知道我可以删除什么使它工作.
我有点卡住,因为我需要缓存预热,无法弄清楚出了什么问题.
我正在尝试使用vfsStream来模拟文件系统操作(实际上是从php://输入读取),但缺乏体面的文档和示例实际上是在妨碍我.
我正在测试的类中的相关代码如下:
class RequestBody implements iface\request\RequestBody
{
const
REQ_PATH = 'php://input',
protected
$requestHandle = false;
/**
* Obtain a handle to the request body
*
* @return resource a file pointer resource on success, or <b>FALSE</b> on error.
*/
protected function getHandle ()
{
if (empty ($this -> requestHandle))
{
$this -> requestHandle = fopen (static::REQ_PATH, 'rb');
}
return $this -> requestHandle;
}
}
Run Code Online (Sandbox Code Playgroud)
我在PHPUnit测试中使用的设置如下:
protected function configureMock ()
{
$mock = $this -> getMockBuilder ('\gordian\reefknot\http\request\RequestBody');
$mock -> setConstructorArgs (array …Run Code Online (Sandbox Code Playgroud) 在Symfony2中,使用此类设置,如何测试每个节点是否在Configuration类中定义,以及它们的值是否已正确配置.
要测试的课程
# My\Bundle\DependencyInjection\Configuration.php
class Configuration implements ConfigurationInterface
{
/**
* {@inheritDoc}
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$treeBuilder->root('my_bundle')
->children()
->scalarNode("scalar")->defaultValue("defaultValue")->end()
->arrayNode("arrayNode")
->children()
->scalarNode("val1")->defaultValue("defaultValue1")->end()
->scalarNode("val2")->defaultValue("defaultValue2")->end()
->end()
->end()
->end()
;
return $treeBuilder;
}
}
以下是我想在单元测试中做的断言:
我试图以节点的形式访问节点,但它似乎不起作用.此外,TreeBuilder似乎没有给我们将配置作为数组获取的可能性,除非它们由bundle扩展加载.
测试
# My\Bundle\Tests\DependencyInjection\ConfigurationTest.php
$configuration = $this->getConfiguration();
$treeBuilder = $configuration->getConfigTreeBuilder();
$this->assertInstanceOf("Symfony\Component\Config\Definition\Builder\TreeBuilder", $treeBuilder);
// How to access the treebuilder's nodes ? … 我在尝试对使用ZfcUser进行身份验证的操作进行单元测试时遇到问题.我需要一些方法来模拟ZfcUser Controller插件,但我不太确定如何做到这一点.我已成功地为表格和模型生成了一些单元测试,但是控制器需要大量注入的对象并导致问题.有谁知道如何设置ZfcUser模拟器来成功测试控制器?
这是我的测试(从ZF2教程复制):
<?php
namespace SmsTest\Controller;
use SmsTest\Bootstrap;
use Sms\Controller\SmsController;
use Zend\Http\Request;
use Zend\Http\Response;
use Zend\Mvc\MvcEvent;
use Zend\Mvc\Router\RouteMatch;
use Zend\Mvc\Router\Http\TreeRouteStack as HttpRouter;
use PHPUnit_Framework_TestCase;
class SmsControllerTest extends PHPUnit_Framework_TestCase
{
protected $controller;
protected $request;
protected $response;
protected $routeMatch;
protected $event;
protected function setUp()
{
$serviceManager = Bootstrap::getServiceManager();
$this->controller = new SmsController();
$this->request = new Request();
$this->routeMatch = new RouteMatch(array('controller' => 'index'));
$this->event = new MvcEvent();
$config = $serviceManager->get('Config');
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);
$this->event->setRouter($router);
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event); …Run Code Online (Sandbox Code Playgroud) 我在Symfony2中使用Monolog,使用默认的MonologBundle.我试图在我的测试中断言,记录了一行.我在我的配置中config_test.yml:
monolog:
handlers:
main:
type: test
level: debug
Run Code Online (Sandbox Code Playgroud)
如何TestHandler在我的测试中获得Monolog的结果(继承自Symfony2 WebTestCase)?
所有PHPUnit测试完成后,我可以自动运行PHP脚本吗?
在完成所有测试后,我想报告一些非致命问题(即正确但不是最理想的测试结果).
phpunit ×10
php ×6
symfony ×3
unit-testing ×2
ant ×1
arrayaccess ×1
mocking ×1
monolog ×1
mysql ×1
netbeans-7 ×1
phing ×1
stty ×1
symfony-2.1 ×1
vfs-stream ×1
whitelist ×1
zfcuser ×1