Overview

Namespaces

  • phpmock
    • phpunit

Traits

  • PHPMock
  • Overview
  • Namespace
  • Class

Trait PHPMock

Adds building a function mock functionality into \PHPUnit\Framework\TestCase.

Use this trait in your \PHPUnit\Framework\TestCase:

<?php

namespace foo;

class FooTest extends \PHPUnit\Framework\TestCase
{

    use \phpmock\phpunit\PHPMock;

    public function testBar()
    {
        $time = $this->getFunctionMock(__NAMESPACE__, "time");
        $time->expects($this->once())->willReturn(3);
        $this->assertEquals(3, time());
    }
}
Namespace: phpmock\phpunit
License: WTFPL
Author: Markus Malkusch markus@malkusch.de
Link: Donations
Located at PHPMock.php
Methods summary
public PHPUnit_Framework_MockObject_MockObject
# getFunctionMock( string $namespace, string $name )

Returns the enabled function mock.

Returns the enabled function mock.

This mock will be disabled automatically after the test run.

Parameters

$namespace
The function namespace.
$name
The function name.

Returns

PHPUnit_Framework_MockObject_MockObject
The PHPUnit mock.
public
# registerForTearDown( phpmock\Deactivatable $deactivatable )

Automatically disable function mocks after the test was run.

Automatically disable function mocks after the test was run.

If you use getFunctionMock() you don't need this method. This method is meant for a Deactivatable (e.g. a MockEnvironment) which was directly created with PHPMock's API.

Parameters

$deactivatable
The function mocks.
public static
# defineFunctionMock( string $namespace, string $name )

Defines the mocked function in the given namespace.

Defines the mocked function in the given namespace.

In most cases you don't have to call this method. phpmock\phpunit\PHPMock::getFunctionMock() is doing this for you. But if the mock is defined after the first call in the tested class, the tested class doesn't resolve to the mock. This is documented in Bug #68541. You therefore have to define the namespaced function before the first call (e.g. with the beforeClass annotation).

Defining the function has no side effects. If the function was already defined this method does nothing.

Parameters

$namespace
The function namespace.
$name
The function name.

See

phpmock\phpunit\PHPMock::getFunctionMock()

Link

Bug #68541
API documentation generated by ApiGen