I'm following through a video series on Laracasts to help me understand how to run tests in Laravel.
The test described is:
$this->visit('/')
->type('some query', '#term')
->press('Search')
->see('Search results for "some query"')
->onPage('search-results');
However Laravel complains that onPage is not a valid method. I've looked through the docs but can't seem to find a change which best describes what alternative method to use.
I tried to do:
$this->visit('/')
->type('some query', '#term')
->press('Search')
->visit('/search-results')
->see('Search results for "some query"');
But it seems that the behaviour of that is to redirect to /search-results before the form submits, meaning the output message is not the same and the assertion fails.
You can use seePageIs method.
Source code
Assert that the current page matches a given URI.
You can see more tips at the docs.
Seems like the method was removed. I think you can use $this->seePageIs() instead
https://github.com/laravel/framework/blob/5.3/src/Illuminate/Foundation/Testing/Concerns/InteractsWithPages.php#L176
Related
I'm trying out Lumen and I've written my first route:
$app->get('hello/{name}', function ($name) {
return "Hello, $name!";
});
The documentation I've read so far doesn't say a word about how the framework approaches HTML-injection so I made a quick test:
http://example.com/hello/<u>café
... which showed up like this:
Hello, %3Cu%3Ecaf%C3%A9!
In other words, route parameters are not URL-decoded when they reach my function. Other frameworks I've tried decode everything for you, just as if you were reading from $_GET.
Is there an issue with my set-up (Apache/2.4, mod_php and mod_rewrite with the default .htaccess bundled with the framework) or that's the expected input?
It's an intentional bug/feature (see Route parameters are URL-encoded #238 for reference). No idea about the rationale behind the design decision.
I'm attempting to port a portion of a Laravel 4 app to Lumen, but I'm unable to figure out the equivalent of Laravel 4's Redirect::away(...); function for Lumen.
I've tried keeping it the same which doesn't work. I've also tried return redirect()->away($location); as suggested in a similar question I found, but that also fails with the error Call to undefined method Laravel\Lumen\Http\Redirector::away().
I feel like the answer to this is really simple, but unfortunately it's not documented anywhere and I can't figure out the right combination of things to get it to work.
My fallback is to use header('Location : '.$location); but would prefer to avoid it if there's a built in way to do it.
You should be fine to do return redirect($location) in most cases.
See https://medium.com/#zwacky/laravel-redirect-to-vs-redirect-away-dd875579951f for the minor differences (it'll trim() your URL and check that it's valid).
I want to use the method match(\Zend\Stdlib\RequestInterface) of Zend\Mvc\Router\Http\TreeRouteStack.
But I don't find any way to get the request I'm searching for.
This articles says how to get a request object outside a controler. I'm searching for an equivalent with ZF2
Finally I found how to fix by browsing the unit testing of the zf2
https://github.com/zendframework/zf2/blob/master/tests/ZendTest/Mvc/Router/Http/TreeRouteStackTest.php
The Zend\Http\PhpEnvironment\Request; will give a valid Request for the match method
use Zend\Http\PhpEnvironment\Request as PhpRequest;
/*TreeRouteStack*/
$request = new PhpRequest();
$stack->match($request);
When I'm trying to execute a functional unittest of a module within my Yii code, I keep receiving the following error:
CException: CHttpRequest is unable to determine the request URI.
At first, I though it was because it couldn't find the module. However, If I change the url to a wrong one, I get a correct error,s tating it couldn't find the view.
This is how my testing code looks like
public function testViewControllerModule()
{
ob_start();
Yii::app()->runController('module/controller/view');
}
Any ideas on what I might be missing?
bool.devs answer works so far.
This blog post explains the origin of the exception pretty well:
http://mattmccormick.ca/2012/09/14/unit-testing-url-routes-in-yii-framework/
In my case, I generalized the solution and have set the following variables in /www/protected/tests/bootstrap.php:
...
$_SERVER['SCRIPT_FILENAME'] = 'index-test.php';
$_SERVER['SCRIPT_NAME'] = '/index-test.php';
$_SERVER['REQUEST_URI'] = 'index-test.php';
Yii::createWebApplication($config);
Consider using 'index-test.php' instead of 'index.php' because it contains the config 'test.php' which is responsible for fixtures and maybe other test relevated configurations.
If someone has better suggestions feel free to comment :)
Kind regards
I think it's because you haven't set any server variables, i.e $_SERVER and you might be doing something like this in your controller:
Yii::app()->request ....
So before you run your test, make sure you use a fixture for the server variables also. I think this should suffice for now:
$_SERVER=array(
'REQUEST_URI'=>'index.php', // the other fields should follow
);
However to run functional tests i would recommend using SeleniumRC, you won't have to do these workarounds then, and can simulate user clicks also, i think.
Read the initial guide to Functional Testing , read the selenium rc phpunit guide, and also the CWebTestCase documentation.
Notes: You might still have to use fixtures for some variables, and i don't have much experience in testing(which is bad), so i'm not very sure if i am completely correct about selenium.
In PHP/Kohana, I have controller action method which does some processing. When it is finished, I want to send it to another controller, e.g.:
public function action_import_csv()
{
Kohana_Import_Driver_Csv::process_files_from_csv_to_mysql($this->import_directory);
//url::redirect(Route::get('backend_application')->uri()); //undefined method URL::redirect()
//redirect(Route::get('backend_application')->uri(), null); //undefined function
}
According to this documentation at least the first redirect should work. I'm using Kohana 3.
How can I send execution from this controller action method to a new controller/action?
Addendum
For some reason, url::redirect is not available, here is the code completion I get for url:::
#bharath, I tried url::current() and got this error:
The problem is that you are looking at the Kohana 2 docs. Go to the kohana homepage and find the correct docs. Also, for some reason, everyone is giving you Kohana 2 answers even though you stated you're working with 3.
To redirect, do this from the context of a controller:
$this->request->redirect($something);
$something could be:
controller
controller/action
http://url.com
Here are the api docs for the redirect method (note that this uses url::site to parse the url; you may want to look at the source of that method too.
i am not very sure but i think you can simple use the redirect() function passing in the other controller you want to send to with any parameters
example
redirect(controllername/method)
Shouldn't that be :
url::redirect('controller/method');
And if it doesn't work, you probably had some output before calling the redirect (you'll probably get the "Headers already sent" error when that is the case).