我正在使用phpunit框架,我有这样的代码:
public function A() {
try {
(...some code...)
die (json_encode ($data));
}
catch (Exception $e) {
die(false);
}
}
Run Code Online (Sandbox Code Playgroud)
这个函数是通过AJAX调用的,我不能用return替换die.问题是:如何使用这样的代码进行单元测试?
断言,我不能用它.
谢谢.
我有一个用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%的覆盖率也就不足为奇了.
我错过了什么?
我刚开始使用PHPUnit.我写的一些简单测试正在进行中.所以一般来说PHPUnit已启动并正在运行.但MySQLi类有问题.
在我的代码中它工作正常.这是行:
$this->mysqli = new \mysqli($this->host, $user->getUser(), $user->getPwd(), $this->db);
Run Code Online (Sandbox Code Playgroud)
当运行phpunit解析此行时,我收到以下错误消息(指向该行):
PHP Fatal error: Class 'mysqli' not found in /opt/lampp/htdocs/...
Run Code Online (Sandbox Code Playgroud)
两种可能性(我认为):
1)我缺少一些功能/扩展/配置步骤/与使用MySQLi扩展的PHPUnit的正确设置相关的其他内容.
编辑
如果我测试扩展,extension_loaded('mysqli')它会true在我的正常代码中返回.如果我在测试中执行以下操作,它会跳过测试(即返回false):
if (!extension_loaded('mysqli')) {
$this->markTestSkipped(
'The MySQLi extension is not available.'
);
}
Run Code Online (Sandbox Code Playgroud)
/编辑
2)我的代码可能有问题.我正在尝试模拟User对象以进行测试连接.所以这里是:
<?php
class ConnectionTest extends \PHPUnit_Framework_TestCase
{
private $connection;
protected function setUp()
{
$user = $this->getMockBuilder('mysqli\User')
->setMethods(array('getUser', 'getPwd'))
->getMock();
$user->expects($this->once())
->method('getUser')
->will($this->returnValue('username'));
$user->expects($this->once())
->method('getPwd')
->will($this->returnValue('p@ssw0rd'));
$this->connection = new \mysqli\Connection($user);
}
public function testInternalTypeGetMysqli()
{
$actual = …Run Code Online (Sandbox Code Playgroud) 这是我正在编写测试套件的类的构造函数(它扩展了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) phpunit ×10
php ×8
symfony ×2
unit-testing ×2
ant ×1
arrayaccess ×1
mocking ×1
mysql ×1
mysqli ×1
netbeans-7 ×1
phing ×1
stty ×1
symfony-2.1 ×1
vfs-stream ×1
whitelist ×1
zfcuser ×1