我有以下非常简单的PHPUnit XML配置:
<phpunit bootstrap="/_tests/TestAutoload.php">
<testsuites>
<testsuite name="Unit Tests">
<directory suffix=".php">_tests</directory>
</testsuite>
</testsuites>
</phpunit>
Run Code Online (Sandbox Code Playgroud)
如何从测试套件中排除此目录中的某些文件?我试过<exclude>和<blacklist>,但它似乎并没有在这方面的工作.还找不到除phpunit.de之外的任何其他文档,其中没有提及任何相关内容.除此之外,此配置完美无缺.
我正在尝试设置单元测试,但每当我运行"phpunit -c app"时,我都会收到此错误:
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException:您已请求不存在的服务"test.client".
我的测试用例中的代码只是:
public function testNonAuthenticatedPathsIndex()
{
$client = $this->createClient();
}
Run Code Online (Sandbox Code Playgroud)
如果我没有调用createClient,一切都正常运行.我已经检查过AppKernel.php以确保FrameworkBundle(我相信这个服务定义的位置)肯定仍在那里,它确实存在.
我有点困惑,因为我之前已经设法做了这件事.
谢谢你的帮助.
我正在使用Symfony2,一切都已安装,我的测试到目前为止工作得很好.
我想自动完成PHPUnit的方法.
Symfony的 WebTestCase类扩展PHPUnit_Framework_TestCase如下:
abstract class WebTestCase extends \PHPUnit_Framework_TestCase
Run Code Online (Sandbox Code Playgroud)
尽管如此,父类突出显示为不存在.
如何告诉我的IDE使用PHPUnit的库?
我正在使用PHPStorm
PHPUnit可以通过路径获得/Users/myUser/pear/share/pear/PHPUnit/
在Laravel的单元测试中,我可以像这样测试一个JSON API:
$this->post('/user', ['name' => 'Sally'])
->seeJson([
'created' => true,
]);
Run Code Online (Sandbox Code Playgroud)
但是,如果我想使用响应怎么办?如何使用$this->post()?获取JSON响应(作为数组)?
有时在我的代码中,我会检查一个特定的对象是否实现了一个接口:
if ($instance instanceof Interface) {};
Run Code Online (Sandbox Code Playgroud)
但是,在PHPUnit中创建所述接口的模拟,我似乎无法通过该测试.
// class name is Mock_Interface_431469d7, does not pass above check
$instance = $this->getMock('Interface');
Run Code Online (Sandbox Code Playgroud)
我知道有一个名为Interface的类与实现Interface的类不同,但我不知道如何处理它.
我是否被迫模拟实现Interface的具体类?难道这不会破坏使用接口进行移植的目的吗?
谢谢
当我使用PHPUnit对我的PHP代码进行单元测试时,我试图找出模拟对象的正确方法,而不是模仿它的任何方法.
问题是,如果我不调用getMockBuilder()->setMethods(),那么对象上的所有方法都将被模拟,我无法调用我想要测试的方法; 但如果我不打电话setMethods(),然后我需要告诉它嘲笑什么方法,但我不希望在所有的嘲笑任何方法.但我需要创建模拟,这样我就可以避免在测试中调用构造函数.
这是我想要测试的方法的一个简单示例:
class Foobar
{
public function __construct()
{
// stuff happens here ...
}
public function myMethod($s)
{
// I want to test this
return (strlen($s) > 3);
}
}
Run Code Online (Sandbox Code Playgroud)
我可能测试myMethod():
$obj = new Foobar();
$this->assertTrue($obj->myMethod('abcd'));
Run Code Online (Sandbox Code Playgroud)
但这会叫Foobar的构造函数,这是我不想要的.所以相反,我会尝试:
$obj = $this->getMockBuilder('Foobar')->disableOriginalConstructor()->getMock();
$this->assertTrue($obj->myMethod('abcd'));
Run Code Online (Sandbox Code Playgroud)
但是getMockBuilder()不使用调用setMethods()将导致其所有方法被模拟并返回null,因此我的调用myMethod()将返回null而不触及我打算测试的代码.
到目前为止我的解决方法是:
$obj = $this->getMockBuilder('Foobar')->setMethods(array('none'))
->disableOriginalConstructor()->getMock();
$this->assertTrue($obj->myMethod('abcd'));
Run Code Online (Sandbox Code Playgroud)
这将模拟名为'none'的方法,该方法不存在,但PHPUnit并不关心.它会让myMethod()取消模拟,以便我可以调用它,它也会让我禁用构造函数,这样我就不会调用它.完善!除了必须指定一个不存在的方法名称 - "无",或"blargh"或"xyzzy"之外,似乎在作弊.
这样做的正确方法是什么?
我想知道什么是"PHPUnit中的严格模式"?
例如:
phpunit --strict
Run Code Online (Sandbox Code Playgroud)
或者在phpunit.xml中
<phpunit strict="true"/>
Run Code Online (Sandbox Code Playgroud)
我打开它只是为了尝试它,我的测试开始失败了
PHP_Invoker_TimeoutException: Execution aborted after 1 second
Run Code Online (Sandbox Code Playgroud) 如何安装phpunit?
我阅读了文档https://github.com/sebastianbergmann/phpunit,但有一个错误:
>pear upgrade PEAR
Nothing to upgrade
>pear config-set auto_discover 1
config-set succeeded
>pear install pear.phpunit.de/PHPUnit
No releases available for package "pear.phpunit.de/PHPUnit"
install failed
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
我想知道是否有关于如何对PHP特征进行单元测试的解决方案.
我知道我们可以测试一个使用该特性的类,但我想知道是否有更好的方法.
在此先感谢任何建议:)
编辑
另一种方法是在测试类本身中使用Trait,因为我将展示下面的内容.
但我并不热衷于这种方法,因为没有保证在特征,类和PHPUnit_Framework_TestCase(在本例中)之间没有类似的方法名称:
这是一个示例特征:
trait IndexableTrait
{
/** @var int */
private $index;
/**
* @param $index
* @return $this
* @throw \InvalidArgumentException
*/
public function setIndex($index)
{
if (false === filter_var($index, FILTER_VALIDATE_INT)) {
throw new \InvalidArgumentException('$index must be integer.');
}
$this->index = $index;
return $this;
}
/**
* @return int|null
*/
public function getIndex()
{
return $this->index;
}
}
Run Code Online (Sandbox Code Playgroud)
及其测试:
class TheAboveTraitTest extends \PHPUnit_Framework_TestCase
{
use TheAboveTrait;
public function test_indexSetterAndGetter()
{
$this->setIndex(123);
$this->assertEquals(123, …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试我的ZF2应用程序的控制器.假设这个控制器在我的A模块中.
在模块的onBootstrap方法中,我使用服务管理器来检索另一个模块的服务,比如说我没有加载.Module.phpAB
如何在服务管理器中设置所请求服务的模拟?请注意,我不能$this->getApplicationServiceLocator()在测试中使用它,因为这已经调用了Module.onBootstrap我的A模块的方法.
要发布一些代码,这就是我现在正在做的事情
bootstrap.php中
namespace Application;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use RuntimeException;
class Bootstrap
{
protected static $serviceManager;
public static function init()
{
$modulePath = static::findParentPath('module');
$vendorPath = static::findParentPath('vendor');
if (is_readable($vendorPath . '/autoload.php')) {
$loader = include $vendorPath . '/autoload.php';
} else {
throw new RuntimeException('Cannot locate autoload.php');
}
$config = [
'modules' => [
'Application',
],
'module_listener_options' => [
'module_paths' => [
$modulePath,
$vendorPath
] …Run Code Online (Sandbox Code Playgroud)