2

i write a test for my controller,it need $_SERVER[REQUEST_URI],how can i mock this and make it run my controller is

public function action_index()
{
  $presenter = Presenter::forge('www/php');
  $this->template->content = $presenter;
}

my test is

public function test_action_index()
{
  $response = Request::forge('www/php')
    ->set_method('GET')
    ->execute()
    ->response();
  $this->assertContains(' List', $response->body->__toString());
  Cli::write(__METHOD__ . "   √ \n", 'green');
}
yang
  • 183
  • 12
  • 1
    i think testing controllers its not a business of PhpUnit. I d better use some E2E testing for that – M0rtiis Sep 21 '15 at 04:08

1 Answers1

0
  1. You can set this global yourself, but it's very ugly.
  2. You can set it via SetEnv (see this blog :http://westsworld.dk/blog/2011/01/handle-different-environments-with-php/)
  3. But, it's better to change your line with $_SERVER['*']

Why?

The description of an unit test is:

a small isolated test of your method

Your code will depend on globals! No! Use your request object for example to get the uri.

schellingerht
  • 5,726
  • 2
  • 28
  • 56