Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit testing applications using SlmLocale #26

Closed
intellix opened this issue Aug 28, 2013 · 6 comments
Closed

Unit testing applications using SlmLocale #26

intellix opened this issue Aug 28, 2013 · 6 comments

Comments

@intellix
Copy link

I'm attempting to create unit tests for my application which makes use of SlmLocale UriPathStrategy and am having issues with testing controller dispatches like so:

public function testLoginActionCanBeAccessed()
{
    $this->dispatch('/en/account/login');
    $this->assertResponseStatusCode(200);
}

This returns 302 and when I dump the RequestURI within UriPathStrategy, I'm getting the following twice:

class Zend\Uri\Http#819 (10) {
  protected $validHostTypes => int(19)
  protected $user => NULL
  protected $password => NULL
  protected $scheme => string(4) "http"
  protected $userInfo =>  NULL
  protected $host => NULL
  protected $port => NULL
  protected $path => string(1) "/"
  protected $query => NULL
  protected $fragment => NULL
}

Whereas when I dump the RequestURI from a typical request via my browser:

object(Zend\Uri\Http)[210]
  protected 'validHostTypes' => int 19
  protected 'user' => null
  protected 'password' => null
  protected 'scheme' => string 'http' (length=4)
  protected 'userInfo' => null
  protected 'host' => string 'konoro.dev' (length=16)
  protected 'port' => null
  protected 'path' => string '/en/' (length=4)
  protected 'query' => string '' (length=0)
  protected 'fragment' => null

From what I've been reading in the code, it looks like the only time detection is done (and redirection) is onBootstrap of the Module. So the unit test defaults to / without a locale and every other dispatch uses the same requestUri and it needs to redirect to /en/ with them all.

Are you doing anything in particular when testing your stuff or does there need to be some change so the detection is done onDispatch() instead?

Thanks, Dom

@juriansluiman
Copy link
Collaborator

Do you use the latest master? I have, because of several issues, made a refactoring branch which I need to finish with unit tests. There are several changes internally in the new branch, but for you the most important one: the redirect is made via short circuiting the route event.

Instead of sending a response in every strategy, the strategy modifies the response and returns it. The response will be returned during route so it now happens in the "normal" zf2 flow. Check the return here: https://github.com/juriansluiman/SlmLocale/blob/feature/refactoring/Module.php#L71

I can understand there are many difficulties with testing the current master with SlmLocale. Can you switch to the feature/refactoring branch and see what happens?

@intellix
Copy link
Author

I was using the latest master. Just tried with feature/refactoring and with each of the following, I was getting response of 302 when I tested for 200:

/en/account/login
en/account/login
/account/login
account/login

When I dump the URI inside UriPathStrategy I'm getting the path: / which then gets transformed to /en/ and redirects to that.

@juriansluiman
Copy link
Collaborator

@intellix to be honest, I haven't made such tests yet and have thus no experience in using SlmLocale with controller tests. The only thing to be certain SlmLocale is causing this: disabling the module fixes the problem?

Hopefully I have some time to set these things up and test SlmLocale, but it's unfortunately not happening in the next days.

@tigs001
Copy link

tigs001 commented Nov 11, 2014

If you are attempting to test your code and are NOT testing the Locale capabilities of SlmLocale, you can remove the module SlmLocale from the configuration.

I did this by using the following code in as my setUp() function.

public function setUp()
{
    $config = include './config/application.config.php'
            ;

    $ind = array_search('SlmLocale', $config['modules']);
    if ($ind !== false)
    {
        $newconf = array_values(array_diff($config['modules'], array('SlmLocale')));
        $config['modules'] = $newconf;
    }

    $this->setApplicationConfig($config);

    parent::setUp();
}

With the above in place you can set up your testing the normal ZF2, PHPUnit way. You specify your URI paths without the SlmLocale locale code (e.g. use "/account/login" instead of "/en/account/login") in the path.

Of course if you are testing SlmLocale, then the above is not a suitable work around.

@basz
Copy link
Owner

basz commented Mar 14, 2017

Hi, maintenance of this package has been taken over by me. If you still feel this issue is valid please go ahead and create a PR or contribute in any other manner. It's appreciated.

@koseduhemak
Copy link
Contributor

Having the same issue. UriPathStrategy does not work with phpunit tests. Response code will be 302 no matter if you try with /en/test or /test and redirection is set to / as @intellix wrote.
So i wrote a little fix which disables the strategy for phpunit initiated requests completely unless someone has got a better idea.

Will issue pull request soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants