|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Zenstruck\Messenger\Test\Transport; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Messenger\MessageBusInterface; |
|
6
|
|
|
use Symfony\Component\Messenger\Transport\Serialization\SerializerInterface; |
|
7
|
|
|
use Symfony\Component\Messenger\Transport\TransportFactoryInterface; |
|
8
|
|
|
use Symfony\Component\Messenger\Transport\TransportInterface; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @author Kevin Bond <[email protected]> |
|
12
|
|
|
* |
|
13
|
|
|
* @internal |
|
14
|
|
|
*/ |
|
15
|
|
|
final class TestTransportFactory implements TransportFactoryInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private MessageBusInterface $bus; |
|
18
|
|
|
|
|
19
|
|
|
/** @var array<string, self> */ |
|
20
|
|
|
private static array $transports = []; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(MessageBusInterface $bus) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->bus = $bus; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function createTransport(string $dsn, array $options, SerializerInterface $serializer): TransportInterface |
|
28
|
|
|
{ |
|
29
|
|
|
return self::$transports[$options['transport_name']] ??= new TestTransport($this->bus, $serializer, $this->parseDsn($dsn)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function supports(string $dsn, array $options): bool |
|
33
|
|
|
{ |
|
34
|
|
|
return 0 === \mb_strpos($dsn, 'test://'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public static function reset(): void |
|
38
|
|
|
{ |
|
39
|
|
|
self::$transports = []; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
private function parseDsn(string $dsn): array |
|
43
|
|
|
{ |
|
44
|
|
|
$query = []; |
|
45
|
|
|
|
|
46
|
|
|
if ($queryAsString = \mb_strstr($dsn, '?')) { |
|
47
|
|
|
\parse_str(\ltrim($queryAsString, '?'), $query); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return [ |
|
51
|
|
|
'intercept' => \filter_var($query['intercept'] ?? true, \FILTER_VALIDATE_BOOLEAN), |
|
52
|
|
|
'catch_exceptions' => \filter_var($query['catch_exceptions'] ?? true, \FILTER_VALIDATE_BOOLEAN), |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|