/** * Builds a sleep(), usleep(), date(), time() and microtime() mock environment. * * @return MockEnvironment */ public function build() { $environment = new MockEnvironment(); $builder = new MockBuilder(); $builder->setNamespace($this->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, $date]; // 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; }
/** * 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()); }
/** * @before */ public function before() { $this->unlockCommand = new UnlockCommand(); self::$isUnlinkCalled = false; self::$fileExistsResult = false; self::$isFileExists = false; $mockBuilder = new MockBuilder(); $fileExistsMock = $mockBuilder->setName('file_exists')->setNamespace('Mage\\Command\\BuiltIn')->setFunction(function ($filePath) { UnlockCommandTest::$fileExistsResult = $filePath; return UnlockCommandTest::$isFileExists; })->build(); $unlinkMock = $mockBuilder->setName('unlink')->setNamespace('Mage\\Command\\BuiltIn')->setFunction(function () { UnlockCommandTest::$isUnlinkCalled = true; })->build(); $getCwdMock = $mockBuilder->setNamespace('Mage\\Command\\BuiltIn')->setName('getcwd')->setFunction(function () { return ''; })->build(); $fileExistsMock->disable(); $unlinkMock->disable(); $getCwdMock->disable(); $fileExistsMock->enable(); $unlinkMock->enable(); $getCwdMock->enable(); $configMock = $this->getMock('Mage\\Config'); $configMock->expects($this->atLeastOnce())->method('getEnvironment')->willReturn('production'); $this->unlockCommand->setConfig($configMock); $this->setUpConsoleStatics(); }
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()); }
/** * 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()); }
/** * @before */ public function before() { $this->iniGetValue = new FixedValueFunction(); $mockBuilder = new MockBuilder(); $iniGetMock = $mockBuilder->setNamespace('Mage\\Command\\BuiltIn')->setName("ini_get")->setCallableProvider($this->iniGetValue)->build(); $iniGetMock->disable(); $iniGetMock->enable(); $this->setUpConsoleStatics(); }
/** * 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"); }
/** * @before */ public function before() { $this->listCommand = new ListCommand(); $this->scandirValueObj = new FixedValueFunction(); $mockBuilder = new MockBuilder(); $scandirMock = $mockBuilder->setNamespace('Mage\\Command\\BuiltIn')->setName("scandir")->setCallableProvider($this->scandirValueObj)->build(); $scandirMock->disable(); $scandirMock->enable(); $this->setUpConsoleStatics(); }
/** * 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(); }
/** * @requires PHP 5.4 * * @expectedException \Phalcon\Exception * @expectedExceptionMessage iconv module not loaded */ public function testExtensionNotLoaded() { $mock = $this->getMockBuilder('\\malkusch\\phpmock\\phpunit\\MockDelegate')->getMock(); $functionMockBuilder = new MockBuilder(); $functionMockBuilder->setNamespace(__NAMESPACE__)->setName("extension_loaded")->setFunctionProvider(new MockDelegateFunction($mock)); $functionMock = $functionMockBuilder->build(); $functionMock->enable(); $result = $this->getTestResultObject(); $result->addListener(new MockDisabler($functionMock)); $iconv = new MockObjectProxy($mock); $iconv->expects($this->once())->willReturn(false); Slug::generate('test 233'); $iconv->disable(); }
/** * 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; }
/** * 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(); }
/** * @before */ public function before() { self::$fgetsCount = 0; self::$mockName = ''; self::$mockEmail = ''; self::$mockDesc = ''; self::$filePutContentsResult = ''; self::$filePutContentsFile = ''; $this->lockCommand = new LockCommand(); $mockBuilder = new MockBuilder(); $fopenMock = $mockBuilder->setName('fopen')->setNamespace('Mage')->setFunction(function () { return 'a'; })->build(); $this->fgetsValue = new FixedValueFunction(); $fgetsMock = $mockBuilder->setNamespace('Mage')->setName('fgets')->setFunction(function () { switch (LockCommandTest::$fgetsCount) { case 0: LockCommandTest::$fgetsCount++; return LockCommandTest::$mockName; case 1: LockCommandTest::$fgetsCount++; return LockCommandTest::$mockEmail; case 2: LockCommandTest::$fgetsCount++; return LockCommandTest::$mockDesc; default: throw new \Exception('"fgets" count limit exceed'); } })->build(); $getCwdMock = $mockBuilder->setNamespace('Mage\\Command\\Builtin')->setName('getcwd')->setFunction(function () { return ''; })->build(); $fileGetContentsMock = $mockBuilder->setNamespace('Mage\\Command\\Builtin')->setName('file_put_contents')->setFunction(function ($file, $contents) { LockCommandTest::$filePutContentsFile = $file; LockCommandTest::$filePutContentsResult = $contents; })->build(); $dateMock = $mockBuilder->setNamespace('Mage\\Command\\BuiltIn')->setName('date')->setFunction(function () { return '2015-01-01 12:00:00'; })->build(); $fopenMock->disable(); $fgetsMock->disable(); $getCwdMock->disable(); $fileGetContentsMock->disable(); $dateMock->disable(); $fopenMock->enable(); $fgetsMock->enable(); $getCwdMock->enable(); $fileGetContentsMock->enable(); $dateMock->enable(); $this->setUpConsoleStatics(); }
/** * 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")); }