Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log in subscribers when broadcasting a subscription update #1306

Merged
merged 16 commits into from
Apr 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 1 addition & 11 deletions tests/Unit/Subscriptions/BroadcastManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,14 @@
use Nuwave\Lighthouse\Subscriptions\BroadcastManager;
use Nuwave\Lighthouse\Subscriptions\Contracts\Broadcaster;
use Nuwave\Lighthouse\Subscriptions\Subscriber;
use Nuwave\Lighthouse\Subscriptions\SubscriptionServiceProvider;
use Tests\TestCase;

class BroadcastManagerTest extends TestCase
class BroadcastManagerTest extends SubscriptionTestCase
{
/**
* @var \Nuwave\Lighthouse\Subscriptions\BroadcastManager
*/
protected $broadcastManager;

protected function getPackageProviders($app)
{
return array_merge(
parent::getPackageProviders($app),
[SubscriptionServiceProvider::class]
);
}

/**
* Set up test environment.
*/
Expand Down
173 changes: 173 additions & 0 deletions tests/Unit/Subscriptions/Iterators/GuardContextSyncIteratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
<?php

namespace Tests\Unit\Subscriptions\Iterators;

use Exception;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Auth\AuthManager;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Mockery\MockInterface;
use Nuwave\Lighthouse\Schema\Context;
use Nuwave\Lighthouse\Subscriptions\Iterators\GuardContextSyncIterator;
use Nuwave\Lighthouse\Subscriptions\Subscriber;
use Nuwave\Lighthouse\Subscriptions\SubscriptionGuard;
use Tests\Unit\Subscriptions\SubscriptionTestCase;

class GuardContextSyncIteratorTest extends SubscriptionTestCase
{
/**
* @var string
*/
public const EXCEPTION_MESSAGE = 'test_exception';

/**
* @var \Nuwave\Lighthouse\Subscriptions\Iterators\GuardContextSyncIterator
*/
protected $iterator;

protected function setUp(): void
{
parent::setUp();

$this->iterator = $this->app->make(GuardContextSyncIterator::class);
}

public function testCanIterateOverItemsWithCallback(): void
{
$items = [];

$this->iterator->process(
$this->subscribers(),
static function ($item) use (&$items): void {
$items[] = $item;
}
);

$this->assertCount(3, $items);
}

public function testCanPassExceptionToHandler(): void
{
/** @var \Exception|null $exception */
$exception = null;

$this->iterator->process(
$this->subscribers(),
static function (): void {
throw new Exception(self::EXCEPTION_MESSAGE);
},
static function (Exception $e) use (&$exception): void {
$exception = $e;
}
);

$this->assertSame(self::EXCEPTION_MESSAGE, $exception->getMessage());
stayallive marked this conversation as resolved.
Show resolved Hide resolved
}

public function testSetsAndResetsGuardContextAfterEachIteration(): void
{
// Give each subscriber a user stub and an ID based on the index of the subscriber in the collection
$subscribers = $this->subscribers()->map(static function (Subscriber $subscriber, int $index) {
$subscriber->context->user = new GuardContextSyncIteratorAuthenticatableStub($index + 1);

return $subscriber;
});

$guard = $this->mock(SubscriptionGuard::class, static function (MockInterface $mock) use ($subscribers) {
$subscribers->each(static function (Subscriber $subscriber) use ($mock) {
$mock->shouldReceive('setUser')->with($subscriber->context->user())->once();
$mock->shouldReceive('user')->andReturn($subscriber->context->user())->once();
$mock->shouldReceive('reset')->once();
});
});

$authManager = $this->app->make(AuthManager::class);

$authManager->extend(SubscriptionGuard::GUARD_NAME, static function () use ($guard) {
return $guard;
});

$processedItems = [];
$authenticatedUser = [];
$guardBeforeIteration = $authManager->guard();

$this->iterator->process(
$subscribers,
static function (Subscriber $subscriber) use (&$processedItems, &$authenticatedUser, $authManager): void {
$processedItems[] = $subscriber;
$authenticatedUser[] = $authManager->user();
}
);

$this->assertCount(3, $processedItems);
$this->assertSame($subscribers->pluck('context.user')->all(), $authenticatedUser);
$this->assertSame($guardBeforeIteration, $authManager->guard());
}

/**
* @return \Illuminate\Support\Collection<\Nuwave\Lighthouse\Subscriptions\Subscriber>
*/
protected function subscribers(): Collection
{
return new Collection([
$this->generateSubscriber(),
$this->generateSubscriber(),
$this->generateSubscriber(),
]);
}

private function generateSubscriber(): Subscriber
{
$resolveInfo = $this->getMockBuilder(ResolveInfo::class)
stayallive marked this conversation as resolved.
Show resolved Hide resolved
->disableOriginalConstructor()
->getMock();

$resolveInfo->operation = (object)[
'name' => (object)[
'value' => 'lighthouse',
],
];

return new Subscriber([], new Context(new Request), $resolveInfo);
}
}

class GuardContextSyncIteratorAuthenticatableStub implements Authenticatable
{
/**
* @var int
*/
private $id;

public function __construct(int $id)
{
$this->id = $id;
}

public function getAuthIdentifierName()
{
}

public function getAuthIdentifier()
{
return $this->id;
}

public function getAuthPassword()
{
}

public function getRememberToken()
{
}

public function setRememberToken($value)
{
}

public function getRememberTokenName()
{
}
}
6 changes: 3 additions & 3 deletions tests/Unit/Subscriptions/Iterators/SyncIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testCanIterateOverItemsWithCallback(): void

$this->iterator->process(
$this->items(),
function ($item) use (&$items): void {
static function ($item) use (&$items): void {
$items[] = $item;
}
);
Expand All @@ -47,10 +47,10 @@ public function testCanPassExceptionToHandler(): void

$this->iterator->process(
spawnia marked this conversation as resolved.
Show resolved Hide resolved
$this->items(),
function (): void {
static function (): void {
throw new Exception(self::EXCEPTION_MESSAGE);
},
function (Exception $e) use (&$exception): void {
static function (Exception $e) use (&$exception): void {
$exception = $e;
}
);
Expand Down
12 changes: 1 addition & 11 deletions tests/Unit/Subscriptions/StorageManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,14 @@
use GraphQL\Utils\AST;
use Nuwave\Lighthouse\Subscriptions\StorageManager;
use Nuwave\Lighthouse\Subscriptions\Subscriber;
use Nuwave\Lighthouse\Subscriptions\SubscriptionServiceProvider;
use Tests\TestCase;

class StorageManagerTest extends TestCase
class StorageManagerTest extends SubscriptionTestCase
{
/**
* @var \Nuwave\Lighthouse\Subscriptions\StorageManager
*/
protected $storage;

protected function getPackageProviders($app)
{
return array_merge(
parent::getPackageProviders($app),
[SubscriptionServiceProvider::class]
);
}

protected function setUp(): void
{
parent::setUp();
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/Subscriptions/SubscriptionTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Tests\Unit\Subscriptions;

use Nuwave\Lighthouse\Subscriptions\SubscriptionServiceProvider;
use Tests\TestCase;

class SubscriptionTestCase extends TestCase
{
protected function getPackageProviders($app)
{
return array_merge(
parent::getPackageProviders($app),
[SubscriptionServiceProvider::class]
);
}
}