Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
| TestRunnerFeature | |
0.00% |
0 / 34 |
|
0.00% |
0 / 9 |
702 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
210 | |||
| checkPluginsIds | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| getId | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getPluginsIds | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| isEnabledByDefault | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isActive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setActive | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAllPlugins | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __toPhpCode | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2016 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoTests\models\runner\features; |
| 23 | |
| 24 | use oat\taoTests\models\runner\plugins\TestPlugin; |
| 25 | use Psr\Log\LoggerAwareInterface; |
| 26 | use oat\oatbox\log\LoggerAwareTrait; |
| 27 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 28 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 29 | |
| 30 | /** |
| 31 | * A test runner feature is a user feature that can be expressed by one or more test runner plugins. |
| 32 | * They can be toggled at the delivery level and work only in the new test runner. |
| 33 | * |
| 34 | * @author Christophe Noël <christophe@taotesting.com> |
| 35 | */ |
| 36 | |
| 37 | abstract class TestRunnerFeature implements |
| 38 | TestRunnerFeatureInterface, |
| 39 | LoggerAwareInterface, |
| 40 | ServiceLocatorAwareInterface |
| 41 | { |
| 42 | use LoggerAwareTrait; |
| 43 | use ServiceLocatorAwareTrait; |
| 44 | |
| 45 | /** |
| 46 | * @var string |
| 47 | */ |
| 48 | protected $id; |
| 49 | |
| 50 | /** |
| 51 | * @var string[] must match active test runner plugins Ids |
| 52 | */ |
| 53 | protected $pluginsIds; |
| 54 | |
| 55 | /** |
| 56 | * @var bool Determine if the feature will be automatically enabled upon delivery creation |
| 57 | */ |
| 58 | protected $isEnabledByDefault; |
| 59 | |
| 60 | /** |
| 61 | * @var TestPlugin[] Used to check the existence of plugins Ids |
| 62 | */ |
| 63 | protected $allPlugins; |
| 64 | |
| 65 | /** |
| 66 | * @var boolean |
| 67 | */ |
| 68 | protected $active; |
| 69 | |
| 70 | /** |
| 71 | * @param string $id |
| 72 | * @param string[] $pluginsIds |
| 73 | * @param bool $isEnabledByDefault |
| 74 | * @param TestPlugin[] $allPlugins |
| 75 | * @throws \common_exception_InconsistentData |
| 76 | */ |
| 77 | public function __construct( |
| 78 | $id, |
| 79 | $pluginsIds, |
| 80 | $isEnabledByDefault, |
| 81 | $allPlugins, |
| 82 | $active = true |
| 83 | ) { |
| 84 | if (! is_string($id) || empty($id)) { |
| 85 | throw new \common_exception_InconsistentData('id should be a valid string'); |
| 86 | } |
| 87 | |
| 88 | if (! is_array($pluginsIds) || empty($pluginsIds) || ! is_string($pluginsIds[0])) { |
| 89 | throw new \common_exception_InconsistentData('pluginsIds should be a array of strings'); |
| 90 | } |
| 91 | |
| 92 | if (! is_bool($isEnabledByDefault)) { |
| 93 | throw new \common_exception_InconsistentData('isEnabledByDefault should be a boolean'); |
| 94 | } |
| 95 | |
| 96 | if (! is_array($allPlugins) || empty($allPlugins) || ! current($allPlugins) instanceof TestPlugin) { |
| 97 | throw new \common_exception_InconsistentData('allPlugins should be an array of TestPlugin'); |
| 98 | } |
| 99 | |
| 100 | $this->id = $id; |
| 101 | $this->pluginsIds = $pluginsIds; |
| 102 | $this->isEnabledByDefault = $isEnabledByDefault; |
| 103 | $this->allPlugins = $allPlugins; |
| 104 | $this->active = $active; |
| 105 | |
| 106 | // also check that abstract methods have been implemented correctly |
| 107 | if (! is_string($this->getLabel()) || empty($this->getLabel())) { |
| 108 | throw new \common_exception_InconsistentData('The test runner feature needs a label'); |
| 109 | } |
| 110 | |
| 111 | if (! is_string($this->getDescription()) || empty($this->getDescription())) { |
| 112 | throw new \common_exception_InconsistentData('The test runner feature needs a description'); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Check that the content of $pluginsIds matches existing and active plugin Ids |
| 118 | * @throws \common_exception_InconsistentData |
| 119 | */ |
| 120 | private function checkPluginsIds() |
| 121 | { |
| 122 | $allPluginIds = []; |
| 123 | $inactivePluginsIds = []; |
| 124 | |
| 125 | foreach ($this->getAllPlugins() as $plugin) { |
| 126 | $allPluginIds[] = $plugin->getId(); |
| 127 | if ($plugin->isActive() === false) { |
| 128 | $inactivePluginsIds[] = $plugin->getId(); |
| 129 | } |
| 130 | } |
| 131 | foreach ($this->pluginsIds as $id) { |
| 132 | if (! in_array($id, $allPluginIds)) { |
| 133 | $this->logWarning('Invalid plugin Id ' . $id . ' for test runner feature ' . $this->id); |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * @return string |
| 140 | */ |
| 141 | public function getId() |
| 142 | { |
| 143 | return $this->id; |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * @return string[] |
| 148 | * @throws \common_exception_InconsistentData |
| 149 | */ |
| 150 | public function getPluginsIds() |
| 151 | { |
| 152 | $this->checkPluginsIds(); |
| 153 | return $this->pluginsIds; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return bool |
| 158 | */ |
| 159 | public function isEnabledByDefault() |
| 160 | { |
| 161 | return $this->isEnabledByDefault; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * Is feature activated |
| 166 | * @return boolean |
| 167 | */ |
| 168 | public function isActive() |
| 169 | { |
| 170 | return $this->active; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * @param boolean $active |
| 175 | */ |
| 176 | public function setActive($active) |
| 177 | { |
| 178 | $this->active = $active; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @return TestPlugin[] |
| 183 | */ |
| 184 | protected function getAllPlugins() |
| 185 | { |
| 186 | return $this->allPlugins; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * (non-PHPdoc) |
| 191 | * @see \oat\oatbox\PhpSerializable::__toPhpCode() |
| 192 | */ |
| 193 | public function __toPhpCode() |
| 194 | { |
| 195 | return 'new ' . get_class($this) . '()'; |
| 196 | } |
| 197 | } |