-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDriver.php
222 lines (195 loc) · 4.84 KB
/
Driver.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
namespace NoelDeMartin\LaravelDusk;
use Illuminate\Support\Facades\Event;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use NoelDeMartin\LaravelDusk\Fakes\EventFake;
use Symfony\Component\HttpFoundation\Response;
use NoelDeMartin\LaravelDusk\Fakes\StorageFake;
abstract class Driver
{
/**
* The array of active facade mocks.
*
* @var array
*/
protected $mocks = [];
/**
* The array of fake facade classes.
*
* @var array
*/
protected $fakes = [];
/**
* Storage fake must remain the same instance because it can
* be faked more than once (when testing multiple disks).
*
* @var NoelDeMartin\LaravelDusk\Fakes\StorageFake|null
*/
protected $storageFake = null;
/**
* Start mocking facades.
*
* @return void
*/
public function start()
{
$this->load();
foreach ($this->mocks as $facade => $mock) {
$facade::swap($mock);
switch ($facade) {
case Event::class:
Model::setEventDispatcher($mock);
break;
}
}
}
/**
* Save facades state.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @return void
*/
public function save(Response $response)
{
$this->persist($response);
}
/**
* Replace facade instance with a mock.
*
* @param string $facade
* @param mixed[] ...$arguments
* @return void
*/
public function mock(string $facade, ...$arguments)
{
$mock = $this->createMock($facade, ...$arguments);
$facade::swap($mock);
$this->mocks[$facade] = $mock;
switch ($facade) {
case Event::class:
Model::setEventDispatcher($mock);
break;
}
}
/**
* Register new facade fake.
*
* @param string $facade
* @param string $fake
* @return void
*/
public function registerFake(string $facade, string $fake)
{
$this->fakes[$facade] = $fake;
}
/**
* Determine if a facade is being mocked.
*
* @param string $facade
* @return bool
*/
public function has(string $facade)
{
return isset($this->mocks[$facade]);
}
/**
* Get facade mock.
*
* @param string $facade
* @return mixed
*/
public function get(string $facade)
{
return $this->has($facade) ? $this->mocks[$facade] : null;
}
/**
* Determine if a facade fake is registered.
*
* @param string $facade
* @return bool
*/
public function hasFake(string $facade)
{
return isset($this->fakes[$facade]);
}
/**
* Get registered fake.
*
* @param string $facade
* @return string | null
*/
public function getFake(string $facade)
{
return $this->hasFake($facade) ? $this->fakes[$facade] : null;
}
/**
* Serialize a facade mock.
*
* @param string $facade
* @return string
*/
public function serialize(string $facade)
{
return base64_encode(serialize($this->mocks[$facade]));
}
/**
* Unserialize a facade mock.
*
* @param string $serializedMock
* @return mixed
*/
public function unserialize(string $serializedMock)
{
return unserialize(base64_decode($serializedMock));
}
/**
* Create a facade mock.
*
* @param $facade string
* @param mixed[] ...$arguments
* @return mixed
*/
protected function createMock(string $facade, ...$arguments)
{
if (isset($this->fakes[$facade])) {
return new $this->fakes[$facade](...$arguments);
}
switch ($facade) {
case Storage::class:
$storageFake = $this->getStorageFake();
$storageFake->fake(...$arguments);
return $storageFake;
case Event::class:
return new EventFake($facade::getFacadeRoot(), ...$arguments);
default:
$facade::fake(...$arguments);
return $facade::getFacadeRoot();
}
}
/**
* Lazy-load the storage fake.
*
* @return NoelDeMartin\LaravelDusk\Fakes\StorageFake
*/
protected function getStorageFake()
{
if (is_null($this->storageFake)) {
$this->storageFake = new StorageFake;
}
return $this->storageFake;
}
/**
* Load data from storage.
*
* @return void
*/
abstract protected function load();
/**
* Persists data.
*
* @param \Symfony\Component\HttpFoundation\Response $response
* @return void
*/
abstract protected function persist(Response $response);
}