]> git.mxchange.org Git - core.git/blob - application/tests/classes/filter/tests/configuration/classes/class_TestConfigurationLoadableClassesFilter.php
Continued:
[core.git] / application / tests / classes / filter / tests / configuration / classes / class_TestConfigurationLoadableClassesFilter.php
1 <?php
2 // Own namespace
3 namespace Org\Mxchange\CoreFramework\Tests\Filter\Configuration\Classes;
4
5 // Import framework stuff
6 use Org\Mxchange\CoreFramework\Bootstrap\FrameworkBootstrap;
7 use Org\Mxchange\CoreFramework\Filter\Filterable;
8 use Org\Mxchange\CoreFramework\Request\Requestable;
9 use Org\Mxchange\CoreFramework\Response\Responseable;
10 use Org\Mxchange\CoreFramework\Tests\Filter\BaseTestsFilter;
11
12 // Import SPL stuff
13 use \InvalidArgumentException;
14
15 /**
16  * A LoadableClasses filter for tests
17  *
18  * @author              Roland Haeder <webmaster@ship-simu.org>
19  * @version             0.0.0
20  * @copyright   Copyright (c) 2007, 2008 Roland Haeder, 2009 - 2023 Core Developer Team
21  * @license             GNU GPL 3.0 or any newer version
22  * @link                http://www.ship-simu.org
23  *
24  * This program is free software: you can redistribute it and/or modify
25  * it under the terms of the GNU General Public License as published by
26  * the Free Software Foundation, either version 3 of the License, or
27  * (at your option) any later version.
28  *
29  * This program is distributed in the hope that it will be useful,
30  * but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32  * GNU General Public License for more details.
33  *
34  * You should have received a copy of the GNU General Public License
35  * along with this program. If not, see <http://www.gnu.org/licenses/>.
36  */
37 class TestConfigurationLoadableClassesFilter extends BaseTestsFilter implements Filterable {
38         /**
39          * Protected constructor
40          *
41          * @return      void
42          */
43         private function __construct () {
44                 // Call parent constructor
45                 parent::__construct(__CLASS__);
46         }
47
48         /**
49          * Creates an instance of this filter class
50          *
51          * @return      $filterInstance         An instance of this filter class
52          */
53         public final static function createTestConfigurationLoadableClassesFilter () {
54                 // Get a new instance
55                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('TESTS-CONFIGURATION-LOADABLE-CLASSES-FILTER: CALLED!');
56                 $filterInstance = new TestConfigurationLoadableClassesFilter();
57
58                 // Return the instance
59                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('TESTS-CONFIGURATION-LOADABLE-CLASSES-FILTER: filterInstance=%s - EXIT!', $filterInstance->__toString()));
60                 return $filterInstance;
61         }
62
63         /**
64          * Executes the filter with given request and response objects
65          *
66          * @param       $requestInstance        An instance of a class with an Requestable interface
67          * @param       $responseInstance       An instance of a class with an Responseable interface
68          * @return      void
69          */
70         public function execute (Requestable $requestInstance, Responseable $responseInstance) {
71                 // Init counter
72                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage(sprintf('TESTS-CONFIGURATION-LOADABLE-CLASSES-FILTER: requestInstance=%s,responseInstance=%s - CALLED!', $requestInstance->__toString(), $responseInstance->__toString()));
73                 $passed = $failed = $skipped = $warning = 0;
74
75                 // Loop through all configuration keys
76                 foreach (FrameworkBootstrap::getConfigurationInstance()->getConfigurationArray() as $configKey => $configValue) {
77                         // Key must end with _class
78                         //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('TESTS-CONFIGURATION-LOADABLE-CLASSES-FILTER: configKey[%s]=%s,configValue[%s]=%s', gettype($configKey), $configKey, gettype($configValue), $configValue));
79                         if (substr($configKey, -6, 6) != '_class') {
80                                 // Skip this
81                                 $skipped++;
82                                 continue;
83                         }
84
85                         // This may throw exceptions
86                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('Testing configKey[%s]=%s,configValue[%s]=%s', gettype($configKey), $configKey, gettype($configValue), $configValue));
87                         try {
88                                 // Is the config entry valid and class is there?
89                                 if (!is_string($configValue)) {
90                                         // Skip further tests
91                                         self::createDebugInstance(__CLASS__, __LINE__)->warningMessage(sprintf('configValue=%s has unexpected type "%s", required: string - FAILED!', $configValue, gettype($configValue)));
92                                         $failed++;
93                                         continue;
94                                 } elseif (!class_exists($configValue)) {
95                                         // Skip further tests
96                                         self::createDebugInstance(__CLASS__, __LINE__)->warningMessage(sprintf('Class "%s" not found. FAILED!', $configValue));
97                                         $failed++;
98                                         continue;
99                                 }
100                         } catch (InvalidArgumentException $e) {
101                                 // Maybe not conform?
102                                 self::createDebugInstance(__CLASS__, __LINE__)->warningMessage(sprintf('Class "%s" failed to load. Message: "%s"', $configValue, $e->getMessage()));
103
104                                 // Skip further tests
105                                 $failed++;
106                                 continue;
107                         }
108
109                         // class_exists() didn't fail
110                         /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->debugMessage(sprintf('Class "%s" loaded successfully. OKAY', $configValue));
111                         $passed++;
112                 }
113
114                 // Calculate percentage
115                 $percent = (($passed + $failed) > 0 ? ($passed / ($passed + $failed) * 100) : 0);
116
117                 // Output result
118                 self::createDebugInstance(__CLASS__, __LINE__)->infoMessage(sprintf('Test result: %d okay, %d failed (%0.02f%% passed), %d skipped, %d warning - EXIT!', $passed, $failed, $percent, $skipped, $warning));
119
120                 // Trace message
121                 /* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__, __LINE__)->traceMessage('TESTS-CONFIGURATION-LOADABLE-CLASSES-FILTER: EXIT!');
122         }
123
124 }