|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* T3Bot. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Frank Nägler <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @link http://www.t3bot.de |
|
8
|
|
|
* @link http://wiki.typo3.org/T3Bot |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace T3Bot\Tests\Unit\Commands; |
|
11
|
|
|
|
|
12
|
|
|
use Doctrine\DBAL\Configuration; |
|
13
|
|
|
use Doctrine\DBAL\DriverManager; |
|
14
|
|
|
use React\EventLoop\LoopInterface; |
|
15
|
|
|
use Slack\Payload; |
|
16
|
|
|
use Slack\RealTimeClient; |
|
17
|
|
|
use T3Bot\Commands\TellCommand; |
|
18
|
|
|
use T3Bot\Tests\Unit\BaseCommandTestCase; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class TellCommandTest. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** @noinspection LongInheritanceChainInspection */ |
|
25
|
|
|
class TellCommandTest extends BaseCommandTestCase |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* |
|
29
|
|
|
*/ |
|
30
|
|
View Code Duplication |
public function tearDown() |
|
|
|
|
|
|
31
|
|
|
{ |
|
32
|
|
|
DriverManager::getConnection($GLOBALS['config']['db'], new Configuration()) |
|
33
|
|
|
->delete('notifications', [ |
|
34
|
|
|
'to_user' => 'U12345' |
|
35
|
|
|
]); |
|
36
|
|
|
parent::tearDown(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function tellDataProvider() : array |
|
43
|
|
|
{ |
|
44
|
|
|
return [ |
|
45
|
|
|
'tell <@U12345> about review:47640' => ['tell <@U12345> about review:47640', 'OK, I will tell <@U12345> about your message'], |
|
46
|
|
|
'tell <@U12345> about forge:23456' => ['tell <@U12345> about forge:23456', 'OK, I will tell <@U12345> about your message'], |
|
47
|
|
|
'tell <@U12345> you are so nice' => ['tell <@U12345> you are so nice', 'OK, I will tell <@U12345> about your message'], |
|
48
|
|
|
]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @test |
|
53
|
|
|
* @dataProvider tellDataProvider |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $message |
|
56
|
|
|
* @param string $expectedMessage |
|
57
|
|
|
*/ |
|
58
|
|
View Code Duplication |
public function processTellReturnsCorrectResponse($message, $expectedMessage) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
$this->initCommandWithPayload(TellCommand::class, [ |
|
61
|
|
|
'user' => 'U54321', |
|
62
|
|
|
'text' => $message, |
|
63
|
|
|
]); |
|
64
|
|
|
$result = $this->command->process(); |
|
|
|
|
|
|
65
|
|
|
static::assertEquals($expectedMessage, $result); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @test |
|
70
|
|
|
*/ |
|
71
|
|
|
public function processPresenceChangeReturnsCorrectResponseForPresenceAway() |
|
72
|
|
|
{ |
|
73
|
|
|
$loop = $this->getMock(LoopInterface::class); |
|
74
|
|
|
/** @var Payload $payload */ |
|
75
|
|
|
$payload = new Payload([ |
|
76
|
|
|
'user' => 'U12345', |
|
77
|
|
|
'presence' => 'away', |
|
78
|
|
|
]); |
|
79
|
|
|
/** @var RealTimeClient $client */ |
|
80
|
|
|
$client = $this->getMock(RealTimeClient::class, [], [$loop]); |
|
81
|
|
|
/** @var TellCommand|\PHPUnit_Framework_MockObject_MockObject $command */ |
|
82
|
|
|
$command = $this->getMock(TellCommand::class, ['sendResponse'], [$payload, $client]); |
|
83
|
|
|
$command->expects(static::exactly(0)) |
|
|
|
|
|
|
84
|
|
|
->method('sendResponse'); |
|
85
|
|
|
$command->processPresenceChange('U12345', 'away'); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @test |
|
90
|
|
|
* @dataProvider tellDataProvider |
|
91
|
|
|
*/ |
|
92
|
|
|
public function processPresenceChangeReturnsCorrectResponseForPresenceActive($message) |
|
93
|
|
|
{ |
|
94
|
|
|
$this->initCommandWithPayload(TellCommand::class, [ |
|
95
|
|
|
'user' => 'U54321', |
|
96
|
|
|
'text' => $message, |
|
97
|
|
|
]); |
|
98
|
|
|
$this->command->process(); |
|
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
$loop = $this->getMock(LoopInterface::class); |
|
101
|
|
|
$payload = new Payload([ |
|
102
|
|
|
'user' => 'U12345', |
|
103
|
|
|
'presence' => 'active', |
|
104
|
|
|
]); |
|
105
|
|
|
$client = $this->getMock(RealTimeClient::class, [], [$loop]); |
|
106
|
|
|
/** @var TellCommand|\PHPUnit_Framework_MockObject_MockObject $command */ |
|
107
|
|
|
$command = $this->getMock(TellCommand::class, ['sendResponse'], [$payload, $client]); |
|
108
|
|
|
$command |
|
|
|
|
|
|
109
|
|
|
->expects(static::exactly(1)) |
|
110
|
|
|
->method('sendResponse'); |
|
111
|
|
|
$command->processPresenceChange('U12345', 'active'); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.