我正在尝试使用PHPunit创建\ SplObserver的模拟对象,并将模拟对象附加到\ SplSubject.当我尝试将模拟对象附加到实现\ SplSubject的类时,我得到一个可捕获的致命错误,说明模拟对象没有实现\ SplObserver:
PHP Catchable fatal error: Argument 1 passed to ..\AbstractSubject::attach() must implement interface SplObserver, instance of PHPUnit_Framework_MockObject_Builder_InvocationMocker given, called in ../Decorator/ResultCacheTest.php on line 44 and defined in /users/.../AbstractSubject.php on line 49
Run Code Online (Sandbox Code Playgroud)
或多或少,这是代码:
// Edit: Using the fully qualified name doesn't work either
$observer = $this->getMock('SplObserver', array('update'))
->expects($this->once())
->method('update');
// Attach the mock object to the cache object and listen for the results to be set on cache
$this->_cache->attach($observer);
doSomethingThatSetsCache();
Run Code Online (Sandbox Code Playgroud)
我不确定它是否有所作为,但我使用的是PHP 5.3和PHPUnit 3.4.9
当我创建一个新的模拟时,我需要调用expected方法.到底是做什么的?它的论点怎么样?
$todoListMock = $this->getMock('\Model\Todo_List');
$todoListMock->expects($this->any())
->method('getItems')
->will($this->returnValue(array($itemMock)));
Run Code Online (Sandbox Code Playgroud)
我找不到任何原因(我试过docs).我已经阅读了这些消息来源,但我无法理解.谢谢.
您如何进行单元测试curl实现?
public function get() {
$ch = curl_init($this->request->getUrl());
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$type = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
curl_close($ch);
if (!strstr($type, 'application/json')) {
throw new HttpResponseException('JSON response not found');
}
return new HttpResponse($code, $result);
}
Run Code Online (Sandbox Code Playgroud)
我需要测试返回的内容类型,以便它可以抛出异常.
我不得不按照这些指示去除并重新安装更新版本的PHPUnit .现在我正在推出这条线
sudo pear install --alldeps phpunit/PHPUnit
Run Code Online (Sandbox Code Playgroud)
我看到一条错误消息,看起来像这样.
Unknown remote channel: pear.symfony.com
phpunit/PHPUnit requires package "channel://pear.symfony.com/Yaml" (version >= 2.1.0)
No valid packages found
Run Code Online (Sandbox Code Playgroud)
如果我通过启动安装Yaml
sudo pear install symfony/YAML
Run Code Online (Sandbox Code Playgroud)
将安装不符合PHPUnit依赖性的旧版本(1.0.6).我怎么可能解决这个问题?
任何人都知道如何将Selenium 2与Phpunit一起使用?PHP中是否有任何Selenium 2样本?
我遇到了PHPUnit模拟对象的一个奇怪问题.我有一个应该被调用两次的方法,所以我使用的是"at"匹配器.这是第一次调用该方法,但由于某种原因,第二次调用它时,我得到"模拟方法不存在".我之前使用过"at"匹配器并且从未遇到过这种情况.
我的代码看起来像:
class MyTest extends PHPUnit_Framework_TestCase
{
...
public function testThis()
{
$mock = $this->getMock('MyClass', array('exists', 'another_method', '...'));
$mock->expects($this->at(0))
->method('exists')
->with($this->equalTo('foo'))
->will($this->returnValue(true));
$mock->expects($this->at(1))
->method('exists')
->with($this->equalTo('bar'))
->will($this->returnValue(false));
}
...
}
Run Code Online (Sandbox Code Playgroud)
当我运行测试时,我得到:
Expectation failed for method name is equal to <string:exists> when invoked at sequence index 1.
Mocked method does not exist.
Run Code Online (Sandbox Code Playgroud)
如果我删除第二个匹配器,我不会收到错误.
有没有人遇到过这个?
谢谢!
我试图在谷歌上找到一些关于此的东西,但没有任何结果.我有一个继承自WebTestCase的TestCase类,我希望在所有单元/功能测试中使用一些方法:
<?php
namespace Application\FaxServerBundle\Test;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Application\FaxServerBundle\DataFixtures\ORM\NetworkConfigurationData;
class TestCase extends WebTestCase
{
protected $kernel;
public function setUp()
{
parent::setUp();
}
public function getEm()
{
return $this->getService( 'doctrine.orm.entity_manager' );
}
public function getNetworkConfigurationRepository()
{
return $this->getEm()->getRepository( 'Application\FaxServerBundle\Entity\NetworkConfiguration' );
}
public function loadNetworkConfigurationFixtures()
{
$loader = new Loader();
$loader->addFixture( new NetworkConfigurationData() );
$this->loadFixtures( $loader );
}
public function loadFixtures( $loader )
{
$purger = new ORMPurger();
$executor = new ORMExecutor( $this->getEm(), $purger );
$executor->execute( $loader->getFixtures() …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用PHPUnit的returnValueMap()来删除读取的结果.它没有产生预期的结果,但是等效的returnCallback()确实如此.如果您想亲自检查,我已经提供了我的测试用例.
returnValueMap()
$enterprise = $this->getMock('Enterprise', array('field'));
$enterprise->expects($this->any())
->method('field')
->will($this->returnValueMap(array(
array('subscription_id', null),
array('name', 'Monday Farms')
)));
$enterprise->subscribe('basic');
Run Code Online (Sandbox Code Playgroud)
结果:
Subscription ID: NULL
Name: NULL
Run Code Online (Sandbox Code Playgroud)
returnCallback()
$enterprise = $this->getMock('Enterprise', array('field'));
$enterprise->expects($this->any())
->method('field')
->will($this->returnCallback(function ($arg) {
$map = array(
'subscription_id' => null,
'name' => 'Monday Farms'
);
return $map[$arg];
}));
$enterprise->subscribe('basic');
Run Code Online (Sandbox Code Playgroud)
结果:
Subscription ID: NULL
Name: string(12) "Monday Farms"
Run Code Online (Sandbox Code Playgroud)
企业::订阅
public function subscribe() {
echo 'Subscription ID: ';
var_dump($this->field('subscription_id'));
echo 'Name: ';
var_dump($this->field('name'));
}
Run Code Online (Sandbox Code Playgroud)
为什么returnValueMap()不像我期望的那样工作?我到底错过了什么?
我正在使用phpunit和Laravel 4框架.为什么在测试期间出现PHP错误时,没有显示错误消息(例如:缺少方法)?
我们怎样才能让phpunit显示所有错误?

我有一个我想要模拟的界面.我知道我可以模拟该接口的实现,但有没有办法只是模拟接口?
<?php
require __DIR__ . '/../vendor/autoload.php';
use My\Http\IClient as IHttpClient; // The interface
use My\SomethingElse\Client as SomethingElseClient;
class SomethingElseClientTest extends PHPUnit_Framework_TestCase {
public function testPost() {
$url = 'some_url';
$http_client = $this->getMockBuilder('Cpm\Http\IClient');
$something_else = new SomethingElseClient($http_client, $url);
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的是:
1) SomethingElseTest::testPost
Argument 1 passed to Cpm\SomethingElse\Client::__construct() must be an instance of
My\Http\IClient, instance of PHPUnit_Framework_MockObject_MockBuilder given, called in
$PATH_TO_PHP_TEST_FILE on line $NUMBER and defined
Run Code Online (Sandbox Code Playgroud)
有趣的是,PHPUnit,模拟接口和instanceof会建议这可行.