diff --git a/composer.json b/composer.json index 4ebd5c7..ba66341 100644 --- a/composer.json +++ b/composer.json @@ -35,7 +35,8 @@ "symfony/psr-http-message-bridge": "^1.1" }, "require-dev": { - "larapack/dd": "^1.0", + "mockery/mockery": "^1.2", + "orchestra/testbench": "~3.5", "phpunit/phpunit": "^7.0" }, "autoload": { diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php new file mode 100644 index 0000000..4e5496e --- /dev/null +++ b/tests/ConnectionTest.php @@ -0,0 +1,51 @@ +expectException(UnknownAppKeyException::class); + + /** @var PusherServer $server */ + $server = app(PusherServer::class); + + $server->onOpen($this->getWebSocketConnection('/?appKey=test')); + } + + /** @test */ + public function known_app_keys_can_connect() + { + /** @var PusherServer $server */ + $server = app(PusherServer::class); + + $connection = $this->getWebSocketConnection('/?appKey=TestKey'); + + $server->onOpen($connection); + + $connection->assertSentEvent('pusher:connection_established'); + } + + /** @test */ + public function successful_connections_have_the_client_attached() + { + /** @var PusherServer $server */ + $server = app(PusherServer::class); + + $connection = $this->getWebSocketConnection('/?appKey=TestKey'); + + $server->onOpen($connection); + + $this->assertInstanceOf(Client::class, $connection->client); + $this->assertSame(1234, $connection->client->appId); + $this->assertSame('TestKey', $connection->client->appKey); + $this->assertSame('TestSecret', $connection->client->appSecret); + $this->assertSame('Test Client', $connection->client->name); + } +} \ No newline at end of file diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php deleted file mode 100644 index 91f49d6..0000000 --- a/tests/ExampleTest.php +++ /dev/null @@ -1,14 +0,0 @@ -assertTrue(true); - } -} diff --git a/tests/Mocks/Connection.php b/tests/Mocks/Connection.php new file mode 100644 index 0000000..e1b0f20 --- /dev/null +++ b/tests/Mocks/Connection.php @@ -0,0 +1,32 @@ +sentData[] = json_decode($data, true); + } + + function close() + { + // TODO: Implement close() method. + } + + public function assertSentEvent(string $name) + { + PHPUnit::assertTrue( + ! is_null(collect($this->sentData)->firstWhere('event', '=', $name)) + ); + } +} \ No newline at end of file diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..ccfa15e --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,36 @@ +set('websockets.clients', [ + [ + 'name' => 'Test Client', + 'app_id' => 1234, + 'app_key' => 'TestKey', + 'app_secret' => 'TestSecret' + ] + ]); + } + + protected function getWebSocketConnection(string $url): Connection + { + $connection = new Connection(); + + $connection->httpRequest = new Request('GET', $url); + + return $connection; + } +} \ No newline at end of file