The Artima Developer Community
Sponsored Link

PHP Buzz Forum
Mock Objects in PHPUnit

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Sebastian Bergmann

Posts: 313
Nickname: sbergmann
Registered: Sep, 2004

Sebastian Bergmann is the developer of PHPUnit.
Mock Objects in PHPUnit Posted: Jul 5, 2005 5:15 AM
Reply to this message Reply

This post originated from an RSS feed registered with PHP Buzz by Sebastian Bergmann.
Original Post: Mock Objects in PHPUnit
Feed Title: Sebastian Bergmann
Feed URL: http://sebastian-bergmann.de/
Feed Description: Geek by nature, PHP by choice.
Latest PHP Buzz Posts
Latest PHP Buzz Posts by Sebastian Bergmann
Latest Posts From Sebastian Bergmann

Advertisement
While I am preparing the PHPUNIT_2_3 CVS branch for a release of PHPUnit 2.3.0 alongside PHP 5.1.0, I recently started working on support for Mock Objects in PHPUnit.

Suppose you have the following class Foo:
<?php
class Foo {
    public function bar() {
    }
}
?>
You can now write a test case that checks whether or not the bar() gets called on an object of the Foo class:
<?php
require_once 'PHPUnit2/Framework/TestCase.php';

class FooTest extends PHPUnit2_Framework_TestCase {
    public function testBarGetsCalled() {
        $mock = $this->getMockObject('Foo');
        $mock->__expectAtLeastOnce('bar');
        $mock->bar();
        $mock->__tally();
    }
}
?>
The getMockObject('Foo') call generates a class MockFoo (the Mock Object class for Foo) that extends from Foo and the signatures of all public methods are copied from Foo to MockFoo. In addition, a couple of MockObjects API methods are added to the MockFoo class:
  • __expectArguments($method, $arguments, $message = "")
  • __expectArgumentsAt($method, $arguments, $timing, $message = "")
  • __expectCallCount($method, $count, $message = "")
  • __expectMaximumCallCount($method, $count, $message = "")
  • __expectMinimumCallCount($method, $count, $message = "")
  • __expectNever($method, $message = "")
  • __expectOnce($method, $arguments = FALSE, $message = "")
  • __expectAtLeastOnce($method, $arguments = FALSE, $message = "")
  • __getCallCount($method)
  • __setReturnValue($method, $value, $arguments = FALSE)
  • __setReturnValueAt($method, $value, $timing, $arguments = FALSE)
  • __setReturnReference($method, &$value, $arguments = FALSE)
  • __setReturnReferenceAt($method, &$value, $timing, $arguments = FALSE)
  • __tally()
These methods can be used to set up expectations and return values for the stubbed-out methods of the Mock Object.

The work on the Code Generator that generates the code for a Mock Object class is complete and I started to work on the PHPUnit2_Extensions_MockObjects_CallMap class today. The next step will be to tie the Mock Objects functionality into the PHPUnit framework.

Read: Mock Objects in PHPUnit

Topic: Conferences, the odd call for submissions Previous Topic   Next Topic Topic: String Object in PHP: the Good, the Bad, the Kludge

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use