我如何从PHP,重新创建我的数据库,可能插入默认数据.目前,我打算使用单元测试的行为.
我使用Doctrine 2,Zend Framework 1.11 Zend_Test
进行单元测试
我可以使用CLI
doctrine orm:schema-tool:update --force
Run Code Online (Sandbox Code Playgroud)
要么
doctrine orm:schema-tool:drop --force
doctrine orm:schema-tool:create
Run Code Online (Sandbox Code Playgroud)
我正在寻找一个PHP替代品,到目前为止找到了这个
但它看起来像
$tool = new \Doctrine\ORM\Tools\SchemaTool($em);
$classes = array(
$em->getClassMetadata('Entities\User'),
$em->getClassMetadata('Entities\Profile')
);
$tool->dropSchema($classes, \Doctrine\ORM\Tools\SchemaTool::DROP_DATABASE);
$tool->createSchema($classes);
Run Code Online (Sandbox Code Playgroud)
而且我真的不想指定模型类,尤其是当它们可以改变时.它应该只读取下面...中指定的所有类...就像使用CLI一样,您不需要指定所需的类?
$driverImpl = $config->newDefaultAnnotationDriver(array(realpath('../models')));
$config->setMetadataDriverImpl($driverImpl);
Run Code Online (Sandbox Code Playgroud) 我按书安装了PHPUnit:
sudo pear channel-discover pear.phpunit.de
sudo pear install phpunit/PHPUnit
Run Code Online (Sandbox Code Playgroud)
包含路径添加在 /etc/php5/cli/php.ini
include_path = ".:/usr/share/php"
$ ls /usr/share/php/PHPUnit/
Extensions Framework
Run Code Online (Sandbox Code Playgroud)
但是现在,如果我想运行Zend Framework的测试
user@server:/var/www/page/tests$ ./runtests.sh
+ phpunit --verbose AllTests
./runtests.sh: line 72: phpunit: command not found
user@server:/var/www/page/tests$ php AllTests.php
PHP Fatal error: Class 'PHPUnit_Framework_TestCase' not found in /var/www/page/tests/Zend/Acl/AclTest.php on line 37
Run Code Online (Sandbox Code Playgroud)
当然,phpunit: command not found
当我尝试遵循Zend Framework Context之外的PHPUnit手册http://www.phpunit.de/manual/3.6/en/writing-tests-for-phpunit.html中的说明时,我也会得到一个.
我得到的感觉我在这里缺少必要的东西......
解决了
看起来PEAR频道存在问题,在添加其他2之后再次,它工作:
pear channel-discover components.ez.no
pear channel-discover pear.symfony-project.com
Run Code Online (Sandbox Code Playgroud) 是否可以通过Eclipse PDT运行PHP Zend测试用例(那些扩展Zend_Test_PHPUnit_ControllerTestCase等)?
我希望能够以与在Eclipse中运行JUnit测试类似的方式运行它们,方法是右键单击测试文件并选择"Run as a JUnit test case".
我很乐意看到绿色或红色条而不必去命令行:).
提前致谢.
我正在尝试测试一个控制器.Zend Tool生成了以下代码:
class Default_CarrinhoControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
public function setUp()
{
$this->bootstrap = new Zend_Application(APPLICATION_ENV, APPLICATION_PATH . '/configs/application.ini');
parent::setUp();
}
public function testIndexAction()
{
$params = array('action' => 'index', 'controller' => 'Carrinho', 'module' => 'default');
$urlParams = $this->urlizeOptions($params);
$url = $this->url($urlParams);
$this->dispatch($url);
// assertions
$this->assertModule($urlParams['module']);
$this->assertController($urlParams['controller']);
$this->assertAction($urlParams['action']);
$this->assertQueryContentContains(
'div#view-content p',
'View script for controller <b>' . $params['controller'] . '</b> and script/action name <b>' . $params['action'] . '</b>'
);
}
}
Run Code Online (Sandbox Code Playgroud)
PHPUnit Bootstrap
<?php
// Define path to application directory
defined('APPLICATION_PATH') …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Zend Test和我的Zend Framework应用程序使用PHPUnit.我可以从命令行运行PHPUnit命令phpunit --configuration phpunit.xml
.我已经尝试过本教程,该教程基于Matthew Weier O'Phinney的博客文章.当PHPUnit尝试写日志文件时,我收到错误.这是我的phpunit.xml
<phpunit bootstrap="./Bootstrap.php" colors="true">
<testsuite name="Zend Framework Tests">
<directory>./</directory>
</testsuite>
<!-- Optional filtering and logging settings -->
<filter>
<whitelist>
<directory suffix=".php">../library/</directory>
<directory suffix=".php">../application/</directory>
<exclude>
<directory suffix=".phtml">../application/</directory>
</exclude>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/report" charset="UTF-8" yui="true" highlight="true" lowUpperBound="50" highLowerBound="80"/>
<log type="testdox-html" target="./log/testdox.html"/>
</logging>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
我的测试引导程序:
<?php
//Set app paths and environment
define('BASE_PATH', realpath(dirname(__FILE__) . '/../'));
define('APPLICATION_PATH', BASE_PATH . '/application');
define('TEST_PATH', BASE_PATH . '/tests');
define('APPLICATION_ENV', 'testing');
//Set include …
Run Code Online (Sandbox Code Playgroud) 我需要测试用ZF2编写的大型网站.有443个测试和大约10000个断言.使用代码覆盖测试需要6个小时!我想我发现了问题:在控制器的测试中,我使用了AbstractHttpControllerTestCase的调度方法.每次测试后(从秒到数十秒的分数),调度方法的执行时间都在增加.
我使用ZF 2.1.3,PHPUnit 3.7,PHP_CodeCoverage 1.2,Xdebug v2.2.1,PHP 5.4.7.
我的调度方法:
public function dispatch($url, $method = HttpRequest::METHOD_GET, $params = array())
{
$s = microtime(true);
parent::dispatch($url, $method, $params);
$end = microtime(true) - $s;
echo 'dis: '.$end."\n";
return $this->getApplication()->getMvcEvent()->getResult();
}
Run Code Online (Sandbox Code Playgroud)
parent :: dispatch是AbstractHttpControllerTestCase的方法.
测试样本:
$result = $this->dispatch('/archive/finance/older');
$this->assertControllerName('skycontent\controller\article');
$this->assertActionName('archive');
$this->assertParamValue('older', true);
$this->assertParamValue('category', 'finance');
$vars = (array) $result->getVariables();
$this->assertArrayHasKey('archivePosts', $vars);
Run Code Online (Sandbox Code Playgroud)
请帮忙.谢谢.
更新:
我使用进程隔离和测试在大约15分钟内完成(没有代码覆盖)但我在测试中得到标记为跳过的错误:
PHPUnit_Framework_Exception: PHP Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed' in -:44
Run Code Online (Sandbox Code Playgroud) 我想为我的Zend Framework/Doctrine 2.0应用程序编写单元测试,但我不太明白如何在ZF中设置单元测试.另外,我想在这些单元测试中包含Doctrine 2.0.我该如何设置呢?你能指点我一个例子吗?
谢谢
如何在Zend Framework 1.8+应用程序中开始测试我的模型?
假设我已将我的应用程序设置为开始测试.我已经测试了一个控制器,所以我知道它有效.我有我的所有控制器扩展我的ControllerTestCase.php
文件:
<?php
require_once 'Zend/Application.php';
require_once 'Zend/Test/PHPUnit/ControllerTestCase.php';
abstract class ControllerTestCase extends Zend_Test_PHPUnit_ControllerTestCase
{
public $application;
public function setUp()
{
$this->application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$this->bootstrap = array($this, 'appBootstrap');
parent::setUp();
}
public function appBootstrap()
{
$this->application->bootstrap();
}
public function tearDown()
{
Zend_Controller_Front::getInstance()->resetInstance();
$this->resetRequest();
$this->resetResponse();
$this->request->setPost(array());
$this->request->setQuery(array());
parent::tearDown();
}
}
Run Code Online (Sandbox Code Playgroud)
但现在我想开始测试我的模型.似乎我ModelTestCase.php
不会延伸Zend_Test_PHPUnit_ControllerTestCase
而是延伸Zend_Test_PHPUnit_ModelTestCase
,但是我知道不存在这样的类.如何开始测试我的Zend Framework模型?
我收到了这个错误
Fatal error: Call to a member function hasResource() on a non-object in D:\Projects\Tickle\application\controllers\ErrorController.php on line 53
第53行看起来像
if (!$bootstrap->hasResource('Log')) {
Run Code Online (Sandbox Code Playgroud)
这似乎是$this->getInvokeArg('bootstrap')
回归null
.我没有其他错误.我在其他地方读到另一个问题,可能会引发异常,重置我的控制器或引导程序或类似的东西.是否有可能不显示该例外?甚至在PHP错误日志中?
我当前的设置看起来像
单个测试运行正常,但我仍然遇到错误
D:\Projects\Tickle\tests>phpunit
PHPUnit 3.5.5 by Sebastian Bergmann.
.
Fatal error: Call to a member function hasResource() on a non-object in D:\Projects\Tickle\application\controllers\Error
Controller.php on line 53
Run Code Online (Sandbox Code Playgroud) PhpUnit ::如何测试受保护变量的__construct?
(并不总是我们应该添加公共方法getVal() - soo而不添加返回受保护变量值的方法)
例:
class Example{
protected $_val=null;
function __construct($val){
$this->_val=md5 ($val);
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
在返回void的函数中也存在测试问题
EDIT2:
我们需要测试__construct的原因示例:
class Example{
protected $_val=null;
//user write _constract instead __construct
function _constract($val){
$this->_val=md5 ($val);
}
function getLen($value){
return strlen($value);
}
}
class ExampleTest extends PHPUnit_Framework_TestCase{
test_getLen(){
$ob=new Example();//call to __construct and not to _constract
$this->assertEquals( $ob->getLen('1234'), 4);
}
}
Run Code Online (Sandbox Code Playgroud)
测试运行正常,但没有创建示例类"构造函数"!
谢谢
zend-test ×10
phpunit ×8
php ×4
doctrine ×2
doctrine-orm ×2
unit-testing ×2
eclipse ×1
zend-route ×1