Completed
Push — master ( 20a550...cf14de )
by Piotr
03:18
created

IsValidTest   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 222
Duplicated Lines 20.72 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 5
Bugs 0 Features 3
Metric Value
wmc 24
c 5
b 0
f 3
lcom 1
cbo 2
dl 46
loc 222
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A shouldNotThrowExceptionWhenStyleIsValid() 0 7 1
A validStyles() 0 7 1
A shouldThrowExceptionWhenStyleIsInValid() 0 12 2
A shouldNotThrowExceptionWhenUseIsValid() 0 7 1
A validUses() 0 7 1
A shouldThrowExceptionWhenUseIsInValid() 0 12 2
A shouldNotThrowExceptionWhenSoapVersionIsValid() 0 7 1
A validSoapVersions() 0 7 1
A shouldThrowExceptionWhenSoapVersionIsInValid() 0 12 2
A shouldNotThrowExceptionWhenValueIsNotEmpty() 0 7 1
A shouldThrowExceptionWhenValueIsEmpty() 11 12 2
A shouldThrowExceptionWithCustomMessageWhenValueIsEmpty() 11 12 2
A emptyValues() 0 7 1
A shouldNotThrowExceptionWhenParameterStyleIsValid() 0 7 1
A validParameterStyles() 0 7 1
A shouldThrowExceptionWhenParameterStyleIsInValid() 12 12 2
A shouldThrowExceptionWhenSetParameterStyleWrappedForRpc() 12 12 2

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
 * Copyright (C) 2013-2016
4
 * Piotr Olaszewski <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
namespace Tests\WSDL\Builder;
25
26
use InvalidArgumentException;
27
use PHPUnit_Framework_TestCase;
28
use WSDL\Annotation\BindingType;
29
use WSDL\Annotation\SoapBinding;
30
use WSDL\Builder\IsValid;
31
32
/**
33
 * IsValidTest
34
 *
35
 * @author Piotr Olaszewski <[email protected]>
36
 */
37
class IsValidTest extends PHPUnit_Framework_TestCase
38
{
39
    /**
40
     * @test
41
     * @dataProvider validStyles
42
     * @param string $style
43
     */
44
    public function shouldNotThrowExceptionWhenStyleIsValid($style)
45
    {
46
        //when
47
        IsValid::style($style);
48
49
        //then no exception
50
    }
51
52
    public function validStyles()
53
    {
54
        return [
55
            [SoapBinding::RPC],
56
            [SoapBinding::DOCUMENT]
57
        ];
58
    }
59
60
    /**
61
     * @test
62
     */
63
    public function shouldThrowExceptionWhenStyleIsInValid()
64
    {
65
        //when
66
        try {
67
            IsValid::style('INVALID_STYLE');
68
            $this->assertFalse(true, 'Triggered when exception is not throw');
69
        } catch (InvalidArgumentException $e) {
70
            //then
71
            $this->assertEquals('Invalid style [INVALID_STYLE] available styles: [RPC, DOCUMENT]', $e->getMessage());
72
            $this->assertInstanceOf('\InvalidArgumentException', $e);
73
        }
74
    }
75
76
    /**
77
     * @test
78
     * @dataProvider validUses
79
     * @param string $use
80
     */
81
    public function shouldNotThrowExceptionWhenUseIsValid($use)
82
    {
83
        //when
84
        IsValid::useStyle($use);
85
86
        //then no exception
87
    }
88
89
    public function validUses()
90
    {
91
        return [
92
            [SoapBinding::LITERAL],
93
            [SoapBinding::ENCODED]
94
        ];
95
    }
96
97
    /**
98
     * @test
99
     */
100
    public function shouldThrowExceptionWhenUseIsInValid()
101
    {
102
        //when
103
        try {
104
            IsValid::useStyle('INVALID_USE');
105
            $this->assertFalse(true, 'Triggered when exception is not throw');
106
        } catch (InvalidArgumentException $e) {
107
            //then
108
            $this->assertEquals('Invalid use [INVALID_USE] available uses: [LITERAL, ENCODED]', $e->getMessage());
109
            $this->assertInstanceOf('\InvalidArgumentException', $e);
110
        }
111
    }
112
113
    /**
114
     * @test
115
     * @dataProvider validSoapVersions
116
     * @param string $soapVersion
117
     */
118
    public function shouldNotThrowExceptionWhenSoapVersionIsValid($soapVersion)
119
    {
120
        //when
121
        IsValid::soapVersion($soapVersion);
122
123
        //then no exception
124
    }
125
126
    public function validSoapVersions()
127
    {
128
        return [
129
            [BindingType::SOAP_11],
130
            [BindingType::SOAP_12]
131
        ];
132
    }
133
134
    /**
135
     * @test
136
     */
137
    public function shouldThrowExceptionWhenSoapVersionIsInValid()
138
    {
139
        //when
140
        try {
141
            IsValid::soapVersion('INVALID_SOAP_VERSION');
142
            $this->assertFalse(true, 'Triggered when exception is not throw');
143
        } catch (InvalidArgumentException $e) {
144
            //then
145
            $this->assertEquals('Invalid binding type [INVALID_SOAP_VERSION] available types: [SOAP_11, SOAP_12]', $e->getMessage());
146
            $this->assertInstanceOf('\InvalidArgumentException', $e);
147
        }
148
    }
149
150
    /**
151
     * @test
152
     */
153
    public function shouldNotThrowExceptionWhenValueIsNotEmpty()
154
    {
155
        //when
156
        IsValid::notEmpty('some value');
157
158
        //then no exception
159
    }
160
161
    /**
162
     * @test
163
     * @dataProvider emptyValues
164
     * @param mixed $value
165
     */
166 View Code Duplication
    public function shouldThrowExceptionWhenValueIsEmpty($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
167
    {
168
        //when
169
        try {
170
            IsValid::notEmpty($value);
171
            $this->assertFalse(true, 'Triggered when exception is not throw');
172
        } catch (InvalidArgumentException $e) {
173
            //then
174
            $this->assertEquals('Value cannot be empty', $e->getMessage());
175
            $this->assertInstanceOf('\InvalidArgumentException', $e);
176
        }
177
    }
178
179
    /**
180
     * @test
181
     * @dataProvider emptyValues
182
     * @param mixed $value
183
     * @param string $customMessage
184
     */
185 View Code Duplication
    public function shouldThrowExceptionWithCustomMessageWhenValueIsEmpty($value, $customMessage)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
186
    {
187
        //when
188
        try {
189
            IsValid::notEmpty($value, $customMessage);
190
            $this->assertFalse(true, 'Triggered when exception is not throw');
191
        } catch (InvalidArgumentException $e) {
192
            //then
193
            $this->assertEquals($customMessage, $e->getMessage());
194
            $this->assertInstanceOf('\InvalidArgumentException', $e);
195
        }
196
    }
197
198
    public function emptyValues()
199
    {
200
        return [
201
            ['', 'First empty value'],
202
            [null, 'Second empty value']
203
        ];
204
    }
205
206
    /**
207
     * @test
208
     * @dataProvider validParameterStyles
209
     * @param string $parameterStyle
210
     */
211
    public function shouldNotThrowExceptionWhenParameterStyleIsValid($parameterStyle)
212
    {
213
        //when
214
        IsValid::parameterStyle($parameterStyle, SoapBinding::DOCUMENT);
215
216
        //then no exception
217
    }
218
219
    public function validParameterStyles()
220
    {
221
        return [
222
            [SoapBinding::BARE],
223
            [SoapBinding::WRAPPED]
224
        ];
225
    }
226
227
    /**
228
     * @test
229
     */
230 View Code Duplication
    public function shouldThrowExceptionWhenParameterStyleIsInValid()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
231
    {
232
        //when
233
        try {
234
            IsValid::parameterStyle('INVALID_PARAMETER_STYLE', SoapBinding::RPC);
235
            $this->assertFalse(true, 'Triggered when exception is not throw');
236
        } catch (InvalidArgumentException $e) {
237
            //then
238
            $this->assertEquals('Invalid parameter style [INVALID_PARAMETER_STYLE] available parameter styles: [BARE, WRAPPED]', $e->getMessage());
239
            $this->assertInstanceOf('\InvalidArgumentException', $e);
240
        }
241
    }
242
243
    /**
244
     * @test
245
     */
246 View Code Duplication
    public function shouldThrowExceptionWhenSetParameterStyleWrappedForRpc()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
247
    {
248
        //when
249
        try {
250
            IsValid::parameterStyle(SoapBinding::WRAPPED, SoapBinding::RPC);
251
            $this->assertFalse(true, 'Triggered when exception is not throw');
252
        } catch (InvalidArgumentException $e) {
253
            //then
254
            $this->assertEquals('For RPC style parameters cannot be wrapped', $e->getMessage());
255
            $this->assertInstanceOf('\InvalidArgumentException', $e);
256
        }
257
    }
258
}
259