build() public method

Builds a mock.
public build ( ) : Mock
return Mock The mock.
Esempio 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());
 }
Esempio n. 2
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();
 }
Esempio 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;
 }
Esempio n. 4
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());
 }
 /**
  * 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");
 }
Esempio n. 6
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();
 }
Esempio n. 8
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);
 }
Esempio n. 9
0
 /**
  * Builds a function mock.
  *
  * Disable this mock after the test with Mockery::close().
  *
  * @param string $namespace The function namespace.
  * @param string $name      The function name.
  *
  * @return Expectation The mockery expectation.
  * @SuppressWarnings(PHPMD)
  */
 public static function mock($namespace, $name)
 {
     $delegateBuilder = new MockDelegateFunctionBuilder();
     $delegateBuilder->build($name);
     $mockeryMock = Mockery::mock($delegateBuilder->getFullyQualifiedClassName());
     $expectation = $mockeryMock->makePartial()->shouldReceive(MockDelegateFunctionBuilder::METHOD);
     $builder = new MockBuilder();
     $builder->setNamespace($namespace)->setName($name)->setFunctionProvider($mockeryMock);
     $mock = $builder->build();
     $mock->enable();
     $disabler = new MockDisabler($mock);
     Mockery::getContainer()->rememberMock($disabler);
     return $expectation;
 }
Esempio 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;
 }
Esempio 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);
 }
Esempio n. 12
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);
 }
Esempio n. 13
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();
 }
Esempio n. 14
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;
 }
Esempio n. 15
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"));
 }