public function testAnonymousMockWorksWithNotAllowingMockingOfNonExistantMethods() { $before = ehough_mockery_Mockery::getConfiguration()->mockingNonExistentMethodsAllowed(); ehough_mockery_Mockery::getConfiguration()->allowMockingNonExistentMethods(false); $m = $this->container->mock(); $m->shouldReceive("test123")->andReturn(true); assertThat($m->test123(), equalTo(true)); ehough_mockery_Mockery::getConfiguration()->allowMockingNonExistentMethods(true); }
/** * Handle a method call being directed by this instance * * @param array $args * @return mixed */ public function call(array $args) { $expectation = $this->findExpectation($args); if (is_null($expectation)) { $exception = new ehough_mockery_mockery_exception_NoMatchingExpectationException('No matching handler found for ' . $this->_mock->mockery_getName() . '::' . ehough_mockery_Mockery::formatArgs($this->_name, $args) . '. Either the method was unexpected or its arguments matched' . ' no expected argument list for this method' . PHP_EOL . PHP_EOL . ehough_mockery_Mockery::formatObjects($args)); $exception->setMock($this->_mock)->setMethodName($this->_name)->setActualArguments($args); throw $exception; } return $expectation->verifyCall($args); }
/** * After each test, perform ehough_mockery_Mockery verification tasks and cleanup the * statically stored ehough_mockery_Mockery container for the next test. * * @param PHPUnit_Framework_Test $test * @param float $time */ public function endTest(PHPUnit_Framework_Test $test, $time) { try { $container = ehough_mockery_Mockery::getContainer(); if ($container != null) { $expectation_count = $container->mockery_getExpectationCount(); $test->addToAssertionCount($expectation_count); } ehough_mockery_Mockery::close(); } catch (Exception $e) { $result = $test->getTestResultObject(); $result->addError($test, $e, $time); } }
/** * Intercept all calls on the subject, and use the call details to create * a new expectation. The actual return value is then returned after being * recorded. * * @param string $method * @param array $args * @return mixed */ public function __call($method, array $args) { $return = call_user_func_array(array($this->_subject, $method), $args); $expectation = $this->_mock->shouldReceive($method)->andReturn($return); if ($this->_strict) { $exactArgs = array(); foreach ($args as $arg) { $exactArgs[] = ehough_mockery_Mockery::mustBe($arg); } $expectation->once()->ordered(); call_user_func_array(array($expectation, 'with'), $exactArgs); } else { call_user_func_array(array($expectation, 'with'), $args); } return $return; }
public function tearDown() { ehough_mockery_Mockery::close(); }
/** * Set expected method calls * * @param mixed * @return ehough_mockery_mockery_Expectation */ public function shouldReceive() { //http://stackoverflow.com/questions/4979507/difference-in-behaviour-of-func-num-args-func-get-arg-and-func-get-args-from-php $args = func_get_args(); $lastExpectation = ehough_mockery_Mockery::parseShouldReturnArgs($this, $args, array($this, '_callbackShouldReceive')); return $lastExpectation; }
protected static function _renderPublicMethodParameters(ReflectionMethod $method) { $class = $method->getDeclaringClass(); if ($class->isInternal()) { // check for parameter overrides for internal PHP classes $paramMap = ehough_mockery_Mockery::getConfiguration()->getInternalClassMethodParamMap($class->getName(), $method->getName()); if (!is_null($paramMap)) { return $paramMap; } } $methodParams = array(); $params = $method->getParameters(); $typehintMatch = array(); $isCompatibleWithSelf = version_compare(PHP_VERSION, '5.4.1') >= 0; foreach ($params as $i => $param) { $paramDef = ''; if ($param->isArray()) { $paramDef .= 'array '; } elseif ($isCompatibleWithSelf && $param->getClass()) { $paramDef .= $param->getClass()->getName() . ' '; } elseif (preg_match('/^Parameter #[0-9]+ \\[ \\<(required|optional)\\> (?<typehint>\\S+ )?.*\\$' . $param->getName() . ' .*\\]$/', $param->__toString(), $typehintMatch)) { if (!empty($typehintMatch['typehint'])) { $paramDef .= $typehintMatch['typehint'] . ' '; } } $paramName = $param->getName(); if (empty($paramName) || $paramName === '...') { $paramName = 'arg' . $i; } $paramDef .= ($param->isPassedByReference() ? '&' : '') . '$' . $paramName; if ($param->isOptional()) { if ($param->isDefaultValueAvailable()) { $default = var_export($param->getDefaultValue(), true); } else { $default = 'null'; } $paramDef .= ' = ' . $default; } $methodParams[] = $paramDef; } return $methodParams; }
/** * Sets up expectations on the members of the CompositeExpectation and * builds up any demeter chain that was passed to shouldReceive * * @param ehough_mockery_mockery_MockInterface $mock * @param string $arg * @param Closure $add * @return ehough_mockery_mockery_ExpectationDirector */ protected static function _buildDemeterChain(ehough_mockery_mockery_MockInterface $mock, $arg, $add) { $container = $mock->mockery_getContainer(); $names = explode('->', $arg); reset($names); if (!ehough_mockery_Mockery::getConfiguration()->mockingNonExistentMethodsAllowed() && method_exists($mock, "mockery_getMockableMethods") && !in_array(current($names), $mock->mockery_getMockableMethods())) { throw new ehough_mockery_mockery_Exception('Mockery\'s configuration currently forbids mocking the method ' . current($names) . ' as it does not exist on the class or object ' . 'being mocked'); } $exp = null; $nextExp = array('ehough_mockery_Mockery', '_callbackNextExp1'); //function ($n) use ($add) {return $add($n);}; $useArg = $add; while (true) { $method = array_shift($names); $exp = $mock->mockery_getExpectationsFor($method); $needNew = false; if (is_null($exp) || empty($names)) { $needNew = true; } if ($needNew) { $exp = call_user_func($nextExp, $method, $useArg); } //$nextExp($method); if (empty($names)) { break; } if ($needNew) { $mock = $container->mock('demeter_' . $method); $exp->andReturn($mock); } $nextExp = array('ehough_mockery_Mockery', '_callbackNextExp2'); //function ($n) use ($mock) {return $mock->shouldReceive($n);}; $useArg = $mock; } return $exp; }
/** @group issue/175 */ public function testExistingStaticMethodMocking() { ehough_mockery_Mockery::setContainer($this->container); $mock = $this->container->mock('MockeryTest_PartialStatic[mockMe]'); $mock->shouldReceive('mockMe')->with(5)->andReturn(10); $this->assertEquals(10, $mock::mockMe(5)); $this->assertEquals(3, $mock::keepMe(3)); }
public function teardown() { ehough_mockery_Mockery::getConfiguration()->allowMockingNonExistentMethods(true); $this->container->mockery_close(); }
/** * Return a string with the method name and arguments formatted * * @param string $name Name of the expected method * @param array $args List of arguments to the method * @return string */ public function __toString() { return ehough_mockery_Mockery::formatArgs($this->_name, $this->_expectedArgs); }
public function testAnExampleWithSomeExpectationAmendsOnCallCounts() { $service = $this->container->mock('MyService'); $service->shouldReceive('login')->with('user', 'pass')->once()->andReturn(true); $service->shouldReceive('hasBookmarksTagged')->with('php')->once()->andReturn(false); $service->shouldReceive('addBookmark')->with('/^http:/', ehough_mockery_Mockery::type('string'))->times(3)->andReturn(true); $service->shouldReceive('hasBookmarksTagged')->with('php')->twice()->andReturn(true); $this->assertTrue($service->login('user', 'pass')); $this->assertFalse($service->hasBookmarksTagged('php')); $this->assertTrue($service->addBookmark('http://example.com/1', 'some_tag1')); $this->assertTrue($service->addBookmark('http://example.com/2', 'some_tag2')); $this->assertTrue($service->addBookmark('http://example.com/3', 'some_tag3')); $this->assertTrue($service->hasBookmarksTagged('php')); $this->assertTrue($service->hasBookmarksTagged('php')); $this->container->mockery_verify(); }
/** * @dataProvider formatObjectsDataProvider */ public function testFormatObjects($args, $expected) { $this->assertEquals($expected, ehough_mockery_Mockery::formatObjects($args)); }