Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Request class fais to retrieve URI with .'format' and related issues
  • I'm starting to use the request class mainly to create unit test for a simple REST API. I have not used the request class for FuelPHP but I have played around with the request class in Kohana. I have run into some issues: I have created a very simple controller that extends from the Controller_Rest
    class Controller_Api_V1_Auth extends Controller_Rest {
    
        public function get_authenticate(){
            $this->response(array(
                'foo' => Input::get('foo'),
                'baz' => array(
                    1, 50, 219
                ),
                'empty' => null
            ));
        }
        
        public function post_register(){
            $this->response(array(
                'foo' => Input::get('foo'),
                'baz' => array(
                    1, 50, 219
                ),
                'empty' => null
            ));
        }
    }
    
    

    and a very simple test case that looks like this:
    /**
     * @group Api
     */
    class Test_Controller_Api extends TestCase
    {
        public function test_register_user() {
            $request = Request::forge('api/v1/auth/register.json');
            $request->set_method('POST');
            $response = $request->execute()->response();
            $this->assertEquals($response->status, 200);
        }
    }
    

    1) As long as the .json is there the test will always fail:
    $ oil test --group=Api
    Tests Running...This may take a few moments.
    PHPUnit 3.6.12 by Sebastian Bergmann.
    
    Configuration read from /.../fuel/core/phpunit.xml
    
    FFuel\Core\Response Object
    (
        [status] => 405
        [headers] => Array
            (
            )
    
        [body] => 
    )
    
    
    Time: 0 seconds, Memory: 21.00Mb
    
    There was 1 failure:
    
    1) Test_Controller_Api::test_register_user
    Failed asserting that 200 matches expected 405.
    
    /.../fuel/app/tests/controller/api.php:14
    
    FAILURES!
    Tests: 1, Assertions: 1, Failures: 1.
    
    

    2) The following request also fails:
     $request = Request::forge('http://localhost/api/v1/auth/register');
    
    it returns a "not found" exception (Fuel\Core\HttpNotFoundException). 3) Finally, I'm having trouble understanding how to pass parameters via POST or GET to my request.
    In Kohana I would do something like this
    $request = Request::factory('http://localhost/api/v1/auth/register')
            ->method(Request::POST)
            ->post(array('foo' => 'bar', 'bar' => 'baz'));
    

    How would I do this in fuel? Thank you, Onema
  • The REST controller expects to be called by an AJAX call. If you want to test that, you need to fake that. Easiest is to add this to the init method or the constructor:
    // fake an AJAX request!
    $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest';
    
  • Is it a good idea to test the REST API this way? or is there a better way to do it. I got around some of my issues by using the request class like this:
    $request = \Request::forge('http://localhost/api/v1/auth/register.json', 'curl');
    $request->set_method('POST');
    $response = $request->execute(array('test' => 'hello POST!'))->response();
    
    $this->assertEquals($response->status, 200);
    
    

    Are there any plans to improve, or extend the request class?
  • That also works, and is perhaps cleaner as it really mimics an AJAX request.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion