/** * Testing newly created function. */ public function testMockNewFunction() { $this->test_function_name = 'new_random_function_' . uniqid(); $this->object = new PHPUnit_Extensions_MockFunction($this->test_function_name, $this->test_scope_object); // Return normally, only checks the call. $this->object->expects($this->any())->will($this->returnValue('OK')); $this->assertSame('OK', $this->test_scope_object->callFunction($this->test_function_name, array())); $this->object->restore(); }
/** * Test simple function return faking without consraints. */ public function testMockWithReturn() { $this->object = new PHPUnit_Extensions_MockStaticMethod('TestStatic::test', $this->test_scope_object); // CHanging return value. $this->object->expects($this->atLeastOnce())->will($this->returnValue('DEF')); $this->assertSame('DEF', $this->test_scope_object->callStatic()); // From this scope the original method is called. $this->assertSame('ABC', TestStatic::test()); $this->object->restore(); // We are back in 1985. $this->assertSame('ABC', $this->test_scope_object->callStatic()); }
/** * @test */ public function function_switch_off_and_on_return_correct_value() { $value = 'non date value'; $mockFunction = new PHPUnit_Extensions_MockFunction('date', $this); $mockFunction->expects($this->any())->will($this->returnValue($value)); $mockFunction->restore(); $mockFunction->mock(); $this->assertEquals($value, date()); }
/** * Clean-up function. * * Removes mocked method and restores the original was there is any. * Also removes the reference to the object from self::$instances. */ public function restore() { if ($this->active) { list($class, $method) = $this->getClassAndMethod(); runkit_method_remove($class, $method); runkit_method_rename($class, $this->restore_name, $method); $this->active = false; } parent::restore(); }