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); }
The URL you should get to make it work look like this:
http://localhost/manage/users/search?choices=username&search=Test
I checked again and it looks like it's not given in the parameters with the get request. How can I fix this?
PHPUnit is a unit testing framework for the PHP programming language. It is an instance of the xUnit design for unit testing systems that began with SUnit and became popular with JUnit. Even a small software development project usually takes hours of hard work.
Laravel is built with testing in mind. In fact, support for testing with PHPUnit is included out of the box and a phpunit.xml file is already set up for your application. The framework also ships with convenient helper methods that allow you to expressively test your applications.
} The get method makes a GET request into the application, while the assertStatus method asserts that the returned response should have the given HTTP status code. In addition to this simple assertion, Laravel also contains a variety of assertions for inspecting the response headers, content, JSON structure, and more.
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.
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); }
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']; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With