Hi i am trying to set a a test to test my database calls. I want to pass a variables in url but cant seem to get it to work.
I wanted to do this
public function testDb()
{
$response = $this->call('GET', '/searchAvailability?keyword=test product');
$content = $response->getContent();
$this->assertEquals('[{"name":"test product"}]',$content );
}
But i keep getting "Undefined variable : keyword" when i try. It works in the browser just not when i run phpunit. Anyone got any ideas on why this is not working thanks.
The answer here is you need to specify the parameters differently in your call method:
$this->call('GET', '/searchAvailability', array('keyword' => 'test product'));
Below is the implementation of Illuminate\Foundation\Testing\TestCase::call method:
/**
* Call the given URI and return the Response.
*
* #param string $method
* #param string $uri
* #param array $parameters
* #param array $files
* #param array $server
* #param string $content
* #param bool $changeHistory
* #return \Illuminate\Http\Response
*/
public function call()
{
call_user_func_array(array($this->client, 'request'), func_get_args());
return $this->client->getResponse();
}
Related
I just wrote a function like this
/**
* Send an asynchronous GET request
*
* #param string $url
* #param array $options
*
* #return \React\Promise\ExtendedPromiseInterface
*/
public function getAsync( $url, array $options = [] );
but when making docblock, I realized that #return \React\Promise\ExtendedPromiseInterface is very generic and doesn't really help client understand what returns are to be expected in case of rejection or fulfillment.
Is there some established convention for documenting which values or exception are expected as a result of this function so that the client could chain on this function by looking at the interface only?
For exceptions you can add:
/**
* #throws customException if the bad thing happens
*/
You could add as many of these as you like too. After the #return you can add a type before and a short description of what it is after.
Not having found anything, I ended up making this up
/**
* Send an asynchronous GET request
*
* #param string $url
* #param array $options
*
* #return \React\Promise\ExtendedPromiseInterface
*
* #promise.resolve \MyApp\Interfaces\ResponseInterface
* #promise.reject \MyApp\Exceptions\RequestException
*/
public function getAsync( $url, array $options = [] );
I'm trying to make a custom redirect function. I have created a custom route function in a new file (helpers.php) that works fine:
if (! function_exists('cms_route')) {
/**
* Generate a URL to a named route with predefined cms path.
*
* #param string $name
* #param array $parameters
* #param bool $absolute
* #param \Illuminate\Routing\Route $route
* #return string
*/
function cms_route($name, $parameters = [], $absolute = true, $route = null)
{
return app('url')->route(config('constants.cms_path').'.'.$name, $parameters, $absolute, $route);
}
}
I'm trying to call this function with redirect()->cms_route('name') instead of redirect()->route('name')
So when the cms path is changed everything keeps working.
How would I accomplish this?
Added as quick fix:
if (! function_exists('cms_redirect')) {
/**
* Get an instance of the redirector.
*
* #param string $name
* #param array $parameters
* #param bool $absolute
* #param \Illuminate\Routing\Route $route
* #return \Illuminate\Routing\Redirector|\Illuminate\Http\RedirectResponse
*/
function cms_redirect($name, $parameters = [])
{
return redirect()->route(config('constants.cms_path').'.'.$name, $parameters);
}
}
According to:
/**
* Call the given URI and return the Response.
*
* #param string $method
* #param string $uri
* #param array $parameters
* #param array $files
* #param array $server
* #param string $content
* #param bool $changeHistory
* #return \Illuminate\Http\Response
*/
public function call()
In order to pass headers, I set them in the $server parameter, which I'm doing like so:
public function testCreateLocation(){
$response = $this->call('POST', '/api/v1/token', $this->test_token_request);
$tokenObject = json_decode($response->getContent());
/** Run the test */
$response = $this->call('POST', '/api/v1/location', $this->test_1_create_tag,
[], ['Authorization' => 'Bearer '.$tokenObject->token]);
/** Read the response */
$this->assertResponseOk();
}
However, when I run the unit test, I get the following error:
vagrant#homestead:~/Code$ php phpunit.phar tests/LocationModelTest.php
PHPUnit 4.6.2 by Sebastian Bergmann and contributors.
Configuration read from /home/vagrant/Code/phpunit.xml.dist
EFF
Time: 3.75 seconds, Memory: 35.75Mb
There was 1 error:
1) LocationModelTest::testCreateLocation
InvalidArgumentException: An uploaded file must be an array or an instance of UploadedFile.
I've tried passing in null and an empty array as it states, but the error is never resolved. Anyone have any ideas?
I ran into this problem on Laravel 5.1 as well. Once I checked the up to date documentation on the call() method I saw the issue though:
call(string $method, string $uri, array $parameters = array(), **array $cookies = array(),** array $files = array(), array $server = array(), string $content = null)
Somewhere along the way an extra parameter (cookies) got added to the method.
So add an extra empty array and you should be good:
$response = $this->call('POST', '/api/v1/location', $this->test_1_create_tag, [], [], ['Authorization' => 'Bearer '.$tokenObject->token]);
As the documentation says the function Redirect::action() receives a string which is separated into 2 parts by the symbol #
The controller name
The method name
e.g. Redirect::action('MyController#myFunction')
I've recently tried to give the function an input: Redirect::action('someRouteName') and see what's gonna happen. Surprisingly it didn't return with an error but actually made the link just as if I was using the Redirect::route() function (I had a route named as someRouteName).
Does the function Redirect::action() falls back to Redirect::route() if the value it gets is invalid? Couldn't find any source that says that.
Yes, it does. Some insight on it can be seen in sources.
https://github.com/laravel/framework/blob/master/src/Illuminate/Routing/UrlGenerator.php#L455
/**
* Get the URL to a controller action.
*
* #param string $action
* #param mixed $parameters
* #param bool $absolute
* #return string
*/
public function action($action, $parameters = array(), $absolute = true)
{
return $this->route($action, $parameters, $absolute, $this->routes->getByAction($action));
}
I like clean docs and phpdoc will automagically look up the type. When documenting a controller function that returns View::make, I have no idea what type to use for the #return in my documentation.
<?php
class FooController extends BaseController {
/**
* Show a view.
*
* #return ??? description of the view
*/
public function show(){
return View::make('bar');
}
}
What is the type here or is there a better way to document the function for this purpose?
The return value is
Illuminate\View\View
I traced through the ServiceProvider which lead me to
Illuminate\View\Environment::make
Which is line 113 of vendor/laravel/framework/src/Illuminate/View/Environment.php (in 4.1 at least)
/**
* Get a evaluated view contents for the given view.
*
* #param string $view
* #param array $data
* #param array $mergeData
* #return \Illuminate\View\View
*/
public function make($view, $data = array(), $mergeData = array())
{
$path = $this->finder->find($view);
$data = array_merge($mergeData, $this->parseData($data));
$this->callCreator($view = new View($this, $this->getEngineFromPath($path), $view, $path, $data));
return $view;
}