duanlidi1051
2018-01-18 01:04 阅读 30
已采纳

如果DI不可行,如何将实例化与单元测试的实现分离?

I have the following code (simplified and details changed for this question):

    class model_to_be_tested {
        // an array that holds a collection of thing A
        public $array_of_thing_A;

        // already doing constructor injection for the data object
        public __construct($data_object) {
         // details here
        }

        public function add_new_thing_A($has_relationship) {
          $thing_A = new Thing_A();
          $thing_A->is_thing = true;
          $thing_A->has_relationship_with_thing_B = $has_relationship;

          if ($has_relationship) {
             $thing_B = new Thing_B();
             $thing_A->relationship_with = $thing_B;
          }

          $this->array_of_thing_A[] = $thing_A;
      }
    }

In the above example, I have to decouple the instantiation of Thing_A and Thing_B from the add_new_thing method. However, a simple constructor injection will not do for these two classes. This is because I need fresh instances of Thing_A and Thing_B every time add_new_thing is called so that Thing_A can be added to the array_of_thing_A.

How can I make this function unit testable? And more specifically for me to use mocks of Thing_A and Thing_B in testing this function in PHPUnit?

Any suggestions with code example will be appreciated.

Additionally, I would like to mention that Thing_A and Thing_B are used elsewhere in the codebase that I am working with and the code using these classes will eventually need to be unit tested. Solutions that are too localized and would cause repeated code elsewhere will not be too ideal in my situation. Thank you.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongpi9494 dongpi9494 2018-01-18 14:33

    As commenter xmike mentioned, you could use the factory pattern. You would inject a factory object through the ctor as well. Then you could have a factory that provides simplified instances of your Thing_A and Thing_B.

    class ThingFactory {
        public function buildThingA() {
            return new Thing_A(); // or MockThing_A if you go the ducktyping route
        }
    
        public function buildThingB() {
            return new Thing_B();
        }
    }
    
    class model_to_be_tested {
            // an array that holds a collection of thing A
            public $array_of_thing_A;
    
            // you could go the typed route and have an interface for this
            private $factory;
    
            // already doing constructor injection for the data object
            public __construct($data_object, $factory) {
             // details here
             $this->factory = $factory;
            }
    
            public function add_new_thing_A($has_relationship) {
              $thing_A = $this->factory->buildThingA();
              $thing_A->is_thing = true;
              $thing_A->has_relationship_with_thing_B = $has_relationship;
    
              if ($has_relationship) {
                 $thing_B = $this->factory->buildThingB();
                 $thing_A->relationship_with = $thing_B;
              }
    
              $this->array_of_thing_A[] = $thing_A;
          }
        }
    
    点赞 评论 复制链接分享
  • dongmou3615 dongmou3615 2018-01-18 01:47

    PHP is such a strange language, you can't assign a class to a variable. But you can do it as a string. Inject ThingA and ThingB on the constructor as strings. You can call new on the string member.

    class ThingA {};
    class ThingB{};
    
    class model_to_be_tested {
        // an array that holds a collection of thing A
        public $array_of_thing_A;
        private $_thingA;
        private $_thingB;
    
    
        public function __construct($data_object, $thingA, $thingB) {
         $this->_thingA = $thingA;
         $this->_thingB = $thingB;
        }
    
        public function add_new_thing_A($has_relationship) {
           $thing_A = new $this->_thingA();
    
          if ($has_relationship) {
             $thing_B = new $this->_thingB();
          }
    
          $this->array_of_thing_A[] = $thing_A; 
      }
    }
    
    
    $model = new model_to_be_tested('foo', 'ThingA', 'ThingB');
    $model->add_new_thing_A(true);
    

    There's a live version here: https://repl.it/@rmoskal/InconsequentialAnotherGermanshorthairedpointer

    Or provide a static constructor for the class.

    点赞 评论 复制链接分享

相关推荐