Skip to content

Commit 52c4cb9

Browse files
committed
[consumption] fix onStart extension tests.
1 parent 75095cb commit 52c4cb9

19 files changed

+134
-54
lines changed

pkg/enqueue/Client/ConsumptionExtension/SetupBrokerExtension.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Enqueue\Client\ConsumptionExtension;
44

55
use Enqueue\Client\DriverInterface;
6-
use Enqueue\Consumption\Context;
6+
use Enqueue\Consumption\Context\Start;
77
use Enqueue\Consumption\EmptyExtensionTrait;
88
use Enqueue\Consumption\ExtensionInterface;
99

@@ -30,10 +30,7 @@ public function __construct(DriverInterface $driver)
3030
$this->isDone = false;
3131
}
3232

33-
/**
34-
* {@inheritdoc}
35-
*/
36-
public function onStart(Context $context)
33+
public function onStart(Start $context): void
3734
{
3835
if (false == $this->isDone) {
3936
$this->isDone = true;

pkg/enqueue/Consumption/Extension/LoggerExtension.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
namespace Enqueue\Consumption\Extension;
44

55
use Enqueue\Consumption\Context;
6+
use Enqueue\Consumption\Context\Start;
67
use Enqueue\Consumption\EmptyExtensionTrait;
78
use Enqueue\Consumption\ExtensionInterface;
89
use Enqueue\Consumption\Result;
910
use Interop\Queue\Message as InteropMessage;
1011
use Psr\Log\LoggerInterface;
12+
use Psr\Log\NullLogger;
1113

1214
class LoggerExtension implements ExtensionInterface
1315
{
@@ -26,19 +28,16 @@ public function __construct(LoggerInterface $logger)
2628
$this->logger = $logger;
2729
}
2830

29-
/**
30-
* {@inheritdoc}
31-
*/
32-
public function onStart(Context $context)
31+
public function onStart(Start $context): void
3332
{
34-
if ($context->getLogger()) {
33+
if ($context->getLogger() && false == $context->getLogger() instanceof NullLogger) {
3534
$context->getLogger()->debug(sprintf(
3635
'Skip setting context\'s logger "%s". Another one "%s" has already been set.',
3736
get_class($this->logger),
3837
get_class($context->getLogger())
3938
));
4039
} else {
41-
$context->setLogger($this->logger);
40+
$context->changeLogger($this->logger);
4241
$this->logger->debug(sprintf('Set context\'s logger "%s"', get_class($this->logger)));
4342
}
4443
}

pkg/enqueue/Consumption/Extension/NicenessExtension.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Enqueue\Consumption\Extension;
44

5-
use Enqueue\Consumption\Context;
5+
use Enqueue\Consumption\Context\Start;
66
use Enqueue\Consumption\EmptyExtensionTrait;
77
use Enqueue\Consumption\ExtensionInterface;
88

@@ -32,10 +32,7 @@ public function __construct($niceness)
3232
$this->niceness = $niceness;
3333
}
3434

35-
/**
36-
* {@inheritdoc}
37-
*/
38-
public function onStart(Context $context)
35+
public function onStart(Start $context): void
3936
{
4037
if (0 !== $this->niceness) {
4138
$changed = @proc_nice($this->niceness);

pkg/enqueue/Consumption/Extension/SignalExtension.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue\Consumption\Extension;
44

55
use Enqueue\Consumption\Context;
6+
use Enqueue\Consumption\Context\Start;
67
use Enqueue\Consumption\EmptyExtensionTrait;
78
use Enqueue\Consumption\Exception\LogicException;
89
use Enqueue\Consumption\ExtensionInterface;
@@ -22,10 +23,7 @@ class SignalExtension implements ExtensionInterface
2223
*/
2324
protected $logger;
2425

25-
/**
26-
* {@inheritdoc}
27-
*/
28-
public function onStart(Context $context)
26+
public function onStart(Start $context): void
2927
{
3028
if (false == extension_loaded('pcntl')) {
3129
throw new LogicException('The pcntl extension is required in order to catch signals.');

pkg/enqueue/Symfony/Consumption/QueueConsumerOptionsCommandTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ protected function configureQueueConsumerOptions()
2626
protected function setQueueConsumerOptions(QueueConsumerInterface $consumer, InputInterface $input)
2727
{
2828
if (null !== $idleTimeout = $input->getOption('idle-time')) {
29-
$consumer->setIdleTime((float) $idleTimeout);
29+
$consumer->setIdleTime((int) $idleTimeout);
3030
}
3131

3232
if (null !== $receiveTimeout = $input->getOption('receive-timeout')) {
33-
$consumer->setReceiveTimeout((float) $receiveTimeout);
33+
$consumer->setReceiveTimeout((int) $receiveTimeout);
3434
}
3535
}
3636
}

pkg/enqueue/Tests/Client/ConsumptionExtension/FlushSpoolProducerExtensionTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use Enqueue\Client\ConsumptionExtension\FlushSpoolProducerExtension;
66
use Enqueue\Client\SpoolProducer;
77
use Enqueue\Consumption\Context;
8+
use Enqueue\Consumption\Context\Start;
89
use Enqueue\Consumption\ExtensionInterface;
910
use Enqueue\Test\ClassExtensionTrait;
1011
use PHPUnit\Framework\TestCase;
12+
use Psr\Log\NullLogger;
1113

1214
class FlushSpoolProducerExtensionTest extends TestCase
1315
{
@@ -32,7 +34,9 @@ public function testShouldDoNothingOnStart()
3234
;
3335

3436
$extension = new FlushSpoolProducerExtension($producer);
35-
$extension->onStart($this->createContextMock());
37+
$extension->onStart(
38+
new Start($this->createMock(\Interop\Queue\Context::class), new NullLogger(), [], 0, 0, 0)
39+
);
3640
}
3741

3842
public function testShouldDoNothingOnBeforeReceive()

pkg/enqueue/Tests/Client/ConsumptionExtension/SetupBrokerExtensionTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use Enqueue\Client\ConsumptionExtension\SetupBrokerExtension;
66
use Enqueue\Client\DriverInterface;
7-
use Enqueue\Consumption\Context;
7+
use Enqueue\Consumption\Context\Start;
88
use Enqueue\Consumption\ExtensionInterface;
99
use Enqueue\Test\ClassExtensionTrait;
1010
use Interop\Queue\Context as InteropContext;
@@ -36,8 +36,7 @@ public function testShouldSetupBroker()
3636
->with($this->identicalTo($logger))
3737
;
3838

39-
$context = new Context($this->createMock(InteropContext::class));
40-
$context->setLogger($logger);
39+
$context = new Start($this->createMock(InteropContext::class), $logger, [], 0, 0, 0);
4140

4241
$extension = new SetupBrokerExtension($driver);
4342
$extension->onStart($context);
@@ -54,8 +53,7 @@ public function testShouldSetupBrokerOnlyOnce()
5453
->with($this->identicalTo($logger))
5554
;
5655

57-
$context = new Context($this->createMock(InteropContext::class));
58-
$context->setLogger($logger);
56+
$context = new Start($this->createMock(InteropContext::class), $logger, [], 0, 0, 0);
5957

6058
$extension = new SetupBrokerExtension($driver);
6159
$extension->onStart($context);

pkg/enqueue/Tests/Consumption/ChainExtensionTest.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
use Enqueue\Consumption\ChainExtension;
66
use Enqueue\Consumption\Context;
7+
use Enqueue\Consumption\Context\Start;
78
use Enqueue\Consumption\ExtensionInterface;
89
use Enqueue\Test\ClassExtensionTrait;
910
use PHPUnit\Framework\TestCase;
11+
use Psr\Log\LoggerInterface;
1012

1113
class ChainExtensionTest extends TestCase
1214
{
@@ -24,7 +26,7 @@ public function testCouldBeConstructedWithExtensionsArray()
2426

2527
public function testShouldProxyOnStartToAllInternalExtensions()
2628
{
27-
$context = $this->createContextMock();
29+
$context = new Start($this->createMock(\Interop\Queue\Context::class), $this->createLoggerMock(), [], 0, 0, 0);
2830

2931
$fooExtension = $this->createExtension();
3032
$fooExtension
@@ -176,6 +178,14 @@ public function testShouldProxyOnInterruptedToAllInternalExtensions()
176178
$extensions->onInterrupted($context);
177179
}
178180

181+
/**
182+
* @return \PHPUnit_Framework_MockObject_MockObject
183+
*/
184+
protected function createLoggerMock(): LoggerInterface
185+
{
186+
return $this->createMock(LoggerInterface::class);
187+
}
188+
179189
/**
180190
* @return \PHPUnit_Framework_MockObject_MockObject|Context
181191
*/

pkg/enqueue/Tests/Consumption/Extension/LoggerExtensionTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue\Tests\Consumption\Extension;
44

55
use Enqueue\Consumption\Context;
6+
use Enqueue\Consumption\Context\Start;
67
use Enqueue\Consumption\Extension\LoggerExtension;
78
use Enqueue\Consumption\ExtensionInterface;
89
use Enqueue\Consumption\Result;
@@ -12,6 +13,7 @@
1213
use Interop\Queue\Context as InteropContext;
1314
use PHPUnit\Framework\TestCase;
1415
use Psr\Log\LoggerInterface;
16+
use Psr\Log\NullLogger;
1517

1618
class LoggerExtensionTest extends TestCase
1719
{
@@ -33,7 +35,7 @@ public function testShouldSetLoggerToContextOnStart()
3335

3436
$extension = new LoggerExtension($logger);
3537

36-
$context = new Context($this->createContextMock());
38+
$context = new Start($this->createContextMock(), new NullLogger(), [], 0, 0, 0);
3739

3840
$extension->onStart($context);
3941

@@ -51,7 +53,7 @@ public function testShouldAddInfoMessageOnStart()
5153

5254
$extension = new LoggerExtension($logger);
5355

54-
$context = new Context($this->createContextMock());
56+
$context = new Start($this->createContextMock(), new NullLogger(), [], 0, 0, 0);
5557

5658
$extension->onStart($context);
5759
}
@@ -168,8 +170,7 @@ public function testShouldNotSetLoggerIfOneHasBeenSetOnStart()
168170

169171
$extension = new LoggerExtension($logger);
170172

171-
$context = new Context($this->createContextMock());
172-
$context->setLogger($alreadySetLogger);
173+
$context = new Start($this->createContextMock(), $alreadySetLogger, [], 0, 0, 0);
173174

174175
$extension->onStart($context);
175176
}

pkg/enqueue/Tests/Consumption/Extension/NicenessExtensionTest.php

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace Enqueue\Tests\Consumption\Extension;
44

5-
use Enqueue\Consumption\Context;
5+
use Enqueue\Consumption\Context\Start;
66
use Enqueue\Consumption\Extension\NicenessExtension;
7-
use Interop\Queue\Consumer;
87
use Interop\Queue\Context as InteropContext;
9-
use Interop\Queue\Processor;
108
use PHPUnit\Framework\TestCase;
11-
use Psr\Log\LoggerInterface;
9+
use Psr\Log\NullLogger;
1210

1311
class NicenessExtensionTest extends TestCase
1412
{
@@ -28,20 +26,17 @@ public function testShouldThrowWarningOnInvalidArgument()
2826
$this->expectException(\InvalidArgumentException::class);
2927
$this->expectExceptionMessage('proc_nice(): Only a super user may attempt to increase the priority of a process');
3028

29+
$context = new Start($this->createContextMock(), new NullLogger(), [], 0, 0, 0);
30+
3131
$extension = new NicenessExtension(-1);
32-
$extension->onStart($this->createContext());
32+
$extension->onStart($context);
3333
}
3434

3535
/**
36-
* @return Context
36+
* @return \PHPUnit_Framework_MockObject_MockObject|InteropContext
3737
*/
38-
protected function createContext(): Context
38+
protected function createContextMock(): InteropContext
3939
{
40-
$context = new Context($this->createMock(InteropContext::class));
41-
$context->setLogger($this->createMock(LoggerInterface::class));
42-
$context->setConsumer($this->createMock(Consumer::class));
43-
$context->setProcessor($this->createMock(Processor::class));
44-
45-
return $context;
40+
return $this->createMock(InteropContext::class);
4641
}
4742
}

pkg/enqueue/Tests/Consumption/Extension/ReplyExtensionTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Enqueue\Tests\Consumption\Extension;
44

55
use Enqueue\Consumption\Context;
6+
use Enqueue\Consumption\Context\Start;
67
use Enqueue\Consumption\Extension\ReplyExtension;
78
use Enqueue\Consumption\ExtensionInterface;
89
use Enqueue\Consumption\Result;
@@ -39,7 +40,7 @@ public function testShouldDoNothingOnStart()
3940
{
4041
$extension = new ReplyExtension();
4142

42-
$extension->onStart(new Context($this->createNeverUsedContextMock()));
43+
$extension->onStart(new Start($this->createNeverUsedContextMock(), new NullLogger(), [], 0, 0, 0));
4344
}
4445

4546
public function testShouldDoNothingOnBeforeReceive()

pkg/enqueue/Tests/Symfony/Client/ConsumeCommandTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,26 @@
1212
use Enqueue\Container\Container;
1313
use Enqueue\Null\NullQueue;
1414
use Enqueue\Symfony\Client\ConsumeCommand;
15+
use Enqueue\Test\ClassExtensionTrait;
1516
use PHPUnit\Framework\TestCase;
1617
use Psr\Container\ContainerInterface;
18+
use Symfony\Component\Console\Command\Command;
1719
use Symfony\Component\Console\Tester\CommandTester;
1820

1921
class ConsumeCommandTest extends TestCase
2022
{
23+
use ClassExtensionTrait;
24+
25+
public function testShouldBeSubClassOfCommand()
26+
{
27+
$this->assertClassExtends(Command::class, ConsumeCommand::class);
28+
}
29+
30+
public function testShouldNotBeFinal()
31+
{
32+
$this->assertClassNotFinal(ConsumeCommand::class);
33+
}
34+
2135
public function testCouldBeConstructedWithRequiredAttributes()
2236
{
2337
new ConsumeCommand($this->createMock(ContainerInterface::class));

pkg/enqueue/Tests/Symfony/Client/ProduceCommandTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@
55
use Enqueue\Client\ProducerInterface;
66
use Enqueue\Container\Container;
77
use Enqueue\Symfony\Client\ProduceCommand;
8+
use Enqueue\Test\ClassExtensionTrait;
89
use PHPUnit\Framework\TestCase;
910
use Psr\Container\ContainerInterface;
11+
use Symfony\Component\Console\Command\Command;
1012
use Symfony\Component\Console\Tester\CommandTester;
1113

1214
class ProduceCommandTest extends TestCase
1315
{
16+
use ClassExtensionTrait;
17+
18+
public function testShouldBeSubClassOfCommand()
19+
{
20+
$this->assertClassExtends(Command::class, ProduceCommand::class);
21+
}
22+
23+
public function testShouldNotBeFinal()
24+
{
25+
$this->assertClassNotFinal(ProduceCommand::class);
26+
}
27+
1428
public function testCouldBeConstructedWithContainerAsFirstArgument()
1529
{
1630
new ProduceCommand($this->createMock(ContainerInterface::class));

pkg/enqueue/Tests/Symfony/Client/RoutesCommandTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function testShouldBeSubClassOfCommand()
2323
$this->assertClassExtends(Command::class, RoutesCommand::class);
2424
}
2525

26-
public function testShouldBeFinal()
26+
public function testShouldNotBeFinal()
2727
{
28-
$this->assertClassFinal(RoutesCommand::class);
28+
$this->assertClassNotFinal(RoutesCommand::class);
2929
}
3030

3131
public function testCouldBeConstructedWithConfigAndRouteCollectionAsArguments()

pkg/enqueue/Tests/Symfony/Client/SetupBrokerCommandTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,26 @@
55
use Enqueue\Client\DriverInterface;
66
use Enqueue\Container\Container;
77
use Enqueue\Symfony\Client\SetupBrokerCommand;
8+
use Enqueue\Test\ClassExtensionTrait;
89
use PHPUnit\Framework\TestCase;
910
use Psr\Container\ContainerInterface;
11+
use Symfony\Component\Console\Command\Command;
1012
use Symfony\Component\Console\Tester\CommandTester;
1113

1214
class SetupBrokerCommandTest extends TestCase
1315
{
16+
use ClassExtensionTrait;
17+
18+
public function testShouldBeSubClassOfCommand()
19+
{
20+
$this->assertClassExtends(Command::class, SetupBrokerCommand::class);
21+
}
22+
23+
public function testShouldNotBeFinal()
24+
{
25+
$this->assertClassNotFinal(SetupBrokerCommand::class);
26+
}
27+
1428
public function testCouldBeConstructedWithContainerAsFirstArgument()
1529
{
1630
new SetupBrokerCommand($this->createMock(ContainerInterface::class));

0 commit comments

Comments
 (0)