Mocking a build-in PHP function is achieved by using PHP's namespace fallback policy. A mock will provide the namespaced function. I.e. only unqualified functions in a non-global namespace can be mocked. Example: namespace foo; use phpmock\Mock; $time = new Mock( __NAMESPACE__, "time", function () { return 3; } ); $time->enable(); assert (3 == time()); $time->disable(); assert (3 != time());
See also: MockBuilder
Author: Markus Malkusch (markus@malkusch.de)
Inheritance: implements phpmock\Deactivatable
コード例 #1
0
ファイル: MockNamespaceTest.php プロジェクト: OndraM/php-mock
 /**
  * Tests redefining mocks in a different namespace.
  *
  * @test
  * @dataprovider provideTestNamespace
  */
 public function testRedefiningNamespaces()
 {
     $this->builder->setNamespace(__NAMESPACE__);
     $this->mock = $this->builder->build();
     $this->mock->enable();
     $this->assertEquals(1234, time());
 }
コード例 #2
0
 /**
  * Tests disabling the mock.
  *
  * @test
  */
 public function testEndTest()
 {
     $min = new Mock(__NAMESPACE__, "min", "max");
     $min->enable();
     $this->assertEquals(9, min(1, 9));
     $disabler = new MockDisabler($min);
     $disabler->endTest($this, 1);
     $this->assertEquals(1, min(1, 9));
 }
コード例 #3
0
 /**
  * Tests case insensitive mocks.
  *
  * @param string $mockName  The mock function name.
  *
  * @test
  * @dataProvider provideTestCaseSensitivity
  */
 public function testCaseSensitivity($mockName)
 {
     $builder = new MockBuilder();
     $builder->setNamespace(__NAMESPACE__)->setName($mockName)->setFunctionProvider(new FixedValueFunction(1234));
     $this->mock = $builder->build();
     $this->mock->enable();
     $this->assertEquals(1234, time(), "time() is not mocked");
     $this->assertEquals(1234, Time(), "Time() is not mocked");
     $this->assertEquals(1234, TIME(), "TIME() is not mocked");
 }
コード例 #4
0
ファイル: EngineTest.php プロジェクト: phpab/phpab
 public function setUp()
 {
     \phpmock\Mock::disableAll();
     $this->alwaysParticipateFilter = new Percentage(100);
     $this->chooser = new StaticChooser(0);
     $this->variant = $this->getMockBuilder(VariantInterface::class)->setMethods(['getIdentifier', 'run'])->getMock();
     $this->manager = $manager = $this->getMockBuilder(ManagerInterface::class)->getMock();
 }
コード例 #5
0
ファイル: Spy.php プロジェクト: php-mock/php-mock
 public function call(array $arguments)
 {
     $return = null;
     $exception = null;
     try {
         $return = parent::call($arguments);
         return $return;
     } catch (\Exception $e) {
         $exception = $e;
         throw $e;
     } finally {
         $this->invocations[] = new Invocation($arguments, $return, $exception);
     }
 }
コード例 #6
0
ファイル: PercentageTest.php プロジェクト: phpab/phpab
 public function tearDown()
 {
     // disable all mocked functions
     Mock::disableAll();
 }
コード例 #7
0
 /**
  * Checks all predictions defined by prophecies of this Prophet.
  *
  * It will also disable all previously revealed function prophecies.
  *
  * @throws AggregateException If any prediction fails.
  * @SuppressWarnings(PHPMD)
  */
 public function checkPredictions()
 {
     Mock::disableAll();
     $this->prophet->checkPredictions();
 }
コード例 #8
0
ファイル: SpyTest.php プロジェクト: php-mock/php-mock
 protected function disableMocks()
 {
     Mock::disableAll();
 }
コード例 #9
0
ファイル: ExampleTest.php プロジェクト: php-mock/php-mock
 /**
  * Tests the example from the documentation.
  *
  * @test
  */
 public function testExample5()
 {
     $time = new Mock(__NAMESPACE__, "time", function () {
         return 3;
     });
     $time->enable();
     assert(3 == time());
 }
コード例 #10
0
ファイル: CookieTest.php プロジェクト: phpab/phpab
 /**
  * Reset global cookies array and disable
  * global function mocks
  */
 protected function tearDown()
 {
     parent::tearDown();
     Mock::disableAll();
 }
コード例 #11
0
ファイル: Spy.php プロジェクト: OndraM/php-mock
 public function call(array $arguments)
 {
     $return = parent::call($arguments);
     $this->invocations[] = new Invocation($arguments, $return);
     return $return;
 }