标签: phpunit

使用 PHPUnit 进行功能测试

我看到 WebTestCase 和 Symfony 出现了很多...我已经在 PHPUnit 中进行了一些单元测试,但确实需要更高级别的测试...我不需要像 Selenium 这样的任何花哨的东西(UI 是在 ExtJS 中完成的,但这可以完全更改为未来的 Polymer)我只需要发布数据并检查 JSON 结果。

这可以通过 PHPUnit 完成还是我需要引入 Symfony 及其 WebTestCase?

使用 PHPUnit 测试 RESTful Web 服务

注意:我实际上想测试服务——不需要模拟对象——我需要在测试套件首次运行时登录——存储会话 ID 并完成每个测试...

phpunit unit-testing symfony

3
推荐指数
1
解决办法
5045
查看次数

times() 方法在 Mockery PHP 中不起作用

我是 PHP Mockery 框架的新手。我有一个模拟函数executePrepared($arg1, $arg2, arg3),我调用它两次,但似乎不起作用,并在 PHPUnit 命令行中给出以下错误:

\n\n
Configuration read from C:\\xampp\\htdocs\\DatabaseTesting\\phpunit.xml\n\n..\xe2\x86\x90[31;1mE\xe2\x86\x90[0m\n\nTime: 47 ms, Memory: 3.25Mb\n\nThere was 1 error:\n\n\n1) Test\\Database\\Table\\TableTest::testinsertMany\n Mockery\\Exception\\NoMatchingExpectationException: No matching handler found for\n Mockery_0_Database_PGC::executePrepared(array(0=>\'ClayPipe\',1=>2000,2=>2100,3=>1\n ,4=>\'2000-01-01\',5=>\'{"1":"1","2":6,"3":8,"4":10}\',), "insert_assets", "\\Databas\n e\\Model\\Asset"). Either the method was unexpected or its arguments matched no ex\n pected argument list for this method\n
Run Code Online (Sandbox Code Playgroud)\n\n

我的测试功能如下:

\n\n
 public function testinsertMany() {\n      $this->PGCMock->shouldReceive(\'executePrepared\')->twice()->withArgs(array(\n[array(\'Clay Pipe\',2000,2100,1,\'2000-01-01\',\'{"1":"1","2":6,"3":8,"4":10}\'), \'insert_assets\', \'\\Database\\Model\\Asset\'],\n[array(\'Main Street\',1000,1100,0,\'2000-02-01\',\'{"1":"1","2":6,"3":8,"4":10}\'), \'insert_assets\', \'\\Database\\Model\\Asset\']))\n->andReturn($expectedResult1);\n\n$data1 = array(\'name\'=>\'Clay Pipe\',\n                    \'hist_cost\' => 2000,\n                    \'val_cost\' => 2100,\n                    \'val_method\' => 1,\n                    \'service_date\' => \'2000-01-01\',\n                    \'tags\' => …
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing mockery

3
推荐指数
1
解决办法
3602
查看次数

使用 app()->make 创建的模拟类

我的 __constructurct 中有这段代码:

public function __construct(Guard $auth)
{
    $this->auth = $auth;
    $this->dbUserService = app()->make('DBUserService');
}
Run Code Online (Sandbox Code Playgroud)

现在,当我进行单元测试时,我知道我可以模拟 Guard 并将其模拟传递给 $auth,但我如何模拟dbUserService?它是通过 IoC 容器实例化的。

php phpunit unit-testing laravel

3
推荐指数
2
解决办法
2800
查看次数

Symfony3+PHPUnit。如何禁用控制台中的调试日志?

我想我关闭了所有地方的调试日志记录(在 config.yml、config_dev、config_test 中):

monolog:
handlers:
    main:
        type: stream
        path: "%kernel.logs_dir%/%kernel.environment%.log"
        level: debug
        channels: [!event]
    console:
        type: stream
        path: "%kernel.logs_dir%/console.log"
        level: critical
        channels: [!event]
Run Code Online (Sandbox Code Playgroud)

但是当我运行 phpunit 时它会显示调试日志:

[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\DebugHandlersListener::configure"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\DumpListener::configure"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\\Bundle\\FrameworkBundle\\EventListener\\TestSessionListener::onKernelRequest"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\\Bundle\\FrameworkBundle\\EventListener\\SessionListener::onKernelRequest"} []
[2016-06-19 16:53:29] event.DEBUG: Notified event "{event}" to listener "{listener}". {"event":"kernel.request","listener":"Symfony\\Component\\HttpKernel\\EventListener\\FragmentListener::onKernelRequest"} …
Run Code Online (Sandbox Code Playgroud)

logging phpunit symfony monolog

3
推荐指数
1
解决办法
5970
查看次数

Laravel 5.1 测试控制器时忽略嘲笑期望

我有一个依赖于 UserManager 的控制器。这是控制器构造函数:

public function __construct(UserManager $manager) {
    $this->manager = $manager;
}
Run Code Online (Sandbox Code Playgroud)

这是测试代码。

public function test_something() {
    $this->withoutMiddleware();

    // Setup Input.
    $user = ['email' => 'foo@bar.baz', 'password' => 'pass', 'accessLevel' => 'admin'];

    // Setup expectations.
    $mock = \Mockery::mock("Users\UserManager")->shouldReceive('foo');

    // Bind to container... not sure whether this is needed.
    $this->app->instance("Users\UserManager", $mock);

    // Call action.
    $this->call('POST', 'api/v1/temp/users', ['user' => $user]);
}
Run Code Online (Sandbox Code Playgroud)

我对该方法设置了期望foo,该方法不存在,因此不会在任何地方调用,但是我的测试不会失败。

为什么?

php phpunit laravel mockery laravel-5.1

3
推荐指数
1
解决办法
516
查看次数

PHPUnit 使用注释断言异常与方法调用

在 Phpunit 中断言异常有两种方法:

  • 使用注释@expectedException
  • 使用方法调用$this->expectException()

我已经尝试过它们两个,它们工作得很好并且完全相同。

哪个是正确的方法?是否有关于应该使用哪一种的指南?

PS:当异常是基于某种条件并且并不总是发生时,显然应该使用该方法。

php phpunit unit-testing

3
推荐指数
1
解决办法
1052
查看次数

PHPUnit测试错误,找不到类

我是 PHPUnit 和单元测试的新手,所以我通过 Composer 安装 PHPUnit 和 phar,一切都很顺利,直到我尝试开始简单的测试。我正在使用 PhpStorm,可以看到所有类都是自动加载的,但是当我尝试开始测试时出现错误:

Fatal error: Class 'PharIo\Manifest\Simple' not found in C:\xampp\htdocs\mydocs\
Run Code Online (Sandbox Code Playgroud)

我不明白为什么他要在 PHPUnit 存在的文件夹上方寻找它?

我试图在composer.json 中配置自动加载部分并检查phpunit.xml 中的设置,但没有任何效果。

添加:

我必须在没有 PharIO 的情况下重新安装 PHPUnit,所以现在我有了一点进展,现在我遇到了一种情况,如果我使用被测试类的名称创建 require_once 行,我就可以测试我的类。看起来像:

require_once '../src/Simple.php';

class SimpleTest extends PHPUnit_Framework_TestCase
{

    public function testAdd() {

        $sum = new Simple();

        $this->assertEquals(5, $sum->add(2, 3));

    }

}
Run Code Online (Sandbox Code Playgroud)

所以我的简单课程是:

class Simple {

public function add($a, $b) {

    return (int) $a + (int) $b;

}

}
Run Code Online (Sandbox Code Playgroud)

但是,当然,我想使用命名空间。我尝试根据这个问题进行一些更改:使用 Composer 和 autoload.php 在 PHPUnit 中自动加载类(我什至尝试使用该存储库进行测试,但错误仍然存​​在),但对我来说没有任何作用。我尝试像这样编辑composer.json中的自动加载部分

"autoload": {
    "psr-4": { …
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing autoload phpstorm

3
推荐指数
1
解决办法
1万
查看次数

如何对受保护的方法进行单元测试?

有没有办法对类的受保护或私有方法进行单元测试?现在,我公开了很多方法以便能够测试它们,这破坏了 API。

编辑:实际上在这里回答:使用 PHPUnit 测试受保护方法的最佳实践

phpunit

3
推荐指数
1
解决办法
7245
查看次数

单元测试 Laravel 包时未找到配置类

我正在开发 Laravel (5.4) 包,并且正在尝试进行单元测试。我有这门课:

<?php

namespace Sample;

class Foo
{
    public function getConfig()
    {
        $config = \Config::get('test');

        return $config;
    }   
}
Run Code Online (Sandbox Code Playgroud)

我有这个测试:

<?php

use PHPUnit\Framework\TestCase;
use Sample\Foo;

class FooTest extends TestCase
{
    public function testGetConfig()
    {
        $foo = new Foo;
        $config = $foo->getConfig();
    }
}
Run Code Online (Sandbox Code Playgroud)

当我执行 phpunit 时出现此错误:

错误:未找到“配置”类

我如何对这个类进行单元测试?

谢谢。

php phpunit unit-testing package laravel-5

3
推荐指数
1
解决办法
3442
查看次数

模拟在方法中间创建的对象

Class我知道在 a 中间创建 a 实例method是一种不好的做法,因为它使代码难以测试。但我无法重构代码,所以我需要找到一种方法来模拟在测试过程中Object创建的对象。newmethod

使用的框架:PHPUnit, Mockery,WP_Mock

示例:这里我需要模拟get_second_string()类实例中的方法ExternalClass

Class MyClass {

    function methodUnderTest($string) {
        $objToMock = new ExternalClass();
        $second_string = $objToMock->get_second_string();
        $final_string = $string . $second_string;
        return $final_string;
    }
}
Class TestMyClass extends PHPUnit_Framework_TestCase {
    public function setUp() {
    }
    public function tearDown() {
    }

    public function test_methodUnderTest() {
        $externalObject = $this->getMockBuilder('ExternalClass')
                               ->setMethods(['get_second_string'])
                               ->getMock;
        $externalObject->expects($this->once())
                       ->method('get_second_string')
                       ->willReturn(' two');
        $testObj = new MyClass();
        $this->assertEquals('one two', $testObj->methodUnderTest('one');
    } …
Run Code Online (Sandbox Code Playgroud)

php testing phpunit mockery

3
推荐指数
1
解决办法
4601
查看次数