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); }
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; }
public function testCanOverrideExpectedParametersOfInternalPHPClassesToPreserveRefs() { if (!class_exists('MongoCollection', false)) { $this->markTestSkipped('ext/mongo not installed'); } ehough_mockery_Mockery::getConfiguration()->setInternalClassMethodParamMap('MongoCollection', 'insert', array('&$data', '$options')); $m = $this->container->mock('MongoCollection'); $m->shouldReceive('insert')->with(ehough_mockery_Mockery::on(function (&$data) { $data['_id'] = 123; return true; }), ehough_mockery_Mockery::type('array')); $data = array('a' => 1, 'b' => 2); $m->insert($data, array()); $this->assertTrue(isset($data['_id'])); $this->assertEquals(123, $data['_id']); $this->container->mockery_verify(); ehough_mockery_Mockery::resetContainer(); }
public function teardown() { ehough_mockery_Mockery::getConfiguration()->allowMockingNonExistentMethods(true); $this->container->mockery_close(); }
/** * @expectedException ehough_mockery_mockery_Exception */ public function testGlobalConfigMayForbidMockingNonExistentMethodsOnObjects() { ehough_mockery_Mockery::getConfiguration()->allowMockingNonExistentMethods(false); $mock = $this->container->mock(new stdClass()); $mock->shouldReceive('foo'); }