沃梦达 / 编程问答 / php问题 / 正文

Laravel phpunit 测试获取参数

Laravel phpunit testing get with parameters(Laravel phpunit 测试获取参数)

本文介绍了Laravel phpunit 测试获取参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的控制器编写一些测试,但其中一个测试不起作用.应该搜索并将结果返回到页面.但它实际上是重定向到主页.这是我的代码:

I am writing some tests for my controllers but one of my tests doesn't work. It's supossed to search and get the results back to the page. But it's actually redirecting to the home page. Here is my code:

use DatabaseMigrations;
protected $user;
public function setUp()
{
    parent::setUp();

    $this->seed();

    $this->user = factory(User::class)->create(['role_id' => 3]);
}

/** @test */
public function test_manage_search_user()
{
    $response = $this->followingRedirects()->actingAs($this->user)->get('/manage/users/search', [
        'choices' => 'username',
        'search' => $this->user->username,
    ]);

    $response->assertViewIs('manage.users');
    $response->assertSuccessful();
    $response->assertSee($this->user->email);
}

您应该获得的 URL 如下所示:

The URL you should get to make it work look like this:

http://localhost/manage/users/search?choices=username&search=Test

我再次检查,它似乎没有在 get 请求的参数中给出.我该如何解决这个问题?

I checked again and it looks like it's not given in the parameters with the get request. How can I fix this?

推荐答案

我在尝试测试 GET 请求时遇到了同样的问题,您实际上无法使用 $this- 传递参数>get('uri', [header]) 但你可以使用 $this->call,如果你签入 MakesHttpRequests.php 你可以看到this->get()其实是在使用call方法.

I had the same issue trying to test GET Requests, you actually can't pass parameter with the $this->get('uri', [header]) but you can by using $this->call, if you check in MakesHttpRequests.php you can see that this->get() is actually using call method.

通过将数组添加到 get 方法,您正在更改请求标头,这就是您没有获取参数的原因.

By adding an array to get method, you are changing the request headers, this is why you are not getting your parameters.

public function get($uri, array $headers = [])
{
    $server = $this->transformHeadersToServerVars($headers);

    return $this->call('GET', $uri, [], [], [], $server);
}

public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
{
    $kernel = $this->app->make(HttpKernel::class);

    $files = array_merge($files, $this->extractFilesFromDataArray($parameters));

    $symfonyRequest = SymfonyRequest::create(
        $this->prepareUrlForRequest($uri), $method, $parameters,
        $cookies, $files, array_replace($this->serverVariables, $server), $content
    );

    $response = $kernel->handle(
        $request = Request::createFromBase($symfonyRequest)
    );

    if ($this->followRedirects) {
        $response = $this->followRedirects($response);
    }

    $kernel->terminate($request, $response);

    return $this->createTestResponse($response);
}

所以如果你想测试一个 GET 请求,你必须这样做:

So if you want to test a GET Request you will have to do this:

$request = $this->call('GET', '/myController', ["test"=>"test"]);

在您的控制器中,您应该能够像这样获得这些参数:

In your controller you should be able to get theses parameters like so:

public function myController(Request $request)
{
    $requestContent = $request->all();
    $parameter = $requestContent['test'];
}

这篇关于Laravel phpunit 测试获取参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel phpunit 测试获取参数