*/ abstract class TestRunnerFeature implements TestRunnerFeatureInterface, LoggerAwareInterface, ServiceLocatorAwareInterface { use LoggerAwareTrait; use ServiceLocatorAwareTrait; /** * @var string */ protected $id; /** * @var string[] must match active test runner plugins Ids */ protected $pluginsIds; /** * @var bool Determine if the feature will be automatically enabled upon delivery creation */ protected $isEnabledByDefault; /** * @var TestPlugin[] Used to check the existence of plugins Ids */ protected $allPlugins; /** * @var boolean */ protected $active; /** * @param string $id * @param string[] $pluginsIds * @param bool $isEnabledByDefault * @param TestPlugin[] $allPlugins * @throws \common_exception_InconsistentData */ public function __construct( $id, $pluginsIds, $isEnabledByDefault, $allPlugins, $active = true ) { if (! is_string($id) || empty($id)) { throw new \common_exception_InconsistentData('id should be a valid string'); } if (! is_array($pluginsIds) || empty($pluginsIds) || ! is_string($pluginsIds[0])) { throw new \common_exception_InconsistentData('pluginsIds should be a array of strings'); } if (! is_bool($isEnabledByDefault)) { throw new \common_exception_InconsistentData('isEnabledByDefault should be a boolean'); } if (! is_array($allPlugins) || empty($allPlugins) || ! current($allPlugins) instanceof TestPlugin) { throw new \common_exception_InconsistentData('allPlugins should be an array of TestPlugin'); } $this->id = $id; $this->pluginsIds = $pluginsIds; $this->isEnabledByDefault = $isEnabledByDefault; $this->allPlugins = $allPlugins; $this->active = $active; // also check that abstract methods have been implemented correctly if (! is_string($this->getLabel()) || empty($this->getLabel())) { throw new \common_exception_InconsistentData('The test runner feature needs a label'); } if (! is_string($this->getDescription()) || empty($this->getDescription())) { throw new \common_exception_InconsistentData('The test runner feature needs a description'); } } /** * Check that the content of $pluginsIds matches existing and active plugin Ids * @throws \common_exception_InconsistentData */ private function checkPluginsIds() { $allPluginIds = []; $inactivePluginsIds = []; foreach ($this->getAllPlugins() as $plugin) { $allPluginIds[] = $plugin->getId(); if ($plugin->isActive() === false) { $inactivePluginsIds[] = $plugin->getId(); } } foreach ($this->pluginsIds as $id) { if (! in_array($id, $allPluginIds)) { $this->logWarning('Invalid plugin Id ' . $id . ' for test runner feature ' . $this->id); } } } /** * @return string */ public function getId() { return $this->id; } /** * @return string[] * @throws \common_exception_InconsistentData */ public function getPluginsIds() { $this->checkPluginsIds(); return $this->pluginsIds; } /** * @return bool */ public function isEnabledByDefault() { return $this->isEnabledByDefault; } /** * Is feature activated * @return boolean */ public function isActive() { return $this->active; } /** * @param boolean $active */ public function setActive($active) { $this->active = $active; } /** * @return TestPlugin[] */ protected function getAllPlugins() { return $this->allPlugins; } /** * (non-PHPdoc) * @see \oat\oatbox\PhpSerializable::__toPhpCode() */ public function __toPhpCode() { return 'new ' . get_class($this) . '()'; } }