所以我通过从 pear.phpunit.de 下载文件并将它们解压缩到 C:\PHP\PEAR 文件夹来手动安装 PHPUnit。我在包含路径中有 PEAR 文件夹。我怎样才能运行这个测试?
class StackTest extends PHPUnit_Framework_TestCase
{
public function testEmpty()
{
$stack = array();
$this->assertEmpty($stack);
return $stack;
}
/**
* @depends testEmpty
*/
public function testPush(array $stack)
{
array_push($stack, 'foo');
$this->assertEquals('foo', $stack[count($stack)-1]);
$this->assertNotEmpty($stack);
return $stack;
}
/**
* @depends testPush
*/
public function testPop(array $stack)
{
$this->assertEquals('foo', array_pop($stack));
$this->assertEmpty($stack);
}
}
Run Code Online (Sandbox Code Playgroud)
PHPUnit 目录中没有可执行的命令行工具。
我知道“--repeat”选项,但我宁愿在测试和每个测试中定义重复。在我的单元测试中,有些测试我不想重复,有些测试我比其他测试更想重复。
我刚在想:
protected function tearDown() {
if (test has not been ran 3 times) {
$this->runTest(); // Re-run the test
}
}
Run Code Online (Sandbox Code Playgroud)
这似乎不起作用,$this->run() 也不起作用。我看过 PHPUnit 源代码,但我不确定。我猜它正在检查测试状态,如果它已经运行,它会拒绝再次运行它。
以 PHP 中的这两个数组为例:
$array1 = [
2 => 'Search',
1 => 'Front-End / GUI'
];
$array2 = [
1 => 'Front-End / GUI',
2 => 'Search'
];
Run Code Online (Sandbox Code Playgroud)
大多数数组比较函数不关心顺序。执行 anarray_diff将导致一个空数组。
在顺序方面比较两个数组的最有效/最短/最干净的方法是什么:
$this->assertEquals( $array1, $array2 );理想情况下,在 PHPUnit 中运行应该产生如下结果:
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
Array (
- 2 => 'Search'
- 1 => 'Front-End / GUI'
+ 1 => 'Front-End / GUI'
+ 2 => 'Search'
)
Run Code Online (Sandbox Code Playgroud)
我有在调试模式下运行的 netbeans。现在我想用 ?XDEBUG_SESSION_START=netbeans-xdebug 运行单元测试,目的是在我运行时设置断点。是否可以?
我试过这样的事情:
phpunit myTest.php?XDEBUG_SESSION_START=netbeans-xdebug
Run Code Online (Sandbox Code Playgroud)
不起作用。有任何想法吗?
更新
根据这个:http : //blog.doh.ms/2011/05/13/debugging-phpunit-tests-in-netbeans-with-xdebug/
转到命令行并运行 phpunit-debug。现在调试开始,如果您选择“在第一行停止”,则 IDE 将打开 phpunit 文件,单击播放即可关闭。
如果我运行 phpunit --debug
Starting test 'xxxx'
OK (1 test, 4 assertions)
Run Code Online (Sandbox Code Playgroud)
问题是,它从来没有捕捉到我在控制器中的断点。为什么抓不住?是否有可能是因为它是使用以下方式执行的:
$this->dispatch('/ajax/update-bla');
Run Code Online (Sandbox Code Playgroud) 我正在阅读zend 教程,我正在使用 phpunit 测试一个带有模拟对象的类。当我将从 Zend\Db\TableGateway 创建的模拟传递给我的类时,谁的构造函数需要 Zend\Db\TableGateway,我得到一个类型错误:
"...Argument 1 passed to Album\Model\AlbumTable::__construct() must be an instance of Zend\Db\TableGateway\TableGateway, instance of Mock_TableGateway_65b55cb0 given..."
Run Code Online (Sandbox Code Playgroud)
这是应该发生的吗?phpunit 模拟对象是否应该能够“愚弄”该类?
这是真正的课堂:
class AlbumTable {
protected $tableGateway;
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
}
public function fetchAll() {
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getAlbum($id){
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if(!$row) {
throw new \Exception("Couldn't find row: $id");
}
return $row;
}
public function …Run Code Online (Sandbox Code Playgroud) 首先让我说我已经在多个论坛上发布了这个,甚至试图在 ZF IRC 频道上获得帮助。我一直在谷歌上搜索一个星期,但仍然没有结果。过去我在这个网站上阅读了很多 Q 和 A,所以我想我会注册一个帐户并试着问你。
(是的,我搜索了以前提出的问题,但没有一个答案对我有帮助。)
我正在尝试学习如何将 Zend Framework 用于我加入的新项目。出于兼容性原因,他们使用 Zend 1(而不是较新的 Zend 2)。我找到并遵循了许多在线和实体书籍教程,但我一遍又一遍地得到相同的结果。
所以这里是(这是所有教程给出的说明)。我去framework.zend.com下载了完整版的ZF 1.12。
我跑了
zf --help
此命令按预期工作。我也成功运行了 zf show version (Zend Framework Version 1.12.7)。
我运行了命令
zf 创建项目 myproject
执行此操作后,我收到以下错误消息:
Fatal error: Class 'PHPUnit_Framework_TestCase' not found in D:\Zend\library\Zend\Test\PHPUnit\ControllerTestCase.php on line 48
Run Code Online (Sandbox Code Playgroud)
有问题的那一行是扩展 PHPUnit_Framework_TestCase 的类声明。我不知道 PHPUnit_Framework_TestCase 在哪里定义的。它不在我从 Zend 下载的单个 ZIP 文件中的任何文件或目录中。我什至在所有文件和文件夹上运行 grep 搜索字符串“class PHPUnit_Framework_TestCase”,但没有打印任何结果。
有人建议我没有安装 …
我有以下类“MyControllerTest”用于测试“MyController”。我想在此类的不同测试中共享同一个对象,即“$algorithms”,但在尝试在不同位置添加变量后,我不知道该怎么做:
class MyControllerTest extends Zend_Test_PHPUnit_ControllerTestCase
{
// $algorithms = ... <----- does not work
public static function main()
{
$suite = new PHPUnit_Framework_TestSuite("MyControllerTest");
$result = PHPUnit_TextUI_TestRunner::run($suite);
}
public function setUp()
{
$this->bootstrap = new Zend_Application(
'testing',
APPLICATION_PATH . '/configs/application.ini'
);
$configuration = Zend_Registry::get('configuration');
// $algorithms = ... <----- does not work
parent::setUp();
}
public function tearDown()
{
}
public function test1() {
// this array shared in different tests
$algorithms[0] = array(
"class" => "Mobile",
"singleton" => "true",
"params" => …Run Code Online (Sandbox Code Playgroud) 我正在写一个wordpressplugin,并想为它创建一个phpunit测试环境.为此,我使用php:7.2-apache基本容器创建了一个docker容器,并通过图像上的phar archive在其上安装了phpunit.之后我设置了一些环境变量并使用了以下bashscript,它是由"wp scaffold plugin-tests"创建的bashscript,作为入口点.
# INSTALL WP-CORE
wp core download --path="${WPPATH}" --allow-root
waitforit -t 60 database:3306 -- wp config create --dbuser="${WPDBUSER}" --dbpass="${WPDBPASS}" --dbname="${WPDBNAME}" --dbhost="${WPDBHOST}" --path="${WPPATH}" --allow-root
wp db create --path="${WPPATH}" --allow-root
wp core install --url="${WPURL}" --title="SpitzeDev" --admin_user="${ADMINUSER}" --admin_password="${ADMINPASS}" --admin_email="${ADMINMAIL}" --path="${WPPATH}" --allow-root
chown www-data:www-data "/var/www/html" -R
# Install WP-Testsuite for PHPUnit
if [ ! -d $WP_TESTS_DIR ]; then
mkdir -p $WP_TESTS_DIR
svn co --quiet https://develop.svn.wordpress.org/tags/$(wp core version --allow-root --path=${WPPATH})/tests/phpunit/includes/ $WP_TESTS_DIR/includes
svn co --quiet https://develop.svn.wordpress.org/tags/$(wp core version --allow-root --path=${WPPATH})/tests/phpunit/data/ $WP_TESTS_DIR/data
fi
# …Run Code Online (Sandbox Code Playgroud) 我正在使用PHP Faker在数据库中生成随机数据(使用工厂),有时它会生成撇号。
在测试中,我使用assertSeeText()。问题在于,当测试的字符串包含特殊字符(例如撇号)时,这些字符将在视图中转换为html实体,因此断言为false。
$siteShow = factory(Site::class)->create();
$this->get('admin/site/'. $siteShow->id_site)
->assertStatus(200)
->assertSeeText($siteShow->name)
->assertSeeText($siteShow->address)
Run Code Online (Sandbox Code Playgroud)
断言失败的示例案例: $siteShow->name equals O'Neil。它失败,因为在视图中显示为O'Neil。
我的问题是:在两种情况下,如何使断言为真:视图中的名称为O'Neil或O'Neil?我希望解决方案能够处理任何html实体。先感谢您。
我试图多次调用同一方法时获得不同的返回值。我尝试了很多事情,但没有得到确切的答案。
$mock = $this->getMockBuilder('Test')
->disableOriginalConstructor()
->setMethods(array('testMethod'))
->getMock();
$mock->expects($this->once())->method('testMethod')->will($this->returnValue(true));
$mock->expects($this->second())->method('testMethod')->will($this->returnValue(false));
Run Code Online (Sandbox Code Playgroud)