SHARE
TWEET

Untitled

a guest Dec 12th, 2012 70 Never
  1. <?php
  2. // [...] //
  3.  
  4. function mockGetNumber($number) {
  5.    return $number;
  6. }
  7.  
  8. function testPHUnitCandDecideByParameter() {
  9.    $someObject = new SomeClass();
  10.    // With PHPUnit
  11.    $phpunitMock = $this->getMock('AClassToBeMocked');
  12.    $phpunitMock->expects($this->any())->method('getNumber')->will($this->returnCallback(array($this, 'mockGetNumber')));
  13.    $this->assertEquals(4, $someObject->doubleNumber($phpunitMock, 2));
  14.    $this->assertEquals(6, $someObject->doubleNumber($phpunitMock, 3));
  15. }
  16.  
  17. class AClassToBeMocked {
  18. // [...] //
  19.    function getNumber($number) {
  20.       return $number;
  21.    }
  22. }
  23.  
  24. class SomeClass {
  25.    // [...] //
  26.    function doubleNumber($anotherObject, $number) {
  27.       return $anotherObject->getNumber($number) * 2;
  28.    }
  29. }
RAW Paste Data
Top