private static function setTime($timestamp) { $handler = function () use($timestamp) { return $timestamp; }; uopz_function('time', $handler); uopz_function('DateTime', 'getTimestamp', $handler); }
/** * @test */ public function itShouldPassOptionsToRedisArray() { $redisArrayOptions = array("previous" => "something", "function" => array($this, '__callback_itShouldPassOptionsToRedisArray_returnKey'), "distributor" => array($this, '__callback_itShouldPassOptionsToRedisArray_return0'), "index" => "something", "autorehash" => "something", "pconnect" => "something", "retry_interval" => "something", "lazy_connect" => "something", "connect_timeout" => "something"); $driverOptions = array_merge($this->getOptions(), $redisArrayOptions); if (!extension_loaded('uopz')) { $this->markTestSkipped('uopz extension is necessarry in order to stub "new".'); } uopz_backup('RedisArray', '__construct'); uopz_function('RedisArray', '__construct', $this->_getClosure($redisArrayOptions)); $this->getFreshDriver($driverOptions); uopz_restore('RedisArray', '__construct'); }
/** * Overwrites a native PHP function. * * @param string $functionName * @param callable $callback * * @return self */ protected function overwrite($functionName, $callback) { if (!function_exists('uopz_function')) { $this->markTestSkipped('uopz is not enabled'); } if (in_array($functionName, $this->overwrites)) { throw new PHPUnit_Framework_Exception(sprintf('"%s" is already overwritten', $functionName)); } uopz_backup($functionName); uopz_function($functionName, $callback); $this->overwrites[] = $functionName; return $this; }
private static function spyOnFunction($function) { if (!function_exists($function)) { throw new \InvalidArgumentException("The {$function} function doesn't exist."); } $newFunctionName = uniqid($function, true); uopz_rename($function, $newFunctionName); $spy = new Spy($newFunctionName); $handler = self::createHandler($spy); uopz_function($function, $handler); self::$investigations[$function] = true; return $spy; }
/** * @covers CodeCollab\CsrfToken\Generator\RandomBytes32::generate */ public function testGenerateNoSufficientStrength() { if (!function_exists('uopz_backup')) { $this->markTestSkipped('uopz extension is not installed.'); return; } uopz_backup('random_bytes'); uopz_function('random_bytes', function () { return false; }); $this->setExpectedException('CodeCollabLib\\CsrfToken\\Generator\\InsufficientStrengthException'); (new RandomBytes32($this->getMock('CodeCollabLib\\CsrfToken\\Storage\\Storage')))->generate(); uopz_restore('random_bytes'); }
/** * @covers CodeCollab\CsrfToken\Generator\RandomBytes32::generate */ public function testGenerateNoAppropriateSourceOfRandomness() { if (!function_exists('uopz_backup')) { $this->markTestSkipped('uopz extension is not installed.'); return; } uopz_backup('random_bytes'); uopz_function('random_bytes', function () { throw new \Exception('Could not gather sufficient random data', 1); }); $this->expectException(InsufficientStrengthException::class); $this->expectExceptionMessage('Could not gather sufficient random data'); (new RandomBytes32($this->createMock(Storage::class)))->generate(); uopz_restore('random_bytes'); }
/** * @covers CodeCollab\Encryption\Defuse\Decryptor::__construct * @covers CodeCollab\Encryption\Defuse\Decryptor::decrypt */ public function testDecryptThrowsOnOpensslGetCipherMethodsCipherMethodNotAvailable() { if (!function_exists('uopz_backup')) { $this->markTestSkipped('uopz extension is not installed.'); return; } uopz_backup('openssl_get_cipher_methods'); uopz_function('openssl_get_cipher_methods', function (bool $aliases = false) : array { return []; }); $this->expectException(CryptoException::class); $decryptor = new Decryptor(base64_decode('iikuhrV0bgDuN8496EbSFA==')); $decryptor->decrypt('ciphertext'); uopz_restore('openssl_get_cipher_methods'); }
/** * @test */ public function itShouldPassOptionsToRedisArray() { $redisArrayOptions = array("previous" => "something", "function" => function ($key) { return $key; }, "distributor" => function ($key) { return 0; }, "index" => "something", "autorehash" => "something", "pconnect" => "something", "retry_interval" => "something", "lazy_connect" => "something", "connect_timeout" => "something"); $driverOptions = array_merge($this->getOptions(), $redisArrayOptions); if (!extension_loaded('uopz')) { $this->markTestSkipped('uopz extension is necessarry in order to stub "new".'); } uopz_backup('\\RedisArray', '__construct'); $self = $this; uopz_function('\\RedisArray', '__construct', function ($serverArray, $actualRedisArrayOptions) use($self, $redisArrayOptions) { $self->assertEquals($redisArrayOptions, $actualRedisArrayOptions); }); $this->getFreshDriver($driverOptions); uopz_restore('\\RedisArray', '__construct'); }
/** * @requires extension uopz * @dataProvider provideQueueInterfaceMethods */ public function testThrowExceptionOnInabilityToCreateResource($method) { uopz_backup('msg_get_queue'); uopz_function('msg_get_queue', function () { return false; }); $passed = false; try { // suppress notices/warnings triggered by msg_* functions // to avoid a PHPUnit_Framework_Error_Notice to be thrown @$this->callQueueMethod($this->queue, $method); } catch (NoItemAvailableException $e) { } catch (QueueException $e) { $this->assertSame('Failed to create/attach to the queue.', $e->getMessage()); $passed = true; } uopz_restore('msg_get_queue'); if (!$passed) { $this->fail(); } }
public function testMockingPHPFunctions() { // Or, we can use the fancy UOPZ extension! uopz_backup('microtime'); // backup "microtime" before we override it, so we don't affect other tests /* * Replace "microtime" to return a simple constant */ uopz_function('microtime', function () { return 12345; }); // Run the test $helper = new MockingFunctionsHelper(); $this->assertSame(12345, $helper->whatMicrotimeTimeIsIt()); // Restore the original "microtime" function uopz_restore('microtime'); /* * Read more about UOPZ at the links below: * * http://php.net/manual/en/ref.uopz.php * http://blog.krakjoe.ninja/2015/01/mocking-php.html */ }
}); // replace strtotime() uopz_rename("strtotime", "original_strtotime"); uopz_function("strtotime", function ($time, $now = 0) { if (isset($now) && !empty($now)) { return original_strtotime($time, $now); } else { return original_strtotime($time, time()); } }); // replace date() uopz_rename("date", "original_date"); uopz_function("date", function ($format, $timestamp = 0) { if (isset($timestamp) && !empty($timestamp)) { return original_date($format, $timestamp); } else { return original_date($format, time()); } }); return true; }; $travelByRunkit = function () use($travelName) { // replace time() runkit_function_rename("time", "original_time"); runkit_function_add("time", "", <<<FUNC \t return original_time() + \$_ENV["{$travelName}"]; FUNC ); // replace strtotime() runkit_function_rename("strtotime", "original_strtotime"); runkit_function_add("strtotime", '$time, $now = 0', <<<'FUNC'