Example #1
0
 /**
  * @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');
 }
Example #2
0
 /**
  * @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');
 }
Example #3
0
 /**
  * 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;
 }
Example #4
0
 /**
  * @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');
 }
Example #5
0
 /**
  * @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');
 }
Example #7
0
 /**
  * @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');
 }
Example #8
0
 /**
  * @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');
 }
Example #9
0
 /**
  * @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
      */
 }
Example #11
0
 /**
  * @requires PHP 5.4
  * @requires function uopz_backup
  */
 public function testLog()
 {
     $msg = 'Test message';
     uopz_backup('error_log');
     JAXLLogger::log($msg);
 }