From 93730e3904f24022b65be3b80105a51defdb5fe6 Mon Sep 17 00:00:00 2001 From: Nikolay Savchuk Date: Thu, 15 Oct 2020 12:23:10 +0300 Subject: [PATCH] Fixed deprecations and up minimal symfony version from 4.0 to 4.4 --- Command/NodeJsServerCommand.php | 2 + DependencyInjection/SocketIoConfiguration.php | 10 +--- Service/Process.php | 9 ++- Tests/Data/LoadTrait.php | 12 +--- Tests/Service/BroadcastTest.php | 59 +++++++++++-------- Tests/Service/RedisDriverTest.php | 2 +- composer.json | 21 +++++-- phpunit.xml.dist | 11 +++- 8 files changed, 75 insertions(+), 51 deletions(-) diff --git a/Command/NodeJsServerCommand.php b/Command/NodeJsServerCommand.php index fc11c18..552140c 100644 --- a/Command/NodeJsServerCommand.php +++ b/Command/NodeJsServerCommand.php @@ -65,5 +65,7 @@ public function execute(InputInterface $input, OutputInterface $output) $process->setIdleTimeout(false); $process->setTimeout(null); $process->run(); + + return 0; } } diff --git a/DependencyInjection/SocketIoConfiguration.php b/DependencyInjection/SocketIoConfiguration.php index 7165d0c..dc14473 100644 --- a/DependencyInjection/SocketIoConfiguration.php +++ b/DependencyInjection/SocketIoConfiguration.php @@ -5,7 +5,6 @@ use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition; use Symfony\Component\Config\Definition\Builder\TreeBuilder; use Symfony\Component\Config\Definition\ConfigurationInterface; -use Symfony\Component\HttpKernel\Kernel; /** * Class SocketIoConfiguration. @@ -23,13 +22,8 @@ class SocketIoConfiguration implements ConfigurationInterface */ public function getConfigTreeBuilder() { - if (Kernel::VERSION_ID >= 40300) { - $treeBuilder = new TreeBuilder('sfcod_socketio'); - $rootNode = $treeBuilder->getRootNode(); - } else { - $treeBuilder = new TreeBuilder(); - $rootNode = $treeBuilder->root('sfcod_socketio'); - } + $treeBuilder = new TreeBuilder('sfcod_socketio'); + $rootNode = $treeBuilder->getRootNode(); $this->addNamespaces($rootNode); diff --git a/Service/Process.php b/Service/Process.php index fe1fbee..1580a70 100644 --- a/Service/Process.php +++ b/Service/Process.php @@ -85,7 +85,14 @@ private function inWork() */ private function push(string $handle, array $data): \Symfony\Component\Process\Process { - $cmd = sprintf('php %s socket-io:process --handler=%s --data=%s --env=%s', $this->scriptName, escapeshellarg($handle), escapeshellarg(serialize($data)), getenv('APP_ENV')); + $cmd = [ + 'php', + $this->scriptName, + 'socket-io:process', + '--handler=' . escapeshellarg($handle), + '--data=' . escapeshellarg(serialize($data)), + '--env=prod', + ]; $process = new \Symfony\Component\Process\Process($cmd, $this->binPath); $process->setTimeout(10); diff --git a/Tests/Data/LoadTrait.php b/Tests/Data/LoadTrait.php index 09133fe..9e83654 100644 --- a/Tests/Data/LoadTrait.php +++ b/Tests/Data/LoadTrait.php @@ -6,8 +6,6 @@ use Psr\Log\LoggerInterface; use SfCod\SocketIoBundle\DependencyInjection\SocketIoExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\Dotenv\Dotenv; -use Symfony\Component\Dotenv\Exception\PathException; trait LoadTrait { @@ -15,17 +13,11 @@ trait LoadTrait protected function configure() { - $dotenv = new Dotenv(); - try { - $dotenv->load(__DIR__ . '/../../.env'); - } catch (PathException $e) { - // Nothing - } - $extension = new SocketIoExtension(); $container = new ContainerBuilder(); - $container->setParameter('kernel.project_dir', getenv('KERNEL_PROJECT_DIR')); + $container->setParameter('kernel.project_dir', ''); $container->setParameter('kernel.root_dir', realpath(__DIR__ . '/../../../../SfCod/')); + $container->setParameter('kernel.bundles', []); $container->set(LoggerInterface::class, new Logger('test')); $extension->load([ diff --git a/Tests/Service/BroadcastTest.php b/Tests/Service/BroadcastTest.php index f14e6e4..b20082f 100644 --- a/Tests/Service/BroadcastTest.php +++ b/Tests/Service/BroadcastTest.php @@ -1,12 +1,15 @@ configure(); + $redisDriverMock = $this->createMock(RedisDriver::class); + $eventManagerMock = $this->createMock(EventManager::class); + $this->eventManagerMock = $eventManagerMock; + $loggerMock = $this->createMock(LoggerInterface::class); + $processMock = $this->createMock(\SfCod\SocketIoBundle\Service\Process::class); + $this->processMock = $processMock; - $this->broadcast = $this->container->get(Broadcast::class); + $this->broadcast = new Broadcast($redisDriverMock, $eventManagerMock, $loggerMock, $processMock); } /** @@ -44,9 +55,10 @@ protected function setUp() */ public function testOn() { + $this->processMock->method('run')->willReturn(new Process([])); $result = $this->broadcast->on(MarkAsReadSubscriber::name(), []); - $this->assertInstanceOf(Process::class, $result); + self::assertInstanceOf(Process::class, $result); } /** @@ -58,35 +70,34 @@ public function testProcess() $handler = $this->getMockBuilder([EventInterface::class, EventSubscriberInterface::class])->getMock(); $handler - ->expects($this->once()) + ->expects(self::once()) ->method('setPayload') ->with($data); $handler - ->expects($this->once()) + ->expects(self::once()) ->method('handle'); - $this->container->set(sprintf('socketio.%s', get_class($handler)), $handler); + $this->eventManagerMock->method('resolve')->willReturn($handler); $this->broadcast->process(get_class($handler), $data); } -// /** -// */ -// public function testEmit() -// { -// $data = range(1, 10); -// -// $handler = $this->getMockBuilder([EventInterface::class, EventPublisherInterface::class])->getMock(); -// $handler -// ->expects($this->once()) -// ->method('setPayload') -// ->with($data); -// $handler -// ->expects($this->once()) -// ->method('fire'); -// -// $this->container->set(sprintf('socketio.%s', get_class($handler)), $handler); -// $this->broadcast->emit(get_class($handler), $data); -// } + /** + */ + public function testEmit() + { + $data = range(1, 10); + + $handler = $this->getMockBuilder([EventInterface::class, EventPublisherInterface::class])->getMock(); + $handler + ->expects(self::once()) + ->method('setPayload') + ->with($data); + $handler + ->expects(self::once()) + ->method('fire'); + $this->eventManagerMock->method('resolve')->willReturn($handler); + $this->broadcast->emit(get_class($handler), $data); + } // /** // */ diff --git a/Tests/Service/RedisDriverTest.php b/Tests/Service/RedisDriverTest.php index c32bc47..353840f 100644 --- a/Tests/Service/RedisDriverTest.php +++ b/Tests/Service/RedisDriverTest.php @@ -1,6 +1,6 @@ @@ -17,6 +16,16 @@ + + + ./ + + ./Tests + ./vendor + + + +