Mocking a build-in PHP function is achieved by using
PHP's namespace fallback policy. A mock will provide the namespaced function.
I.e. only unqualified functions in a non-global namespace can be mocked.
Example:
namespace foo;
use phpmock\Mock;
$time = new Mock(
__NAMESPACE__,
"time",
function () {
return 3;
}
);
$time->enable();
assert (3 == time());
$time->disable();
assert (3 != time());
protected function disableMocks() { Mock::disableAll(); }
/** * Tests redefining mocks in a different namespace. * * @test * @dataprovider provideTestNamespace */ public function testRedefiningNamespaces() { $this->builder->setNamespace(__NAMESPACE__); $this->mock = $this->builder->build(); $this->mock->enable(); $this->assertEquals(1234, time()); }
/** * Tests disabling the mock. * * @test */ public function testEndTest() { $min = new Mock(__NAMESPACE__, "min", "max"); $min->enable(); $this->assertEquals(9, min(1, 9)); $disabler = new MockDisabler($min); $disabler->endTest($this, 1); $this->assertEquals(1, min(1, 9)); }
/** * Tests case insensitive mocks. * * @param string $mockName The mock function name. * * @test * @dataProvider provideTestCaseSensitivity */ public function testCaseSensitivity($mockName) { $builder = new MockBuilder(); $builder->setNamespace(__NAMESPACE__)->setName($mockName)->setFunctionProvider(new FixedValueFunction(1234)); $this->mock = $builder->build(); $this->mock->enable(); $this->assertEquals(1234, time(), "time() is not mocked"); $this->assertEquals(1234, Time(), "Time() is not mocked"); $this->assertEquals(1234, TIME(), "TIME() is not mocked"); }
public function setUp() { \phpmock\Mock::disableAll(); $this->alwaysParticipateFilter = new Percentage(100); $this->chooser = new StaticChooser(0); $this->variant = $this->getMockBuilder(VariantInterface::class)->setMethods(['getIdentifier', 'run'])->getMock(); $this->manager = $manager = $this->getMockBuilder(ManagerInterface::class)->getMock(); }
public function call(array $arguments) { $return = null; $exception = null; try { $return = parent::call($arguments); return $return; } catch (\Exception $e) { $exception = $e; throw $e; } finally { $this->invocations[] = new Invocation($arguments, $return, $exception); } }
public function tearDown() { // disable all mocked functions Mock::disableAll(); }
/** * Checks all predictions defined by prophecies of this Prophet. * * It will also disable all previously revealed function prophecies. * * @throws AggregateException If any prediction fails. * @SuppressWarnings(PHPMD) */ public function checkPredictions() { Mock::disableAll(); $this->prophet->checkPredictions(); }
/** * Tests the example from the documentation. * * @test */ public function testExample5() { $time = new Mock(__NAMESPACE__, "time", function () { return 3; }); $time->enable(); assert(3 == time()); }
/** * Reset global cookies array and disable * global function mocks */ protected function tearDown() { parent::tearDown(); Mock::disableAll(); }
public function call(array $arguments) { $return = parent::call($arguments); $this->invocations[] = new Invocation($arguments, $return); return $return; }