Completed
Push — master ( 28c8cd...7e44c6 )
by André
18:53
created

DispatcherTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 156
Duplicated Lines 16.67 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 26
loc 156
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 5

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getParsingDispatcherMock() 0 4 1
A testParseMissingContentType() 0 9 1
A testParseInvalidContentType() 13 13 1
A testParseMissingFormatHandler() 13 13 1
B testParse() 0 30 1
B testParseSpecialUrlHeader() 0 39 1
B testParseMediaTypeCharset() 0 26 1

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
/**
4
 * File containing the DispatcherTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\REST\Common\Tests\Input;
10
11
use eZ\Publish\Core\REST\Common;
12
use eZ\Publish\Core\REST\Common\Input\ParsingDispatcher;
13
use eZ\Publish\Core\REST\Common\Input\Handler;
14
use PHPUnit\Framework\TestCase;
15
16
/**
17
 * Dispatcher test class.
18
 */
19
class DispatcherTest extends TestCase
20
{
21
    protected function getParsingDispatcherMock()
22
    {
23
        return $this->createMock(ParsingDispatcher::class);
24
    }
25
26
    /**
27
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
28
     */
29
    public function testParseMissingContentType()
30
    {
31
        $message = new Common\Message();
32
33
        $parsingDispatcher = $this->getParsingDispatcherMock();
34
        $dispatcher = new Common\Input\Dispatcher($parsingDispatcher);
35
36
        $dispatcher->parse($message);
37
    }
38
39
    /**
40
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
41
     */
42 View Code Duplication
    public function testParseInvalidContentType()
43
    {
44
        $message = new Common\Message(
45
            array(
46
                'Content-Type' => 'text/html',
47
            )
48
        );
49
50
        $parsingDispatcher = $this->getParsingDispatcherMock();
51
        $dispatcher = new Common\Input\Dispatcher($parsingDispatcher);
52
53
        $dispatcher->parse($message);
54
    }
55
56
    /**
57
     * @expectedException \eZ\Publish\Core\REST\Common\Exceptions\Parser
58
     */
59 View Code Duplication
    public function testParseMissingFormatHandler()
60
    {
61
        $message = new Common\Message(
62
            array(
63
                'Content-Type' => 'text/html+unknown',
64
            )
65
        );
66
67
        $parsingDispatcher = $this->getParsingDispatcherMock();
68
        $dispatcher = new Common\Input\Dispatcher($parsingDispatcher);
69
70
        $dispatcher->parse($message);
71
    }
72
73
    public function testParse()
74
    {
75
        $message = new Common\Message(
76
            array(
77
                'Content-Type' => 'text/html+format',
78
            ),
79
            'Hello world!'
80
        );
81
82
        $parsingDispatcher = $this->getParsingDispatcherMock();
83
        $parsingDispatcher
84
            ->expects($this->at(0))
85
            ->method('parse')
86
            ->with(array(42), 'text/html')
87
            ->will($this->returnValue(23));
88
89
        $handler = $this->createMock(Handler::class);
90
        $handler
91
            ->expects($this->at(0))
92
            ->method('convert')
93
            ->with('Hello world!')
94
            ->will($this->returnValue(array(array(42))));
95
96
        $dispatcher = new Common\Input\Dispatcher($parsingDispatcher, array('format' => $handler));
97
98
        $this->assertSame(
99
            23,
100
            $dispatcher->parse($message)
101
        );
102
    }
103
104
    /**
105
     * @todo This is a test for a feature that needs refactoring. There must be
106
     * a sensible way to submit the called URL to the parser.
107
     */
108
    public function testParseSpecialUrlHeader()
109
    {
110
        $message = new Common\Message(
111
            array(
112
                'Content-Type' => 'text/html+format',
113
                'Url' => '/foo/bar',
114
            ),
115
            'Hello world!'
116
        );
117
118
        $parsingDispatcher = $this->getParsingDispatcherMock();
119
        $parsingDispatcher
120
            ->expects($this->at(0))
121
            ->method('parse')
122
            ->with(array('someKey' => 'someValue', '__url' => '/foo/bar'), 'text/html')
123
            ->will($this->returnValue(23));
124
125
        $handler = $this->createMock(Handler::class);
126
        $handler
127
            ->expects($this->at(0))
128
            ->method('convert')
129
            ->with('Hello world!')
130
            ->will(
131
                $this->returnValue(
132
                    array(
133
                        array(
134
                            'someKey' => 'someValue',
135
                        ),
136
                    )
137
                )
138
            );
139
140
        $dispatcher = new Common\Input\Dispatcher($parsingDispatcher, array('format' => $handler));
141
142
        $this->assertSame(
143
            23,
144
            $dispatcher->parse($message)
145
        );
146
    }
147
148
    public function testParseMediaTypeCharset()
149
    {
150
        $message = new Common\Message(
151
            array(
152
                'Content-Type' => 'text/html+format; version=1.1; charset=UTF-8',
153
                'Url' => '/foo/bar',
154
            ),
155
            'Hello world!'
156
        );
157
158
        $parsingDispatcher = $this->getParsingDispatcherMock();
159
        $parsingDispatcher
160
            ->expects($this->any())
161
            ->method('parse')
162
            ->with($this->anything(), 'text/html; version=1.1');
163
164
        $handler = $this->createMock(Handler::class);
165
        $handler
166
            ->expects($this->any())
167
            ->method('convert')
168
            ->will($this->returnValue(array()));
169
170
        $dispatcher = new Common\Input\Dispatcher($parsingDispatcher, array('format' => $handler));
171
172
        $dispatcher->parse($message);
173
    }
174
}
175