This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Dec 12th, 2012  |  syntax: PHP  |  size: 0.75 KB  |  views: 45  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }
clone this paste RAW Paste Data