我使用它FacebookBundle来验证我的Symfony2应用程序中的用户.但是,我想用phpunit创建功能测试,该测试使用经过身份验证的用户.
此外,我不想使用facebook用户,而是使用一个用户.
有人知道如何实现这个吗?
请考虑以下方案(这不是生产代码):
class MyClass {
public function myMethod() {
// create a directory
$path = sys_get_temp_dir() . '/' . md5(rand());
if(!mkdir($path)) {
throw new Exception("mkdir() failed.");
}
// create a file in that folder
$myFile = fopen("$path/myFile.txt", "w");
if(!$myFile) {
throw new Exception("Cannot open file handle.");
}
}
}
Run Code Online (Sandbox Code Playgroud)
对,那有什么问题?代码覆盖率报告此行未涵盖:
throw new Exception("Cannot open file handle.");
Run Code Online (Sandbox Code Playgroud)
哪个是正确的,但是因为我在逻辑上创建了上面的文件夹,所以fopen()失败似乎是不可能的(除非在极端情况下,例如100%的磁盘).
我可以忽略代码覆盖中的代码但这有点作弊.有什么方法可以模拟文件系统,以便它可以识别myFile.txt和模拟无法创建文件的文件系统?
我遇到了一个有趣的问题.我正在使用PHPUnit,我的测试每次运行时都会占用更多内存.IE浏览器...
2.25 MB
2.5 MB
3.0 MB
3.5 MB .......
有谁知道如何清除正在消耗的内存,任何人都可以建议我深入探讨这个问题吗?当前的问题是我的一些较大的测试耗尽了内存,并且只是继续增加PHP中的最大内存分配还不够......我需要知道为什么从命令行运行的PHPUnit测试会有内存在运行之间"粘附"的用法.
我正在尝试为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编码体的测试方法?
我使用本教程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)
请帮我在其他浏览器上运行此测试.如果您需要更多信息,请问我.谢谢
我试图模拟一个对象,该对象获得两个相同函数但具有不同参数的调用.为多个调用返回不同的返回值非常简单但我无法在任何地方找到如何使用参数验证来执行此操作.
我试过了:
$this->eventDispatcher
->shouldReceive('dispatch')
->twice()
->with(Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event'))
->with(Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event');
Run Code Online (Sandbox Code Playgroud)
和
$this->eventDispatcher
->shouldReceive('dispatch')
->twice()
->with(
[Events::SELECT,\Mockery::type('\Not\Really\A\Namespace\Event')],
[Events::ACTIVITY,\Mockery::type('\Not\Really\A\Namespace\Event')]
);
Run Code Online (Sandbox Code Playgroud)
但他们不工作.
从输出PHPUnit给我看起来我似乎得到一个数组?
我有一个远程服务器来托管我的项目.我在本地使用我的PhpStorm,因此每次保存时,它都会自动与远程服务器同步.
但是,我无法配置PhpStorm以在远程服务器上运行PHPUnit.
在Configure Remote PHP Interpreter我填写正确的信息(主机,用户名和密码).
我遇到的错误是"Algorithm negotiation fail"当我验证并"Test SFTP Connection: Connection to 'id address' failed. Connection failed"尝试指定PHP解释器的路径时.
我该如何解决这个问题?
我定义了一个测试用户创建的测试.控制器设置为在出错时重定向回同一页面(通过生成验证App\Http\Requests\Request).这在手动单击浏览器时可正常工作,但在测试期间失败.而不是重定向到:
http://localhost/account/create
Run Code Online (Sandbox Code Playgroud)
测试重定向到(缺少斜线):
http://localhostaccount/create
Run Code Online (Sandbox Code Playgroud)
这些网址都不是我在.htaccess或$urlconfig/app.php 中的变量中设置的.哪个是(在OSX Yosemite上):
http://~username/laravel_projects/projectname/public
Run Code Online (Sandbox Code Playgroud)
我最终指出这个问题与如何Request::root()产生结果有关.在测试之外调用此结果会导致在.htaccess和.htaccess中定义的期望值$url.在测试中,它导致:
http://localhost
Run Code Online (Sandbox Code Playgroud)
需要更改哪些配置才能使此函数在两个上下文中返回正确的值?
我还要提到我从Laravel 4到当前版本5.0.27的痛苦升级.
******更新*******
我能够找到一个可接受的解决方案/解决方案来解决这个问题!
在Laravel 5中,引入了FormRequests以帮助将验证逻辑移出控制器.一旦请求映射到控制器,如果指定了FormRequest(或只是Request),则在执行控制器操作之前执行此操作.
这种FormRequest默认处理响应如果验证失败.它会尝试根据您发布表单数据的路径构建重定向.在我的情况下,可能与我从Laravel 4更新到5的错误有关,这个默认重定向的构造不正确.用于处理响应的Laravel System代码如下所示:
/**
* Get the proper failed validation response for the request.
*
* @param array $errors
* @return \Symfony\Component\HttpFoundation\Response
*/
public function response(array $errors)
{
if ($this->ajax() || $this->wantsJson())
{
return new JsonResponse($errors, 422);
}
return $this->redirector->to($this->getRedirectUrl())
->withInput($this->except($this->dontFlash))
->withErrors($errors, $this->errorBag);
}
Run Code Online (Sandbox Code Playgroud)
请注意返回的重定向与调用的方式不同 …
我正在使用laravel并且我已经编写了一些测试文件.但是我怎么能只执行一个文件呢?当我这样做时:phpunit tests/resulttesting/school/deleteSchoolForRealTest它抛出一个错误:
无法打开文件"tests/resulttesting/school/deleteSchoolForRealTest.php".
当我运行phpunit时,它会运行我的所有测试.我怎么能只执行一个文件夹?我在Mac上.
我有一个用于验证帐户的系统的应用程序(注册 - >获取带有激活链接的电子邮件 - >已验证帐户).该验证流程是可选的,可以使用配置值关闭:
// 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时它是否都有效.测试的行为符合预期.
知道为什么控制器失败以及如何解决这个问题?