Example: namespace foo; use phpmock\MockBuilder; use phpmock\functions\FixedValueFunction; $builder = new MockBuilder(); $builder->setNamespace(__NAMESPACE__) ->setName("time") ->setFunctionProvider(new FixedValueFunction(1417011228)); $mock = $builder->build(); The mock is not enabled yet. assert (time() != 1417011228); $mock->enable(); assert (time() == 1417011228); The mock is disabled and PHP's built-in time() is called. $mock->disable(); assert (time() != 1417011228);
See also: Mock
Author: Markus Malkusch (markus@malkusch.de)
Exemplo n.º 1
0
 /**
  * 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());
 }
Exemplo n.º 2
0
 protected function setUp()
 {
     $builder = new MockBuilder();
     $builder->setNamespace(__NAMESPACE__)->setFunctionProvider(new FixedValueFunction(1234));
     $this->environment = new MockEnvironment();
     $this->environment->addMock($builder->setName("time")->build());
     $this->environment->addMock($builder->setName("rand")->build());
 }
Exemplo n.º 3
0
 /**
  * Reveals the function prophecy.
  *
  * I.e. the prophesized function will become effective.
  *
  * @return Mock enabled function mock
  */
 public function reveal()
 {
     $delegate = $this->prophecy->reveal();
     $builder = new MockBuilder();
     $builder->setNamespace($this->namespace)->setName($this->functionName)->setFunction([$delegate, MockDelegateFunctionBuilder::METHOD]);
     $mock = $builder->build();
     $mock->enable();
     return $mock;
 }
Exemplo n.º 4
0
 /**
  * @test
  * @return void
  */
 public function testOutputWillLogToFile()
 {
     $echoMock = $this->mockBuilder->setName('fwrite')->setNamespace('Symfony\\Component\\Console\\Output')->setFunction(function () {
     })->build();
     $echoMock->enable();
     $serviceMock = $this->getMock('\\Neusta\\Facilior\\Services\\ConsoleService', array('log'));
     $serviceMock->expects($this->once())->method('log');
     $serviceMock->output('Test');
 }
Exemplo n.º 5
0
 /**
  * Tests the example from the documentation.
  *
  * @test
  */
 public function testExample2()
 {
     $builder = new MockBuilder();
     $builder->setNamespace(__NAMESPACE__)->setName("time")->setFunctionProvider(new FixedValueFunction(12345));
     $mock = $builder->build();
     $mock->enable();
     assert(time() == 12345);
     $this->assertEquals(12345, time());
 }
Exemplo n.º 6
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");
 }
Exemplo n.º 7
0
 private function setFilterInputArrayReturnValue($returnValue)
 {
     if ($this->mockFilterInputArray) {
         $this->mockFilterInputArray->disable();
     }
     $builder = new MockBuilder();
     $builder->setNamespace(__NAMESPACE__)->setName("filter_input_array")->setFunctionProvider(new FixedValueFunction($returnValue));
     $this->mockFilterInputArray = $builder->build();
     $this->mockFilterInputArray->enable();
 }
 /**
  * Tests getCallable()
  *
  * @test
  */
 public function testGetCallable()
 {
     $function = new FixedMicrotimeFunction();
     $function->setMicrotimeAsFloat(1.00000001);
     $builder = new MockBuilder();
     $builder->setNamespace(__NAMESPACE__)->setName("microtime")->setFunctionProvider($function);
     $mock = $builder->build();
     $mock->enable();
     $this->assertEquals("0.00000001 1", microtime());
     $this->assertEquals(1.00000001, microtime(true));
     $mock->disable();
 }
Exemplo n.º 9
0
 public function testShouldParticipateWithCustomPropabilityAndNegativeResult()
 {
     // Arrange
     // Override mt_rand
     $builder = new MockBuilder();
     $builder->setNamespace(__NAMESPACE__)->setName('mt_rand')->setFunctionProvider(new FixedValueFunction(99));
     $mock = $builder->build();
     $mock->enable();
     $lottery = new Percentage(23);
     // Act
     $participates = $lottery->shouldParticipate();
     // Assert
     $this->assertFalse($participates);
 }
Exemplo n.º 10
0
 /**
  * Returns the enabled function mock.
  *
  * This mock will be disabled automatically after the test run.
  *
  * @param string $namespace The function namespace.
  * @param string $name      The function name.
  *
  * @return \PHPUnit_Framework_MockObject_MockObject The PHPUnit mock.
  */
 public function getFunctionMock($namespace, $name)
 {
     $delegateBuilder = new MockDelegateFunctionBuilder();
     $delegateBuilder->build($name);
     $mock = $this->getMockBuilder($delegateBuilder->getFullyQualifiedClassName())->getMockForAbstractClass();
     $mock->__phpunit_getInvocationMocker()->addMatcher(new DefaultArgumentRemover());
     $functionMockBuilder = new MockBuilder();
     $functionMockBuilder->setNamespace($namespace)->setName($name)->setFunctionProvider($mock);
     $functionMock = $functionMockBuilder->build();
     $functionMock->enable();
     $this->registerForTearDown($functionMock);
     $proxy = new MockObjectProxy($mock);
     return $proxy;
 }
Exemplo n.º 11
0
 public function testChooseVariantsWithKeys()
 {
     // Override mt_rand
     $builder = new MockBuilder();
     $builder->setNamespace(__NAMESPACE__)->setName('mt_rand')->setFunctionProvider(new FixedValueFunction(0));
     $mock = $builder->build();
     $mock->enable();
     // Arrange
     $variant1 = $this->getMock(VariantInterface::class, [], ['v1']);
     $variant2 = $this->getMock(VariantInterface::class, [], ['v2']);
     $chooser = new RandomChooser();
     // Act
     $chosen = $chooser->chooseVariant(['Walter' => $variant1, 'White' => $variant2]);
     // Assert
     $this->assertSame($variant1, $chosen);
 }
Exemplo n.º 12
0
 /**
  * Tests build().
  *
  * @test
  */
 public function testBuild()
 {
     $builder = new MockBuilder();
     $builder->setNamespace(__NAMESPACE__)->setName("time")->setFunction(function () {
         return 1234;
     });
     $mock = $builder->build();
     $mock->enable();
     $this->assertEquals(1234, time());
     $mock->disable();
     $builder->setFunctionProvider(new FixedValueFunction(123));
     $mock = $builder->build();
     $mock->enable();
     $this->assertEquals(123, time());
     $mock->disable();
 }
Exemplo n.º 13
0
 /**
  * Builds a sleep(), usleep(), date(), time() and microtime() mock environment.
  *
  * @return MockEnvironment
  */
 public function build()
 {
     $environment = new MockEnvironment();
     $builder = new MockBuilder();
     $incrementables = [];
     foreach ($this->namespaces as $namespace) {
         $builder->setNamespace($namespace);
         // microtime() mock
         $microtime = new FixedMicrotimeFunction($this->timestamp);
         $builder->setName("microtime")->setFunctionProvider($microtime);
         $environment->addMock($builder->build());
         // time() mock
         $builder->setName("time")->setFunction([$microtime, "getTime"]);
         $environment->addMock($builder->build());
         // date() mock
         $date = new FixedDateFunction($this->timestamp);
         $builder->setName("date")->setFunctionProvider($date);
         $environment->addMock($builder->build());
         $incrementables[] = $microtime;
         $incrementables[] = $date;
     }
     // Need a complete list of $incrementables.
     foreach ($this->namespaces as $namespace) {
         $builder->setNamespace($namespace);
         // sleep() mock
         $builder->setName("sleep")->setFunctionProvider(new SleepFunction($incrementables));
         $environment->addMock($builder->build());
         // usleep() mock
         $builder->setName("usleep")->setFunctionProvider(new UsleepFunction($incrementables));
         $environment->addMock($builder->build());
     }
     return $environment;
 }
Exemplo n.º 14
0
 public function testCreateServiceSession()
 {
     // Arrange
     $builder = new MockBuilder();
     $builder->setNamespace('PhpAb\\Storage');
     $builder->setName("session_status");
     $builder->setFunctionProvider(new FixedValueFunction(PHP_SESSION_ACTIVE));
     $sessionStatusMock = $builder->build();
     $sessionStatusMock->enable();
     $serviceLocator = $this->getMockForAbstractClass(ServiceLocatorInterface::class);
     $serviceLocator->expects($this->once())->method('get')->with($this->equalTo('Config'))->willReturn(['phpab' => ['storage' => 'session', 'storage_options' => ['name' => 'namespace']]]);
     $service = new StorageFactory();
     // Act
     $storage = $service->createService($serviceLocator);
     // Assert
     $this->assertInstanceOf(Session::class, $storage);
 }
Exemplo n.º 15
0
 protected function setUp()
 {
     $this->logPath = "tst/logs/Test.log";
     # Wed, 11 May 2016 21:21:00 GMT
     $this->sampleTimestamp = 1463001660;
     $this->sampleLogfile = $this->getLogfile($this->sampleTimestamp);
     $monoLogMockBuilder = new MockBuilder();
     $monoLogMockBuilder->setNamespace("Monolog")->setName("microtime")->setFunction(function () {
         return MetricTest::$now;
     });
     $this->mockMonoLog = $monoLogMockBuilder->build();
     $this->mockMonoLog->enable();
     $haymetricMockBuilder = new MockBuilder();
     $haymetricMockBuilder->setNamespace("haymetric")->setName("date")->setFunction(function ($format) {
         return date($format, MetricTest::$now);
     });
     $this->mockHaymetric = $haymetricMockBuilder->build();
     $this->mockHaymetric->enable();
 }
Exemplo n.º 16
0
 /**
  * Defines the function prophecy in the given namespace.
  *
  * In most cases you don't have to call this method. {@link prophesize()}
  * is doing this for you. But if the prophecy is defined after the first
  * call in the tested class, the tested class doesn't resolve to the prophecy.
  * This is documented in Bug #68541. You therefore have to define
  * the namespaced function before the first call.
  *
  * Defining the function has no side effects. If the function was
  * already defined this method does nothing.
  *
  * @param string $namespace function namespace
  * @param string $name      function name
  *
  * @see prophesize()
  * @link https://bugs.php.net/bug.php?id=68541 Bug #68541
  */
 public static function define($namespace, $name)
 {
     $builder = new MockBuilder();
     $builder->setNamespace($namespace)->setName($name)->setFunction(function () {
     })->build()->define();
 }
Exemplo n.º 17
0
 /**
  * Tests defining the mock after calling the qualified function name.
  *
  * @test
  */
 public function testDefiningAfterCallingQualified()
 {
     $function = __NAMESPACE__ . '\\str_word_count';
     $this->assertFalse(function_exists($function));
     \str_word_count("foo");
     $builder = new MockBuilder();
     $builder->setNamespace(__NAMESPACE__)->setName("str_word_count")->setFunctionProvider(new FixedValueFunction("bar"));
     $this->mock = $builder->build();
     $this->mock->enable();
     $this->assertTrue(function_exists($function));
     $this->assertEquals("bar", str_word_count("foo"));
 }