lstrojny / phpunit-function-mocker
Allows mocking otherwise untestable PHP functions through the use of namespaces
Installs: 326 121
Dependents: 11
Suggesters: 0
Security: 0
Stars: 53
Watchers: 6
Forks: 4
Open Issues: 2
Requires
- php: ~7
Requires (Dev)
- phpunit/phpunit: ~6
This package is auto-updated.
Last update: 2024-10-29 03:58:40 UTC
README
Allows mocking otherwise untestable PHP functions through the use of namespaces.
<?php namespace MyNamespace; class Tool { public function isString($string) { return strlen($string) > 0 && ctype_alpha($string); } }
<?php require_once 'PHPUnit/Extension/FunctionMocker.php'; class MyTestCase extends PHPUnit_Framework_TestCase { public function setUp() { $this->php = PHPUnit_Extension_FunctionMocker::start($this, 'MyNamespace') ->mockFunction('strlen') ->mockFunction('ctype_alpha') ->getMock(); } /** @runInSeparateProcess */ public function testIsStringUsesStrlenAndCtypeAlpha() { $this->php ->expects($this->once()) ->method('strlen') ->with('foo') ->will($this->returnValue(3)) ; $this->php ->expects($this->once()) ->method('ctype_alpha') ->with('foo') ->will($this->returnValue(false)) ; $tool = new MyNamespace\Tool(); $this->assertFalse($tool->isString('foo')); } }
NOTE
Use @runInSeparateProcess
annotation to make sure that the mocking is reliably working in PHP >=5.4