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