我使用Eclipse PDT for PHP.我可以运行我的PhpUnit测试:工作正常.
但我无法调试我的单元测试.有人已经这样做了吗?有人可以帮忙吗?
Messxoud,Thanx
我正在尝试使用PHPUnit 3.6.4对我的Zend Framework应用程序进行单元测试.我在命令提示符中尝试此命令时出现以下错误.
C:\xampp\htdocs\testsample\tests>phpunit --configuration phpunit.xml
PHPUnit 3.6.4 by Sebastian Bergmann.
Configuration read from C:\xampp\htdocs\testsample\tests\phpunit.xml
?[31;1mE?[0m?[31;1mE?[0m..
Time: 0 seconds, Memory: 10.00Mb
There were 2 errors:
1) IndexControllerTest::testIndexWithMessageAction
Declaration of Zend_Test_PHPUnit_Constraint_DomQuery::evaluate() should be compatible
with that of PHPUnit_Framework_Constraint::evaluate()
C:\xampp\htdocs\hive\library\Zend\Test\PHPUnit\Constraint\DomQuery.php:40
C:\xampp\htdocs\hive\library\Zend\Test\PHPUnit\ControllerTestCase.php:512
C:\xampp\htdocs\testsample\tests\application\controllers\IndexControllerTest.php
:14
C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:925
C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:787
C:\xampp\php\PEAR\PHPUnit\Framework\TestResult.php:649
C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:734
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:772
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:745
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:705
C:\xampp\php\PEAR\PHPUnit\TextUI\TestRunner.php:325
C:\xampp\php\PEAR\PHPUnit\TextUI\Command.php:187
C:\xampp\php\PEAR\PHPUnit\TextUI\Command.php:125
C:\xampp\php\phpunit:44
2) IndexControllerTest::testIndexNoMessageAction
Declaration of Zend_Test_PHPUnit_Constraint_ResponseHeader::evaluate() should be
compatible with that of PHPUnit_Framework_Constraint::evaluate()
C:\xampp\htdocs\hive\library\Zend\Test\PHPUnit\Constraint\ResponseHeader.php:400
C:\xampp\htdocs\hive\library\Zend\Test\PHPUnit\ControllerTestCase.php:769
C:\xampp\htdocs\hive\library\Zend\Test\PHPUnit\ControllerTestCase.php:769
C:\xampp\htdocs\testsample\tests\application\controllers\IndexControllerTest.php
:22
C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:925
C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:787
C:\xampp\php\PEAR\PHPUnit\Framework\TestResult.php:649
C:\xampp\php\PEAR\PHPUnit\Framework\TestCase.php:734
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:772
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:745
C:\xampp\php\PEAR\PHPUnit\Framework\TestSuite.php:705
C:\xampp\php\PEAR\PHPUnit\TextUI\TestRunner.php:325
C:\xampp\php\PEAR\PHPUnit\TextUI\Command.php:187
C:\xampp\php\PEAR\PHPUnit\TextUI\Command.php:125
C:\xampp\php\phpunit:44 …Run Code Online (Sandbox Code Playgroud) 我在模拟重载的__get($ index)方法时遇到了问题.要模拟的类的代码和使用它的被测系统如下:
<?php
class ToBeMocked
{
protected $vars = array();
public function __get($index)
{
if (isset($this->vars[$index])) {
return $this->vars[$index];
} else {
return NULL;
}
}
}
class SUTclass
{
protected $mocky;
public function __construct(ToBeMocked $mocky)
{
$this->mocky = $mocky;
}
public function getSnack()
{
return $this->mocky->snack;
}
}
Run Code Online (Sandbox Code Playgroud)
测试看起来如下:
<?php
class GetSnackTest extends PHPUnit_Framework_TestCase
{
protected $stub;
protected $sut;
public function setUp()
{
$mock = $this->getMockBuilder('ToBeMocked')
->setMethods(array('__get')
->getMock();
$sut = new SUTclass($mock);
}
/**
* @test
*/
public function …Run Code Online (Sandbox Code Playgroud) 我对Laravel和PHPUnit都很陌生,我在Ubuntu 12.04上使用Laravel 4.
当我phpunit从项目的主目录运行时,它运行Laravel附带的ExampleTest.php测试.我创建了自己的示例测试(与他们的示例完全相同,在同一目录中,只重命名文件和测试并执行不同的操作),并phpunit像以前一样再次运行; 但它仍然只运行ExampleTest.php - 而不是我创建的测试.但是,如果我跑phpunit path/to/myTest,它运行我的测试就好了.所以我觉得这是一个愚蠢的问题,但我怎么用一个命令运行所有测试(我认为phpunit应该这样做)?
谢谢您的帮助!
我有一个带有两个环境和两个配置的Laravel 5应用程序:测试(用于PHPUnit配置,内存中的数据库)和本地(我的开发配置).
即使将环境配置为local,应用程序也只会在resources/config/testing文件夹中加载配置.我可以从APP_ENV环境变量中看到同一个应用程序中的环境local.
我应该不使用测试配置目录来配置我的测试吗?
在Laravel 5中配置测试环境的更好方法是什么?
当我启动我的PHPUnit测试套件时,我达到了MySQL的最大连接限制(200).
热门修复是将max_connection设置为500,但我正在寻找Zend Framework 2上下文中更好的解决方案.
我试图把一些tearDown方法,但没有运气似乎是无用或不完整的解决方案.
这是一个代码示例:
protected function tearDown()
{
// i have two entitymanager
$this->getObjectManager()->get('doctrine.connection.orm_alternate')->close();
$this->getObjectManager()->get('doctrine.connection.orm_default')->close();
$this->application = null;
gc_collect_cycles();
parent::tearDown();
}
Run Code Online (Sandbox Code Playgroud)
我也尝试使用processIsolation转为true但是有些测试是如此之长以至于我认为我的控制台已经冻结或类似的东西....
使用doctrine2连接和Zend Framework,如何在PHPunit测试期间防止这种"连接太多"?
到目前为止,我试图修改@awons的提示
$this->getObjectManager()->get('doctrine.connection.orm_alternate')->close();
至
$this->getObjectManager()->get('doctrine.entitymanager.orm_alternate')->close();
我检查了拆机是否被叫,是的.
我不明白的是:在每次测试中,都会创建一个新的连接实例,为什么这不是同一个实例?(像单身人士或登记处)?但是这个实例永远不会关闭,即使它在测试通过后也没有使用过.
我误会了什么,但不知道是什么.
我尝试为我的应用程序编写功能测试但是我收到以下错误:
InvalidArgumentException: The option "test_case" must be set.
Run Code Online (Sandbox Code Playgroud)
我试着谷歌搜索它,但发现字面上没有线索.
我的代码:
class WhateverTest extends WebTestCase {
public function testWhatever() {
$client = static::createClient();
$crawler = $client->request('GET', '/home');
$this->assertEquals(1, 1);
}
Run Code Online (Sandbox Code Playgroud)
在phpunit.xml中设置了内核
<php>
<server name="KERNEL_DIR" value="app/" />
</php>
Run Code Online (Sandbox Code Playgroud)
我AppKernel只有两个功能:registerBundles()和registerContainerConfiguration
我试图验证php7函数只接受整数.
这是班级:
<?php
declare(strict_types=1);
class Post
{
private $id;
public function setId(int $id)
{
$this->id = $id;
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试:
<?php
declare(strict_types=1);
class PostTest extends \PHPUnit_Framework_TestCase
{
private function getPostEntity()
{
return new Post();
}
public function testSetId()
{
$valuesExpected = [123, '123a'];
foreach ($valuesExpected as $input) {
$this->getPostEntity()->setId($input);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
TypeError: Argument 1 passed to Post::setId() must be of the type integer, string given, called in /path/test/PostTest.php on line 35
有可能验证这样的错误吗?还有,运行这样的支票是否有意义?
我正在测试我的代码,我的标题有问题.在每个api我使用
$headers = getallheaders();
Run Code Online (Sandbox Code Playgroud)
为了得到它,当我使用app或crhome邮递员扩展测试时,这工作正常.当我开始测试时,就像这样
$client = $this->createClient();
$client->request('GET', '/api/shotcard',
['qrcode'=>'D0m1c173'], [],
['HTTP_API_TOKEN' => 'abc123']
);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
Run Code Online (Sandbox Code Playgroud)
我尝试用带有该测试令牌的用户拍摄带有该qrcode的卡(不是我将在应用程序中使用的令牌),我在这里看到这样的呼叫:https://stackoverflow.com/a/11681422/ 5475228.测试以这种方式失败:
PHP致命错误:在第42行的/var/www/pitstop/src/AppBackendBundle/Controller/ApiController.php中调用未定义的函数AppBackendBundle\Controller\getallheaders()
我正在玩php 7和phpunit 6.这是我写的测试:
<?php declare(strict_types=1);
namespace Test;
use DesignPatterns\Observer\User;
use DesignPatterns\Observer\UserObserver;
use PHPUnit\Framework\TestCase;
class ObserverTest extends TestCase
{
public function testChangeInUserLeadsToUserObserverBeingNotified()
{
$observer = new UserObserver();
$user = new User();
$user->attach($observer);
$user->changeEmail('foo@bar.com');
$this->assertCount(1, $observer->getChangedUsers());
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行此测试时,我收到以下错误消息:
PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found in /home/.../.../Test/ObserverTest.php on line 9
Run Code Online (Sandbox Code Playgroud)
我用composer安装了PHPUnit,这是我的composer.json文件内容:
{
"require": {
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": {"DesignPatterns\\": "src/"}
}
}
Run Code Online (Sandbox Code Playgroud)
根据PHPUnit 6文档,您的测试现在应该扩展PHPUnit\Framework\TestCase而不是PHPUnit_Framework_TestCase.
我知道这不是自动加载的问题.实际上,如果我用PHPUnit_Framework_TestCase替换PHPUnit\Framework\TestCase,它工作正常,但我想知道为什么这个语法不起作用.
我尝试了一些谷歌,stackoverflow和PHPUnit的github存储库的研究,但找不到任何东西.
我期待着你的回答,
编辑
这就是我的文件组织方式:
src/
??? DataMapper
? ??? StorageAdapter.php
? ??? …Run Code Online (Sandbox Code Playgroud) phpunit ×10
php ×5
laravel ×2
php-7 ×2
api ×1
built-in ×1
debugging ×1
eclipse ×1
environment ×1
http-headers ×1
laravel-4 ×1
laravel-5 ×1
mysql ×1
overloading ×1
symfony ×1
testing ×1
unit-testing ×1