Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
1 / 1 |
| FakeSocket | |
100.00% |
27 / 27 |
|
100.00% |
12 / 12 |
17 | |
100.00% |
1 / 1 |
| queueResponse | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| failOnConnect | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| failOnRead | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| failOnSend | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| isConnected | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sentData | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| lastSent | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| create | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| connect | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| close | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| read | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| send | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AqwSocketClient\Sockets; |
| 6 | |
| 7 | use AqwSocketClient\Interfaces\SocketInterface; |
| 8 | use Psl\Str\Byte; |
| 9 | use RuntimeException; |
| 10 | |
| 11 | /** |
| 12 | * Fake in-memory socket for unit tests. |
| 13 | */ |
| 14 | final class FakeSocket implements SocketInterface |
| 15 | { |
| 16 | private bool $connected = false; |
| 17 | |
| 18 | /** @var string */ |
| 19 | private string $buffer = ''; |
| 20 | |
| 21 | /** @var string[] */ |
| 22 | private array $sentData = []; |
| 23 | |
| 24 | private bool $shouldFailOnConnect = false; |
| 25 | private bool $shouldFailOnRead = false; |
| 26 | private bool $shouldFailOnSend = false; |
| 27 | |
| 28 | public function queueResponse(string $message): self |
| 29 | { |
| 30 | $this->buffer .= $message . "\u{0000}"; |
| 31 | |
| 32 | return $this; |
| 33 | } |
| 34 | |
| 35 | public function failOnConnect(): self |
| 36 | { |
| 37 | $this->shouldFailOnConnect = true; |
| 38 | |
| 39 | return $this; |
| 40 | } |
| 41 | |
| 42 | public function failOnRead(): self |
| 43 | { |
| 44 | $this->shouldFailOnRead = true; |
| 45 | |
| 46 | return $this; |
| 47 | } |
| 48 | |
| 49 | public function failOnSend(): self |
| 50 | { |
| 51 | $this->shouldFailOnSend = true; |
| 52 | |
| 53 | return $this; |
| 54 | } |
| 55 | |
| 56 | public function isConnected(): bool |
| 57 | { |
| 58 | return $this->connected; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @return array<string> |
| 63 | */ |
| 64 | public function sentData(): array |
| 65 | { |
| 66 | return $this->sentData; |
| 67 | } |
| 68 | |
| 69 | public function lastSent(): ?string |
| 70 | { |
| 71 | return $this->sentData !== [] ? end($this->sentData) : null; |
| 72 | } |
| 73 | |
| 74 | #[\Override] |
| 75 | public function create(): void {} |
| 76 | |
| 77 | #[\Override] |
| 78 | public function connect(string $hostname, int $port): void |
| 79 | { |
| 80 | if ($this->shouldFailOnConnect) { |
| 81 | throw new RuntimeException('Failed to connect: Connection refused'); |
| 82 | } |
| 83 | |
| 84 | $this->connected = true; |
| 85 | } |
| 86 | |
| 87 | #[\Override] |
| 88 | public function close(): void |
| 89 | { |
| 90 | $this->connected = false; |
| 91 | } |
| 92 | |
| 93 | #[\Override] |
| 94 | public function read(int $length): array |
| 95 | { |
| 96 | if ($this->shouldFailOnRead) { |
| 97 | throw new RuntimeException('Failed to receive data: Socket error'); |
| 98 | } |
| 99 | |
| 100 | if ($this->buffer === '') { |
| 101 | return ['bytes' => 0, 'chunk' => '']; |
| 102 | } |
| 103 | |
| 104 | $chunk = substr($this->buffer, 0, $length); |
| 105 | $this->buffer = substr($this->buffer, $length); |
| 106 | |
| 107 | return ['bytes' => Byte\length($chunk), 'chunk' => $chunk]; |
| 108 | } |
| 109 | |
| 110 | #[\Override] |
| 111 | public function send(string $data): int |
| 112 | { |
| 113 | if ($this->shouldFailOnSend) { |
| 114 | throw new RuntimeException('Failed to send data: Socket error'); |
| 115 | } |
| 116 | |
| 117 | $this->sentData[] = $data; |
| 118 | |
| 119 | return Byte\length($data); |
| 120 | } |
| 121 | } |