1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpab/phpab. (https://github.com/phpab/phpab) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/phpab/phpab for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2015-2016 phpab. (https://github.com/phpab/) |
7
|
|
|
* @license https://raw.githubusercontent.com/phpab/phpab/master/LICENSE.md MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace PhpAb\Test; |
11
|
|
|
|
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
use PhpAb\Exception\DuplicateVariantException; |
14
|
|
|
use PhpAb\Variant\VariantInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The implementation of a Test. |
18
|
|
|
* |
19
|
|
|
* @package PhpAb |
20
|
|
|
*/ |
21
|
|
|
class Test implements TestInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* The identifier of this test. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $identifier; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* The available variantns for this test. |
32
|
|
|
* |
33
|
|
|
* @var VariantInterface[] |
34
|
|
|
*/ |
35
|
|
|
private $variants; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Case specific options |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $options = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Initializes a new instance of this class. |
46
|
|
|
* |
47
|
|
|
* @param string $identifier The identifier |
48
|
|
|
* @param VariantInterface[] $variants The variants that this test has. |
49
|
|
|
* @param array $options Case specific test options. |
50
|
|
|
*/ |
51
|
28 |
|
public function __construct($identifier, $variants = [], array $options = []) |
52
|
|
|
{ |
53
|
28 |
|
if (!is_string($identifier) || $identifier === '') { |
54
|
1 |
|
throw new InvalidArgumentException('The provided identifier is not a valid identifier.'); |
55
|
|
|
} |
56
|
|
|
|
57
|
27 |
|
$this->identifier = $identifier; |
58
|
27 |
|
$this->setVariants($variants); |
59
|
27 |
|
$this->options = $options; |
60
|
27 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
17 |
|
public function getIdentifier() |
66
|
|
|
{ |
67
|
17 |
|
return $this->identifier; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds a variant to this test. |
72
|
|
|
* |
73
|
|
|
* @throws DuplicateVariantException |
74
|
|
|
* |
75
|
|
|
* @param VariantInterface $variant The variant to add to this test. |
76
|
|
|
*/ |
77
|
12 |
|
public function addVariant(VariantInterface $variant) |
78
|
|
|
{ |
79
|
12 |
|
if (array_key_exists($variant->getIdentifier(), $this->variants)) { |
80
|
1 |
|
throw new DuplicateVariantException('A variant with this identifier has already been added.'); |
81
|
|
|
} |
82
|
|
|
|
83
|
12 |
|
$this->variants[$variant->getIdentifier()] = $variant; |
84
|
12 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sets the variants in this test. |
88
|
|
|
* |
89
|
|
|
* @param VariantInterface[] $variants The variants to set. |
90
|
|
|
*/ |
91
|
29 |
|
public function setVariants($variants) |
92
|
|
|
{ |
93
|
29 |
|
$this->variants = []; |
94
|
|
|
|
95
|
29 |
|
foreach ($variants as $variant) { |
96
|
4 |
|
$this->addVariant($variant); |
97
|
29 |
|
} |
98
|
29 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* {@inheritDoc} |
102
|
|
|
*/ |
103
|
7 |
|
public function getVariants() |
104
|
|
|
{ |
105
|
7 |
|
return $this->variants; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritDoc} |
110
|
|
|
* |
111
|
|
|
* @param string $identifier The identifier of the variant to get. |
112
|
|
|
*/ |
113
|
6 |
|
public function getVariant($identifier) |
114
|
|
|
{ |
115
|
6 |
|
if (!array_key_exists($identifier, $this->variants)) { |
116
|
2 |
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
4 |
|
return $this->variants[$identifier]; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get the test options |
124
|
|
|
* |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
4 |
|
public function getOptions() |
128
|
|
|
{ |
129
|
4 |
|
return $this->options; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|