title: TDD Best Practice In Using Restful Api in Yii application
body: <p>I'm constantly looking for the best way to use TDD in Yii app development. Nowday most web app are composed by a fronted, an API layer (usually JSON) to provide async calls to the server and a backend. By my side, most used test in this of app are unit tests and functional ones. The latter the most widely showed in guides and books leverage PHPUnit + Selenium, but Behat + Mink seems very cool too (but I'm not really confident with it yet)</p>
  
  <p>If you ever used functional tests that use a browser (like Selenium) you know that the less you have to run them the better you feel. This cause they're slower, harder to maintain and sometimes (like the popup FB Login using JS SDK) are painful. </p>
  
  <p>When working with a single web page application I care about testing JSON output of my apis. I'd like to test these functionalities with a unit test like approach in order to have faster tests that are easier to maintain. Considering that most of my Controller's action are availaible to Logged only user using accessControl filter I wondered on the best ways to have my tests up and running.</p>
  
  <p>At this moment I think to have two ways to accomplish this</p>
  
  <ul>
  <li>use cUrl toward the desired enpoint to get the JSON directly invoke</li>
  <li>the controller's function</li>
  </ul>
  
  <p>In the first scenario I can use fixtures but I got no way to mock CWebUser class (to emulate a logged user), using Apache when the cUrl comes it gets executed by an instance of my CWebApplication that is not the one executed by PHPUnit. I can get rid of this problem by making all my API calls stateless and, as a consequence, removing accessControl filter.</p>
  
  <p>In the second one the only way I found to mock CWebUser class is to override it in the test class that I'm executing. This approach pays until I dont need to test use cases requiring different type of user, and I got no way to change at runtime (or at setup time) my webuser mock. The only way I found to mock my webuser is the one you can find below, this cause $this->getMock('WebUser') doesnt affect anyway CWebApplication's WebUser (read-only) singleton defined in the configuration file.</p>
  
  <p>Here comes a concrete example:</p>
  
  <pre><code>class UserControllerTest extends CDbTestCase
  {
      public $fixtures=array(
              /* NEEDED FIXTURES*/
      );
  
      public function testUserCanGetFavouriteStore() {
  
              $controller = new UserController(1);
              $result = json_decode($controller-&gt;actionAjaxGetFavStore());                    
              $this-&gt;assertInternalType('array', $result-&gt;data);              
  
              $model  = $result-&gt;data[0];
              $this-&gt;assertEquals($model-&gt;name, "Nome dello Store");  
  
      }
  }
  
  class WebUser extends CWebUser {
  
      public function getId() {
          return 1;
      }
      public function getIsGuest() {
              return false;
      }
  };
  </code></pre>
  
  <p>I was wondering if being able to  authenticate with the api interface, either by an API key or a user/password combo could be useful. 
  This is ok if I move toward a almost stateless API integration, but most of the time I just have controller's actions (permitted to logged user only) that returns Json data to populate the frontend. </p>
  
  <p>Anyone can suggest me a better method? Maybe it's just useless to test this kind of JSON output? </p>
  
  <p>Best Regards </p>