From 04116ba2acff5d06a4797b6c7fe9192b67d1023d Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Wed, 12 Feb 2020 00:53:07 +0100 Subject: [PATCH 1/3] Fix PHPStorm findings in tests --- tests/Backend/AMQPBackendDispatcherTest.php | 49 ++++++++------- tests/Backend/AMQPBackendTest.php | 17 +++-- tests/Backend/BackendHealthCheckTest.php | 2 +- .../MessageManagerBackendDispatcherTest.php | 2 +- tests/Backend/MessageManagerBackendTest.php | 12 ++-- tests/Backend/PostponeRuntimeBackendTest.php | 14 ++--- tests/Backend/RuntimeBackendTest.php | 8 +-- tests/Consumer/LoggerConsumerTest.php | 6 +- tests/Consumer/SwiftMailerConsumerTest.php | 2 +- .../Controller/Api/MessageControllerTest.php | 10 ++- .../SonataNotificationExtensionTest.php | 22 +++---- tests/Entity/Message.php | 4 +- tests/Entity/MessageManagerTest.php | 63 ++++++++----------- tests/Entity/MessageTest.php | 11 ++-- tests/Event/DoctrineOptimizeListenerTest.php | 4 +- .../InvalidParameterExceptionTest.php | 2 +- tests/Iterator/AMQPMessageIteratorTest.php | 11 ++-- .../IteratorProxyMessageIteratorTest.php | 4 +- .../MessageManagerMessageIterator.php | 7 +-- .../MessageManagerMessageIteratorTest.php | 6 +- 20 files changed, 118 insertions(+), 138 deletions(-) diff --git a/tests/Backend/AMQPBackendDispatcherTest.php b/tests/Backend/AMQPBackendDispatcherTest.php index 2dedbfda..957f0cee 100644 --- a/tests/Backend/AMQPBackendDispatcherTest.php +++ b/tests/Backend/AMQPBackendDispatcherTest.php @@ -15,6 +15,7 @@ use Enqueue\AmqpLib\AmqpConnectionFactory; use Interop\Amqp\AmqpContext; +use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Sonata\NotificationBundle\Backend\AMQPBackend; use Sonata\NotificationBundle\Backend\AMQPBackendDispatcher; @@ -33,7 +34,7 @@ protected function setUp(): void AmqpConnectionFactoryStub::$context = null; } - public function testThrowIfSettingsMissFactoryClassOptionOnGetContext() + public function testThrowIfSettingsMissFactoryClassOptionOnGetContext(): void { $dispatcher = new AMQPBackendDispatcher([], [], 'default', []); @@ -42,7 +43,7 @@ public function testThrowIfSettingsMissFactoryClassOptionOnGetContext() $dispatcher->getContext(); } - public function testThrowIfFactoryClassIsNotRealClass() + public function testThrowIfFactoryClassIsNotRealClass(): void { $dispatcher = new AMQPBackendDispatcher(['factory_class' => 'anInvalidClass'], [], 'default', []); @@ -51,7 +52,7 @@ public function testThrowIfFactoryClassIsNotRealClass() $dispatcher->getContext(); } - public function testThrowIfFactoryClassIsNotInstanceOfAmqpConnectionFactoryInterface() + public function testThrowIfFactoryClassIsNotInstanceOfAmqpConnectionFactoryInterface(): void { $dispatcher = new AMQPBackendDispatcher(['factory_class' => \stdClass::class], [], 'default', []); @@ -60,7 +61,7 @@ public function testThrowIfFactoryClassIsNotInstanceOfAmqpConnectionFactoryInter $dispatcher->getContext(); } - public function testShouldPassExpectedOptionsToAmqpConnectionFactoryConstructor() + public function testShouldPassExpectedOptionsToAmqpConnectionFactoryConstructor(): void { $dispatcher = new AMQPBackendDispatcher( [ @@ -87,7 +88,7 @@ public function testShouldPassExpectedOptionsToAmqpConnectionFactoryConstructor( ], AmqpConnectionFactoryStub::$config); } - public function testShouldReturnExpectedAmqpContext() + public function testShouldReturnExpectedAmqpContext(): void { $expectedContext = $this->createMock(AmqpContext::class); @@ -112,7 +113,7 @@ public function testShouldReturnExpectedAmqpContext() $this->assertSame($expectedContext, $actualContext); } - public function testQueue() + public function testQueue(): void { $mock = $this->getMockQueue('foo', 'message.type.foo', $this->once()); $mock2 = $this->getMockQueue('bar', 'message.type.foo', $this->never()); @@ -123,7 +124,7 @@ public function testQueue() $dispatcher->createAndPublish('message.type.foo', []); } - public function testDefaultQueue() + public function testDefaultQueue(): void { $mock = $this->getMockQueue('foo', 'message.type.foo', $this->once()); $fooBackend = ['type' => 'default', 'backend' => $mock]; @@ -131,7 +132,7 @@ public function testDefaultQueue() $dispatcher->createAndPublish('some.other.type', []); } - public function testDefaultQueueNotFound() + public function testDefaultQueueNotFound(): void { $mock = $this->getMockQueue('foo', 'message.type.foo', $this->never()); $fooBackend = ['type' => 'message.type.foo', 'backend' => $mock]; @@ -141,7 +142,7 @@ public function testDefaultQueueNotFound() $dispatcher->createAndPublish('some.other.type', []); } - public function testInvalidQueue() + public function testInvalidQueue(): void { $mock = $this->getMockQueue('foo', 'message.type.bar'); $dispatcher = $this->getDispatcher( @@ -153,7 +154,7 @@ public function testInvalidQueue() $dispatcher->createAndPublish('message.type.bar', []); } - public function testAllQueueInitializeOnce() + public function testAllQueueInitializeOnce(): void { $queues = [ ['queue' => 'foo', 'routing_key' => 'message.type.foo'], @@ -176,7 +177,20 @@ public function testAllQueueInitializeOnce() $dispatcher->createAndPublish('message.type.foo', []); } - protected function getMockQueue($queue, $type, $called = null) + protected function getDispatcher(array $backends, array $queues = [['queue' => 'foo', 'routing_key' => 'message.type.foo']]): AMQPBackendDispatcher + { + $settings = [ + 'host' => 'foo', + 'port' => 'port', + 'user' => 'user', + 'pass' => 'pass', + 'vhost' => '/', + ]; + + return new AMQPBackendDispatcher($settings, $queues, 'default', $backends); + } + + private function getMockQueue($queue, $type, $called = null): MockObject { $methods = ['createAndPublish', 'initialize']; $args = ['', 'foo', false, 'message.type.foo']; @@ -193,17 +207,4 @@ protected function getMockQueue($queue, $type, $called = null) return $mock; } - - protected function getDispatcher(array $backends, array $queues = [['queue' => 'foo', 'routing_key' => 'message.type.foo']]) - { - $settings = [ - 'host' => 'foo', - 'port' => 'port', - 'user' => 'user', - 'pass' => 'pass', - 'vhost' => '/', - ]; - - return new AMQPBackendDispatcher($settings, $queues, 'default', $backends); - } } diff --git a/tests/Backend/AMQPBackendTest.php b/tests/Backend/AMQPBackendTest.php index b997b179..a80427a3 100644 --- a/tests/Backend/AMQPBackendTest.php +++ b/tests/Backend/AMQPBackendTest.php @@ -49,7 +49,7 @@ protected function setUp(): void AmqpConnectionFactoryStub::$config = null; } - public function testInitializeWithNoDeadLetterExchangeAndNoDeadLetterRoutingKey() + public function testInitializeWithNoDeadLetterExchangeAndNoDeadLetterRoutingKey(): void { $backend = $this->buildBackend(); @@ -98,7 +98,7 @@ public function testInitializeWithNoDeadLetterExchangeAndNoDeadLetterRoutingKey( $backend->initialize(); } - public function testInitializeWithDeadLetterExchangeAndNoDeadLetterRoutingKey() + public function testInitializeWithDeadLetterExchangeAndNoDeadLetterRoutingKey(): void { $backend = $this->buildBackend(false, self::DEAD_LETTER_EXCHANGE); @@ -152,7 +152,7 @@ public function testInitializeWithDeadLetterExchangeAndNoDeadLetterRoutingKey() $backend->initialize(); } - public function testInitializeWithDeadLetterExchangeAndDeadLetterRoutingKey() + public function testInitializeWithDeadLetterExchangeAndDeadLetterRoutingKey(): void { $backend = $this->buildBackend(false, self::DEAD_LETTER_EXCHANGE, self::DEAD_LETTER_ROUTING_KEY); @@ -198,7 +198,7 @@ public function testInitializeWithDeadLetterExchangeAndDeadLetterRoutingKey() $backend->initialize(); } - public function testInitializeWithTTL() + public function testInitializeWithTTL(): void { $backend = $this->buildBackend(false, null, null, self::TTL); @@ -237,7 +237,7 @@ public function testInitializeWithTTL() $backend->initialize(); } - public function testGetIteratorWithNoPrefetchCount() + public function testGetIteratorWithNoPrefetchCount(): void { $backend = $this->buildBackend(); @@ -272,7 +272,7 @@ public function testGetIteratorWithNoPrefetchCount() $this->assertInstanceOf(AMQPMessageIterator::class, $iterator); } - public function testGetIteratorWithPrefetchCount() + public function testGetIteratorWithPrefetchCount(): void { $backend = $this->buildBackend(false, null, null, null, self::PREFETCH_COUNT); @@ -307,10 +307,7 @@ public function testGetIteratorWithPrefetchCount() $this->assertInstanceOf(AMQPMessageIterator::class, $iterator); } - /** - * @return AMQPBackend - */ - protected function buildBackend($recover = false, $deadLetterExchange = null, $deadLetterRoutingKey = null, $ttl = null, $prefetchCount = null) + protected function buildBackend($recover = false, $deadLetterExchange = null, $deadLetterRoutingKey = null, $ttl = null, $prefetchCount = null): AMQPBackend { $backend = new AMQPBackend( self::EXCHANGE, diff --git a/tests/Backend/BackendHealthCheckTest.php b/tests/Backend/BackendHealthCheckTest.php index 39eb1da9..f2df668c 100644 --- a/tests/Backend/BackendHealthCheckTest.php +++ b/tests/Backend/BackendHealthCheckTest.php @@ -27,7 +27,7 @@ protected function setUp(): void } } - public function testCheck() + public function testCheck(): void { $result = new Success('Test check', 'OK'); diff --git a/tests/Backend/MessageManagerBackendDispatcherTest.php b/tests/Backend/MessageManagerBackendDispatcherTest.php index f0ba1693..8345bd4d 100644 --- a/tests/Backend/MessageManagerBackendDispatcherTest.php +++ b/tests/Backend/MessageManagerBackendDispatcherTest.php @@ -24,7 +24,7 @@ */ class MessageManagerBackendDispatcherTest extends TestCase { - public function testCreate() + public function testCreate(): void { $testBackend = $this->createMock(MessageManagerBackend::class); diff --git a/tests/Backend/MessageManagerBackendTest.php b/tests/Backend/MessageManagerBackendTest.php index 4b41c776..cf4a1a45 100644 --- a/tests/Backend/MessageManagerBackendTest.php +++ b/tests/Backend/MessageManagerBackendTest.php @@ -26,7 +26,7 @@ class MessageManagerBackendTest extends TestCase { - public function testCreateAndPublish() + public function testCreateAndPublish(): void { $message = new Message(); $modelManager = $this->createMock(MessageManagerInterface::class); @@ -43,7 +43,7 @@ public function testCreateAndPublish() $this->assertSame(['message' => 'salut'], $message->getBody()); } - public function testHandleSuccess() + public function testHandleSuccess(): void { $message = new Message(); $modelManager = $this->createMock(MessageManagerInterface::class); @@ -61,7 +61,7 @@ public function testHandleSuccess() $this->assertNotNull($message->getCompletedAt()); } - public function testHandleError() + public function testHandleError(): void { $message = new Message(); $modelManager = $this->createMock(MessageManagerInterface::class); @@ -88,14 +88,14 @@ public function testHandleError() /** * @dataProvider statusProvider */ - public function testStatus($counts, $expectedStatus, $message) + public function testStatus($counts, $expectedStatus, $message): void { if (!class_exists(Success::class)) { $this->markTestSkipped('The class ZendDiagnostics\Result\Success does not exist'); } $modelManager = $this->createMock(MessageManagerInterface::class); - $modelManager->expects($this->exactly(1))->method('countStates')->willReturn($counts); + $modelManager->expects($this->once())->method('countStates')->willReturn($counts); $backend = new MessageManagerBackend($modelManager, [ MessageInterface::STATE_IN_PROGRESS => 10, @@ -110,7 +110,7 @@ public function testStatus($counts, $expectedStatus, $message) $this->assertSame($message, $status->getMessage()); } - public static function statusProvider() + public static function statusProvider(): array { if (!class_exists(Success::class)) { return [[1, 1, 1]]; diff --git a/tests/Backend/PostponeRuntimeBackendTest.php b/tests/Backend/PostponeRuntimeBackendTest.php index b1cce756..176e5017 100644 --- a/tests/Backend/PostponeRuntimeBackendTest.php +++ b/tests/Backend/PostponeRuntimeBackendTest.php @@ -26,7 +26,7 @@ */ class PostponeRuntimeBackendTest extends TestCase { - public function testIteratorContainsPublishedMessages() + public function testIteratorContainsPublishedMessages(): void { $backend = new PostponeRuntimeBackend( $this->createMock(EventDispatcherInterface::class), @@ -55,7 +55,7 @@ public function testIteratorContainsPublishedMessages() } } - public function testNoMessagesOnEvent() + public function testNoMessagesOnEvent(): void { $backend = $this->getMockBuilder(PostponeRuntimeBackend::class) ->setMethods(['handle']) @@ -70,7 +70,7 @@ public function testNoMessagesOnEvent() $backend->onEvent(); } - public function testLiveEnvironment() + public function testLiveEnvironment(): void { $dispatcher = new EventDispatcher(); $backend = new PostponeRuntimeBackend($dispatcher, true); @@ -96,7 +96,7 @@ public function testLiveEnvironment() $this->assertSame(MessageInterface::STATE_DONE, $message->getState()); } - public function testRecursiveMessage() + public function testRecursiveMessage(): void { $dispatcher = new EventDispatcher(); $backend = new PostponeRuntimeBackend($dispatcher, true); @@ -111,7 +111,7 @@ public function testRecursiveMessage() $phpunit->passed1 = false; $phpunit->passed2 = false; - $dispatcher->addListener('notification.demo1', static function (ConsumerEventInterface $event) use ($phpunit, $message1, $message2, $backend, $dispatcher) { + $dispatcher->addListener('notification.demo1', static function (ConsumerEventInterface $event) use ($phpunit, $message1, $message2, $backend) { $phpunit->assertSame($message1, $event->getMessage()); $phpunit->passed1 = true; @@ -134,7 +134,7 @@ public function testRecursiveMessage() $this->assertSame(MessageInterface::STATE_DONE, $message2->getState()); } - public function testStatusIsOk() + public function testStatusIsOk(): void { if (!class_exists(Success::class)) { $this->markTestSkipped('The class ZendDiagnostics\Result\Success does not exist'); @@ -149,7 +149,7 @@ public function testStatusIsOk() $this->assertInstanceOf(Success::class, $status); } - public function testOnCliPublishHandlesDirectly() + public function testOnCliPublishHandlesDirectly(): void { $backend = $this->getMockBuilder(PostponeRuntimeBackend::class) ->setMethods(['handle']) diff --git a/tests/Backend/RuntimeBackendTest.php b/tests/Backend/RuntimeBackendTest.php index e3447430..c252ee61 100644 --- a/tests/Backend/RuntimeBackendTest.php +++ b/tests/Backend/RuntimeBackendTest.php @@ -22,7 +22,7 @@ class RuntimeBackendTest extends TestCase { - public function testCreateAndPublish() + public function testCreateAndPublish(): void { $dispatcher = $this->createMock(EventDispatcherInterface::class); $backend = new RuntimeBackend($dispatcher); @@ -36,7 +36,7 @@ public function testCreateAndPublish() $this->assertSame(['message' => 'salut'], $message->getBody()); } - public function testIterator() + public function testIterator(): void { $dispatcher = $this->createMock(EventDispatcherInterface::class); $backend = new RuntimeBackend($dispatcher); @@ -44,7 +44,7 @@ public function testIterator() $this->assertInstanceOf('Iterator', $backend->getIterator()); } - public function testHandleSuccess() + public function testHandleSuccess(): void { $message = new Message(); @@ -59,7 +59,7 @@ public function testHandleSuccess() $this->assertNotNull($message->getCompletedAt()); } - public function testHandleError() + public function testHandleError(): void { $message = new Message(); diff --git a/tests/Consumer/LoggerConsumerTest.php b/tests/Consumer/LoggerConsumerTest.php index 880dc215..29d5b7f2 100644 --- a/tests/Consumer/LoggerConsumerTest.php +++ b/tests/Consumer/LoggerConsumerTest.php @@ -28,7 +28,7 @@ class LoggerConsumerTest extends TestCase * @param $type * @param $calledType */ - public function testProcess($type, $calledType) + public function testProcess($type, $calledType): void { $logger = $this->createMock(LoggerInterface::class); $logger->expects($this->once())->method($calledType); @@ -48,7 +48,7 @@ public function testProcess($type, $calledType) /** * @return array[] */ - public function calledTypeProvider() + public function calledTypeProvider(): array { return [ ['emerg', 'emergency'], @@ -62,7 +62,7 @@ public function calledTypeProvider() ]; } - public function testInvalidType() + public function testInvalidType(): void { $this->expectException(InvalidParameterException::class); diff --git a/tests/Consumer/SwiftMailerConsumerTest.php b/tests/Consumer/SwiftMailerConsumerTest.php index 008d01ec..e0b53084 100644 --- a/tests/Consumer/SwiftMailerConsumerTest.php +++ b/tests/Consumer/SwiftMailerConsumerTest.php @@ -44,7 +44,7 @@ protected function setUp(): void /** * Tests the sendEmail method. */ - public function testSendEmail() + public function testSendEmail(): void { $message = new Message(); $message->setBody([ diff --git a/tests/Controller/Api/MessageControllerTest.php b/tests/Controller/Api/MessageControllerTest.php index 95d0cf29..36ab139f 100644 --- a/tests/Controller/Api/MessageControllerTest.php +++ b/tests/Controller/Api/MessageControllerTest.php @@ -29,7 +29,7 @@ */ class MessageControllerTest extends TestCase { - public function testGetMessagesAction() + public function testGetMessagesAction(): void { $messageManager = $this->createMock(MessageManagerInterface::class); $messageManager->expects($this->once())->method('getPager')->willReturn([]); @@ -41,7 +41,7 @@ public function testGetMessagesAction() $this->assertSame([], $this->createMessageController(null, $messageManager)->getMessagesAction($paramFetcher)); } - public function testPostMessageAction() + public function testPostMessageAction(): void { $message = $this->createMock(MessageInterface::class); @@ -61,7 +61,7 @@ public function testPostMessageAction() $this->assertInstanceOf(MessageInterface::class, $message); } - public function testPostMessageInvalidAction() + public function testPostMessageInvalidAction(): void { $message = $this->createMock(MessageInterface::class); @@ -84,10 +84,8 @@ public function testPostMessageInvalidAction() * @param $message * @param $messageManager * @param $formFactory - * - * @return MessageController */ - public function createMessageController($message = null, $messageManager = null, $formFactory = null) + public function createMessageController($message = null, $messageManager = null, $formFactory = null): MessageController { if (null === $messageManager) { $messageManager = $this->createMock(SiteManagerInterface::class); diff --git a/tests/DependencyInjection/SonataNotificationExtensionTest.php b/tests/DependencyInjection/SonataNotificationExtensionTest.php index 2da4b81d..d67a4691 100644 --- a/tests/DependencyInjection/SonataNotificationExtensionTest.php +++ b/tests/DependencyInjection/SonataNotificationExtensionTest.php @@ -36,7 +36,7 @@ protected function tearDown(): void unset($this->container); } - public function testEmptyConfig() + public function testEmptyConfig(): void { $container = $this->getContainerBuilder([ 'MonologBundle' => MonologBundle::class, @@ -53,7 +53,7 @@ public function testEmptyConfig() $container->compile(); } - public function testDoNotRegisterDefaultConsumers() + public function testDoNotRegisterDefaultConsumers(): void { $container = $this->getContainerBuilder(); $extension = new SonataNotificationExtension(); @@ -77,7 +77,7 @@ public function testDoNotRegisterDefaultConsumers() $container->compile(); } - public function testDoctrineBackendNoConfig() + public function testDoctrineBackendNoConfig(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Please configure the sonata_notification.backends.doctrine section'); @@ -94,7 +94,7 @@ public function testDoctrineBackendNoConfig() ], $container); } - public function testDoctrineBackend() + public function testDoctrineBackend(): void { $container = $this->getContainerBuilder([ 'DoctrineBundle' => DoctrineBundle::class, @@ -125,7 +125,7 @@ public function testDoctrineBackend() $container->compile(); } - public function testRabbitMQBackendNoConfig() + public function testRabbitMQBackendNoConfig(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Please configure the sonata_notification.backends.rabbitmq section'); @@ -140,7 +140,7 @@ public function testRabbitMQBackendNoConfig() ], $container); } - public function testRabbitMQBackend() + public function testRabbitMQBackend(): void { $container = $this->getContainerBuilder(); $extension = new SonataNotificationExtension(); @@ -168,7 +168,7 @@ public function testRabbitMQBackend() $container->compile(); } - private function getContainerBuilder(array $bundles = []) + private function getContainerBuilder(array $bundles = []): ContainerBuilder { $container = new ContainerBuilder(); @@ -198,28 +198,28 @@ private function getContainerBuilder(array $bundles = []) return $this->container = $container; } - private function assertAlias($alias, $service) + private function assertAlias($alias, $service): void { $this->assertSame( $alias, (string) $this->container->getAlias($service), sprintf('%s alias is correct', $service) ); } - private function assertParameter($expectedValue, $name) + private function assertParameter($expectedValue, $name): void { $this->assertSame( $expectedValue, $this->container->getParameter($name), sprintf('%s parameter is correct', $name) ); } - private function assertHasDefinition($definition) + private function assertHasDefinition($definition): void { $this->assertTrue( $this->container->hasDefinition($definition) ? true : $this->container->hasAlias($definition) ); } - private function assertHasNoDefinition($service) + private function assertHasNoDefinition($service): void { $this->assertFalse( $this->container->hasDefinition($service) ? true : $this->container->hasAlias($service) diff --git a/tests/Entity/Message.php b/tests/Entity/Message.php index d72261e0..ee23f61e 100644 --- a/tests/Entity/Message.php +++ b/tests/Entity/Message.php @@ -19,7 +19,7 @@ class Message extends BaseMessage { - public function setId($id) + public function setId($id): void { $this->id = $id; } @@ -27,7 +27,7 @@ public function setId($id) class BaseMessageTest extends TestCase { - public function testClone() + public function testClone(): void { $originalMessage = new Message(); $originalMessage->setId(42); diff --git a/tests/Entity/MessageManagerTest.php b/tests/Entity/MessageManagerTest.php index bdf3312e..a2d83a35 100644 --- a/tests/Entity/MessageManagerTest.php +++ b/tests/Entity/MessageManagerTest.php @@ -26,7 +26,7 @@ class MessageManagerTest extends TestCase { - public function testCancel() + public function testCancel(): void { $manager = $this->getMessageManagerMock(); @@ -37,7 +37,7 @@ public function testCancel() $this->assertTrue($message->isCancelled()); } - public function testRestart() + public function testRestart(): void { $manager = $this->getMessageManagerMock(); @@ -55,12 +55,12 @@ public function testRestart() $this->assertSame(13, $newMessage->getRestartCount()); } - public function testGetPager() + public function testGetPager(): void { $self = $this; $this ->getMessageManager(static function ($qb) use ($self) { - $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); + $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']); $qb->expects($self->never())->method('andWhere'); $qb->expects($self->once())->method('setParameters')->with([]); $qb->expects($self->once())->method('orderBy')->with( @@ -71,24 +71,23 @@ public function testGetPager() ->getPager([], 1); } - public function testGetPagerWithInvalidSort() + public function testGetPagerWithInvalidSort(): void { $this->expectException(\RuntimeException::class); $this->expectExceptionMessage('Invalid sort field \'invalid\' in \'Sonata\\NotificationBundle\\Entity\\BaseMessage\' class'); - $self = $this; $this - ->getMessageManager(static function ($qb) use ($self) { + ->getMessageManager(static function ($qb) { }) ->getPager([], 1, 10, ['invalid' => 'ASC']); } - public function testGetPagerWithMultipleSort() + public function testGetPagerWithMultipleSort(): void { $self = $this; $this ->getMessageManager(static function ($qb) use ($self) { - $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); + $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']); $qb->expects($self->never())->method('andWhere'); $qb->expects($self->once())->method('setParameters')->with([]); $qb->expects($self->exactly(2))->method('orderBy')->with( @@ -109,12 +108,12 @@ public function testGetPagerWithMultipleSort() ]); } - public function testGetPagerWithOpenedMessages() + public function testGetPagerWithOpenedMessages(): void { $self = $this; $this ->getMessageManager(static function ($qb) use ($self) { - $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); + $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']); $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state')); $qb->expects($self->once())->method('setParameters')->with($self->equalTo([ 'state' => MessageInterface::STATE_OPEN, @@ -123,12 +122,12 @@ public function testGetPagerWithOpenedMessages() ->getPager(['state' => MessageInterface::STATE_OPEN], 1); } - public function testGetPagerWithCanceledMessages() + public function testGetPagerWithCanceledMessages(): void { $self = $this; $this ->getMessageManager(static function ($qb) use ($self) { - $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); + $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']); $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state')); $qb->expects($self->once())->method('setParameters')->with($self->equalTo([ 'state' => MessageInterface::STATE_CANCELLED, @@ -137,12 +136,12 @@ public function testGetPagerWithCanceledMessages() ->getPager(['state' => MessageInterface::STATE_CANCELLED], 1); } - public function testGetPagerWithInProgressMessages() + public function testGetPagerWithInProgressMessages(): void { $self = $this; $this ->getMessageManager(static function ($qb) use ($self) { - $qb->expects($self->once())->method('getRootAliases')->will($self->returnValue(['m'])); + $qb->expects($self->once())->method('getRootAliases')->willReturn(['m']); $qb->expects($self->once())->method('andWhere')->with($self->equalTo('m.state = :state')); $qb->expects($self->once())->method('setParameters')->with($self->equalTo([ 'state' => MessageInterface::STATE_IN_PROGRESS, @@ -151,22 +150,14 @@ public function testGetPagerWithInProgressMessages() ->getPager(['state' => MessageInterface::STATE_IN_PROGRESS], 1); } - /** - * @return MessageManagerMock - */ - protected function getMessageManagerMock() + protected function getMessageManagerMock(): MessageManagerMock { $registry = $this->createMock(ManagerRegistry::class); - $manager = new MessageManagerMock(Message::class, $registry); - - return $manager; + return new MessageManagerMock(Message::class, $registry); } - /** - * @return MessageManager - */ - protected function getMessageManager($qbCallback) + protected function getMessageManager($qbCallback): MessageManager { $query = $this->getMockForAbstractClass( AbstractQuery::class, @@ -177,42 +168,40 @@ protected function getMessageManager($qbCallback) true, ['execute'] ); - $query->expects($this->any())->method('execute')->willReturn(true); + $query->method('execute')->willReturn(true); $qb = $this->getMockBuilder(QueryBuilder::class) ->setConstructorArgs([$this->createMock(EntityManager::class)]) ->getMock(); - $qb->expects($this->any())->method('select')->willReturn($qb); - $qb->expects($this->any())->method('getQuery')->willReturn($query); + $qb->method('select')->willReturn($qb); + $qb->method('getQuery')->willReturn($query); $qbCallback($qb); $repository = $this->createMock(EntityRepository::class); - $repository->expects($this->any())->method('createQueryBuilder')->willReturn($qb); + $repository->method('createQueryBuilder')->willReturn($qb); $metadata = $this->createMock(ClassMetadata::class); - $metadata->expects($this->any())->method('getFieldNames')->willReturn([ + $metadata->method('getFieldNames')->willReturn([ 'state', 'type', ]); $em = $this->createMock(EntityManager::class); - $em->expects($this->any())->method('getRepository')->willReturn($repository); - $em->expects($this->any())->method('getClassMetadata')->willReturn($metadata); + $em->method('getRepository')->willReturn($repository); + $em->method('getClassMetadata')->willReturn($metadata); $registry = $this->createMock(ManagerRegistry::class); - $registry->expects($this->any())->method('getManagerForClass')->willReturn($em); + $registry->method('getManagerForClass')->willReturn($em); return new MessageManager(BaseMessage::class, $registry); } /** * @param int $state - * - * @return Message */ - protected function getMessage($state = MessageInterface::STATE_OPEN) + protected function getMessage($state = MessageInterface::STATE_OPEN): Message { $message = new Message(); diff --git a/tests/Entity/MessageTest.php b/tests/Entity/MessageTest.php index ca795c89..8027df24 100644 --- a/tests/Entity/MessageTest.php +++ b/tests/Entity/MessageTest.php @@ -21,7 +21,7 @@ class MessageTest extends TestCase /** * @dataProvider getBodyValues */ - public function testGetValue($body, $names, $expected, $default) + public function testGetValue($body, $names, $expected, $default): void { $message = new Message(); @@ -30,7 +30,7 @@ public function testGetValue($body, $names, $expected, $default) $this->assertSame($expected, $message->getValue($names, $default)); } - public function testClone() + public function testClone(): void { $message = new Message(); $message->setId(42); @@ -45,7 +45,7 @@ public function testClone() $this->assertNull($newMessage->getId()); } - public function testStatuses() + public function testStatuses(): void { $message = new Message(); @@ -62,10 +62,7 @@ public function testStatuses() $this->assertTrue($message->isOpen()); } - /** - * @return array - */ - public function getBodyValues() + public function getBodyValues(): array { return [ [['name' => 'foobar'], ['name'], 'foobar', null], diff --git a/tests/Event/DoctrineOptimizeListenerTest.php b/tests/Event/DoctrineOptimizeListenerTest.php index 2ee65ca0..997a69d0 100644 --- a/tests/Event/DoctrineOptimizeListenerTest.php +++ b/tests/Event/DoctrineOptimizeListenerTest.php @@ -24,7 +24,7 @@ class DoctrineOptimizeListenerTest extends TestCase { - public function testWithClosedManager() + public function testWithClosedManager(): void { $this->expectException(\RuntimeException::class); @@ -43,7 +43,7 @@ public function testWithClosedManager() )); } - public function testOptimize() + public function testOptimize(): void { $unitofwork = $this->createMock(UnitOfWork::class); $unitofwork->expects($this->once())->method('clear'); diff --git a/tests/Exception/InvalidParameterExceptionTest.php b/tests/Exception/InvalidParameterExceptionTest.php index 29b737e4..772b96a2 100644 --- a/tests/Exception/InvalidParameterExceptionTest.php +++ b/tests/Exception/InvalidParameterExceptionTest.php @@ -21,7 +21,7 @@ */ class InvalidParameterExceptionTest extends TestCase { - public function testException() + public function testException(): void { $this->expectException(InvalidParameterException::class); diff --git a/tests/Iterator/AMQPMessageIteratorTest.php b/tests/Iterator/AMQPMessageIteratorTest.php index 32cb16f7..1101e553 100644 --- a/tests/Iterator/AMQPMessageIteratorTest.php +++ b/tests/Iterator/AMQPMessageIteratorTest.php @@ -27,19 +27,22 @@ */ class AMQPMessageIteratorTest extends TestCase { - public function testShouldImplementMessageIteratorInterface() + public function testShouldImplementMessageIteratorInterface(): void { $rc = new \ReflectionClass(AMQPMessageIterator::class); $this->assertTrue($rc->implementsInterface(MessageIteratorInterface::class)); } - public function testCouldBeConstructedWithChannelAndContextAsArguments() + /** + * @doesNotPerformAssertions + */ + public function testCouldBeConstructedWithChannelAndContextAsArguments(): void { new AMQPMessageIterator($this->createChannelMock(), $this->createConsumerStub()); } - public function testShouldIterateOverThreeMessagesAndExit() + public function testShouldIterateOverThreeMessagesAndExit(): void { $firstMessage = new AmqpMessage('{"body": {"value": "theFirstMessageBody"}, "type": "aType", "state": "aState"}'); $secondMessage = new AmqpMessage('{"body": {"value": "theSecondMessageBody"}, "type": "aType", "state": "aState"}'); @@ -76,14 +79,12 @@ private function createConsumerStub($queueName = null) { $queue = $this->createMock(AmqpQueue::class); $queue - ->expects($this->any()) ->method('getQueueName') ->willReturn($queueName) ; $consumer = $this->createMock(AmqpConsumer::class); $consumer - ->expects($this->any()) ->method('getQueue') ->willReturn($queue) ; diff --git a/tests/Iterator/IteratorProxyMessageIteratorTest.php b/tests/Iterator/IteratorProxyMessageIteratorTest.php index 3abed326..a51b4ef0 100644 --- a/tests/Iterator/IteratorProxyMessageIteratorTest.php +++ b/tests/Iterator/IteratorProxyMessageIteratorTest.php @@ -21,7 +21,7 @@ */ class IteratorProxyMessageIteratorTest extends TestCase { - public function testIteratorProxiesIteratorMethods() + public function testIteratorProxiesIteratorMethods(): void { $content = [ 'foo', @@ -41,7 +41,7 @@ public function testIteratorProxiesIteratorMethods() /** * @see https://gist.github.com/2852498 */ - public function expectIterator($mock, array $content, $withKey = false, $counter = 0) + public function expectIterator($mock, array $content, $withKey = false, $counter = 0): int { $mock ->expects($this->at($counter)) diff --git a/tests/Iterator/MessageManagerMessageIterator.php b/tests/Iterator/MessageManagerMessageIterator.php index 9f5024c7..e267d138 100644 --- a/tests/Iterator/MessageManagerMessageIterator.php +++ b/tests/Iterator/MessageManagerMessageIterator.php @@ -35,15 +35,12 @@ public function __construct(ManagerRegistry $registry, $pause = 0, $batchSize = /** * @param array $types */ - public function _bufferize($types = []) + public function _bufferize($types = []): void { $this->bufferize($types); } - /** - * @return array - */ - public function getBuffer() + public function getBuffer(): array { return $this->buffer; } diff --git a/tests/Iterator/MessageManagerMessageIteratorTest.php b/tests/Iterator/MessageManagerMessageIteratorTest.php index 2afe97b5..8a02cee2 100644 --- a/tests/Iterator/MessageManagerMessageIteratorTest.php +++ b/tests/Iterator/MessageManagerMessageIteratorTest.php @@ -31,7 +31,7 @@ protected function setUp(): void $this->registry = $this->createMock(ManagerRegistry::class); } - public function testBufferize() + public function testBufferize(): void { $iterator = new MessageManagerMessageIterator($this->registry, 0); @@ -40,7 +40,7 @@ public function testBufferize() $this->assertCount(10, $iterator->getBuffer()); } - public function testIterations() + public function testIterations(): void { $size = 10; @@ -63,7 +63,7 @@ public function testIterations() $this->assertNotNull($iterator->current()); } - public function testLongForeach() + public function testLongForeach(): void { $iterator = new MessageManagerMessageIterator($this->registry, 500000, 2); From 45f6b53add04b066e8f52213e546e554239b1804 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Wed, 12 Feb 2020 00:53:27 +0100 Subject: [PATCH 2/3] Add .phpunit.result.cache to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 9349b895..e87997d0 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ .php_cs.cache composer.lock phpunit.xml +.phpunit.result.cache From f9ee2a4a3aec48c0dc8679b9c73aa610b4f9e747 Mon Sep 17 00:00:00 2001 From: Fran Moreno Date: Wed, 12 Feb 2020 00:53:58 +0100 Subject: [PATCH 3/3] Remove deprecated code of symfony < 4.2 --- src/DependencyInjection/Configuration.php | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 3ff44fda..8e0515a0 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -28,12 +28,7 @@ public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder('sonata_notification'); - // Keep compatibility with symfony/config < 4.2 - if (!method_exists($treeBuilder, 'getRootNode')) { - $rootNode = $treeBuilder->root('sonata_notification')->children(); - } else { - $rootNode = $treeBuilder->getRootNode()->children(); - } + $rootNode = $treeBuilder->getRootNode()->children(); $backendInfo = <<<'EOF' Other backends you can use: @@ -192,12 +187,7 @@ protected function getQueueNode() { $treeBuilder = new TreeBuilder('queues'); - // Keep compatibility with symfony/config < 4.2 - if (!method_exists($treeBuilder, 'getRootNode')) { - $node = $treeBuilder->root('queues'); - } else { - $node = $treeBuilder->getRootNode(); - } + $node = $treeBuilder->getRootNode(); $queuesInfo = <<<'EOF' Example for using RabbitMQ