Completed
Branch master (812a3e)
by Frank
04:30 queued 02:09
created

TellCommandTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 89
Duplicated Lines 19.1 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 17
loc 89
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    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
    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