jeudi 5 novembre 2015

PHP unit testing External static method call from different class

I am trying to write a unit test for a function that immediately loads an object from a different class that uses the input to the function as a parameter. I am new to php unit testing and couldn't find anything that address my particular problem. A few leads that I had that led to no avail was using an injector, and trying to us a reflection.

The code I am trying to write a unit test for is:

public static function isUseful($item) {

  $objPromo = MyPromoCodes::Load($item->SavedSku);

  if (!is_null($objPromo)
    && ($objPromo->PromoType == MyPromoCodes::Interesting_Promo_Type)) {
    return true;
  }
  return false;
}

My attempt at mocking this out:

public function testIsUseful() {

  $injector = $this->getMockBuilder('MyPromoCodes')
    ->setMethods(array('Load'))
    ->getMock();
  $objPromo = $this->getMock('MyPromoCodes');
  $objPromo->PromoType = 'very interesting promo type';
  $injector->set($objPromo, 'MyPromoCodes');

  $lineItem1 = $this->getDBMock('LineItem');


  $this->assertTrue(MyClass::isUseful($lineItem1));

}

however this doesn't work because there is no set method for this object....

Not sure what else to try, any help would be appreciated.

Aucun commentaire:

Enregistrer un commentaire