Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TestRunnerFeatureService
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 3
132
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 unregister
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 getAll
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
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 (under the project TAO-PRODUCT);
19 *
20 */
21
22namespace oat\taoTests\models\runner\features;
23
24use oat\oatbox\log\LoggerAwareTrait;
25use oat\oatbox\service\ConfigurableService;
26use Psr\Log\LoggerAwareInterface;
27
28/**
29 * A service to register Test Runner Features
30 *
31 * @author Christophe Noël <christophe@taotesting.com>
32 */
33class TestRunnerFeatureService extends ConfigurableService implements LoggerAwareInterface
34{
35    use LoggerAwareTrait;
36
37    public const SERVICE_ID = 'taoTests/testRunnerFeature';
38
39    public const OPTION_AVAILABLE = 'available';
40
41    /**
42     * Register a feature
43     *
44     * @param TestRunnerFeature $testRunnerFeature
45     * @return string Id of the registered feature
46     * @throws \common_exception_InconsistentData
47     */
48    public function register(TestRunnerFeature $testRunnerFeature)
49    {
50        $registeredFeatures = $this->getOption(self::OPTION_AVAILABLE);
51        if ($registeredFeatures == null) {
52            $registeredFeatures = [];
53        }
54
55        $featureId = $testRunnerFeature->getId();
56
57        if (array_key_exists($featureId, $registeredFeatures)) {
58            throw new \common_exception_InconsistentData('Cannot register two features with the same id ' . $featureId);
59        }
60
61        $registeredFeatures[$featureId] = $testRunnerFeature;
62        $this->setOption(self::OPTION_AVAILABLE, $registeredFeatures);
63
64        return $featureId;
65    }
66
67    /**
68     * Unregister a feature
69     *
70     * @param string $featureId
71     * @throws \common_exception_InconsistentData
72     */
73    public function unregister($featureId)
74    {
75        $registeredFeatures = $this->getOption(self::OPTION_AVAILABLE);
76        if (is_array($registeredFeatures) && array_key_exists($featureId, $registeredFeatures)) {
77            unset($registeredFeatures[$featureId]);
78            $this->setOption(self::OPTION_AVAILABLE, $registeredFeatures);
79        } else {
80            $this->logWarning('Cannot unregister inexistant feature ' . $featureId);
81        }
82    }
83
84    /**
85     * Return all available features
86     * @param boolean $filterDisabled
87     * @return TestRunnerFeature[]
88     */
89    public function getAll($filterDisabled = true)
90    {
91        $result = [];
92        /** @var TestRunnerFeatureInterface[] $features */
93        $features = $this->getOption(self::OPTION_AVAILABLE);
94        if (!$features) {
95            $features = [];
96        }
97        foreach ($features as $id => $feature) {
98            $feature->setServiceLocator($this->getServiceLocator());
99            $this->propagate($feature);
100            if ($filterDisabled) {
101                if ($feature->isActive()) {
102                    $result[$id] = $feature;
103                }
104            } else {
105                $result[$id] = $feature;
106            }
107        }
108        return $result;
109    }
110}