-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMockingProxy.php
73 lines (65 loc) · 1.54 KB
/
MockingProxy.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
<?php
namespace NoelDeMartin\LaravelDusk;
use Exception;
use Laravel\Dusk\Browser;
use NoelDeMartin\LaravelDusk\Facades\Mocking;
class MockingProxy
{
/**
* Browser where facades are being mocked.
*
* @var Browser
*/
private $browser;
/**
* The facade being mocked.
*
* @var string
*/
private $facade;
/**
* Create a new facade mocking proxy.
*
* @param Browser $browser
* @param string $facade
* @return void
*/
public function __construct(Browser $browser, string $facade)
{
$this->browser = $browser;
$this->facade = $facade;
}
/**
* Dynamically pass method calls to the browser mocking the facade.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
if (! is_null($mock = $this->getFacadeMock())) {
return $mock->{$method}(...$parameters);
} else {
throw new Exception(
'Unable to retrieve mock for ['.$this->facade.'].'
);
}
}
/**
* Get mock instance of the facade.
*
* @return mixed
*/
protected function getFacadeMock()
{
$serializedMock = $this->browser->executeJavascriptRequest(
'POST',
'/_dusk-mocking/serialize',
['facade' => $this->facade]
);
return empty($serializedMock)
? null
: Mocking::unserialize($serializedMock);
}
}