Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
georgeboot committed Jan 26, 2021
1 parent c7724d9 commit 944b0e0
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 46 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
},
"autoload": {
"psr-4": {
"Georgeboot\\LaravelEchoApiGateway\\": "js-src"
"Georgeboot\\LaravelEchoApiGateway\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Georgeboot\\LaravelEchoApiGateway\\Tests\\": "tests"
"Tests\\": "tests/"
}
},
"scripts": {
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __construct(ExceptionHandler $exceptionHandler, SubscriptionRepo
public function handleWebsocket(WebsocketEvent $event, Context $context): HttpResponse
{
try {
$method = Str::camel('handle_' . Str::lower($event->getEventType()));
$method = Str::camel('handle_' . Str::lower($event->getEventType() ?? ''));

if (! method_exists($this, $method)) {
throw new \InvalidArgumentException("Event type {$event->getEventType()} has no handler implemented.");
Expand Down
5 changes: 0 additions & 5 deletions tests/ExampleTest.php

This file was deleted.

66 changes: 66 additions & 0 deletions tests/HandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

use Bref\Context\Context;
use Georgeboot\LaravelEchoApiGateway\Handler;
use Georgeboot\LaravelEchoApiGateway\SubscriptionRepository;
use Mockery\Mock;

it('can subscribe to open channels', function () {
$mock = Mockery::mock(SubscriptionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('subscribeToChannel')->withArgs(function (string $connectionId, string $channel): bool {
return $connectionId === 'connection-id-1' and $channel === 'test-channel';
})->once();
});

app()->instance(SubscriptionRepository::class, $mock);

/** @var Handler $handler */
$handler = app(Handler::class);

$context = new Context('request-id-1', 50_000, 'function-arn', 'trace-id-1');

$response = $handler->handle([
'requestContext' => [
'routeKey' => 'my-test-route-key',
'eventType' => 'MESSAGE',
'connectionId' => 'connection-id-1',
'domainName' => 'test-domain',
'apiId' => 'api-id-1',
'stage' => 'stage-test',
],
'body' => json_encode(['event' => 'subscribe', 'data' => ['channel' => 'test-channel']]),
], $context);

expect($response['body'])->toBeJson()->toEqual('{"event":"subscription_succeeded","channel":"test-channel","data":[]}');
});

it('can unsubscribe from a channel', function () {
$mock = Mockery::mock(SubscriptionRepository::class, function ($mock) {
/** @var Mock $mock */
$mock->shouldReceive('unsubscribeFromChannel')->withArgs(function (string $connectionId, string $channel): bool {
return $connectionId === 'connection-id-1' and $channel === 'test-channel';
})->once();
});

app()->instance(SubscriptionRepository::class, $mock);

/** @var Handler $handler */
$handler = app(Handler::class);

$context = new Context('request-id-1', 50_000, 'function-arn', 'trace-id-1');

$response = $handler->handle([
'requestContext' => [
'routeKey' => 'my-test-route-key',
'eventType' => 'MESSAGE',
'connectionId' => 'connection-id-1',
'domainName' => 'test-domain',
'apiId' => 'api-id-1',
'stage' => 'stage-test',
],
'body' => json_encode(['event' => 'unsubscribe', 'data' => ['channel' => 'test-channel']]),
], $context);

expect($response['body'])->toBeJson()->toEqual('{"event":"unsubscription_succeeded","channel":"test-channel","data":[]}');
});
33 changes: 1 addition & 32 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,4 @@
|
*/

// uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
| Expectations
|--------------------------------------------------------------------------
|
| When you're writing tests, you often need to check that values meet certain conditions. The
| "expect()" function gives you access to a set of "expectations" methods that you can use
| to assert different things. Of course, you may extend the Expectation API at any time.
|
*/

expect()->extend('toBeOne', function () {
return $this->toBe(1);
});

/*
|--------------------------------------------------------------------------
| Functions
|--------------------------------------------------------------------------
|
| While Pest is very powerful out-of-the-box, you may have some testing code specific to your
| project that you don't want to repeat in every file. Here you can also expose helpers as
| global functions to help you to reduce the number of lines of code in your test files.
|
*/

function something()
{
// ..
}
uses(\Tests\TestCase::class)->in(__DIR__);
19 changes: 19 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Tests;

use Georgeboot\LaravelEchoApiGateway\ServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
protected function getPackageProviders($app)
{
return [ServiceProvider::class];
}

protected function getEnvironmentSetUp($app): void
{
$app['config']->set('app.key', 'base64:Hupx3yAySikrM2/edkZQNQHslgDWYfiBfCuSThJ5SK8=');
}
}

0 comments on commit 944b0e0

Please sign in to comment.