Test Setup Failed
Push — master ( ac261d...91041f )
by Kacper
02:33
created

TokenTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 24.62 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 2
cbo 5
dl 16
loc 65
rs 10

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
 * Highlighter
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Tests;
17
18
19
use Kadet\Highlighter\Parser\CloseRule;
20
use Kadet\Highlighter\Parser\OpenRule;
21
use Kadet\Highlighter\Parser\Rule;
22
use Kadet\Highlighter\Parser\Token;
23
24
class TokenTest extends \PHPUnit_Framework_TestCase
25
{
26
    public function testCreation()
27
    {
28
        $rule  = new Rule();
29
        $token = new Token(['test.name', 'pos' => 10, 'index' => 10, 'rule' => $rule]);
30
31
        $this->assertEquals($token->name, 'test.name', 'Token name is invalid');
32
        $this->assertEquals($token->pos, 10, 'Position is invalid');
33
        $this->assertEquals($token->index, 10, 'Index is invalid');
34
        $this->assertEquals($token->getRule(), $rule, 'Rule is invalid');
35
    }
36
37
    public function testCreationWithStart()
38
    {
39
        $start = new Token(['test.name', 'pos' => 5]);
40
        $token = new Token(['test.name', 'pos' => 10, 'start' => $start]);
41
42
        $this->assertEquals($token->getStart(), $start, 'Token is not pointing to start.');
43
        $this->assertEquals($start->getEnd(), $token, 'Start is not pointing to token.');
44
    }
45
46
    public function testCreationWithEnd()
47
    {
48
        $end   = new Token(['test.name', 'pos' => 15]);
49
        $token = new Token(['test.name', 'pos' => 10, 'end' => $end]);
50
51
        $this->assertEquals($token->getEnd(), $end, 'Token is not pointing to end.');
52
        $this->assertEquals($end->getStart(), $token, 'End is not pointing to token.');
53
    }
54
55
    public function testCreationWithLength()
56
    {
57
        $token = new Token(['test.name', 'pos' => 15, 'length' => 10]);
58
59
        $this->assertEquals($token->getEnd()->pos, 25, 'Token is not pointing to end.');
60
    }
61
62
    public function testLength()
63
    {
64
        $token = new Token(['test.name', 'pos' => 15, 'length' => 10]);
65
66
        $this->assertEquals($token->getLength(), 10, 'Length is invalid');
67
    }
68
69
    public function testIsStart()
70
    {
71
        $token = new Token(['test.name', 'pos' => 10]);
72
        $close = new Token(['test.name', 'pos' => 10, 'rule' => new CloseRule()]);
73
74
        $this->assertTrue($token->isStart());
75
        $this->assertFalse($close->isStart());
76
    }
77
78
    public function testIsEnd()
79
    {
80
        $token = new Token(['test.name', 'pos' => 15]);
81
        new Token(['test.name', 'pos' => 10, 'end' => $token]);
82
83
        $close = new Token(['test.name', 'pos' => 10, 'rule' => new OpenRule()]);
84
85
        $this->assertTrue($token->isEnd());
86
        $this->assertFalse($close->isEnd());
87
    }
88
}
89