private static function stopSpyingOnFunction($function) { if (!isset(self::$investigations[$function])) { throw new \InvalidArgumentException("The {$function} function isn't being spied on."); } uopz_restore($function); }
/** * Restores an overwritten function. * * @param string $functionName * * @return self */ protected function restore($functionName) { $key = array_search($functionName, $this->overwrites); if (false === $key) { throw new PHPUnit_Framework_Exception(sprintf('"%s" is not overwritten', $functionName)); } uopz_restore($functionName); unset($this->overwrites[$key]); return $this; }
/** * @covers CodeCollab\Encryption\Defusev2\Key::generate */ public function testGenerateThrowsOnCryptoError() { if (!function_exists('uopz_backup')) { $this->markTestSkipped('uopz extension is not installed.'); return; } uopz_backup('random_bytes'); uopz_delete('random_bytes'); $this->expectException(CryptoException::class); $this->key->generate(); uopz_restore('random_bytes'); }
/** * @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'); }
/** * @covers CodeCollab\Encryption\Defuse\Key::generate */ public function testGenerateThrowsOnCryptoErrorThrowsCryptoExceptionBecauseOfObsoleteEncryptionMethod() { if (!function_exists('uopz_backup')) { $this->markTestSkipped('uopz extension is not installed.'); return; } uopz_backup('mcrypt_create_iv'); uopz_delete('mcrypt_create_iv'); $this->expectException(CryptoException::class); $this->expectExceptionMessage('New messages should not be encrypted using the v1 branch of defuse/crypto.'); $this->key->generate(); uopz_restore('mcrypt_create_iv'); }
/** * @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'); }
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 */ }
private static function unsetTime() { uopz_restore('time'); uopz_restore('DateTime', 'getTimestamp'); }
/** * @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(); } }