Просмотр исходного кода

Merge pull request #1523 from jacquesbh/use-another-configuration-loader

Allow to use another Configuration class
El RIDO 1 год назад
Родитель
Сommit
abb49609e0
2 измененных файлов с 20 добавлено и 3 удалено
  1. 5 3
      lib/Controller.php
  2. 15 0
      tst/ControllerTest.php

+ 5 - 3
lib/Controller.php

@@ -112,10 +112,12 @@ class Controller
      *
      *
      * initializes and runs PrivateBin
      * initializes and runs PrivateBin
      *
      *
+     * @param ?Configuration $config
+     *
      * @access public
      * @access public
      * @throws Exception
      * @throws Exception
      */
      */
-    public function __construct()
+    public function __construct(?Configuration $config = null)
     {
     {
         if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION) < 0) {
         if (version_compare(PHP_VERSION, self::MIN_PHP_VERSION) < 0) {
             error_log(I18n::_('%s requires php %s or above to work. Sorry.', I18n::_('PrivateBin'), self::MIN_PHP_VERSION));
             error_log(I18n::_('%s requires php %s or above to work. Sorry.', I18n::_('PrivateBin'), self::MIN_PHP_VERSION));
@@ -126,7 +128,8 @@ class Controller
             return;
             return;
         }
         }
 
 
-        // load config from ini file, initialize required classes
+        // load config (using ini file by default) & initialize required classes
+        $this->_conf = $config ?? new Configuration();
         $this->_init();
         $this->_init();
 
 
         switch ($this->_request->getOperation()) {
         switch ($this->_request->getOperation()) {
@@ -174,7 +177,6 @@ class Controller
      */
      */
     private function _init()
     private function _init()
     {
     {
-        $this->_conf    = new Configuration;
         $this->_model   = new Model($this->_conf);
         $this->_model   = new Model($this->_conf);
         $this->_request = new Request;
         $this->_request = new Request;
         $this->_urlBase = $this->_request->getRequestUri();
         $this->_urlBase = $this->_request->getRequestUri();

+ 15 - 0
tst/ControllerTest.php

@@ -1,6 +1,7 @@
 <?php declare(strict_types=1);
 <?php declare(strict_types=1);
 
 
 use PHPUnit\Framework\TestCase;
 use PHPUnit\Framework\TestCase;
+use PrivateBin\Configuration;
 use PrivateBin\Controller;
 use PrivateBin\Controller;
 use PrivateBin\Data\Filesystem;
 use PrivateBin\Data\Filesystem;
 use PrivateBin\Persistence\ServerSalt;
 use PrivateBin\Persistence\ServerSalt;
@@ -147,6 +148,20 @@ class ControllerTest extends TestCase
         new Controller;
         new Controller;
     }
     }
 
 
+    /**
+     * @runInSeparateProcess
+     */
+    public function testNonDefaultConf()
+    {
+        $newConfig   = new class extends Configuration {};
+        $configValue = (new ReflectionClass(Controller::class))->getProperty('_conf');
+        $configValue->setAccessible(true);
+        ob_start();
+        $controller = new Controller($newConfig);
+        ob_end_clean();
+        $this->assertSame($newConfig, $configValue->getValue($controller));
+    }
+
     /**
     /**
      * @runInSeparateProcess
      * @runInSeparateProcess
      */
      */