1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BotMan\BotMan\Drivers\Tests; |
4
|
|
|
|
5
|
|
|
use BotMan\BotMan\Http\Curl; |
6
|
|
|
use BotMan\BotMan\Drivers\NullDriver; |
7
|
|
|
use BotMan\BotMan\Interfaces\DriverInterface; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use BotMan\BotMan\Messages\Incoming\IncomingMessage; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* A driver that acts as a proxy for a global driver instance. Useful for mock/fake drivers in integration tests. |
13
|
|
|
*/ |
14
|
|
|
final class ProxyDriver implements DriverInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DriverInterface |
18
|
|
|
*/ |
19
|
|
|
private static $instance; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Set driver instance to be used. |
23
|
|
|
* |
24
|
|
|
* @param DriverInterface $driver |
25
|
|
|
*/ |
26
|
|
|
public static function setInstance(DriverInterface $driver) |
27
|
|
|
{ |
28
|
|
|
self::$instance = $driver; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return DriverInterface |
33
|
|
|
*/ |
34
|
|
|
private static function instance() |
35
|
|
|
{ |
36
|
|
|
if (self::$instance === null) { |
37
|
|
|
self::$instance = new NullDriver(new Request, [], new Curl); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
return self::$instance; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function matchesRequest() |
44
|
|
|
{ |
45
|
|
|
return self::instance()->matchesRequest(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function hasMatchingEvent() |
49
|
|
|
{ |
50
|
|
|
return self::instance()->hasMatchingEvent(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function getMessages() |
54
|
|
|
{ |
55
|
|
|
return self::instance()->getMessages(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function isBot() |
59
|
|
|
{ |
60
|
|
|
return self::instance()->isBot(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function isConfigured() |
64
|
|
|
{ |
65
|
|
|
return self::instance()->isConfigured(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getUser(IncomingMessage $matchingMessage) |
69
|
|
|
{ |
70
|
|
|
return self::instance()->getUser($matchingMessage); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function getConversationAnswer(IncomingMessage $message) |
74
|
|
|
{ |
75
|
|
|
return self::instance()->getConversationAnswer($message); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function buildServicePayload($message, $matchingMessage, $additionalParameters = []) |
79
|
|
|
{ |
80
|
|
|
return self::instance()->buildServicePayload($message, $matchingMessage, $additionalParameters); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function sendPayload($payload) |
84
|
|
|
{ |
85
|
|
|
return self::instance()->sendPayload($payload); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getName() |
89
|
|
|
{ |
90
|
|
|
return self::instance()->getName(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function types(IncomingMessage $matchingMessage) |
94
|
|
|
{ |
95
|
|
|
return self::instance()->types($matchingMessage); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Tells if the stored conversation callbacks are serialized. |
100
|
|
|
* |
101
|
|
|
* @return bool |
102
|
|
|
*/ |
103
|
|
|
public function serializesCallbacks() |
104
|
|
|
{ |
105
|
|
|
return self::instance()->serializesCallbacks(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Send a typing indicator and wait for the given amount of seconds. |
110
|
|
|
* @param IncomingMessage $matchingMessage |
111
|
|
|
* @param int $seconds |
112
|
|
|
* @return mixed |
113
|
|
|
*/ |
114
|
|
|
public function typesAndWaits(IncomingMessage $matchingMessage, int $seconds) |
115
|
|
|
{ |
116
|
|
|
return self::instance()->typesAndWaits($matchingMessage, $seconds); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: