SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHPUnit and HTTP test

    I am being pedantic but...

    I am implementing unit test for a framework and the one component I cannot figure out how to test is the Request object, primarily because it is a simple wrapper around the $_REQUEST super global. I need to either fake HTTP request to properly populate this variable (at least PHP 5.3.4) or I need to remove the dependency and initialize the request object using $_REQUEST or whatever I wish.

    While this seems the logical solution (I am always in favour of DI/IoC) I dislike the solution in this case, for reasons I do not wish to get into right now, or I fear this discussion taking on a whole different purpose.

    What I want to know, is there a work around in PHPUnit (perhaps using the now built-in HTTP server of PHP 5.4.0 binary?) to allow me to test my framework at the Request object level **without** changing the interface/implementation of my request object (ie: injecting $_REQUEST)?

    Currently the Request simply acts as a simple wrapper around the $_REQUEST super global - and it does not seem possible to populate super globals manually within the test itself.

    Remember - I am not looking to change the code at all in my Request object so using globals, etc is not the solution I seek. Rather a PHPUnit extension or technique that allows me to invoke the tests in an HTTP context as opposed to CLI.

    Regards,
    Alex
    The only constant in software is change itself

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    FWIW, it really sounds like you should be injecting a mock $_REQUEST array into the Request object; after all, that's what you're testing no?

    Have you thought about using a PHP based HTTP client such as Guzzle or Buzz and emulating a HTTP request with that?
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    FWIW, it really sounds like you should be injecting a mock $_REQUEST array into the Request object; after all, that's what you're testing no?
    I really want to avoid that, I've tried that approach, and while it circumvents my testing issue, it introduces other problems. Thats a whole different discussion though.

    Buzz looks interesting, but I am not sure it's what I am looking for. An HTTP server is what I require, I think anyway.

    Basically my unit tests are executed under the context of CLI environment - as per usual. This works 99% of the time, except when testing my controllers, because they rely on the request object having all required HTTP details. There might be a few instances where views depend on request objects as well, not 100% certain though.

    EDIT | Seems like the only way to test controllers without a mock request object is to use Selenium/PHPUnit or switch over to SimpleTest which seems to support web testing.
    The only constant in software is change itself


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •