views:

1582

answers:

3

In CakePHP putting a querystring in the url doesn't cause it to be automatically parsed and split like it normally is when the controller is directly invoked.

For example:

$this->testAction('/testing/post?company=utCompany', array('return' => 'vars')) ;

will result in:

[url] => /testing/post?company=utCompany

While invoking the url directly via the web browser results in:

[url] => Array
    (
        [url] => testing/post
        [company] => utCompany
    )

Without editing the CakePHP source, is there some way to have the querystring split when running unit tests?

+2  A: 

I have what is either a hack (i.e. may not work for future CakePHP releases) or an undocumented feature.

If the second testAction parameter includes an named array called 'url' then the values will be placed in the $this->params object in the controller. This gives us the same net result as when the controller is directly invoked.

$data = array ('company' => 'utCompany') ;

$result = $this->testAction('/testing/post', array
(
    'return' => 'vars', 
    'method' => 'get', 
    'url' => $data)
) ;

I'm satisfied with this method for what I need to do. I'll open the question to the community shortly so that it in the future a better answer can be provided.

Ryan Boucher
A: 

CakePHP does provide some level of url splitting but it only seems to work in the run-time configuration and not the test configuration. I'll contact the CakePHP if this is intentional.

I suggestion for your querystring parser would be to use the PHP function explode.

I believe you can do something like this:

$result = explode ('&', $queryString, -1) ;

which would give you your key-pairs in seperate array slots upon which you can iterate and perform a second explode like so:

$keyPair = explode ('=', $result[n], -1) ;

However, all this being said it would be better to peek under the hood of CakePHP and see what they are doing.

What I typed above won't correctly handle situations where your querystring contains html escaped characters (prefixed with &), nor will it handle hex encoded url strings.

Ryan Boucher
A: 

use _GET['parmname'];

islam