forked from fuzionnz/codeception-drupal-variable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDrupalVariable.php
More file actions
125 lines (112 loc) · 3.17 KB
/
Copy pathDrupalVariable.php
File metadata and controls
125 lines (112 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Codeception\Module;
use Codeception\Module\Drupal\Variable\VariableStorage\StorageInterface;
use Codeception\TestCase;
class DrupalVariable extends \Codeception\Module
{
/**
* @var array
* List of required config fields.
*/
protected $requiredFields = array('class');
/**
* @var StorageInterface
*/
protected $variableStorage;
/**
* Create the variable storage class to read/write drupal variables.
*/
public function _initialize()
{
$class = $this->config['class'];
// TODO: could I inject this in somehow?
$this->variableStorage = new $class($this->config);
$this->requiredFields = array_merge(
$this->requiredFields,
$this->variableStorage->getRequiredFields()
);
$this->validateConfig();
}
/**
* Ensure the class specified is valid.
*
* @see \Codeception\Module::validateConfig()
* @throws \Codeception\Exception\ModuleConfig
*/
protected function validateConfig()
{
parent::validateConfig();
$class = $this->config['class'];
if (!class_exists($class)) {
throw new \Codeception\Exception\ModuleConfig(
"DrupalVariable",
"Invalid config. Class '$class' does not exist"
);
}
$interface = "Codeception\\Module\\Drupal\\Variable\\VariableStorage\\StorageInterface";
if (!in_array($interface, class_implements($class))) {
throw new \Codeception\Exception\ModuleConfig(
"DrupalVariable",
"Invalid config. Class '$class' must implement '$interface'"
);
}
}
/**
* Set a variable.
*
* @param string $name
* The variable name.
* @param mixed $value
* The variable value, not serialized.
*/
public function haveVariable($name, $value)
{
$this->variableStorage->writeVariable($name, $value);
}
/**
* Delete a variable.
*
* @param string $name
* The variable to delete.
*/
public function dontHaveVariable($name)
{
$this->variableStorage->deleteVariable($name);
}
/**
* See variable matches expected value.
*
* @param string $name
* The variable name.
* @param mixed $expectedValue
* The expected value.
*/
public function seeVariable($name, $expectedValue)
{
$this->assertEquals($expectedValue, $this->variableStorage->readVariable($name));
}
/**
* See variable does not match a value.
*
* @param string $name
* The variable name.
* @param $expectedValue
* The expectedValue that should not be matched.
*/
public function dontSeeVariable($name, $expectedValue)
{
$this->assertNotEquals($expectedValue, $this->variableStorage->readVariable($name));
}
/**
* Get the variable value.
*
* @param string $name
* The variable name.
* @return mixed
* The variable value, not serialized.
*/
public function getVariable($name)
{
return $this->variableStorage->readVariable($name);
}
}