]> git.mxchange.org Git - core.git/blob - application/tests/class_ApplicationHelper.php
Continued:
[core.git] / application / tests / class_ApplicationHelper.php
1 <?php
2 // Must be this namespace, else the launcher cannot find the class.
3 namespace Org\Mxchange\CoreFramework\Helper\Application;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Application\BaseApplication;
7 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
8 use Org\Mxchange\CoreFramework\Factory\Object\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Loader\ClassLoader;
10 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
11 use Org\Mxchange\CoreFramework\Middleware\Debug\DebugMiddleware;
12 use Org\Mxchange\CoreFramework\Registry\Registerable;
13 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
14 use Org\Mxchange\CoreFramework\Utils\Strings\StringUtils;
15
16 /**
17  * A class holding general data about the application and some methods for
18  * the management including the entry point.
19  *
20  * E.g.:
21  *
22  * index.php?app=my_app
23  *
24  * You need to create a folder in the folder "application" named "my_app"
25  * (without the quotes) and create a include file called
26  * class_ApplicationHelper.php. You have to write the same class for your
27  * application and implement the same interface called ManageableApplication
28  * because this class include file will be searched for.
29  *
30  * It is good when you avoid more GET parameters to keep URLs short and sweet.
31  * But sometimes you need some GET paramerers e.g. for your imprint or info page
32  * or other linked pages which you have to create and state some informations.
33  *
34  * Please remember that this include file is being loaded *before* the class
35  * loader is loading classes from "exceptions", "interfaces" and "main"!
36  *
37  * @author              Roland Haeder <webmaster@shipsimu.org>
38  * @version             0.0
39  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
40  * @license             GNU GPL 3.0 or any newer version
41  *
42  * This program is free software: you can redistribute it and/or modify
43  * it under the terms of the GNU General Public License as published by
44  * the Free Software Foundation, either version 3 of the License, or
45  * (at your option) any later version.
46  *
47  * This program is distributed in the hope that it will be useful,
48  * but WITHOUT ANY WARRANTY; without even the implied warranty of
49  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50  * GNU General Public License for more details.
51  *
52  * You should have received a copy of the GNU General Public License
53  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
54  */
55 class ApplicationHelper extends BaseApplication implements ManageableApplication, Registerable {
56         /**
57          * Private constructor
58          *
59          * @return      void
60          */
61         private function __construct () {
62                 // Call parent constructor
63                 parent::__construct(__CLASS__);
64         }
65
66         /**
67          * Getter for an instance of this class
68          *
69          * @return      $selfInstance   An instance of this class
70          */
71         public static final function getSelfInstance () {
72                 // Is the instance there?
73                 if (is_null(self::getApplicationInstance())) {
74                         // Then set it
75                         self::setApplicationInstance(new ApplicationHelper());
76                 }
77
78                 // Return the instance
79                 return self::getApplicationInstance();
80         }
81
82         /**
83          * 1) Setups application data
84          *
85          * @return      void
86          */
87         public function setupApplicationData () {
88                 // Set all application data
89                 $this->setAppName('Unit tests and more');
90                 $this->setAppVersion('0.0.0');
91                 $this->setAppShortName('tests');
92         }
93
94         /**
95          * 2) Does initial stuff before starting the application
96          *
97          * @return      void
98          */
99         public function initApplication () {
100                 // Get config instance
101                 $cfg = FrameworkBootstrap::getConfigurationInstance();
102
103                 // Initialize output system
104                 self::createDebugInstance('ApplicationHelper');
105
106                 /*
107                  * This application needs a database connection then simply call init
108                  * method.
109                  */
110                 FrameworkBootstrap::initDatabaseInstance();
111
112                 // Register core tests
113                 ClassLoader::registerTestsPath('framework/main/tests');
114
115                 // Scan for them now
116                 ClassLoader::scanTestsClasses();
117         }
118
119         /**
120          * 3) Launches the application
121          *
122          * @return      void
123          */
124         public function launchApplication () {
125                 // Get request/response instances
126                 $requestInstance  = FrameworkBootstrap::getRequestInstance();
127                 $responseInstance = FrameworkBootstrap::getResponseInstance();
128
129                 // Get the parameter from the request
130                 $commandName = $requestInstance->getRequestElement('command');
131
132                 // If it is null then get default command
133                 if (is_null($commandName)) {
134                         // Get default command
135                         $commandName = $responseInstance->determineDefaultCommand();
136
137                         // Set it in request
138                         $requestInstance->setRequestElement('command', $commandName);
139                 }
140
141                 // Configuration entry key
142                 $configEntry = sprintf(
143                         '%s_%s_controller_resolver_class',
144                         $this->getAppShortName(),
145                         FrameworkBootstrap::getRequestTypeFromSystem()
146                 );
147
148                 // Get a controller resolver instance
149                 $resolverInstance = ObjectFactory::createObjectByConfiguredName($configEntry, [
150                         $commandName,
151                 ]);
152
153                 // Get a controller instance as well
154                 $this->setControllerInstance($resolverInstance->resolveController());
155
156                 // Launch the test suite here
157                 $this->getControllerInstance()->handleRequest($requestInstance, $responseInstance);
158
159                 // -------------------------- Shutdown phase --------------------------
160                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown in progress ...');
161                 $this->getControllerInstance()->executeShutdownFilters($requestInstance, $responseInstance);
162                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown completed. (This is the last line.)');
163         }
164
165         /**
166          * Handle the indexed array of fatal messages and puts them out in an
167          * acceptable fasion
168          *
169          * @param       $messageList    An array of fatal messages
170          * @return      void
171          */
172         public function handleFatalMessages (array $messageList) {
173                 // Walk through all messages
174                 foreach ($messageList as $message) {
175                         exit(__METHOD__ . ':MSG:' . $message);
176                 }
177         }
178
179         /**
180          * Builds the master template's name
181          *
182          * @return      $masterTemplateName             Name of the master template
183          */
184         public function buildMasterTemplateName () {
185                 return 'tests_main';
186         }
187
188         /**
189          * Assigns extra application-depending data
190          *
191          * @param       $templateInstance       An instance of a CompileableTemplate class
192          * @return      void
193          * @todo        Nothing to add?
194          */
195         public function assignExtraTemplateData (CompileableTemplate $templateInstance) {
196                 DebugMiddleware::getSelfInstance()->partialStub('Unfinished method. templateInstance=' . $templateInstance->__toString());
197         }
198
199 }