-
Notifications
You must be signed in to change notification settings - Fork 611
/
MockElementQuery.php
155 lines (137 loc) · 3.76 KB
/
MockElementQuery.php
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\test\mockclasses\elements;
use Craft;
use craft\base\Element;
use craft\base\ElementInterface;
use craft\elements\db\ElementQuery;
use craft\helpers\StringHelper;
/**
* MockElementQuery is used to mimic element queries and mock their results
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.5
*/
class MockElementQuery extends ElementQuery
{
public const CLASS_TEMPLATE_FILE = '../../templates/mockElementQuery.tpl';
/**
* The "elements" to return when invoking `one()` or `all()`
*
* @var array
*/
protected array $returnValues = [];
/**
* The element query properties
*
* @var array
*/
protected array $properties = [];
/**
* MockElementQuery constructor.
*/
public function __construct()
{
parent::__construct(ExampleElement::class, []);
}
/**
* Generate a more specific query class for the provided element type class.
*
* @param string $elementClass
* @phpstan-param class-string<ElementInterface> $elementClass
* @return ElementQuery
*/
public static function generateSpecificQueryClass(string $elementClass): ElementQuery
{
$parts = explode('\\', $elementClass);
// Split out the relevant parts and generate a prefix
$element = array_pop($parts);
$namespace = implode('\\', $parts);
$prefix = StringHelper::randomStringWithChars('abcdefghijklmnopqrstuvwxyz', 20);
$className = $prefix . 'MockElementQuery';
// Load template and fill it with the relevant values
$template = file_get_contents(__DIR__ . '/' . self::CLASS_TEMPLATE_FILE);
$classData = str_replace(['{element}', '{namespace}', '{className}'], [$element, $namespace, $className], $template);
// Include the class and return an instance of it
$tempPath = Craft::$app->getPath()->getTempPath() . DIRECTORY_SEPARATOR . $prefix . '.php';
file_put_contents($tempPath, $classData);
include($tempPath);
$instance = new $className();
unlink($tempPath);
return $instance;
}
/**
* Set the return values.
*
* @param array $values
* @return static
*/
public function setReturnValues(array $values = []): static
{
$this->returnValues = $values;
return $this;
}
/**
* Setter for mock query arguments.
*
* @param string $name
* @param mixed $value
*/
public function __set($name, $value)
{
$this->properties[$name] = $value;
}
/**
* Check if a property has been set already.
*
* @param string $name
* @return bool
*/
public function __isset($name): bool
{
return array_key_exists($name, $this->properties);
}
/**
* Getter for mock query arguments.
*
* @param string $name
* @return mixed
*/
public function __get($name)
{
return $this->properties[$name] ?? null;
}
/**
* Mock setting query arguments via a method call.
*
* @param string $name
* @param array $params
* @return static
*/
public function __call($name, $params): static
{
$this->properties[$name] = reset($params);
return $this;
}
/**
* Return all the return values.
*
* @param mixed $db
* @return array
*/
public function all($db = null): array
{
return $this->returnValues;
}
/**
* @inheritdoc
*/
public function one($db = null): mixed
{
return !empty($this->returnValues) ? reset($this->returnValues) : null;
}
}