Completed
Push — master ( b6a302...bc8f3e )
by Alexey
04:45
created

SlackMessageTraitTest::testFormatCode()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 18
Ratio 100 %

Importance

Changes 0
Metric Value
dl 18
loc 18
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
/**
3
 * This file is part of the WoW-Apps/Symfony-Slack-Bot bundle for Symfony 3
4
 * https://github.com/wow-apps/symfony-slack-bot
5
 *
6
 * (c) 2016 WoW-Apps
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WowApps\SlackBundle\Tests\Traits;
13
14
use WowApps\SlackBundle\Tests\TestCase;
15
use WowApps\SlackBundle\Traits\SlackMessageTrait;
16
17
/**
18
 * Trait SlackMessageTrait Test
19
 *
20
 * @author Alexey Samara <[email protected]>
21
 * @package WowApps\SlackBundle
22
 */
23
class SlackMessageTraitTest extends TestCase
24
{
25
    use SlackMessageTrait;
26
27
    public function testInlineMultilines()
28
    {
29
        $lines = [];
30
        for ($i = rand(parent::ARRAY_MIN_ITEMS, parent::ARRAY_MAX_ITEMS); $i <= parent::ARRAY_MAX_ITEMS; ++$i) {
31
            $randomString = $this->randomString();
32
            $lines[] = $randomString;
33
        }
34
35
        $expect = implode("\n", $lines);
36
        $actual = $this->inlineMultilines($lines);
37
38
        $this->assertEquals($expect, $actual);
39
    }
40
41
    public function testFormatBold()
42
    {
43
        $randomString = $this->randomString();
44
        $expect = sprintf('*%s*', $randomString);
45
        $actual = $this->formatBold($randomString);
46
47
        $this->assertEquals($expect, $actual);
48
    }
49
50
    public function testFormatItalic()
51
    {
52
        $randomString = $this->randomString();
53
        $expect = sprintf('_%s_', $randomString);
54
        $actual = $this->formatItalic($randomString);
55
56
        $this->assertEquals($expect, $actual);
57
    }
58
59
    public function testFormatStrikeThought()
60
    {
61
        $randomString = $this->randomString();
62
        $expect = sprintf('~%s~', $randomString);
63
        $actual = $this->formatStrikeThought($randomString);
64
65
        $this->assertEquals($expect, $actual);
66
    }
67
68
    public function testFormatListMarker()
69
    {
70
        $lines = [];
71
        for ($i = rand(parent::ARRAY_MIN_ITEMS, parent::ARRAY_MAX_ITEMS); $i <= parent::ARRAY_MAX_ITEMS; ++$i) {
72
            $randomString = $this->randomString();
73
            $lines[] = $randomString;
74
        }
75
76
        $expect = "\n";
77
        foreach ($lines as $line) {
78
            $expect .= "• " . $line . "\n";
79
        }
80
        $actual = $this->formatListMarker($lines);
81
82
        $this->assertEquals($expect, $actual);
83
    }
84
85
    public function testFormatListNumeric()
86
    {
87
        $lines = [];
88
        for ($i = rand(parent::ARRAY_MIN_ITEMS, parent::ARRAY_MAX_ITEMS); $i <= parent::ARRAY_MAX_ITEMS; ++$i) {
89
            $randomString = $this->randomString();
90
            $lines[] = $randomString;
91
        }
92
93
        $expect = "\n";
94
        foreach ($lines as $key => $line) {
95
            $expect .= sprintf("%d. %s\n", $key + 1, $line);
96
        }
97
        $actual = $this->formatListNumeric($lines);
98
99
        $this->assertEquals($expect, $actual);
100
    }
101
102
    public function testFormatCode()
103
    {
104
        $lines = [];
105
        for ($i = rand(parent::ARRAY_MIN_ITEMS, parent::ARRAY_MAX_ITEMS); $i <= parent::ARRAY_MAX_ITEMS; ++$i) {
106
            $randomString = $this->randomString();
107
            $lines[] = $randomString;
108
        }
109
110
        $expect = sprintf(
111
            "%s%s%s",
112
            "\n```",
113
            $this->inlineMultilines($lines),
114
            "```\n"
115
        );
116
        $actual = $this->formatCode($lines);
117
118
        $this->assertEquals($expect, $actual);
119
    }
120
121
    public function testNewLine()
122
    {
123
        $expect = "\n";
124
        $actual = $this->newLine();
125
126
        $this->assertEquals($expect, $actual);
127
    }
128
129
    public function testFormatLink()
130
    {
131
        $url = $this->randomString();
132
        $title = $this->randomString();
133
134
        $expect = sprintf("<%s|%s>", $url, $title);
135
        $actual = $this->formatLink($title, $url);
136
137
        $this->assertEquals($expect, $actual);
138
    }
139
140
    public function testEscapeCharacters()
141
    {
142
        $expect = $testString = $this->randomString() . '<&>';
143
        $expect = str_replace('&', '&amp;', $expect);
144
        $expect = str_replace('<', '&lt;', $expect);
145
        $expect = str_replace('>', '&gt;', $expect);
146
        $actual = $this->escapeCharacters($testString);
147
148
        $this->assertEquals($expect, $actual);
149
    }
150
}
151