Unit test method that creates an object

I am trying to try Unit Testing and another piece of the puzzle that I need to find.

What I'm trying to do is write tests for the following code. In this case, I have a really simple Front Controller (written in PHP).

class frontController
{
   public function routeRequest($oRequest)
   {
      $sClassname = $oRequest->getController();
      $sMethod = $oRequest->getAction();

      $oController = new $sClassname();

      $oResponse = $oController->{$sMethod}($oRequest);

      return $oResponse;
   }

}

The problem is that the code creates new objects. I can easily make fun of the request object so that I can tightly control what it will do in my test case. I am not sure if it is best to replace the controller with a test double.

This IBM article suggests using the factory method to create my controller, and then overriding it with the specific class used for testing:

class frontController
{
   public function routeRequest($oRequest)
   {
      $sMethod = $oRequest->getAction();

      $oController = $this->createController($oRequest);
      $oResponse = $oController->{$sMethod}($oRequest);

      return $oResponse;
   }

   protected function createController($oRequest)
   {
      $sClassname = $oRequest->getController();
      return new $sClassname();
   }

}

- :

class testFrontController extends frontController
{
   public function setMockController($oMockController)
   {
      $this->oMc = $oMockController;
   }

   protected function createController($oRequest)
   {
      return $this->oMockController;
   }
}

( , , , , , )

, . frontController. , factory/creation . - :

class frontController
{
   public function routeRequest($oRequest, $oControllerFactory)
   {
      $sMethod = $oRequest->getAction();

      $oController = $oControllerFactory->create($oRequest);
      $oResponse = $oController->{$sMethod}($oRequest);

      return $oResponse;
   }
}

class controllerFactory
{
   public function create($oRequest)
   {
      $sClassname = $oRequest->getController();
      return new $sClassname();
   }
}

, "".

, 2.

?

(, " " !)

1 2 - . - , , , .

!

+3
4

, . , , , , factory, , , ...

+2

.

, .

+3

, , ?

, , MockController.

0

, , , . , - , $oResponse ( ). . , , , .

PHPUnit , , , PHP imho ( SimpleTest, ... ).

( , . PHPUnit):

class AimTest extends PHPUnit_Framework_TestCase{
      private $_controller = null;
      private $_request = null;

      public function setUp(){
             $this->_controller = new frontController();
             //what does this object type?
             $this->_request = new requestObject();  
      }

      public function testObjectCreation(){
            /*
             * note, that this is only one of several assertions that could
             * be made depending on the return value
             */
             $return = $this->_controller->routeRequest($this->_request);
             //tailor to what you expect your output to be
             $this->assertTrue($return == "my expected output");
      }

, . , , . , instanceof PHP , .

0

Source: https://habr.com/ru/post/1722783/


All Articles