public final function generateCode(ReflectionClass $class, $test_double_class_name, array $interfaces = array(), array $traits = array(), $method_checker = null) { FBMock_Utils::assertString($test_double_class_name); if ($class->isFinal() && !$this->canOverrideFinals()) { throw new FBMock_TestDoubleException("Cannot mock final class %s", $class->getName()); } $code = $this->getMockClassHeader($class, $test_double_class_name, $interfaces, $traits) . "\n"; $method_sources = array(); foreach ($class->getMethods() as $method) { $method_checker && $method_checker($class, $method); if ($method->isFinal() && !$this->canOverrideFinals()) { continue; } // #1137433 if (!$class->isInterface()) { $method = new ReflectionMethod($class->getName(), $method->getName()); } $test_double_method_generator = FBMock_Config::get()->getMethodGenerator(); $method_source = $test_double_method_generator->generateCode($method); if ($method_source) { $method_sources[] = $method_source; } } $code .= implode("\n\n", $method_sources); $code .= "\n}"; // close class return $code; }
/** * Create a strict mock object of type $class_name. * * "Strict" means that if any unmocked method is called, an exception will be * thrown. */ function strict_mock($class_name) { FBMock_Utils::assertString($class_name); $interface_names = func_get_args(); array_shift($interface_names); return FBMock_Config::get()->getMockCreator()->createStrictMock($class_name, $interface_names); }
public final function createStrictMock($class_name, $extra_interfaces = array()) { FBMock_Utils::assertString($class_name); $mock = self::createMock($class_name, $extra_interfaces); FBMock_Utils::getDoubleImplementation($mock)->setStrictMock(); return $mock; }
private function getStub($method_name) { FBMock_Utils::assertString($method_name); if (isset($this->methodsToStubs[strtolower($method_name)])) { return $this->methodsToStubs[strtolower($method_name)]; } return null; }
/** * Assert that the method calls match the array of calls. * * Example usage: * * // Code under test calls method * $mock->testMethod(1,2,3); * $mock->testMethod('a', 'b', 'c'); * * // Test asserts calls * self::assertCalls( * $mock, * 'testMethod', * array(1,2,3), * array('a', 'b', 'c') * ); * * @param $mock a mock object * @param $method_name name of method to check * @param ... arrays of expected arguments for each call * @param $msg message for assert (optional) */ public static function assertCalls(FBMock_Mock $mock, $method_name) { FBMock_Utils::assertString($method_name); $args = func_get_args(); $msg = ''; if (is_string(end($args))) { $msg = array_pop($args); } $expected_calls = array_slice($args, 2); self::assertNumCalls($mock, $method_name, count($expected_calls), $msg); $actual_calls = $mock->mockGetCalls($method_name); foreach ($expected_calls as $i => $call) { PHPUnit_Framework_TestCase::assertEquals($call, $actual_calls[$i], $msg ?: "Call {$i} for method {$method_name} did not match expected call"); } }
public final function createTestDoubleFor($class_name, array $interfaces = array(), array $traits = array(), $method_checker = null) { FBMock_Utils::assertString($class_name); $this->assertAllowed(); if (!class_exists($class_name) && !interface_exists($class_name)) { throw new FBMock_TestDoubleException("Attempting to mock {$class_name} but {$class_name} isn't loaded."); } $mock_class_name = FBMock_Utils::mockClassNameFor($class_name, $interfaces, $traits); $ref_class = new ReflectionClass($class_name); if ($ref_class->isInternal() && !FBMock_Utils::isHHVM()) { throw new FBMock_TestDoubleException("Trying to mock PHP internal class {$class_name}. Mocking of internal " . "classes is only supported in HHVM."); } if (!class_exists($mock_class_name, $autoload = false)) { $class_generator_class = FBMock_Config::get()->getClassGenerator(); $class_generator = new $class_generator_class(); $code = $class_generator->generateCode($ref_class, $mock_class_name, $interfaces, $traits, $method_checker); eval($code); } $mock_object = (new ReflectionClass($mock_class_name))->newInstanceWithoutConstructor(); return $mock_object; }
/** * @expectedException InvalidArgumentException * @expectedExceptionMessage String argument expected, array given */ public function testAssertStringFail() { FBMock_Utils::assertString(array()); }
public function mockGetCalls($method_name) { FBMock_Utils::assertString($method_name); return FBMock_Utils::getDoubleImplementation($this)->getCalls($this, $method_name); }
public function __construct($format_str) { FBMock_Utils::assertString($format_str); $args = array_slice(func_get_args(), 1); parent::__construct(vsprintf($format_str, $args)); }