Hi all,

I'm new to CakePHP and decided to start with 2.0 so I hope my newbie
questions aren't _too_ offensive. (:

I've been trying for a few days to implement testing for my REST
controllers in CakePHP with no avail. I've looked at some examples
from the book and the bakery but still no luck. The controller is
working properly when I invoke it with an HTTP client so that I can
test the REST functionality but when implementing the CakePHP tests
I've been unable to send the data in the proper way so that the
controller being tested receives it in the expected way (JSON object).

I'm including here a bit of code (no pun intended) to better
illustrate what I'm trying to do. Any thoughts will be appreciated.

Thanks!

// this is happening in my beforeFilter for the controller being
tested
// add this request handler for Json requests which will allow the
controller to access Json-encoded data
    // to be accessed directly through the $data property of the
controller's request object
    $this->RequestHandler->addInputType('json',
array('json_decode',true));




// this is my "add" action in the controller
 public function add() {
    try {
// when I test this with my HTTP client I get the parsed/decoded JSON
data (i.e. an array)
// when I test it with the CakePHP test I get a zero-based numeric
array with the zero-index pointing to a string containing the JSON
data, i.e. the data unencoded.
      debug ( $this->request->data );
// when I test this with both my HTTP client and the CakePHP test I
get "json", which indicates that the request is in fact being sent
with an application/json header (i.e. the desired effect)
      debug ( $this->RequestHandler->responseType ( ) );



// this is what my test looks like

  public function testAdd ( ){
    // define the data to be inserted and expected back when the
assertion is run
    $arrUsuarioData = array ( 'c'=> array (
      'rol'=>'rol',
      'nombres'=>'nombres',
      'apellidos'=>'apellidos',
      'email'=>'email_t...@email.com',
      'username'=>'username_test',
      'password'=>'password',
      'borrado'=>false
      )
    );

    // test action
    $this->testAction(
      '/usuarios.json',
      $this->defaultOptions(
        array(
          'method'=>'post',
          'data'=> array (
          // encode the array into a Json object since this is what
our action is expecting
          json_encode ( $arrUsuarioData )
          )
        )
      )
    );



Now, as I understand it, $this->request->data should be getting an
array of json decoded data from the test as well, but I don't know how
to send it properly. I tried setting "data" in the testAction params
as the JSON string itself but that didn't work either. Any thoughts?

Thanks!

-- 
Our newest site for the community: CakePHP Video Tutorials 
http://tv.cakephp.org 
Check out the new CakePHP Questions site http://ask.cakephp.org and help others 
with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to