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\ObjectFactory;
9 use Org\Mxchange\CoreFramework\Loader\ClassLoader;
10 use Org\Mxchange\CoreFramework\Manager\ManageableApplication;
11 use Org\Mxchange\CoreFramework\Registry\Registerable;
12 use Org\Mxchange\CoreFramework\Template\CompileableTemplate;
13
14 /**
15  * A class holding general data about the application and some methods for
16  * the management including the entry point.
17  *
18  * E.g.:
19  *
20  * index.php?app=my_app
21  *
22  * You need to create a folder in the folder "application" named "my_app"
23  * (without the quotes) and create a include file called
24  * class_ApplicationHelper.php. You have to write the same class for your
25  * application and implement the same interface called ManageableApplication
26  * because this class include file will be searched for.
27  *
28  * It is good when you avoid more GET parameters to keep URLs short and sweet.
29  * But sometimes you need some GET paramerers e.g. for your imprint or info page
30  * or other linked pages which you have to create and state some informations.
31  *
32  * Please remember that this include file is being loaded *before* the class
33  * loader is loading classes from "exceptions", "interfaces" and "main"!
34  *
35  * @author              Roland Haeder <webmaster@shipsimu.org>
36  * @version             0.0
37  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2019 Core Developer Team
38  * @license             GNU GPL 3.0 or any newer version
39  *
40  * This program is free software: you can redistribute it and/or modify
41  * it under the terms of the GNU General Public License as published by
42  * the Free Software Foundation, either version 3 of the License, or
43  * (at your option) any later version.
44  *
45  * This program is distributed in the hope that it will be useful,
46  * but WITHOUT ANY WARRANTY; without even the implied warranty of
47  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
48  * GNU General Public License for more details.
49  *
50  * You should have received a copy of the GNU General Public License
51  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
52  */
53 class ApplicationHelper extends BaseApplication implements ManageableApplication, Registerable {
54         /**
55          * The version number of this application
56          */
57         private $appVersion = '';
58
59         /**
60          * The human-readable name for this application
61          */
62         private $appName = '';
63
64         /**
65          * The short uni*-like name for this application
66          */
67         private $shortName = '';
68
69         /**
70          * An instance of this class
71          */
72         private static $selfInstance = NULL;
73
74         /**
75          * Private constructor
76          *
77          * @return      void
78          */
79         protected function __construct () {
80                 // Call parent constructor
81                 parent::__construct(__CLASS__);
82         }
83
84         /**
85          * Getter for an instance of this class
86          *
87          * @return      $selfInstance   An instance of this class
88          */
89         public static final function getSelfInstance () {
90                 // Is the instance there?
91                 if (is_null(self::$selfInstance)) {
92                         self::$selfInstance = new ApplicationHelper();
93                 } // END - if
94
95                 // Return the instance
96                 return self::$selfInstance;
97         }
98
99         /**
100          * Getter for the version number
101          *
102          * @return      $appVersion     The application's version number
103          */
104         public final function getAppVersion () {
105                 return $this->appVersion;
106         }
107         /**
108          * Setter for the version number
109          *
110          * @param       $appVersion     The application's version number
111          * @return      void
112          */
113         public final function setAppVersion ($appVersion) {
114                 // Cast and set it
115                 $this->appVersion = (string) $appVersion;
116         }
117
118         /**
119          * Getter for human-readable name
120          *
121          * @return      $appName        The application's human-readable name
122          */
123         public final function getAppName () {
124                 return $this->appName;
125         }
126
127         /**
128          * Setter for human-readable name
129          *
130          * @param       $appName        The application's human-readable name
131          * @return      void
132          */
133         public final function setAppName ($appName) {
134                 // Cast and set it
135                 $this->appName = (string) $appName;;
136         }
137
138         /**
139          * Getter for short uni*-like name
140          *
141          * @return      $shortName      The application's short uni*-like name
142          */
143         public final function getAppShortName () {
144                 return $this->shortName;
145         }
146
147         /**
148          * Setter for short uni*-like name
149          *
150          * @param       $shortName      The application's short uni*-like name
151          * @return      void
152          */
153         public final function setAppShortName ($shortName) {
154                 // Cast and set it
155                 $this->shortName = (string) $shortName;
156         }
157
158         /**
159          * 1) Setups application data
160          *
161          * @return      void
162          */
163         public function setupApplicationData () {
164                 // Set all application data
165                 $this->setAppName('Unit tests and more');
166                 $this->setAppVersion('0.0.0');
167                 $this->setAppShortName('tests');
168         }
169
170         /**
171          * 2) Does initial stuff before starting the application
172          *
173          * @return      void
174          */
175         public function initApplication () {
176                 // Get config instance
177                 $cfg = FrameworkBootstrap::getConfigurationInstance();
178
179                 // Initialize output system
180                 self::createDebugInstance('ApplicationHelper');
181
182                 /*
183                  * This application needs a database connection then simply call init
184                  * method.
185                  */
186                 FrameworkBootstrap::initDatabaseInstance();
187
188                 // Register core tests
189                 ClassLoader::registerTestsPath('framework/main/tests');
190
191                 // Register own tests
192                 ClassLoader::registerTestsPath('application/tests/tests');
193
194                 // Scan for them now
195                 ClassLoader::scanTestsClasses();
196         }
197
198         /**
199          * 3) Launches the application
200          *
201          * @return      void
202          */
203         public function launchApplication () {
204                 // Get request/response instances
205                 $requestInstance  = FrameworkBootstrap::getRequestInstance();
206                 $responseInstance = FrameworkBootstrap::getResponseInstance();
207
208                 // Get the parameter from the request
209                 $commandName = $requestInstance->getRequestElement('command');
210
211                 // If it is null then get default command
212                 if (is_null($commandName)) {
213                         // Get default command
214                         $commandName = $responseInstance->determineDefaultCommand();
215
216                         // Set it in request
217                         $requestInstance->setRequestElement('command', $commandName);
218                 } // END - if
219
220                 // Get a controller resolver
221                 $resolverClass = sprintf(
222                         'Org\Mxchange\CoreFramework\Tests\Resolver\Controller\%s',
223                         self::convertToClassName(sprintf(
224                                 '%s_%s_controller_resolver',
225                                 $this->getAppShortName(),
226                                 FrameworkBootstrap::getRequestTypeFromSystem()
227                         ))
228                 );
229                 $resolverInstance = ObjectFactory::createObjectByName($resolverClass, array($commandName));
230
231                 // Get a controller instance as well
232                 $this->setControllerInstance($resolverInstance->resolveController());
233
234                 // Launch the test suite here
235                 $this->getControllerInstance()->handleRequest($requestInstance, $responseInstance);
236
237                 // -------------------------- Shutdown phase --------------------------
238                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown in progress ...');
239                 $this->getControllerInstance()->executeShutdownFilters($requestInstance, $responseInstance);
240                 self::createDebugInstance(__CLASS__, __LINE__)->debugOutput('MAIN: Shutdown completed. (This is the last line.)');
241         }
242
243         /**
244          * Handle the indexed array of fatal messages and puts them out in an
245          * acceptable fasion
246          *
247          * @param       $messageList    An array of fatal messages
248          * @return      void
249          */
250         public function handleFatalMessages (array $messageList) {
251                 // Walk through all messages
252                 foreach ($messageList as $message) {
253                         exit(__METHOD__ . ':MSG:' . $message);
254                 } // END - foreach
255         }
256
257         /**
258          * Builds the master template's name
259          *
260          * @return      $masterTemplateName             Name of the master template
261          */
262         public function buildMasterTemplateName () {
263                 return 'tests_main';
264         }
265
266         /**
267          * Assigns extra application-depending data
268          *
269          * @param       $templateInstance       An instance of a CompileableTemplate
270          * @return      void
271          * @todo        Nothing to add?
272          */
273         public function assignExtraTemplateData (CompileableTemplate $templateInstance) {
274                 $this->partialStub('Unfinished method. templateInstance=' . $templateInstance->__toString());
275         }
276
277 }