标签: phpunit

清除PHP正在使用的内存

我遇到了一个有趣的问题.我正在使用PHPUnit,我的测试每次运行时都会占用更多内存.IE浏览器...

2.25 MB

2.5 MB

3.0 MB

3.5 MB .......

有谁知道如何清除正在消耗的内存,任何人都可以建议我深入探讨这个问题吗?当前的问题是我的一些较大的测试耗尽了内存,并且只是继续增加PHP中的最大内存分配还不够......我需要知道为什么从命令行运行的PHPUnit测试会有内存在运行之间"粘附"的用法.

php memory phpunit

11
推荐指数
3
解决办法
8353
查看次数

使用JSON请求体测试laravel控制器

我正在尝试为Laravel控制器编写一个phpunit测试,该控制器期望使用JSON格式的主体发布请求.

控制器的简化版本:

class Account_Controller extends Base_Controller
{
    public $restful = true;

    public function post_login()
    {
        $credentials = Input::json();
        return json_encode(array(
            'email' => $credentials->email,
            'session' => 'random_session_key'
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

目前我有一个测试方法正确地将数据作为urlencoded表单数据发送,但我无法弄清楚如何将数据作为JSON发送.

我的测试方法(我用的是GitHub的要点在这里写测试时)

class AccountControllerTest extends PHPUnit_Framework_TestCase {
    public function testLogin()
    {
        $post_data = array(
            'email' => 'user@example.com',
            'password' => 'example_password'
        );
        Request::foundation()->server->set('REQUEST_METHOD', 'POST');
        Request::foundation()->request->add($post_data);
        $response = Controller::call('account@login', $post_data);
        //check the $response
    }
}
Run Code Online (Sandbox Code Playgroud)

我在前端使用angularjs,默认情况下,发送到服务器的请求是JSON格式.我宁愿不改变它发送urlencoded形式.

有谁知道如何编写一个为控制器提供JSON编码体的测试方法?

phpunit laravel angularjs laravel-3

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

PHPUnit找不到异常类

我在5.3中使用命名空间并尝试使用Symfony2框架在PHPUnit中测试预期的异常.

我期待抛出一个异常,当我使用它时

$this->setExpectedException('ImageResizerException');

我收到以下错误:

Sebastian Bergmann的PHPUnit 3.7.13.

配置从/var/www/branches/3.6.0/api/app/phpunit.xml.dist中读取

.E .................

时间:1秒,记忆:18.25Mb

有1个错误:

1)AssetManagerBundle\Tests\Services\ImageResizerTest :: testOriginalFile ReflectionException:类ImageResizerException不存在

FAILURES!测试:19,断言:54,错误:1.

我有以下结构:

  1. AssetManagerBundle\SERVICES\ImageResizer.php
  2. AssetManagerBundle\SERVICES \例外\ ImageResizerException.php
  3. AssetManagerBundle \测试\ SERVICES\ImageResizerTest.php

我的考试班:

<?php
    namespace AssetManagerBundle\Tests\Services;

    use AssetManagerBundle\Services\ImageResizer;
    use AssetManagerBundle\Services\Exceptions\ImageResizerException;

class ImageResizerTest extends \PHPUnit_Framework_TestCase
{

    public function testOriginalFile()
    {
        $ir = new ImageResizer();
        // test default value
        $this->assertEquals('', $ir->getOriginalFile());

        // test invalid filename
        $this->setExpectedException('ImageResizerException');
        $ir->setOriginalFile('/tmp/test.file');
        $this->assertEquals('/tmp/test.file', $ir->getOriginalFile());


        // test valid filename
        $temp_name  = tempnam(sys_get_temp_dir(), 'test_'.time());
        $handle     = fopen($temp_name, 'w+');
        fwrite($handle, ' ');
        fclose($handle);

        $ir->setOriginalFile($temp_name);
        $this->assertEquals($temp_name, $ir->getOriginalFile());
    } …
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing namespaces symfony

11
推荐指数
2
解决办法
8391
查看次数

在Chrome和IE中启动phpunit-selenium2测试

我使用本教程http://net.tutsplus.com/tutorials/php/how-to-use-selenium-2-with-phpunit/创建了测试.一切正常,但我只能在Firefox上启动此测试.我在互联网上阅读了很多关于此的文章,但我找不到任何解决方案.我有Sebastian Bergmann的Windows XP,PHP 5.4.7,PHPUnit 3.7.13.在运行测试之前,我启动了selenium-server-standalone-2.28.0.jar.有我的考试

<?php
class Example extends PHPUnit_Extensions_Selenium2TestCase
{   protected function setUp()
    {   
    $this->setBrowser("firefox");
        $this->setBrowserUrl('http://test.com/');
    }

    public function testogin()
    {
        $this->url('http://test.com/');
        $this->timeouts()->implicitWait(10000);
        $username = $this->byId('user_login');
        $username->value('test.ru');
        $password = $this->byId('user_pass');
        $password->value('test');
        $this->byId('login_btn')->click();
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

请帮我在其他浏览器上运行此测试.如果您需要更多信息,请问我.谢谢

phpunit selenium-webdriver

11
推荐指数
2
解决办法
5888
查看次数

Laravel尝试单元测试API JSON响应

试图在这里一次学习太多新东西(Laravel,PHPUnit等),所以这可能只是一个疲惫的大脑问题,仍然会欣赏一些帮助.

我有一个非常基本的'博客'项目,使用Laravel作为API层,AngularJS作为前端.我想对API端点进行单元测试,但是在我的测试函数中我无法弄清楚如何处理JSON.

当我尝试运行testGetBlogPosts()时,我看到我的CLI中的JSON输出看起来像什么,但我无法json_decode()并检查对象的某些部分是否符合我的预期结果.在这里,我只想确保结果数组中第一个对象的ID是ID"1".

我从测试中得到的结果是:1)ExampleTest :: testGetBlogPosts ErrorException:试图获取非对象的属性

任何帮助或建议非常感谢!

TL; DR:测试用例未正确处理来自API端点的JSON响应

调节器

class HomeController extends BaseController {

    /*
    |--------------------------------------------------------------------------
    | Default Home Controller
    |--------------------------------------------------------------------------
    |
    | You may wish to use controllers instead of, or in addition to, Closure
    | based routes. That's great! Here is an example controller method to
    | get you started. To route to this controller, just add the route:
    |
    |   Route::get('/', 'HomeController@showWelcome');
    |
    */
    public function showWelcome()
    {
        return View::make('hello');
    }

    public function getBlogPosts()
    { …
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing laravel

11
推荐指数
2
解决办法
2万
查看次数

PhpStorm:算法协商失败

我有一个远程服务器来托管我的项目.我在本地使用我的PhpStorm,因此每次保存时,它都会自动与远程服务器同步.

但是,我无法配置PhpStorm以在远程服务器上运行PHPUnit.

Configure Remote PHP Interpreter我填写正确的信息(主机,用户名和密码).

我遇到的错误是"Algorithm negotiation fail"当我验证并"Test SFTP Connection: Connection to 'id address' failed. Connection failed"尝试指定PHP解释器的路径时.

我该如何解决这个问题?

windows phpunit phpstorm

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

如何在Laravel中测试POST路由

我正在做以下测试对Laravel的POST调用.我希望根据我的路线,问题的POST将作为商店操作方法发送.这适用于浏览器.

我的测试:

public function setUp()
    {   
        parent::setUp();

        Session::start();
    }

    public function testStoreAction()
    {
        $response = $this->call('POST', 'questions', array(
            '_token' => csrf_token(),
        ));

        $this->assertRedirectedTo('questions');
    }
Run Code Online (Sandbox Code Playgroud)

但是,我告诉我重定向不匹配.此外,我可以看到它根本不是商店行动方法.我想知道它将采用什么动作方法,以及它为什么不去存储方法(如果我查看路径:列表我可以看到有一个POST问题/路由应该转到questions.store;这个也适用于浏览器,但不适用于我的测试).此外,我正在为此资源正确编写呼叫吗?我在这里添加了令牌,因为它正在抛出异常,在某些测试中我会让令牌检查通过.

php phpunit laravel

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

Laravel phpunit测试失败了.去意想不到的页面.只在测试中发生

所以,我有一个页面上有一个带有"Create"值的按钮.当我单击"创建"按钮而不填写任何字段时,它会验证表单并在同一页面上显示错误消息.当我在浏览器中这样做时,它工作正常,但当我这样做时phpunit,它有意想不到的结果,我不知道为什么.

这是我的集成测试:

public function testCreateValidation()                                     
{ 
    $this->visit(route('patients.indexes.create', $this->patient->id));    
    $this->press('Create');                                                
    $this->seePageIs(route('patients.indexes.create', $this->patient->id));              
}   
Run Code Online (Sandbox Code Playgroud)

这就是结果:

There was 1 failure:
1) Tests\Integration\IndexControllerTest::testCreateValidation
Did not land on expected page [http://localhost/patients/69/indexes/create].

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'http://localhost/patients/69/indexes/create'
+'http://localhost/patients'

/vagrant/vendor/laravel/framework/src/Illuminate/Foundation/Testing/InteractsWithPages.php:141
/vagrant/tests/Integration/IndexControllerTest.php:51
Run Code Online (Sandbox Code Playgroud)

我不明白为什么它被重定向到patients页面.

以下是create正在测试的Laravel 方法:

public function create($id)                                                         
{                                                                                   
    $index = $this->indexes->newInstance();                                         
    $patient = $this->patients->findOrFail($id);                                    

    return view('patient.index.create', ['index' => $index, 'patient' => $patient]);
} 
Run Code Online (Sandbox Code Playgroud)

以下是该create视图的相关部分:

<?= Form::open(['route' => array('patients.indexes.store', $patient->id), 'class' …
Run Code Online (Sandbox Code Playgroud)

php phpunit unit-testing laravel laravel-5

11
推荐指数
1
解决办法
1138
查看次数

在phpunit中运行一个文件或映射

我正在使用laravel并且我已经编写了一些测试文件.但是我怎么能只执行一个文件呢?当我这样做时:phpunit tests/resulttesting/school/deleteSchoolForRealTest它抛出一个错误:

无法打开文件"tests/resulttesting/school/deleteSchoolForRealTest.php".

当我运行phpunit时,它会运行我的所有测试.我怎么能只执行一个文件夹?我在Mac上.

php phpunit laravel

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

Laravel单元测试 - 根据测试方法更改配置值

我有一个用于验证帐户的系统的应用程序(注册 - >获取带有激活链接的电子邮件 - >已验证帐户).该验证流程是可选的,可以使用配置值关闭:

// config/auth.php
return [
  // ...
  'enable_verification' => true
];
Run Code Online (Sandbox Code Playgroud)

我想测试注册控制器:

  • 它应该在两种情况下都重定向到主页
  • 验证开启时,主页应显示"已发送电子邮件"消息
  • 验证关闭时,主页应显示"已创建帐户"消息
  • 等等

我的测试方法:

public function test_UserProperlyCreated_WithVerificationDisabled()
{
    $this->app['config']->set('auth.verification.enabled', false);

    $this
        ->visit(route('frontend.auth.register.form'))
        ->type('Test', 'name')
        ->type('test@example.com', 'email')
        ->type('123123', 'password')
        ->type('123123', 'password_confirmation')
        ->press('Register');

    $this
        ->seePageIs('/')
        ->see(trans('auth.registration.complete'));
}

public function test_UserProperlyCreated_WithVerificationEnabled()
{
    $this->app['config']->set('auth.verification.enabled', true);

    $this
        ->visit(route('frontend.auth.register.form'))
        ->type('Test', 'name')
        ->type('test@example.com', 'email')
        ->type('123123', 'password')
        ->type('123123', 'password_confirmation')
        ->press('Register');

    $this
        ->seePageIs('/')
        ->see(trans('auth.registration.needs_verification'));
}
Run Code Online (Sandbox Code Playgroud)

在调试时,我注意到在控制器方法内部的配置值总是设置为配置文件中的值,无论我用什么设置我的 $this->app['config']->set...

我对用户存储库本身进行了其他测试,以检查在验证为ON或OFF时它是否都有效.测试的行为符合预期.

知道为什么控制器失败以及如何解决这个问题?

php phpunit unit-testing laravel

11
推荐指数
2
解决办法
3990
查看次数