SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    London
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    SimpleTest howto test a RequestParameter

    Hi,

    Im using SimpleTest and i am wondering about testing global variables
    Below is a example showing what im trying to accomplish
    PHP Code:
    class TestOfExampleWebTest extends WebTestCase
    {
      public function 
    setUp() {
        
    $this->url 'http://localhost/index.php?hello=world';
      }

      public function 
    testHomepageExists() 
      {
         
    $this->assertTrue($this->get($this->url));

         
    // how can i check the request parameters using SimpleTest
         
    $this->assertRequestParameter('hello''world');
      }

    I know that the method assertRequestParameter does not exists.

    David Stevens
    David Stevens, create-inspire
    PHP London, www.phplondon.org

  2. #2
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your request seems kind of backwards to me. The fact that ?hello=world gets translated to $_REQUEST['hello'] = 'world'; is just an artifact of what PHP does with the url. Shouldn't you be testing the result of the web page which depends on that request parameter. Should there be certain text there depending on the value of hello? Should additional links appear? What is the function of the hello parameter, that is what you should be testing with the web test case.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    London
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi sweatje,

    I totally understand what you are saying.
    Ok im using simpletest with symfony instead of the lime test framework.
    It seems symfony automatically clear all request, get, post arrays, symfony filters take over.
    So i was wondering if simpletest was able to check a http request and parse any parameters into something testable.

    PHP Code:
    class TestOfExampleWebTest extends WebTestCase
    {
      public function 
    setUp() {
        
    $this->url 'http://localhost/index.php?foo=bar';
      }

      public function 
    testHomepageExists() 
      {
         
    $this->assertTrue($this->get($this->url), array('hello'=>'world'));

         
    // how can i check the request parameters using SimpleTest
         //$this->assertRequestParameter('hello', 'world');
         
    var_dump($_GET);
         
    var_dump($_REQUEST);
      }


    // displays empty globals
    array(0) {
    }
    array(
    0) {

    Last edited by davro; May 30, 2007 at 11:22.
    David Stevens, create-inspire
    PHP London, www.phplondon.org

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by davro View Post
    So i was wondering if simpletest was able to check a http request and parse any parameters into something testable.
    Why?

    You can use parse_url, but it's rather pointless, since you're supplying the URL yourself. The only reason, why the URL in the response would be different from the one, you're requesting, is if there was a redirect.

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    London
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi kyberfabrikken,

    Why Not ?

    It hardly worth parsing the url when the relevant data is encapsulated with in the object model, eg
    But saying that only the RequestParameters from the actual supplied url show up, The RequestParams passed via $this->get($this->url, array('hello'=>'world'))
    seem to be missing from the SimpleUrl object ?
    PHP Code:
    var_dump($this->_browser->_page->_url);

    object(SimpleUrl)#20 (12) {
      
    ["_scheme"]=>
      
    string(4"http"
      
    ["_username"]=>
      
    bool(false)
      [
    "_password"]=>
      
    bool(false)
      [
    "_host"]=>
      
    string(9"localhost"
      
    ["_port"]=>
      
    bool(false)
      [
    "_path"]=>
      
    string(10"/index.php"
      
    ["_request"]=>
      
    object(SimpleGetEncoding)#21 (1) {
        
    ["_request"]=>
        array(
    1) {
          [
    0]=>
          
    object(SimpleEncodedPair)#22 (2) {
            
    ["_key"]=>
            
    string(3"foo"
            
    ["_value"]=>
            
    string(3"bar"
          
    }
        }
      }
      [
    "_fragment"]=>
      
    bool(false)
      [
    "_x"]=>
      
    bool(false)
      [
    "_y"]=>
      
    bool(false)
      [
    "_target"]=>
      
    bool(false)
      [
    "_raw"]=>
      
    bool(false)

    But there is no way i can find of accessing the data using the WebTestCase methods.
    Personally im not really interested in getting a battle of Why or Why Not something should be done, all im trying todo is use SimpleTest instead of lime within symfony, and the only reason i would like to have access to the RequestParams is because lime has this feature with symfony and i have developers that are used to testing with these features for 'What Ever Reason' is it used quite heavily used and i would like to convert these lime TestCase's to SimpleTest Case's without losing any previous tests.

    The only way i have found to access this data at the moment is by using the below
    And personally i find this way of accessing objects and there methods rather dirty (IMHO).
    PHP Code:
    $this->_browser->_page->getUrl()->getEncodedRequest() 
    Would be much better IMHO if the WebTestCase method getUrl actually returned the browser page Url Object.

    I think i might be best if the extend the SimpleTest WebTestCase class and implement the methods i require, maybe ?

    Would also be nice if SimpleTest had a way of overriding classes like the WebTestCase, UnitTestCase, Like you can in PHPUnit where all main classes are wrapped with, if (! class_exists('WhatEverClass')) { .... so you do not have to change the class name, just re implement it.
    Last edited by davro; May 30, 2007 at 12:07.
    David Stevens, create-inspire
    PHP London, www.phplondon.org

  6. #6
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The point of the WebTestCase is to act like a browser. It is not a part of the PHP process which is actually running the request, it is acting as a browser and fetching the information just like a browser. It can't (and shouldn't) know anything about the state of variables in the PHP script you are testing unless you intentionally expose those to your clients in the resulting output.

    I think perhaps you want a unit test of your request logic. In that case you might do something like:

    set bad $_REQUEST var
    run application Request setup logic
    verify bad $REQUEST var was handled appropriatly

    All of this would be done in a UnitTestCase though, not a WebTestCase.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    London
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well said sweatje
    The point of the WebTestCase is to act like a browser. It is not a part of the PHP process which is actually running the request
    Agreed: WebTestCase's should be for simulating users interactions.
    Browser Response, Text Assertions, Inputing, Checking Clicking, Selecting and Submitting.

    I think it would be nice though for the WebTestCase method getUrl to return the UrlObject, could be done quite simply and maintain backward compatibility eg $this->getUrl(true);

    Current code in SimpleTest
    PHP Code:
     519         /**
     520          *    Accessor for the currently selected URL.
     521          *    @return string        Current location or false if
     522          *                          no page yet fetched.
     523          *    @access public
     524          */
     
    525         function getUrl() {
     
    526             return $this->_browser->getUrl();
     
    527         
    Replaced by. ...
    PHP Code:
     519         /**
     520          *    Accessor for the currently selected URL.
     521          *    @param string $url_object    True returns SimpleUrl 
     522          *                          object False returns a string Url.
     523          *    @return string        Current location or false if
     524          *                          no page yet fetched.
     525          *    @access public
     526          */
     
    527         function getUrl($url_object=false) {
     
    528             if ($url_object) {
     
    529               return $this->_browser->_page->getUrl();
     
    530             } else { 
     
    531               return $this->_browser->getUrl();
     
    532             }
     
    533         
    David Stevens, create-inspire
    PHP London, www.phplondon.org

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I understand your changes, but I still don't understand your motivation. You would be testing SimpleTests Brower's parsing of the URL you just fed it, not your scripts parsing of the input. None of the scrubbing of the Request you talked about your framework doing is modeled in the SimpleBrowser.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  9. #9
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this the kind of thing you want to test?

    PHP Code:
     class TestOfMyRequestClass extends SimpleTestCase {

        function 
    setUp() {
            
    $this->cached_get $_GET;
            
    $_GET = array();
        }
        function 
    tearDown() {
            
    $_GET $this->cached_get;
        }
        function 
    testGetFooWhenGetIsEmptyArray() {
            
    $request =& new MyRequestClass;
            
    $this->assertIdentical($request->getFoo(), false);
        }
        function 
    testGetFooWhenHaveAValidFoo() {
            
    $_GET['foo'] = 'insert valid value here';
            
    $request =& new MyRequestClass;
            
    $this->assertEqual($request->getFoo(), 'bar');
        }
        function 
    testGetFooWhenHaveInValidValue() {
            
    $_GET['foo'] = 'insert invalid value here';
            
    $request =& new MyRequestClass;
            
    $this->assertIdentical($request->getFoo(), false);
        } 

  10. #10
    SitePoint Enthusiast
    Join Date
    Jun 2004
    Location
    London
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi McGruff,

    Thanks for the reply, will have a look for symfony's
    sfWebRequest test case if there is one, else i shall build a test case.

    Cheers all.
    David Stevens, create-inspire
    PHP London, www.phplondon.org


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
  •