On Mon, Oct 17, 2011 at 11:58 PM, Matteo Landi <[email protected]> wrote:
> Hi everybody.
>
> I am looking for some advice on how to test RESTful controllers
> exchanging JSON data (I'm using $this->request->input() on all
> requests methods to fetch data coming from clients). Here is the test
> I started to write:
>
> <?php
> class UsersControllerTest extends ControllerTestCase {
>        public $fixtures = array('app.user');
>
>        public function testViewJson() {
>                $data = json_encode(array(
>                        'id' => 1
>                ));
>                $result = $this->testAction(
>                        '/users.json',
>                        array('data' => $data, 'method' => 'GET')
>                );
>                debug($result);
>        }
> }
>
> If I run the test, I'm getting the following error:
>
> Fatal error: Cannot unset string offsets in
> /home/matteo/workspace/partygoat/lib/Cake/Network/CakeRequest.php on
> line 183 Call Stack: 0.0003 351908 1. {main}()
> /home/matteo/workspace/partygoat/api/webroot/test.php:0 0.0132 1515196
> 2. CakeTestSuiteDispatcher::run()
> /home/matteo/workspace/partygoat/api/webroot/test.php:92 0.0132
> 1515764 3. CakeTestSuiteDispatcher->dispatch()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php:114
> 0.0335 1670872 4. CakeTestSuiteDispatcher->_runTestCase()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php:97
> 0.0361 1948588 5. CakeTestSuiteCommand->run()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/CakeTestSuiteDispatcher.php:252
> 0.0635 5160212 6. CakeTestRunner->doRun()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/CakeTestSuiteCommand.php:113
> 0.0934 8174532 7. PHPUnit_TextUI_TestRunner->doRun()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/CakeTestRunner.php:58
> 0.0945 8304784 8. PHPUnit_Framework_TestSuite->run()
> /usr/share/php/PHPUnit/TextUI/TestRunner.php:305 0.1004 8960128 9.
> PHPUnit_Framework_TestSuite->runTest()
> /usr/share/php/PHPUnit/Framework/TestSuite.php:742 0.1004 8960128 10.
> CakeTestCase->run() /usr/share/php/PHPUnit/Framework/TestSuite.php:766
> 0.1245 8964536 11. PHPUnit_Framework_TestCase->run()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/CakeTestCase.php:81
> 0.1247 8964536 12. PHPUnit_Framework_TestResult->run()
> /usr/share/php/PHPUnit/Framework/TestCase.php:575 0.2543 8978228 13.
> PHPUnit_Framework_TestCase->runBare()
> /usr/share/php/PHPUnit/Framework/TestResult.php:629 0.2580 9083636 14.
> PHPUnit_Framework_TestCase->runTest()
> /usr/share/php/PHPUnit/Framework/TestCase.php:627 0.2581 9084740 15.
> ReflectionMethod->invokeArgs()
> /usr/share/php/PHPUnit/Framework/TestCase.php:737 0.2581 9084768 16.
> UsersControllerTest->testViewJson()
> /home/matteo/workspace/partygoat/api/Test/Case/Controller/UsersControllerTest.php:0
> 0.2582 9085444 17. UsersControllerTest->testAction()
> /home/matteo/workspace/partygoat/api/Test/Case/Controller/UsersControllerTest.php:12
> 0.2582 9085772 18. ControllerTestCase->__call()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/ControllerTestCase.php:0
> 0.2582 9086180 19. call_user_func_array()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/ControllerTestCase.php:169
> 0.2582 9086496 20. ControllerTestCase->_testAction()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/ControllerTestCase.php:0
> 0.2583 9093896 21. CakeRequest->__construct()
> /home/matteo/workspace/partygoat/lib/Cake/TestSuite/ControllerTestCase.php:207
> 0.2588 9094164 22. CakeRequest->_processGet()
> /home/matteo/workspace/partygoat/lib/Cake/Network/CakeRequest.php:136
>
> and if I comment the line raising the error (which tries unset
> '/users.json' from $query), even if the test goes on with no problems,
> inside the Controller $this->request->input() seems to be emtpy.
> Should I have to fill a bug request for this error?

My fault: it seems that the ``data`` parameter of the ``testAction``
should be an array and I was actually trying to pass a string.

At this point:
1 is it possible to test an action passing as data a plain string
instead of a key-valued one?
2 how is it possible to test PUT or DELETE actions?


Regards,
Matteo

-- 
http://www.matteolandi.net/

-- 
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
[email protected] For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to