-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDBConfigurableConsumerTest.php
More file actions
137 lines (117 loc) · 5.34 KB
/
DBConfigurableConsumerTest.php
File metadata and controls
137 lines (117 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Smartbox\Integration\FrameworkBundle\Tests\Unit\Components\DB;
use Smartbox\CoreBundle\Type\SerializableInterface;
use Smartbox\Integration\FrameworkBundle\Components\DB\ConfigurableStepsProviderInterface;
use Smartbox\Integration\FrameworkBundle\Components\DB\Dbal\ConfigurableDbalProtocol;
use Smartbox\Integration\FrameworkBundle\Components\DB\DBConfigurableConsumer;
use Smartbox\Integration\FrameworkBundle\Configurability\ConfigurableServiceHelper;
use Smartbox\Integration\FrameworkBundle\Core\Consumers\ConfigurableConsumerInterface;
use Smartbox\Integration\FrameworkBundle\Core\Endpoints\EndpointInterface;
use Smartbox\Integration\FrameworkBundle\Core\Messages\MessageFactory;
use Smartbox\Integration\FrameworkBundle\Core\Messages\MessageInterface;
use Smartbox\Integration\FrameworkBundle\Tools\Helper\SmartesbHelper;
use Symfony\Component\Stopwatch\Stopwatch;
class DBConfigurableConsumerTest extends \PHPUnit\Framework\TestCase
{
/**
* @var DBConfigurableConsumer
*/
private $consumer;
/**
* @var ConfigurableStepsProviderInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $stepProvider;
/**
* @var MessageFactory|\PHPUnit_Framework_MockObject_MockObject
*/
protected $messageFactory;
/**
* @var SmartesbHelper|\PHPUnit_Framework_MockObject_MockObject
*/
protected $helper;
/**
* {@inheritdoc}
*/
protected function setUp(): void
{
$this->messageFactory = $this->createMock(MessageFactory::class);
$this->stepProvider = $this->createMock(ConfigurableStepsProviderInterface::class);
$this->helper = $this->createMock(SmartesbHelper::class);
$this->consumer = new DBConfigurableConsumer();
$this->consumer->setConfHelper(new ConfigurableServiceHelper());
$this->consumer->setConfigurableStepsProvider($this->stepProvider);
$this->consumer->setMessageFactory($this->messageFactory);
$this->consumer->setSmartesbHelper($this->helper);
$this->consumer->setExpirationCount(2);
$this->consumer->setMethodsConfiguration([
'myMethod' => [
ConfigurableConsumerInterface::CONFIG_ON_CONSUME => [],
ConfigurableConsumerInterface::CONFIG_QUERY_STEPS => [],
ConfigurableConsumerInterface::CONFIG_QUERY_RESULT => $this->createMock(SerializableInterface::class),
],
]);
$this->helper->expects($this->any())->method('getMessageFactory')->willReturn($this->messageFactory);
}
public function testGetConfigurableStepsProvider()
{
$this->assertSame($this->stepProvider, $this->consumer->getConfigurableStepsProvider());
}
/**
* @group time-sensitive
*/
public function testConsume()
{
$options = [
ConfigurableDbalProtocol::OPTION_METHOD => 'myMethod',
ConfigurableDbalProtocol::OPTION_STOP_ON_NO_RESULTS => true,
ConfigurableDbalProtocol::OPTION_SLEEP_TIME => 20000,
ConfigurableDbalProtocol::OPTION_INACTIVITY_TRIGGER => 1,
ConfigurableDbalProtocol::OPTION_ALWAYS_SLEEP => false,
];
/** @var EndpointInterface|\PHPUnit_Framework_MockObject_MockObject $endpoint */
$endpoint = $this->createMock(EndpointInterface::class);
$endpoint->expects($this->any())->method('getOptions')->willReturn($options);
$endpoint->expects($this->any())
->method('getOption')
->willReturnCallback(function ($key) use ($options) {
return $options[$key];
});
$this->messageFactory->expects($this->exactly(2))
->method('createMessage')
->willReturn($this->createMock(MessageInterface::class));
$stopwatch = new Stopwatch();
$stopwatch->start('consume-time');
$this->consumer->consume($endpoint);
$duration = $stopwatch->stop('consume-time')->getDuration();
$this->assertLessThan(10000, $duration, 'Execution should not last more than 10s');
}
/**
* @group time-sensitive
*/
public function testConsumeAlways()
{
$options = [
ConfigurableDbalProtocol::OPTION_METHOD => 'myMethod',
ConfigurableDbalProtocol::OPTION_STOP_ON_NO_RESULTS => true,
ConfigurableDbalProtocol::OPTION_SLEEP_TIME => 1000,
ConfigurableDbalProtocol::OPTION_INACTIVITY_TRIGGER => 1,
ConfigurableDbalProtocol::OPTION_ALWAYS_SLEEP => true,
];
/** @var EndpointInterface|\PHPUnit_Framework_MockObject_MockObject $endpoint */
$endpoint = $this->createMock(EndpointInterface::class);
$endpoint->expects($this->any())->method('getOptions')->willReturn($options);
$endpoint->expects($this->any())
->method('getOption')
->willReturnCallback(function ($key) use ($options) {
return $options[$key];
});
$this->messageFactory->expects($this->exactly(2))
->method('createMessage')
->willReturn($this->createMock(MessageInterface::class));
$stopwatch = new Stopwatch();
$stopwatch->start('consume-time');
$this->consumer->consume($endpoint);
$duration = $stopwatch->stop('consume-time')->getDuration();
$this->assertGreaterThan(1, $duration, 'Execution should not last more than 10s');
}
}