Пример #1
0
 public function testPassingConcreteDependency()
 {
     $simpleClass = new SimpleClass();
     $simpleClass->foo = "bar";
     $object = $this->container->instance(OneDependency::class, $simpleClass);
     $this->assertEquals("bar", $object->injected->foo);
 }
Пример #2
0
 /**
  * @return static
  */
 public static function getProvider()
 {
     if (!self::$defaultSet) {
         self::setProviderClassName(CurlHttpClient::class);
         self::$defaultSet = true;
     }
     return Container::instance(static::class);
 }
Пример #3
0
 public function testSessionRestore()
 {
     $session = UnitTestingSession::singleton();
     $session->TestValue = "abc123";
     $session->storeSession();
     Container::current()->clearSingleton(UnitTestingSession::class);
     $session = UnitTestingSession::singleton();
     $this->assertEquals("abc123", $session->TestValue);
 }
Пример #4
0
 public function testEmailSends()
 {
     // Note this test only confirms the email has made it to the provider.
     $email = Container::instance(SimpleEmail::class);
     $email->addRecipientsByEmail("*****@*****.**")->SetText("This is a test email");
     SendableProvider::selectProviderAndSend($email);
     $lastEmail = UnitTestingEmailProvider::getLastEmail();
     $this->assertEquals($email, $lastEmail);
 }
Пример #5
0
 public static function enableExceptionTrapping()
 {
     /**
      * @var ExceptionSettings $exceptionSettings
      */
     $exceptionSettings = ExceptionSettings::singleton();
     $exceptionSettings->exceptionTrappingOn = true;
     ini_set("display_errors", false);
     // Register the exception handler. Not that this is only to catch exceptions that happen
     // outside of the normal Module response generation pipeline which should be very rare.
     set_exception_handler($exceptionHandler = function ($er) {
         // Upscale non core exceptions to RhubarbExceptions (via NonRhubarbException)
         if (!$er instanceof RhubarbException) {
             $er = new NonRhubarbException($er);
         }
         // Dispatch the exception to the handler.
         /**
          * @var ExceptionHandler $handler
          */
         $handler = Container::instance(ExceptionHandler::class);
         $response = $handler->processException($er);
         $response->send();
     });
     // Register the default php error handler to convert errors to exceptions.
     set_error_handler(function ($code, $message, $file, $line) {
         $reportingLevel = error_reporting();
         // If errors are turned off or they are being suppressed with an '@' we don't do anything with
         // them. This is to allow for optimistic auto loading in the Module class.
         if ($reportingLevel == 0) {
             return;
         }
         throw new ErrorException($message, 0, $code, $file, $line);
     });
     // Make sure we handle fatal errors too.
     register_shutdown_function(function () use($exceptionHandler) {
         /**
          * @var ExceptionSettings $exceptionSettings
          */
         $exceptionSettings = ExceptionSettings::singleton();
         if ($exceptionSettings->exceptionTrappingOn) {
             $error = error_get_last();
             if ($error != null) {
                 // Ensure we're in the project root directory, as some webservers (e.g. apache) can change
                 // the working directory during shutdown.
                 chdir(__DIR__ . '/../../../../../../');
                 if (!file_exists("shutdown_logs")) {
                     @mkdir("shutdown_logs");
                 }
                 @file_put_contents('shutdown_logs/' . date("Y-m-d_H-i-s") . '.txt', "Type: {$error["type"]}\n" . "Message: {$error["message"]}\n" . "File: {$error["file"]}\n" . "Line: {$error["line"]}\n\n", FILE_APPEND);
             }
             if ($error != null && ($error["type"] == E_ERROR || $error["type"] == E_COMPILE_ERROR)) {
                 $exceptionHandler(new ErrorException($error["message"], 0, $error["type"], $error["file"], $error["line"]));
             }
         }
     });
 }
Пример #6
0
 public function testSessionGetsProvider()
 {
     Container::current()->registerClass(SessionProvider::class, UnitTestingSessionProvider::class);
     $session = UnitTestingSession::singleton();
     $this->assertInstanceOf(UnitTestingSessionProvider::class, $session->testGetSessionProvider());
     Container::current()->registerClass(SessionProvider::class, PhpSessionProvider::class);
     // Although we have changed the default provider, we already instantiated the session so the provider will not
     // have changed
     $this->assertInstanceOf(UnitTestingSessionProvider::class, $session->testGetSessionProvider());
 }
Пример #7
0
 public function testSessionEncrypts()
 {
     $session = UnitTestEncryptedSession::singleton();
     $session->testValue = "123456";
     $provider = new UnitTestingSessionProvider();
     $provider->storeSession($session);
     $this->assertEquals("lu3RCzBb/lz4HIqFnlHc7A==", $_SESSION[UnitTestEncryptedSession::class]["testValue"]);
     Container::current()->clearSingleton(UnitTestEncryptedSession::class);
     $session = UnitTestEncryptedSession::singleton();
     $this->assertEquals("123456", $session->testValue);
 }
Пример #8
0
 public function extractSessionData()
 {
     $data = parent::extractSessionData();
     $encrypted = [];
     $keySalt = $this->getEncryptionKeySalt();
     $provider = Container::instance(EncryptionProvider::class);
     foreach ($data as $key => $value) {
         if (is_object($value) || is_array($value)) {
             continue;
         }
         $encrypted[$key] = $provider->encrypt($value, $keySalt);
     }
     return $encrypted;
 }
Пример #9
0
 /**
  * Returns a new instance of the session provider used to store this session.
  *
  * Override this to replace the default behaviour of using the default provider class.
  *
  * @return SessionProviders\SessionProvider
  */
 protected function getNewSessionProvider()
 {
     return Container::instance(SessionProvider::class);
 }