Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit ca4a9a1

Browse files
committed
Running then() closures as block in tests
1 parent b45d786 commit ca4a9a1

File tree

6 files changed

+93
-25
lines changed

6 files changed

+93
-25
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
"symfony/psr-http-message-bridge": "^1.1|^2.0"
4242
},
4343
"require-dev": {
44+
"clue/block-react": "^1.4",
4445
"mockery/mockery": "^1.3",
4546
"orchestra/testbench-browser-kit": "^4.0|^5.0",
4647
"phpunit/phpunit": "^8.0|^9.0"

tests/ConnectionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public function app_can_not_exceed_maximum_capacity_on_redis_replication()
5858

5959
$failedConnection = $this->getConnectedWebSocketConnection(['test-channel']);
6060

61-
$this->markTestIncomplete(
62-
'The $failedConnection should somehow detect the tap($connection)->send($payload)->close() message.'
63-
);
61+
$failedConnection
62+
->assertSentEvent('pusher:error', ['data' => ['message' => 'Over capacity', 'code' => 4100]])
63+
->assertClosed();
6464
}
6565

6666
/** @test */

tests/Mocks/Connection.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function close()
6363
*
6464
* @param string $name
6565
* @param array $additionalParameters
66-
* @return void
66+
* @return $this
6767
*/
6868
public function assertSentEvent(string $name, array $additionalParameters = [])
6969
{
@@ -76,13 +76,15 @@ public function assertSentEvent(string $name, array $additionalParameters = [])
7676
foreach ($additionalParameters as $parameter => $value) {
7777
PHPUnit::assertSame($event[$parameter], $value);
7878
}
79+
80+
return $this;
7981
}
8082

8183
/**
8284
* Assert that an event got not sent.
8385
*
8486
* @param string $name
85-
* @return void
87+
* @return $this
8688
*/
8789
public function assertNotSentEvent(string $name)
8890
{
@@ -91,15 +93,19 @@ public function assertNotSentEvent(string $name)
9193
PHPUnit::assertTrue(
9294
is_null($event)
9395
);
96+
97+
return $this;
9498
}
9599

96100
/**
97101
* Assert the connection is closed.
98102
*
99-
* @return void
103+
* @return $this
100104
*/
101105
public function assertClosed()
102106
{
103107
PHPUnit::assertTrue($this->closed);
108+
109+
return $this;
104110
}
105111
}

tests/Mocks/LazyClient.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@ class LazyClient extends BaseLazyClient
3131
*/
3232
protected $redis;
3333

34+
/**
35+
* The loop.
36+
*
37+
* @var \React\EventLoop\LoopInterface
38+
*/
39+
protected $loop;
40+
3441
/**
3542
* {@inheritdoc}
3643
*/
3744
public function __construct($target, Factory $factory, LoopInterface $loop)
3845
{
3946
parent::__construct($target, $factory, $loop);
4047

48+
$this->loop = $loop;
4149
$this->redis = Redis::connection();
4250
}
4351

@@ -52,7 +60,9 @@ public function __call($name, $args)
5260
$this->redis->__call($name, $args);
5361
}
5462

55-
return parent::__call($name, $args);
63+
return new PromiseResolver(
64+
parent::__call($name, $args), $this->loop
65+
);
5666
}
5767

5868
/**

tests/Mocks/PromiseResolver.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace BeyondCode\LaravelWebSockets\Tests\Mocks;
4+
5+
use Clue\React\Block;
6+
use React\Promise\PromiseInterface;
7+
8+
class PromiseResolver implements PromiseInterface
9+
{
10+
/**
11+
* The promise to resolve.
12+
*
13+
* @var \React\Promise\PromiseInterface
14+
*/
15+
protected $promise;
16+
17+
/**
18+
* The loop.
19+
*
20+
* @var \React\EventLoop\LoopInterface
21+
*/
22+
protected $loop;
23+
24+
/**
25+
* Initialize the promise resolver.
26+
*
27+
* @param PromiseInterface $promise
28+
* @param LoopInterface $loop
29+
* @return void
30+
*/
31+
public function __construct($promise, $loop)
32+
{
33+
$this->promise = $promise;
34+
$this->loop = $loop;
35+
}
36+
37+
/**
38+
* Intercept the promise then() and run it in sync.
39+
*
40+
* @param callable|null $onFulfilled
41+
* @param callable|null $onRejected
42+
* @param callable|null $onProgress
43+
* @return PromiseInterface
44+
*/
45+
public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null)
46+
{
47+
$result = Block\await(
48+
$this->promise, $this->loop
49+
);
50+
51+
$onFulfilled($result);
52+
53+
return $this->promise;
54+
}
55+
56+
/**
57+
* Pass the calls to the promise.
58+
*
59+
* @param string $method
60+
* @param array $args
61+
* @return mixed
62+
*/
63+
public function __call($method, $args)
64+
{
65+
return call_user_func([$this->promise, $method], $args);
66+
}
67+
}

tests/Statistics/Logger/RedisStatisticsLoggerTest.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,8 @@ public function it_counts_connections_with_redis_logger_with_no_data()
9393

9494
$logger->save();
9595

96-
/* $this->assertCount(1, WebSocketsStatisticsEntry::all());
97-
98-
$entry = WebSocketsStatisticsEntry::first();
99-
100-
$this->assertEquals(1, $entry->peak_connection_count);
101-
$this->assertEquals(1, $entry->websocket_message_count);
102-
$this->assertEquals(1, $entry->api_message_count); */
103-
10496
$this->markTestIncomplete(
105-
'The nested callbacks seem to not be working well in tests.'
97+
'The numbers does not seem to match well.'
10698
);
10799
}
108100

@@ -127,16 +119,8 @@ public function it_counts_connections_with_redis_logger_with_existing_data()
127119

128120
$logger->save();
129121

130-
/* $this->assertCount(1, WebSocketsStatisticsEntry::all());
131-
132-
$entry = WebSocketsStatisticsEntry::first();
133-
134-
$this->assertEquals(1, $entry->peak_connection_count);
135-
$this->assertEquals(1, $entry->websocket_message_count);
136-
$this->assertEquals(1, $entry->api_message_count); */
137-
138122
$this->markTestIncomplete(
139-
'The nested callbacks seem to not be working well in tests.'
123+
'The numbers does not seem to match well.'
140124
);
141125
}
142126
}

0 commit comments

Comments
 (0)