Skip to content

Commit

Permalink
Throw an exception when Topic implements the wrong EventSubscriberInt…
Browse files Browse the repository at this point in the history
…erface
  • Loading branch information
alcalyn committed Aug 30, 2017
1 parent 1de08e0 commit 2bde779
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/Websocket/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Eole\Sandstone\Websocket;

use Psr\Log\LoggerAwareTrait;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface as WrongEventSubscriberInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Ratchet\ConnectionInterface;
Expand Down Expand Up @@ -120,6 +121,14 @@ private function loadTopic($topicPath)
$this->sandstoneApplication['dispatcher']->addSubscriber($topic);
}

// debug purpose only. Sometimes I use the wrong namespace (JMS one), and it's hard to debug.
if ($topic instanceof WrongEventSubscriberInterface) {
throw new \LogicException(
get_class($topic).' seems to implements the wrong EventSubscriberInterface. '.
'Use the Symfony one, not the JMS one.'
);
}

return $topic;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Eole\Sandstone\Tests\Unit;
namespace Eole\Sandstone\Tests\Unit\Push;

use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Eole\Sandstone\Push\PushServerInterface;
Expand Down
39 changes: 39 additions & 0 deletions tests/Unit/Websocket/ApplicationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Eole\Sandstone\Tests\Unit\Websocket;

use Eole\Sandstone\Serializer\ServiceProvider as SerializerServiceProvider;
use Eole\Sandstone\Websocket\Routing\TopicRouter;
use Eole\Sandstone\Websocket\Application as WebsocketApplication;
use Eole\Sandstone\Application;

class ApplicationTest extends \PHPUnit_Framework_TestCase
{
public function testLoadTopicThrowExceptionWhenImplementingTheWrongEventDispatcherInterface()
{
$app = new Application();
$websocketAppClass = new \ReflectionClass(WebsocketApplication::class);
$method = $websocketAppClass->getMethod('loadTopic');
$method->setAccessible(true);
$websocketApp = $websocketAppClass->newInstance($app);

$app->register(new SerializerServiceProvider());

$app['sandstone.websocket.router'] = function () {
$wrongTopic = new WrongTopic('my-topic');
$topicRouterMock = $this->createMock(TopicRouter::class);

$topicRouterMock
->method('loadTopic')
->willReturn($wrongTopic)
;

return $topicRouterMock;
};

$this->expectException(\LogicException::class);
$this->expectExceptionMessage('WrongTopic seems to implements the wrong EventSubscriberInterface');

$method->invokeArgs($websocketApp, ['my-topic']);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/Websocket/WrongTopic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Eole\Sandstone\Tests\Unit\Websocket;

use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use Eole\Sandstone\Websocket\Topic;

class WrongTopic extends Topic implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [];
}
}

0 comments on commit 2bde779

Please sign in to comment.