Skip to content

Commit 944b0e0

Browse files
committed
Adding tests
1 parent c7724d9 commit 944b0e0

File tree

7 files changed

+95
-46
lines changed

7 files changed

+95
-46
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@
3434
},
3535
"autoload": {
3636
"psr-4": {
37-
"Georgeboot\\LaravelEchoApiGateway\\": "js-src"
37+
"Georgeboot\\LaravelEchoApiGateway\\": "src/"
3838
}
3939
},
4040
"autoload-dev": {
4141
"psr-4": {
42-
"Georgeboot\\LaravelEchoApiGateway\\Tests\\": "tests"
42+
"Tests\\": "tests/"
4343
}
4444
},
4545
"scripts": {

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function __construct(ExceptionHandler $exceptionHandler, SubscriptionRepo
2424
public function handleWebsocket(WebsocketEvent $event, Context $context): HttpResponse
2525
{
2626
try {
27-
$method = Str::camel('handle_' . Str::lower($event->getEventType()));
27+
$method = Str::camel('handle_' . Str::lower($event->getEventType() ?? ''));
2828

2929
if (! method_exists($this, $method)) {
3030
throw new \InvalidArgumentException("Event type {$event->getEventType()} has no handler implemented.");

tests/ExampleTest.php

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/HandlerTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
use Bref\Context\Context;
4+
use Georgeboot\LaravelEchoApiGateway\Handler;
5+
use Georgeboot\LaravelEchoApiGateway\SubscriptionRepository;
6+
use Mockery\Mock;
7+
8+
it('can subscribe to open channels', function () {
9+
$mock = Mockery::mock(SubscriptionRepository::class, function ($mock) {
10+
/** @var Mock $mock */
11+
$mock->shouldReceive('subscribeToChannel')->withArgs(function (string $connectionId, string $channel): bool {
12+
return $connectionId === 'connection-id-1' and $channel === 'test-channel';
13+
})->once();
14+
});
15+
16+
app()->instance(SubscriptionRepository::class, $mock);
17+
18+
/** @var Handler $handler */
19+
$handler = app(Handler::class);
20+
21+
$context = new Context('request-id-1', 50_000, 'function-arn', 'trace-id-1');
22+
23+
$response = $handler->handle([
24+
'requestContext' => [
25+
'routeKey' => 'my-test-route-key',
26+
'eventType' => 'MESSAGE',
27+
'connectionId' => 'connection-id-1',
28+
'domainName' => 'test-domain',
29+
'apiId' => 'api-id-1',
30+
'stage' => 'stage-test',
31+
],
32+
'body' => json_encode(['event' => 'subscribe', 'data' => ['channel' => 'test-channel']]),
33+
], $context);
34+
35+
expect($response['body'])->toBeJson()->toEqual('{"event":"subscription_succeeded","channel":"test-channel","data":[]}');
36+
});
37+
38+
it('can unsubscribe from a channel', function () {
39+
$mock = Mockery::mock(SubscriptionRepository::class, function ($mock) {
40+
/** @var Mock $mock */
41+
$mock->shouldReceive('unsubscribeFromChannel')->withArgs(function (string $connectionId, string $channel): bool {
42+
return $connectionId === 'connection-id-1' and $channel === 'test-channel';
43+
})->once();
44+
});
45+
46+
app()->instance(SubscriptionRepository::class, $mock);
47+
48+
/** @var Handler $handler */
49+
$handler = app(Handler::class);
50+
51+
$context = new Context('request-id-1', 50_000, 'function-arn', 'trace-id-1');
52+
53+
$response = $handler->handle([
54+
'requestContext' => [
55+
'routeKey' => 'my-test-route-key',
56+
'eventType' => 'MESSAGE',
57+
'connectionId' => 'connection-id-1',
58+
'domainName' => 'test-domain',
59+
'apiId' => 'api-id-1',
60+
'stage' => 'stage-test',
61+
],
62+
'body' => json_encode(['event' => 'unsubscribe', 'data' => ['channel' => 'test-channel']]),
63+
], $context);
64+
65+
expect($response['body'])->toBeJson()->toEqual('{"event":"unsubscription_succeeded","channel":"test-channel","data":[]}');
66+
});

tests/Pest.php

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,35 +11,4 @@
1111
|
1212
*/
1313

14-
// uses(Tests\TestCase::class)->in('Feature');
15-
16-
/*
17-
|--------------------------------------------------------------------------
18-
| Expectations
19-
|--------------------------------------------------------------------------
20-
|
21-
| When you're writing tests, you often need to check that values meet certain conditions. The
22-
| "expect()" function gives you access to a set of "expectations" methods that you can use
23-
| to assert different things. Of course, you may extend the Expectation API at any time.
24-
|
25-
*/
26-
27-
expect()->extend('toBeOne', function () {
28-
return $this->toBe(1);
29-
});
30-
31-
/*
32-
|--------------------------------------------------------------------------
33-
| Functions
34-
|--------------------------------------------------------------------------
35-
|
36-
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
37-
| project that you don't want to repeat in every file. Here you can also expose helpers as
38-
| global functions to help you to reduce the number of lines of code in your test files.
39-
|
40-
*/
41-
42-
function something()
43-
{
44-
// ..
45-
}
14+
uses(\Tests\TestCase::class)->in(__DIR__);

tests/TestCase.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use Georgeboot\LaravelEchoApiGateway\ServiceProvider;
6+
use Orchestra\Testbench\TestCase as BaseTestCase;
7+
8+
class TestCase extends BaseTestCase
9+
{
10+
protected function getPackageProviders($app)
11+
{
12+
return [ServiceProvider::class];
13+
}
14+
15+
protected function getEnvironmentSetUp($app): void
16+
{
17+
$app['config']->set('app.key', 'base64:Hupx3yAySikrM2/edkZQNQHslgDWYfiBfCuSThJ5SK8=');
18+
}
19+
}

0 commit comments

Comments
 (0)