From ca4a9a180e18f333e646246fe8c1913f0f826b2b Mon Sep 17 00:00:00 2001 From: Alex Renoki Date: Sat, 5 Sep 2020 22:40:52 +0300 Subject: [PATCH] Running then() closures as block in tests --- composer.json | 1 + tests/ConnectionTest.php | 6 +- tests/Mocks/Connection.php | 12 +++- tests/Mocks/LazyClient.php | 12 +++- tests/Mocks/PromiseResolver.php | 67 +++++++++++++++++++ .../Logger/RedisStatisticsLoggerTest.php | 20 +----- 6 files changed, 93 insertions(+), 25 deletions(-) create mode 100644 tests/Mocks/PromiseResolver.php diff --git a/composer.json b/composer.json index f34b96d..39e79e6 100644 --- a/composer.json +++ b/composer.json @@ -41,6 +41,7 @@ "symfony/psr-http-message-bridge": "^1.1|^2.0" }, "require-dev": { + "clue/block-react": "^1.4", "mockery/mockery": "^1.3", "orchestra/testbench-browser-kit": "^4.0|^5.0", "phpunit/phpunit": "^8.0|^9.0" diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index c6f44d9..60392d4 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -58,9 +58,9 @@ class ConnectionTest extends TestCase $failedConnection = $this->getConnectedWebSocketConnection(['test-channel']); - $this->markTestIncomplete( - 'The $failedConnection should somehow detect the tap($connection)->send($payload)->close() message.' - ); + $failedConnection + ->assertSentEvent('pusher:error', ['data' => ['message' => 'Over capacity', 'code' => 4100]]) + ->assertClosed(); } /** @test */ diff --git a/tests/Mocks/Connection.php b/tests/Mocks/Connection.php index 904a7a6..f7fb5b4 100644 --- a/tests/Mocks/Connection.php +++ b/tests/Mocks/Connection.php @@ -63,7 +63,7 @@ class Connection implements ConnectionInterface * * @param string $name * @param array $additionalParameters - * @return void + * @return $this */ public function assertSentEvent(string $name, array $additionalParameters = []) { @@ -76,13 +76,15 @@ class Connection implements ConnectionInterface foreach ($additionalParameters as $parameter => $value) { PHPUnit::assertSame($event[$parameter], $value); } + + return $this; } /** * Assert that an event got not sent. * * @param string $name - * @return void + * @return $this */ public function assertNotSentEvent(string $name) { @@ -91,15 +93,19 @@ class Connection implements ConnectionInterface PHPUnit::assertTrue( is_null($event) ); + + return $this; } /** * Assert the connection is closed. * - * @return void + * @return $this */ public function assertClosed() { PHPUnit::assertTrue($this->closed); + + return $this; } } diff --git a/tests/Mocks/LazyClient.php b/tests/Mocks/LazyClient.php index be1df88..0382a6f 100644 --- a/tests/Mocks/LazyClient.php +++ b/tests/Mocks/LazyClient.php @@ -31,6 +31,13 @@ class LazyClient extends BaseLazyClient */ protected $redis; + /** + * The loop. + * + * @var \React\EventLoop\LoopInterface + */ + protected $loop; + /** * {@inheritdoc} */ @@ -38,6 +45,7 @@ class LazyClient extends BaseLazyClient { parent::__construct($target, $factory, $loop); + $this->loop = $loop; $this->redis = Redis::connection(); } @@ -52,7 +60,9 @@ class LazyClient extends BaseLazyClient $this->redis->__call($name, $args); } - return parent::__call($name, $args); + return new PromiseResolver( + parent::__call($name, $args), $this->loop + ); } /** diff --git a/tests/Mocks/PromiseResolver.php b/tests/Mocks/PromiseResolver.php new file mode 100644 index 0000000..e5d9aac --- /dev/null +++ b/tests/Mocks/PromiseResolver.php @@ -0,0 +1,67 @@ +promise = $promise; + $this->loop = $loop; + } + + /** + * Intercept the promise then() and run it in sync. + * + * @param callable|null $onFulfilled + * @param callable|null $onRejected + * @param callable|null $onProgress + * @return PromiseInterface + */ + public function then(callable $onFulfilled = null, callable $onRejected = null, callable $onProgress = null) + { + $result = Block\await( + $this->promise, $this->loop + ); + + $onFulfilled($result); + + return $this->promise; + } + + /** + * Pass the calls to the promise. + * + * @param string $method + * @param array $args + * @return mixed + */ + public function __call($method, $args) + { + return call_user_func([$this->promise, $method], $args); + } +} diff --git a/tests/Statistics/Logger/RedisStatisticsLoggerTest.php b/tests/Statistics/Logger/RedisStatisticsLoggerTest.php index 0741a9c..3232740 100644 --- a/tests/Statistics/Logger/RedisStatisticsLoggerTest.php +++ b/tests/Statistics/Logger/RedisStatisticsLoggerTest.php @@ -93,16 +93,8 @@ class RedisStatisticsLoggerTest extends TestCase $logger->save(); - /* $this->assertCount(1, WebSocketsStatisticsEntry::all()); - - $entry = WebSocketsStatisticsEntry::first(); - - $this->assertEquals(1, $entry->peak_connection_count); - $this->assertEquals(1, $entry->websocket_message_count); - $this->assertEquals(1, $entry->api_message_count); */ - $this->markTestIncomplete( - 'The nested callbacks seem to not be working well in tests.' + 'The numbers does not seem to match well.' ); } @@ -127,16 +119,8 @@ class RedisStatisticsLoggerTest extends TestCase $logger->save(); - /* $this->assertCount(1, WebSocketsStatisticsEntry::all()); - - $entry = WebSocketsStatisticsEntry::first(); - - $this->assertEquals(1, $entry->peak_connection_count); - $this->assertEquals(1, $entry->websocket_message_count); - $this->assertEquals(1, $entry->api_message_count); */ - $this->markTestIncomplete( - 'The nested callbacks seem to not be working well in tests.' + 'The numbers does not seem to match well.' ); } }