Skip to content
makasim edited this page May 13, 2011 · 1 revision

Stubs data

The sfBasePhpunitAmfTestCase class contains helpful method getStub.
It can be used only for making stub objects, not mocks.
The method has the same interface as getMock method.

This method allows you to create stub objects more simply then getMock method:

Compare these two examples:

  <?php

  class FooTestCase extends sfBasePhpunitTestCase 
  {
    public function testFoo()
    { 
      $stub = $this->getMock('DataManager', array('getFoo', 'getBar'));
      $stub->expects($this->any())->method('getFoo')->will($this->returnValue('foo'));
      $stub->expects($this->any())->method('getBar')->will($this->returnValue('bar'));
      
      // test code here
    }
  }

and:

  <?php

  class FooTestCase extends sfBasePhpunitTestCase 
  {
    public function testFoo()
    { 
      $stub = $this->getStub('DataManager', array(
        'getFoo' => 'foo', 
        'getBar' => 'bar'));
      
      // test code here
    }
  }

The getStub method used $this→any() method to set numbers of the stubbed methods called.
You can use more strict version of this method:

  $this->getStubStrict( ... );

This method used $this→atLeastOnce(),

If you want to create two stubs with cross reference to each other, the next way can help you:

  <?php

  class FooTestCase extends sfBasePhpunitTestCase 
  {
    public function testFoo()
    { 
      $stubFoo = $this->getStub('Foo', array('getBar' => $this->stubLater()));
      $stubBar = $this->getStub('Bar', array('getFoo' => $stubFoo));
        
      $stubFoo->expects($this->any())->method('getBar' => $stubBar);
      
      // test code here
    }
  }
Clone this wiki locally