瀏覽代碼

Changing Friendica\App\Mode from static methods to public methods

- Changing from static methods to public methods
- Adding dev-composer-dependency Mockery for static method mocking (f.e. Config, DBA)
- Adding ModeTest with Mocking
- removing bootstrap from phpunit.xml because of double loading tests\bootstrap.php
tags/2019.01
Philipp Holzer 10 月之前
父節點
當前提交
31148e25cf
沒有連結到貢獻者的電子郵件帳戶。

+ 2
- 2
bin/auth_ejabberd.php 查看文件

@@ -54,7 +54,7 @@ require_once "include/dba.php";
54 54
 
55 55
 $a = new App(dirname(__DIR__));
56 56
 
57
-if (App\Mode::isNormal()) {
57
+if ($a->getMode()->isNormal()) {
58 58
 	$oAuth = new ExAuth();
59 59
 	$oAuth->readStdin();
60
-}
60
+}

+ 1
- 1
bin/daemon.php 查看文件

@@ -34,7 +34,7 @@ require_once "include/dba.php";
34 34
 
35 35
 $a = new App(dirname(__DIR__));
36 36
 
37
-if (App\Mode::isInstall()) {
37
+if ($a->getMode()->isInstall()) {
38 38
 	die("Friendica isn't properly installed yet.\n");
39 39
 }
40 40
 

+ 2
- 1
composer.json 查看文件

@@ -75,7 +75,8 @@
75 75
 		"phpunit/dbunit": "^2.0",
76 76
 		"phpdocumentor/reflection-docblock": "^3.0.2",
77 77
 		"phpunit/php-token-stream": "^1.4.2",
78
-		"mikey179/vfsStream": "^1.6"
78
+		"mikey179/vfsStream": "^1.6",
79
+		"mockery/mockery": "^1.2"
79 80
 	},
80 81
 	"scripts": {
81 82
 		"test": "phpunit"

+ 197
- 6
composer.lock 查看文件

@@ -4,7 +4,7 @@
4 4
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
5 5
         "This file is @generated automatically"
6 6
     ],
7
-    "content-hash": "c2306df3b19ec46d53bb16ac2cffa51a",
7
+    "content-hash": "9f0dbeccbae197460a0ce74a940177cd",
8 8
     "packages": [
9 9
         {
10 10
             "name": "asika/simple-console",
@@ -818,6 +818,28 @@
818 818
                 "reference": null,
819 819
                 "shasum": "30dc7a7ce872155b23a33bd10ad4c76c0d613f55"
820 820
             },
821
+            "require-dev": {
822
+                "npm-asset/babel-core": ">=6.26.0,<7.0.0",
823
+                "npm-asset/babel-plugin-external-helpers": ">=6.22.0,<7.0.0",
824
+                "npm-asset/babel-preset-env": ">=1.6.1,<2.0.0",
825
+                "npm-asset/cpy-cli": ">=1.0.1,<2.0.0",
826
+                "npm-asset/cssnano": ">=3.10.0,<4.0.0",
827
+                "npm-asset/del-cli": ">=1.1.0,<2.0.0",
828
+                "npm-asset/eslint": ">=4.14.0,<5.0.0",
829
+                "npm-asset/eslint-config-airbnb-base": ">=12.1.0,<13.0.0",
830
+                "npm-asset/eslint-plugin-import": ">=2.8.0,<3.0.0",
831
+                "npm-asset/node-qunit-phantomjs": ">=2.0.0,<3.0.0",
832
+                "npm-asset/npm-run-all": ">=4.1.2,<5.0.0",
833
+                "npm-asset/postcss-cli": ">=4.1.1,<5.0.0",
834
+                "npm-asset/postcss-cssnext": ">=3.0.2,<4.0.0",
835
+                "npm-asset/postcss-header": ">=1.0.0,<2.0.0",
836
+                "npm-asset/postcss-url": ">=7.3.0,<8.0.0",
837
+                "npm-asset/rollup": ">=0.53.3,<0.54.0",
838
+                "npm-asset/rollup-plugin-babel": ">=3.0.3,<4.0.0",
839
+                "npm-asset/rollup-watch": ">=4.3.1,<5.0.0",
840
+                "npm-asset/stylefmt": ">=6.0.0,<7.0.0",
841
+                "npm-asset/uglify-js": ">=3.3.4,<4.0.0"
842
+            },
821 843
             "type": "npm-asset-library",
822 844
             "extra": {
823 845
                 "npm-asset-bugs": {
@@ -997,6 +1019,22 @@
997 1019
             "require": {
998 1020
                 "npm-asset/ev-emitter": ">=1.0.0,<2.0.0"
999 1021
             },
1022
+            "require-dev": {
1023
+                "npm-asset/chalk": ">=1.1.1,<2.0.0",
1024
+                "npm-asset/cheerio": ">=0.19.0,<0.20.0",
1025
+                "npm-asset/gulp": ">=3.9.0,<4.0.0",
1026
+                "npm-asset/gulp-jshint": ">=1.11.2,<2.0.0",
1027
+                "npm-asset/gulp-json-lint": ">=0.1.0,<0.2.0",
1028
+                "npm-asset/gulp-rename": ">=1.2.2,<2.0.0",
1029
+                "npm-asset/gulp-replace": ">=0.5.4,<0.6.0",
1030
+                "npm-asset/gulp-requirejs-optimize": "dev-github:metafizzy/gulp-requirejs-optimize",
1031
+                "npm-asset/gulp-uglify": ">=1.4.2,<2.0.0",
1032
+                "npm-asset/gulp-util": ">=3.0.7,<4.0.0",
1033
+                "npm-asset/highlight.js": ">=8.9.1,<9.0.0",
1034
+                "npm-asset/marked": ">=0.3.5,<0.4.0",
1035
+                "npm-asset/minimist": ">=1.2.0,<2.0.0",
1036
+                "npm-asset/transfob": ">=1.0.0,<2.0.0"
1037
+            },
1000 1038
             "type": "npm-asset-library",
1001 1039
             "extra": {
1002 1040
                 "npm-asset-bugs": {
@@ -1042,6 +1080,14 @@
1042 1080
                 "reference": null,
1043 1081
                 "shasum": "2736e332aaee73ccf0a14a5f0066391a0a13f4a3"
1044 1082
             },
1083
+            "require-dev": {
1084
+                "npm-asset/grunt": "~0.4.2",
1085
+                "npm-asset/grunt-contrib-cssmin": "~0.9.0",
1086
+                "npm-asset/grunt-contrib-jshint": "~0.6.3",
1087
+                "npm-asset/grunt-contrib-less": "~0.11.0",
1088
+                "npm-asset/grunt-contrib-uglify": "~0.4.0",
1089
+                "npm-asset/grunt-contrib-watch": "~0.6.1"
1090
+            },
1045 1091
             "type": "npm-asset-library",
1046 1092
             "extra": {
1047 1093
                 "npm-asset-bugs": {
@@ -1075,6 +1121,32 @@
1075 1121
                 "reference": null,
1076 1122
                 "shasum": "2c89d6889b5eac522a7eea32c14521559c6cbf02"
1077 1123
             },
1124
+            "require-dev": {
1125
+                "npm-asset/commitplease": "2.0.0",
1126
+                "npm-asset/core-js": "0.9.17",
1127
+                "npm-asset/grunt": "0.4.5",
1128
+                "npm-asset/grunt-babel": "5.0.1",
1129
+                "npm-asset/grunt-cli": "0.1.13",
1130
+                "npm-asset/grunt-compare-size": "0.4.0",
1131
+                "npm-asset/grunt-contrib-jshint": "0.11.2",
1132
+                "npm-asset/grunt-contrib-uglify": "0.9.2",
1133
+                "npm-asset/grunt-contrib-watch": "0.6.1",
1134
+                "npm-asset/grunt-git-authors": "2.0.1",
1135
+                "npm-asset/grunt-jscs": "2.1.0",
1136
+                "npm-asset/grunt-jsonlint": "1.0.4",
1137
+                "npm-asset/grunt-npmcopy": "0.1.0",
1138
+                "npm-asset/gzip-js": "0.3.2",
1139
+                "npm-asset/jsdom": "5.6.1",
1140
+                "npm-asset/load-grunt-tasks": "1.0.0",
1141
+                "npm-asset/qunit-assert-step": "1.0.3",
1142
+                "npm-asset/qunitjs": "1.17.1",
1143
+                "npm-asset/requirejs": "2.1.17",
1144
+                "npm-asset/sinon": "1.10.3",
1145
+                "npm-asset/sizzle": "2.2.1",
1146
+                "npm-asset/strip-json-comments": "1.0.3",
1147
+                "npm-asset/testswarm": "1.1.0",
1148
+                "npm-asset/win-spawn": "2.0.0"
1149
+            },
1078 1150
             "type": "npm-asset-library",
1079 1151
             "extra": {
1080 1152
                 "npm-asset-bugs": {
@@ -1225,6 +1297,12 @@
1225 1297
                 "reference": null,
1226 1298
                 "shasum": "06f0335f16e353a695e7206bf50503cb523a6ee5"
1227 1299
             },
1300
+            "require-dev": {
1301
+                "npm-asset/grunt": "~0.4.1",
1302
+                "npm-asset/grunt-contrib-connect": "~0.5.0",
1303
+                "npm-asset/grunt-contrib-jshint": "~0.7.1",
1304
+                "npm-asset/grunt-contrib-uglify": "~0.2.7"
1305
+            },
1228 1306
             "type": "npm-asset-library",
1229 1307
             "extra": {
1230 1308
                 "npm-asset-bugs": {
@@ -1933,6 +2011,54 @@
1933 2011
             ],
1934 2012
             "time": "2015-06-14T21:17:01+00:00"
1935 2013
         },
2014
+        {
2015
+            "name": "hamcrest/hamcrest-php",
2016
+            "version": "v2.0.0",
2017
+            "source": {
2018
+                "type": "git",
2019
+                "url": "https://github.com/hamcrest/hamcrest-php.git",
2020
+                "reference": "776503d3a8e85d4f9a1148614f95b7a608b046ad"
2021
+            },
2022
+            "dist": {
2023
+                "type": "zip",
2024
+                "url": "https://api.github.com/repos/hamcrest/hamcrest-php/zipball/776503d3a8e85d4f9a1148614f95b7a608b046ad",
2025
+                "reference": "776503d3a8e85d4f9a1148614f95b7a608b046ad",
2026
+                "shasum": ""
2027
+            },
2028
+            "require": {
2029
+                "php": "^5.3|^7.0"
2030
+            },
2031
+            "replace": {
2032
+                "cordoval/hamcrest-php": "*",
2033
+                "davedevelopment/hamcrest-php": "*",
2034
+                "kodova/hamcrest-php": "*"
2035
+            },
2036
+            "require-dev": {
2037
+                "phpunit/php-file-iterator": "1.3.3",
2038
+                "phpunit/phpunit": "~4.0",
2039
+                "satooshi/php-coveralls": "^1.0"
2040
+            },
2041
+            "type": "library",
2042
+            "extra": {
2043
+                "branch-alias": {
2044
+                    "dev-master": "2.0-dev"
2045
+                }
2046
+            },
2047
+            "autoload": {
2048
+                "classmap": [
2049
+                    "hamcrest"
2050
+                ]
2051
+            },
2052
+            "notification-url": "https://packagist.org/downloads/",
2053
+            "license": [
2054
+                "BSD"
2055
+            ],
2056
+            "description": "This is the PHP port of Hamcrest Matchers",
2057
+            "keywords": [
2058
+                "test"
2059
+            ],
2060
+            "time": "2016-01-20T08:20:44+00:00"
2061
+        },
1936 2062
         {
1937 2063
             "name": "mikey179/vfsStream",
1938 2064
             "version": "v1.6.5",
@@ -1979,6 +2105,71 @@
1979 2105
             "homepage": "http://vfs.bovigo.org/",
1980 2106
             "time": "2017-08-01T08:02:14+00:00"
1981 2107
         },
2108
+        {
2109
+            "name": "mockery/mockery",
2110
+            "version": "1.2.0",
2111
+            "source": {
2112
+                "type": "git",
2113
+                "url": "https://github.com/mockery/mockery.git",
2114
+                "reference": "100633629bf76d57430b86b7098cd6beb996a35a"
2115
+            },
2116
+            "dist": {
2117
+                "type": "zip",
2118
+                "url": "https://api.github.com/repos/mockery/mockery/zipball/100633629bf76d57430b86b7098cd6beb996a35a",
2119
+                "reference": "100633629bf76d57430b86b7098cd6beb996a35a",
2120
+                "shasum": ""
2121
+            },
2122
+            "require": {
2123
+                "hamcrest/hamcrest-php": "~2.0",
2124
+                "lib-pcre": ">=7.0",
2125
+                "php": ">=5.6.0"
2126
+            },
2127
+            "require-dev": {
2128
+                "phpunit/phpunit": "~5.7.10|~6.5|~7.0"
2129
+            },
2130
+            "type": "library",
2131
+            "extra": {
2132
+                "branch-alias": {
2133
+                    "dev-master": "1.0.x-dev"
2134
+                }
2135
+            },
2136
+            "autoload": {
2137
+                "psr-0": {
2138
+                    "Mockery": "library/"
2139
+                }
2140
+            },
2141
+            "notification-url": "https://packagist.org/downloads/",
2142
+            "license": [
2143
+                "BSD-3-Clause"
2144
+            ],
2145
+            "authors": [
2146
+                {
2147
+                    "name": "Pádraic Brady",
2148
+                    "email": "padraic.brady@gmail.com",
2149
+                    "homepage": "http://blog.astrumfutura.com"
2150
+                },
2151
+                {
2152
+                    "name": "Dave Marshall",
2153
+                    "email": "dave.marshall@atstsolutions.co.uk",
2154
+                    "homepage": "http://davedevelopment.co.uk"
2155
+                }
2156
+            ],
2157
+            "description": "Mockery is a simple yet flexible PHP mock object framework",
2158
+            "homepage": "https://github.com/mockery/mockery",
2159
+            "keywords": [
2160
+                "BDD",
2161
+                "TDD",
2162
+                "library",
2163
+                "mock",
2164
+                "mock objects",
2165
+                "mockery",
2166
+                "stub",
2167
+                "test",
2168
+                "test double",
2169
+                "testing"
2170
+            ],
2171
+            "time": "2018-10-02T21:52:37+00:00"
2172
+        },
1982 2173
         {
1983 2174
             "name": "myclabs/deep-copy",
1984 2175
             "version": "1.7.0",
@@ -2779,7 +2970,7 @@
2779 2970
                 }
2780 2971
             ],
2781 2972
             "description": "Provides the functionality to compare PHP values for equality",
2782
-            "homepage": "http://www.github.com/sebastianbergmann/comparator",
2973
+            "homepage": "https://github.com/sebastianbergmann/comparator",
2783 2974
             "keywords": [
2784 2975
                 "comparator",
2785 2976
                 "compare",
@@ -2881,7 +3072,7 @@
2881 3072
                 }
2882 3073
             ],
2883 3074
             "description": "Provides functionality to handle HHVM/PHP environments",
2884
-            "homepage": "http://www.github.com/sebastianbergmann/environment",
3075
+            "homepage": "https://github.com/sebastianbergmann/environment",
2885 3076
             "keywords": [
2886 3077
                 "Xdebug",
2887 3078
                 "environment",
@@ -2949,7 +3140,7 @@
2949 3140
                 }
2950 3141
             ],
2951 3142
             "description": "Provides the functionality to export PHP variables for visualization",
2952
-            "homepage": "http://www.github.com/sebastianbergmann/exporter",
3143
+            "homepage": "https://github.com/sebastianbergmann/exporter",
2953 3144
             "keywords": [
2954 3145
                 "export",
2955 3146
                 "exporter"
@@ -3001,7 +3192,7 @@
3001 3192
                 }
3002 3193
             ],
3003 3194
             "description": "Snapshotting of global state",
3004
-            "homepage": "http://www.github.com/sebastianbergmann/global-state",
3195
+            "homepage": "https://github.com/sebastianbergmann/global-state",
3005 3196
             "keywords": [
3006 3197
                 "global state"
3007 3198
             ],
@@ -3103,7 +3294,7 @@
3103 3294
                 }
3104 3295
             ],
3105 3296
             "description": "Provides functionality to recursively process PHP variables",
3106
-            "homepage": "http://www.github.com/sebastianbergmann/recursion-context",
3297
+            "homepage": "https://github.com/sebastianbergmann/recursion-context",
3107 3298
             "time": "2016-11-19T07:33:16+00:00"
3108 3299
         },
3109 3300
         {

+ 7
- 7
index.php 查看文件

@@ -36,7 +36,7 @@ $a->backend = false;
36 36
 require_once "include/dba.php";
37 37
 
38 38
 // Missing DB connection: ERROR
39
-if (App\Mode::has(App\Mode::LOCALCONFIGPRESENT) && !App\Mode::has(App\Mode::DBAVAILABLE)) {
39
+if ($a->getMode()->has(App\Mode::LOCALCONFIGPRESENT) && !$a->getMode()->has(App\Mode::DBAVAILABLE)) {
40 40
 	System::httpExit(500, ['title' => 'Error 500 - Internal Server Error', 'description' => 'Apologies but the website is unavailable at the moment.']);
41 41
 }
42 42
 
@@ -48,7 +48,7 @@ if ($a->isMaxProcessesReached() || $a->isMaxLoadReached()) {
48 48
 	System::httpExit(503, ['title' => 'Error 503 - Service Temporarily Unavailable', 'description' => 'System is currently overloaded. Please try again later.']);
49 49
 }
50 50
 
51
-if (!App\Mode::isInstall()) {
51
+if (!$a->getMode()->isInstall()) {
52 52
 	if (Config::get('system', 'force_ssl') && ($a->get_scheme() == "http")
53 53
 		&& (intval(Config::get('system', 'ssl_policy')) == SSL_POLICY_FULL)
54 54
 		&& (substr(System::baseUrl(), 0, 8) == "https://")
@@ -107,7 +107,7 @@ if (!empty($_SESSION['language']) && $_SESSION['language'] !== $lang) {
107 107
 	L10n::loadTranslationTable($lang);
108 108
 }
109 109
 
110
-if (!empty($_GET['zrl']) && App\Mode::isNormal()) {
110
+if (!empty($_GET['zrl']) && $a->getMode()->isNormal()) {
111 111
 	$a->query_string = Profile::stripZrls($a->query_string);
112 112
 	if (!local_user()) {
113 113
 		// Only continue when the given profile link seems valid
@@ -130,7 +130,7 @@ if (!empty($_GET['zrl']) && App\Mode::isNormal()) {
130 130
 	}
131 131
 }
132 132
 
133
-if (!empty($_GET['owt']) && App\Mode::isNormal()) {
133
+if (!empty($_GET['owt']) && $a->getMode()->isNormal()) {
134 134
 	$token = $_GET['owt'];
135 135
 	$a->query_string = Profile::stripQueryParam($a->query_string, 'owt');
136 136
 	Profile::openWebAuthInit($token);
@@ -165,9 +165,9 @@ $_SESSION['last_updated'] = defaults($_SESSION, 'last_updated', []);
165 165
 
166 166
 // in install mode, any url loads install module
167 167
 // but we need "view" module for stylesheet
168
-if (App\Mode::isInstall() && $a->module != 'view') {
168
+if ($a->getMode()->isInstall() && $a->module != 'view') {
169 169
 	$a->module = 'install';
170
-} elseif (!App\Mode::has(App\Mode::MAINTENANCEDISABLED) && $a->module != 'view') {
170
+} elseif (!$a->getMode()->has(App\Mode::MAINTENANCEDISABLED) && $a->module != 'view') {
171 171
 	$a->module = 'maintenance';
172 172
 } else {
173 173
 	check_url($a);
@@ -320,7 +320,7 @@ if (file_exists($theme_info_file)) {
320 320
 
321 321
 /* initialise content region */
322 322
 
323
-if (App\Mode::isNormal()) {
323
+if ($a->getMode()->isNormal()) {
324 324
 	Addon::callHooks('page_content_top', $a->page['content']);
325 325
 }
326 326
 

+ 0
- 1
phpunit.xml 查看文件

@@ -1,6 +1,5 @@
1 1
 <?xml version="1.0"?>
2 2
 <phpunit
3
-	bootstrap="tests/bootstrap.php"
4 3
 	verbose="true">
5 4
     <testsuites>
6 5
         <testsuite>

+ 27
- 3
src/App.php 查看文件

@@ -11,6 +11,7 @@ use Friendica\Core\L10n;
11 11
 use Friendica\Core\PConfig;
12 12
 use Friendica\Core\System;
13 13
 use Friendica\Database\DBA;
14
+use Friendica\Network\HTTPException\InternalServerErrorException;
14 15
 
15 16
 require_once 'boot.php';
16 17
 require_once 'include/dba.php';
@@ -83,6 +84,11 @@ class App
83 84
 	public $stylesheets = [];
84 85
 	public $footerScripts = [];
85 86
 
87
+	/**
88
+	 * @var App\Mode The Mode of the Application
89
+	 */
90
+	private $mode;
91
+
86 92
 	/**
87 93
 	 * Register a stylesheet file path to be included in the <head> tag of every page.
88 94
 	 * Inclusion is done in App->initHead().
@@ -193,6 +199,8 @@ class App
193 199
 		$this->callstack['rendering'] = [];
194 200
 		$this->callstack['parser'] = [];
195 201
 
202
+		$this->mode = new App\Mode($basepath);
203
+
196 204
 		$this->reload();
197 205
 
198 206
 		set_time_limit(0);
@@ -300,6 +308,22 @@ class App
300 308
 		$this->register_template_engine('Friendica\Render\FriendicaSmartyEngine');
301 309
 	}
302 310
 
311
+	/**
312
+	 * Returns the Mode of the Application
313
+	 *
314
+	 * @return App\Mode The Application Mode
315
+	 *
316
+	 * @throws InternalServerErrorException when the mode isn't created
317
+	 */
318
+	public function getMode()
319
+	{
320
+		if (empty($this->mode)) {
321
+			throw new InternalServerErrorException('Mode of the Application is not defined');
322
+		}
323
+
324
+		return $this->mode;
325
+	}
326
+
303 327
 	/**
304 328
 	 * Reloads the whole app instance
305 329
 	 */
@@ -310,13 +334,13 @@ class App
310 334
 
311 335
 		$this->loadDatabase();
312 336
 
313
-		App\Mode::determine($this->basepath);
337
+		$this->getMode()->determine($this->basepath);
314 338
 
315 339
 		$this->determineUrlPath();
316 340
 
317 341
 		Config::load();
318 342
 
319
-		if (App\Mode::has(App\Mode::DBAVAILABLE)) {
343
+		if ($this->getMode()->has(App\Mode::DBAVAILABLE)) {
320 344
 			Core\Addon::loadHooks();
321 345
 
322 346
 			$this->loadAddonConfig();
@@ -1402,7 +1426,7 @@ class App
1402 1426
 	 */
1403 1427
 	public function getCurrentTheme()
1404 1428
 	{
1405
-		if (App\Mode::isInstall()) {
1429
+		if ($this->getMode()->isInstall()) {
1406 1430
 			return '';
1407 1431
 		}
1408 1432
 

+ 38
- 20
src/App/Mode.php 查看文件

@@ -20,9 +20,19 @@ class Mode
20 20
 	/***
21 21
 	 * @var int the mode of this Application
22 22
 	 *
23
-	 * Default is 0 (= not set)
24 23
 	 */
25
-	private static $mode = 0;
24
+	private $mode;
25
+
26
+	/**
27
+	 * @var string the basepath of the application
28
+	 */
29
+	private $basepath;
30
+
31
+	public function __construct($basepath = '')
32
+	{
33
+		$this->basepath = $basepath;
34
+		$this->mode = 0;
35
+	}
26 36
 
27 37
 	/**
28 38
 	 * Sets the App mode
@@ -34,34 +44,38 @@ class Mode
34 44
 	 * @param string $basepath the Basepath of the Application
35 45
 	 *
36 46
 	 */
37
-	public static function determine($basepath)
47
+	public function determine($basepath = null)
38 48
 	{
39
-		self::$mode = 0;
49
+		if (!empty($basepath)) {
50
+			$this->basepath = $basepath;
51
+		}
52
+
53
+		$this->mode = 0;
40 54
 
41
-		if (!file_exists($basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
42
-			&& !file_exists($basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
55
+		if (!file_exists($this->basepath . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'local.ini.php')
56
+			&& !file_exists($this->basepath . DIRECTORY_SEPARATOR . '.htconfig.php')) {
43 57
 			return;
44 58
 		}
45 59
 
46
-		self::$mode |= Mode::LOCALCONFIGPRESENT;
60
+		$this->mode |= Mode::LOCALCONFIGPRESENT;
47 61
 
48 62
 		if (!DBA::connected()) {
49 63
 			return;
50 64
 		}
51 65
 
52
-		self::$mode |= Mode::DBAVAILABLE;
66
+		$this->mode |= Mode::DBAVAILABLE;
53 67
 
54 68
 		if (DBA::fetchFirst("SHOW TABLES LIKE 'config'") === false) {
55 69
 			return;
56 70
 		}
57 71
 
58
-		self::$mode |= Mode::DBCONFIGAVAILABLE;
72
+		$this->mode |= Mode::DBCONFIGAVAILABLE;
59 73
 
60 74
 		if (Config::get('system', 'maintenance')) {
61 75
 			return;
62 76
 		}
63 77
 
64
-		self::$mode |= Mode::MAINTENANCEDISABLED;
78
+		$this->mode |= Mode::MAINTENANCEDISABLED;
65 79
 	}
66 80
 
67 81
 	/**
@@ -71,9 +85,13 @@ class Mode
71 85
 	 *
72 86
 	 * @return bool returns true, if the mode is set
73 87
 	 */
74
-	public static function has($mode)
88
+	public function has($mode)
75 89
 	{
76
-		return self::$mode & $mode;
90
+		echo "mode: " . $this->mode . " with " . $mode;
91
+
92
+		echo "value: " . ($this->mode & $mode);
93
+
94
+		return ($this->mode & $mode) > 0;
77 95
 	}
78 96
 
79 97
 
@@ -82,10 +100,10 @@ class Mode
82 100
 	 *
83 101
 	 * @return bool
84 102
 	 */
85
-	public static function isInstall()
103
+	public function isInstall()
86 104
 	{
87
-		return !self::has(Mode::LOCALCONFIGPRESENT) ||
88
-			!self::has(MODE::DBCONFIGAVAILABLE);
105
+		return !$this->has(Mode::LOCALCONFIGPRESENT) ||
106
+			!$this->has(MODE::DBCONFIGAVAILABLE);
89 107
 	}
90 108
 
91 109
 	/**
@@ -93,11 +111,11 @@ class Mode
93 111
 	 *
94 112
 	 * @return bool
95 113
 	 */
96
-	public static function isNormal()
114
+	public function isNormal()
97 115
 	{
98
-		return self::has(Mode::LOCALCONFIGPRESENT) &&
99
-			self::has(Mode::DBAVAILABLE) &&
100
-			self::has(Mode::DBCONFIGAVAILABLE) &&
101
-			self::has(Mode::MAINTENANCEDISABLED);
116
+		return $this->has(Mode::LOCALCONFIGPRESENT) &&
117
+			$this->has(Mode::DBAVAILABLE) &&
118
+			$this->has(Mode::DBCONFIGAVAILABLE) &&
119
+			$this->has(Mode::MAINTENANCEDISABLED);
102 120
 	}
103 121
 }

+ 5
- 5
src/Core/Config.php 查看文件

@@ -31,7 +31,7 @@ class Config extends BaseObject
31 31
 	public static function init()
32 32
 	{
33 33
 		// Database isn't ready or populated yet
34
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
34
+		if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
35 35
 			return;
36 36
 		}
37 37
 
@@ -55,7 +55,7 @@ class Config extends BaseObject
55 55
 	public static function load($family = "config")
56 56
 	{
57 57
 		// Database isn't ready or populated yet
58
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
58
+		if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
59 59
 			return;
60 60
 		}
61 61
 
@@ -88,7 +88,7 @@ class Config extends BaseObject
88 88
 	public static function get($family, $key, $default_value = null, $refresh = false)
89 89
 	{
90 90
 		// Database isn't ready or populated yet, fallback to file config
91
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
91
+		if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
92 92
 			return self::getApp()->getConfigValue($family, $key, $default_value);
93 93
 		}
94 94
 
@@ -116,7 +116,7 @@ class Config extends BaseObject
116 116
 	public static function set($family, $key, $value)
117 117
 	{
118 118
 		// Database isn't ready or populated yet
119
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
119
+		if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
120 120
 			return false;
121 121
 		}
122 122
 
@@ -141,7 +141,7 @@ class Config extends BaseObject
141 141
 	public static function delete($family, $key)
142 142
 	{
143 143
 		// Database isn't ready or populated yet
144
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
144
+		if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
145 145
 			return false;
146 146
 		}
147 147
 

+ 2
- 2
src/Core/Console/ArchiveContact.php 查看文件

@@ -39,7 +39,7 @@ HELP;
39 39
 
40 40
 	protected function doExecute()
41 41
 	{
42
-		$a = get_app();
42
+		$a = \Friendica\BaseObject::getApp();
43 43
 
44 44
 		if ($this->getOption('v')) {
45 45
 			$this->out('Class: ' . __CLASS__);
@@ -56,7 +56,7 @@ HELP;
56 56
 			throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
57 57
 		}
58 58
 
59
-		if (App\Mode::isInstall()) {
59
+		if ($a->getMode()->isInstall()) {
60 60
 			throw new RuntimeException('Friendica isn\'t properly installed yet.');
61 61
 		}
62 62
 

+ 1
- 1
src/Core/Console/Cache.php 查看文件

@@ -65,7 +65,7 @@ HELP;
65 65
 			$this->out('Options: ' . var_export($this->options, true));
66 66
 		}
67 67
 
68
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
68
+		if ($a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
69 69
 			$this->out('Database isn\'t ready or populated yet, database cache won\'t be available');
70 70
 		}
71 71
 

+ 2
- 2
src/Core/Console/Config.php 查看文件

@@ -84,7 +84,7 @@ HELP;
84 84
 			throw new CommandArgsException('Too many arguments');
85 85
 		}
86 86
 
87
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
87
+		if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
88 88
 			$this->out('Database isn\'t ready or populated yet, showing file config only');
89 89
 		}
90 90
 
@@ -143,7 +143,7 @@ HELP;
143 143
 		if (count($this->args) == 0) {
144 144
 			Core\Config::load();
145 145
 
146
-			if (Core\Config::get('system', 'config_adapter') == 'jit' && App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
146
+			if (Core\Config::get('system', 'config_adapter') == 'jit' && $a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
147 147
 				$this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
148 148
 			}
149 149
 

+ 1
- 1
src/Core/Console/GlobalCommunityBlock.php 查看文件

@@ -56,7 +56,7 @@ HELP;
56 56
 			throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
57 57
 		}
58 58
 
59
-		if (App\Mode::isInstall()) {
59
+		if ($a->getMode()->isInstall()) {
60 60
 			throw new \RuntimeException('Database isn\'t ready or populated yet');
61 61
 		}
62 62
 

+ 1
- 1
src/Core/Console/GlobalCommunitySilence.php 查看文件

@@ -65,7 +65,7 @@ HELP;
65 65
 			throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
66 66
 		}
67 67
 
68
-		if (App\Mode::isInstall()) {
68
+		if ($a->getMode()->isInstall()) {
69 69
 			throw new RuntimeException('Database isn\'t ready or populated yet');
70 70
 		}
71 71
 

+ 2
- 2
src/Core/Console/Maintenance.php 查看文件

@@ -47,7 +47,7 @@ HELP;
47 47
 
48 48
 	protected function doExecute()
49 49
 	{
50
-		$a = get_app();
50
+		$a = \Friendica\BaseObject::getApp();
51 51
 
52 52
 		if ($this->getOption('v')) {
53 53
 			$this->out('Class: ' . __CLASS__);
@@ -64,7 +64,7 @@ HELP;
64 64
 			throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
65 65
 		}
66 66
 
67
-		if (App\Mode::isInstall()) {
67
+		if ($a->getMode()->isInstall()) {
68 68
 			throw new \RuntimeException('Database isn\'t ready or populated yet');
69 69
 		}
70 70
 

+ 1
- 1
src/Core/Console/NewPassword.php 查看文件

@@ -57,7 +57,7 @@ HELP;
57 57
 			throw new \Asika\SimpleConsole\CommandArgsException('Too many arguments');
58 58
 		}
59 59
 
60
-		if (App\Mode::isInstall()) {
60
+		if ($a->getMode()->isInstall()) {
61 61
 			throw new RuntimeException('Database isn\'t ready or populated yet');
62 62
 		}
63 63
 

+ 2
- 2
src/Core/Console/PostUpdate.php 查看文件

@@ -33,7 +33,7 @@ HELP;
33 33
 
34 34
 	protected function doExecute()
35 35
 	{
36
-		$a = get_app();
36
+		$a = \Friendica\BaseObject::getApp();
37 37
 
38 38
 		if ($this->getOption($this->helpOptions)) {
39 39
 			$this->out($this->getHelp());
@@ -50,7 +50,7 @@ HELP;
50 50
 			return 0;
51 51
 		}
52 52
 
53
-		if (App\Mode::isInstall()) {
53
+		if ($a->getMode()->isInstall()) {
54 54
 			throw new \RuntimeException('Database isn\'t ready or populated yet');
55 55
 		}
56 56
 

+ 8
- 6
src/Core/PConfig.php 查看文件

@@ -29,12 +29,14 @@ class PConfig extends BaseObject
29 29
 
30 30
 	public static function init($uid)
31 31
 	{
32
+		$a = self::getApp();
33
+
32 34
 		// Database isn't ready or populated yet
33
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
35
+		if (!$a->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
34 36
 			return;
35 37
 		}
36 38
 
37
-		if (self::getApp()->getConfigValue('system', 'config_adapter') == 'preload') {
39
+		if ($a->getConfigValue('system', 'config_adapter') == 'preload') {
38 40
 			self::$adapter = new Config\PreloadPConfigAdapter($uid);
39 41
 		} else {
40 42
 			self::$adapter = new Config\JITPConfigAdapter($uid);
@@ -55,7 +57,7 @@ class PConfig extends BaseObject
55 57
 	public static function load($uid, $family)
56 58
 	{
57 59
 		// Database isn't ready or populated yet
58
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
60
+		if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
59 61
 			return;
60 62
 		}
61 63
 
@@ -84,7 +86,7 @@ class PConfig extends BaseObject
84 86
 	public static function get($uid, $family, $key, $default_value = null, $refresh = false)
85 87
 	{
86 88
 		// Database isn't ready or populated yet
87
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
89
+		if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
88 90
 			return;
89 91
 		}
90 92
 
@@ -113,7 +115,7 @@ class PConfig extends BaseObject
113 115
 	public static function set($uid, $family, $key, $value)
114 116
 	{
115 117
 		// Database isn't ready or populated yet
116
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
118
+		if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
117 119
 			return false;
118 120
 		}
119 121
 
@@ -139,7 +141,7 @@ class PConfig extends BaseObject
139 141
 	public static function delete($uid, $family, $key)
140 142
 	{
141 143
 		// Database isn't ready or populated yet
142
-		if (!App\Mode::has(App\Mode::DBCONFIGAVAILABLE)) {
144
+		if (!self::getApp()->getMode()->has(App\Mode::DBCONFIGAVAILABLE)) {
143 145
 			return false;
144 146
 		}
145 147
 

+ 52
- 0
tests/Util/VFSTrait.php 查看文件

@@ -0,0 +1,52 @@
1
+<?php
2
+
3
+namespace Friendica\Test\Util;
4
+
5
+
6
+use org\bovigo\vfs\vfsStream;
7
+use org\bovigo\vfs\vfsStreamDirectory;
8
+
9
+trait VFSTrait
10
+{
11
+	/**
12
+	 * @var vfsStreamDirectory The Stream Directory
13
+	 */
14
+	protected $root;
15
+
16
+	protected function setUpVfsDir() {
17
+		// the used directories inside the App class
18
+		$structure = [
19
+			'config' => [],
20
+			'bin' => []
21
+		];
22
+
23
+		// create a virtual directory and copy all needed files and folders to it
24
+		$this->root = vfsStream::setup('friendica', null, $structure);
25
+
26
+		$this->setConfigFile('config.ini.php');
27
+		$this->setConfigFile('settings.ini.php');
28
+		$this->setConfigFile('local.ini.php');
29
+		$this->setConfigFile('dbstructure.json');
30
+	}
31
+
32
+	protected function setConfigFile($filename)
33
+	{
34
+		$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
35
+			'..' . DIRECTORY_SEPARATOR .
36
+			'config' . DIRECTORY_SEPARATOR .
37
+			$filename;
38
+
39
+		if (file_exists($file)) {
40
+			vfsStream::newFile($filename)
41
+				->at($this->root->getChild('config'))
42
+				->setContent(file_get_contents($file));
43
+		}
44
+	}
45
+
46
+	protected function delConfigFile($filename)
47
+	{
48
+		if ($this->root->hasChild('config/' . $filename)) {
49
+			$this->root->removeChild('config/' . $filename);
50
+		}
51
+	}
52
+}

+ 139
- 0
tests/src/App/ModeTest.php 查看文件

@@ -0,0 +1,139 @@
1
+<?php
2
+
3
+namespace Friendica\Test\src\App;
4
+
5
+use Friendica\App\Mode;
6
+use Friendica\Test\Util\VFSTrait;
7
+use PHPUnit\Framework\TestCase;
8
+
9
+/**
10
+ * @runTestsInSeparateProcesses
11
+ * @preserveGlobalState disabled
12
+ */
13
+class ModeTest extends TestCase
14
+{
15
+	use VFSTrait;
16
+
17
+	public function setUp()
18
+	{
19
+		parent::setUp(); // TODO: Change the autogenerated stub
20
+
21
+		$this->setUpVfsDir();
22
+	}
23
+
24
+	public function testItEmpty()
25
+	{
26
+		$mode = new Mode($this->root->url());
27
+		$this->assertTrue($mode->isInstall());
28
+		$this->assertFalse($mode->isNormal());
29
+	}
30
+
31
+	public function testWithoutConfig()
32
+	{
33
+		$mode = new Mode($this->root->url());
34
+
35
+		$this->assertTrue($this->root->hasChild('config/local.ini.php'));
36
+
37
+		$this->delConfigFile('local.ini.php');
38
+
39
+		$this->assertFalse($this->root->hasChild('config/local.ini.php'));
40
+
41
+		$mode->determine();
42
+
43
+		$this->assertTrue($mode->isInstall());
44
+		$this->assertFalse($mode->isNormal());
45
+
46
+		$this->assertFalse($mode->has(Mode::LOCALCONFIGPRESENT));
47
+	}
48
+
49
+	public function testWithoutDatabase()
50
+	{
51
+		$dba =  \Mockery::mock('alias:Friendica\Database\DBA');
52
+		$dba
53
+			->shouldReceive('connected')
54
+			->andReturn(false);
55
+
56
+		$mode = new Mode($this->root->url());
57
+		$mode->determine();
58
+
59
+		$this->assertFalse($mode->isNormal());
60
+		$this->assertTrue($mode->isInstall());
61
+
62
+		$this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
63
+		$this->assertFalse($mode->has(Mode::DBAVAILABLE));
64
+	}
65
+
66
+	public function testWithoutDatabaseSetup()
67
+	{
68
+		$dba =  \Mockery::mock('alias:Friendica\Database\DBA');
69
+		$dba
70
+			->shouldReceive('connected')
71
+			->andReturn(true);
72
+		$dba
73
+			->shouldReceive('fetchFirst')
74
+			->with('SHOW TABLES LIKE \'config\'')
75
+			->andReturn(false);
76
+
77
+		$mode = new Mode($this->root->url());
78
+		$mode->determine();
79
+
80
+		$this->assertFalse($mode->isNormal());
81
+		$this->assertTrue($mode->isInstall());
82
+
83
+		$this->assertTrue($mode->has(Mode::LOCALCONFIGPRESENT));
84
+	}
85
+
86
+	public function testWithMaintenanceMode()
87
+	{
88
+		$dba =  \Mockery::mock('alias:Friendica\Database\DBA');
89
+		$dba
90
+			->shouldReceive('connected')
91
+			->andReturn(true);
92
+		$dba
93
+			->shouldReceive('fetchFirst')
94
+			->with('SHOW TABLES LIKE \'config\'')
95
+			->andReturn(true);
96
+
97
+		$conf = \Mockery::mock('alias:Friendica\Core\Config');
98
+		$conf
99
+			->shouldReceive('get')
100
+			->with('system', 'maintenance')
101
+			->andReturn(true);
102
+
103
+		$mode = new Mode($this->root->url());
104
+		$mode->determine();
105
+
106
+		$this->assertFalse($mode->isNormal());
107
+		$this->assertFalse($mode->isInstall());
108
+
109
+		$this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
110
+		$this->assertFalse($mode->has(Mode::MAINTENANCEDISABLED));
111
+	}
112
+
113
+	public function testNormalMode()
114
+	{
115
+		$dba =  \Mockery::mock('alias:Friendica\Database\DBA');
116
+		$dba
117
+			->shouldReceive('connected')
118
+			->andReturn(true);
119
+		$dba
120
+			->shouldReceive('fetchFirst')
121
+			->with('SHOW TABLES LIKE \'config\'')
122
+			->andReturn(true);
123
+
124
+		$conf = \Mockery::mock('alias:Friendica\Core\Config');
125
+		$conf
126
+			->shouldReceive('get')
127
+			->with('system', 'maintenance')
128
+			->andReturn(false);
129
+
130
+		$mode = new Mode($this->root->url());
131
+		$mode->determine();
132
+
133
+		$this->assertTrue($mode->isNormal());
134
+		$this->assertFalse($mode->isInstall());
135
+
136
+		$this->assertTrue($mode->has(Mode::DBCONFIGAVAILABLE));
137
+		$this->assertTrue($mode->has(Mode::MAINTENANCEDISABLED));
138
+	}
139
+}

+ 8
- 42
tests/src/Core/Console/ConsoleTest.php 查看文件

@@ -6,12 +6,15 @@ use Friendica\App;
6 6
 use Friendica\BaseObject;
7 7
 use Friendica\Database\DBA;
8 8
 use Friendica\Test\Util\Intercept;
9
+use Friendica\Test\Util\VFSTrait;
9 10
 use org\bovigo\vfs\vfsStream;
10 11
 use org\bovigo\vfs\vfsStreamDirectory;
11 12
 use PHPUnit\Framework\TestCase;
12 13
 
13 14
 abstract class ConsoleTest extends TestCase
14 15
 {
16
+	use VFSTrait;
17
+
15 18
 	/**
16 19
 	 * @var MultiUseConsole Extension of the basic Friendica Console for testing purpose
17 20
 	 */
@@ -21,11 +24,6 @@ abstract class ConsoleTest extends TestCase
21 24
 	 */
22 25
 	protected $app;
23 26
 
24
-	/**
25
-	 * @var vfsStreamDirectory The Stream Directory
26
-	 */
27
-	protected $root;
28
-
29 27
 	protected $stdout;
30 28
 
31 29
 	protected function setUp()
@@ -40,6 +38,11 @@ abstract class ConsoleTest extends TestCase
40 38
 
41 39
 		$this->setUpVfsDir();
42 40
 
41
+		// fake console.php for setting an executable
42
+		vfsStream::newFile('console.php')
43
+			->at($this->root->getChild('bin'))
44
+			->setContent('<? php');
45
+
43 46
 		// Reusable App object
44 47
 		$this->app = new App($this->root->url());
45 48
 		BaseObject::setApp($this->app);
@@ -67,41 +70,4 @@ abstract class ConsoleTest extends TestCase
67 70
 	protected function getExecutablePath() {
68 71
 		return $this->root->getChild('bin' . DIRECTORY_SEPARATOR . 'console.php')->url();
69 72
 	}
70
-
71
-	private function setUpVfsDir() {
72
-		// the used directories inside the App class
73
-		$structure = [
74
-			'config' => [],
75
-			'bin' => []
76
-		];
77
-
78
-		// create a virtual directory and copy all needed files and folders to it
79
-		$this->root = vfsStream::setup('friendica', null, $structure);
80
-
81
-		$this->setConfigFile('config.ini.php');
82
-		$this->setConfigFile('settings.ini.php');
83
-		$this->setConfigFile('local.ini.php');
84
-		$this->setConfigFile('dbstructure.json');
85
-
86
-		// fake console.php for setting an executable
87
-		vfsStream::newFile('console.php')
88
-			->at($this->root->getChild('bin'))
89
-			->setContent('<? php');
90
-	}
91
-
92
-	private function setConfigFile($filename)
93
-	{
94
-		$file = dirname(__DIR__) . DIRECTORY_SEPARATOR .
95
-			'..' . DIRECTORY_SEPARATOR .
96
-			'..' . DIRECTORY_SEPARATOR .
97
-			'..' . DIRECTORY_SEPARATOR .
98
-			'config' . DIRECTORY_SEPARATOR .
99
-			$filename;
100
-
101
-		if (file_exists($file)) {
102
-			vfsStream::newFile($filename)
103
-				->at($this->root->getChild('config'))
104
-				->setContent(file_get_contents($file));
105
-		}
106
-	}
107 73
 }

Loading…
取消
儲存