Skip to content

Instantly share code, notes, and snippets.

@mbfisher
Last active December 28, 2015 23:49
Show Gist options
  • Save mbfisher/7581593 to your computer and use it in GitHub Desktop.
Save mbfisher/7581593 to your computer and use it in GitHub Desktop.
PHPUnit Cheatsheet

Run a test suite; phpunit -c path/to/phpunit.xml --testsuite ${name}

getMockBuilder

setMethods(array $methods) can be called on the Mock Builder object to specify the methods that are to be replaced with a configurable test double. The behavior of the other methods is not changed. If you call setMethods(NULL), then no methods will be replaced.

setConstructorArgs(array $args) can be called to provide a parameter array that is passed to the original class' constructor (which is not replaced with a dummy implementation by default).

setMockClassName($name) can be used to specify a class name for the generated test double class.

disableOriginalConstructor() can be used to disable the call to the original class' constructor.

disableOriginalClone() can be used to disable the call to the original class' clone constructor.

disableAutoload() can be used to disable __autoload() during the generation of the test double class.

getMockClass

/**
 * Mocks the specified class and returns the name of the mocked class.
 *
 * @param  string  $originalClassName
 * @param  array   $methods
 * @param  array   $arguments
 * @param  string  $mockClassName
 * @param  boolean $callOriginalConstructor
 * @param  boolean $callOriginalClone
 * @param  boolean $callAutoload
 * @param  boolean $cloneArguments
 * @return string
 * @throws PHPUnit_Framework_Exception
 * @since  Method available since Release 3.5.0
 */
protected function getMockClass($originalClassName, $methods = array(), array $arguments = array(), $mockClassName = '', $callOriginalConstructor = FALSE, $callOriginalClone = TRUE, $callAutoload = TRUE, $cloneArguments = FALSE)
{
    $mock = $this->getMock(
      $originalClassName,
      $methods,
      $arguments,
      $mockClassName,
      $callOriginalConstructor,
      $callOriginalClone,
      $callAutoload,
      $cloneArguments
    );

    return get_class($mock);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment