Running then() closures as block in tests

This commit is contained in:
Alex Renoki 2020-09-05 22:40:52 +03:00
parent b45d786bfd
commit ca4a9a180e
6 changed files with 93 additions and 25 deletions

View File

@ -41,6 +41,7 @@
"symfony/psr-http-message-bridge": "^1.1|^2.0" "symfony/psr-http-message-bridge": "^1.1|^2.0"
}, },
"require-dev": { "require-dev": {
"clue/block-react": "^1.4",
"mockery/mockery": "^1.3", "mockery/mockery": "^1.3",
"orchestra/testbench-browser-kit": "^4.0|^5.0", "orchestra/testbench-browser-kit": "^4.0|^5.0",
"phpunit/phpunit": "^8.0|^9.0" "phpunit/phpunit": "^8.0|^9.0"

View File

@ -58,9 +58,9 @@ class ConnectionTest extends TestCase
$failedConnection = $this->getConnectedWebSocketConnection(['test-channel']); $failedConnection = $this->getConnectedWebSocketConnection(['test-channel']);
$this->markTestIncomplete( $failedConnection
'The $failedConnection should somehow detect the tap($connection)->send($payload)->close() message.' ->assertSentEvent('pusher:error', ['data' => ['message' => 'Over capacity', 'code' => 4100]])
); ->assertClosed();
} }
/** @test */ /** @test */

View File

@ -63,7 +63,7 @@ class Connection implements ConnectionInterface
* *
* @param string $name * @param string $name
* @param array $additionalParameters * @param array $additionalParameters
* @return void * @return $this
*/ */
public function assertSentEvent(string $name, array $additionalParameters = []) public function assertSentEvent(string $name, array $additionalParameters = [])
{ {
@ -76,13 +76,15 @@ class Connection implements ConnectionInterface
foreach ($additionalParameters as $parameter => $value) { foreach ($additionalParameters as $parameter => $value) {
PHPUnit::assertSame($event[$parameter], $value); PHPUnit::assertSame($event[$parameter], $value);
} }
return $this;
} }
/** /**
* Assert that an event got not sent. * Assert that an event got not sent.
* *
* @param string $name * @param string $name
* @return void * @return $this
*/ */
public function assertNotSentEvent(string $name) public function assertNotSentEvent(string $name)
{ {
@ -91,15 +93,19 @@ class Connection implements ConnectionInterface
PHPUnit::assertTrue( PHPUnit::assertTrue(
is_null($event) is_null($event)
); );
return $this;
} }
/** /**
* Assert the connection is closed. * Assert the connection is closed.
* *
* @return void * @return $this
*/ */
public function assertClosed() public function assertClosed()
{ {
PHPUnit::assertTrue($this->closed); PHPUnit::assertTrue($this->closed);
return $this;
} }
} }

View File

@ -31,6 +31,13 @@ class LazyClient extends BaseLazyClient
*/ */
protected $redis; protected $redis;
/**
* The loop.
*
* @var \React\EventLoop\LoopInterface
*/
protected $loop;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
@ -38,6 +45,7 @@ class LazyClient extends BaseLazyClient
{ {
parent::__construct($target, $factory, $loop); parent::__construct($target, $factory, $loop);
$this->loop = $loop;
$this->redis = Redis::connection(); $this->redis = Redis::connection();
} }
@ -52,7 +60,9 @@ class LazyClient extends BaseLazyClient
$this->redis->__call($name, $args); $this->redis->__call($name, $args);
} }
return parent::__call($name, $args); return new PromiseResolver(
parent::__call($name, $args), $this->loop
);
} }
/** /**

View File

@ -0,0 +1,67 @@
<?php
namespace BeyondCode\LaravelWebSockets\Tests\Mocks;
use Clue\React\Block;
use React\Promise\PromiseInterface;
class PromiseResolver implements PromiseInterface
{
/**
* The promise to resolve.
*
* @var \React\Promise\PromiseInterface
*/
protected $promise;
/**
* The loop.
*
* @var \React\EventLoop\LoopInterface
*/
protected $loop;
/**
* Initialize the promise resolver.
*
* @param PromiseInterface $promise
* @param LoopInterface $loop
* @return void
*/
public function __construct($promise, $loop)
{
$this->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);
}
}

View File

@ -93,16 +93,8 @@ class RedisStatisticsLoggerTest extends TestCase
$logger->save(); $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( $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(); $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( $this->markTestIncomplete(
'The nested callbacks seem to not be working well in tests.' 'The numbers does not seem to match well.'
); );
} }
} }