Completed
Pull Request — master (#21)
by Harry
06:22
created

ValidGigyaResponseMiddlewareTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 86
Duplicated Lines 55.81 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 7
dl 48
loc 86
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A testValidResponse() 0 13 1
A testMissingFieldWillThrowAnException() 16 16 1
A testNoBodyWillFail() 16 16 1
A testInvalidBody() 16 16 1
A testUnknownResponseContainsTheOriginalResponse() 0 18 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
 * This file is part of graze/gigya-client
4
 *
5
 * Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/gigya-client/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/gigya-client
12
 */
13
14
namespace Graze\Gigya\Test\Unit\Validation;
15
16
use Graze\Gigya\Exception\UnknownResponseException;
17
use Graze\Gigya\Test\TestCase;
18
use Graze\Gigya\Test\TestFixtures;
19
use Graze\Gigya\Validation\ValidGigyaResponseMiddleware;
20
use GuzzleHttp\Handler\MockHandler;
21
use GuzzleHttp\Psr7\Request;
22
use GuzzleHttp\Psr7\Response;
23
use Mockery as m;
24
use Psr\Http\Message\RequestInterface;
25
26
class ValidGigyaResponseMiddlewareTest extends TestCase
27
{
28
    public function testValidResponse()
29
    {
30
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
31
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
32
                return new Response(200, [], TestFixtures::getFixture('accounts.search_simple'));
33
            },
34
        ]);
35
36
        $m = ValidGigyaResponseMiddleware::middleware();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
37
38
        $f = $m($h);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $f. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
39
        $f(new Request('GET', 'https://foo.com'), [])->wait();
40
    }
41
42 View Code Duplication
    public function testMissingFieldWillThrowAnException()
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...
43
    {
44
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
45
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
                return new Response(200, [], TestFixtures::getFixture('missing_field'));
47
            },
48
        ]);
49
50
        $this->expectException(UnknownResponseException::class);
51
        $this->expectExceptionMessage("The contents of the response could not be determined. Missing required field: 'statusReason'");
52
53
        $m = ValidGigyaResponseMiddleware::middleware();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
54
55
        $f = $m($h);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $f. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
56
        $f(new Request('GET', 'https://foo.com'), [])->wait();
57
    }
58
59 View Code Duplication
    public function testNoBodyWillFail()
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...
60
    {
61
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
62
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
                return new Response(200);
64
            },
65
        ]);
66
67
        $this->expectException(UnknownResponseException::class);
68
        $this->expectExceptionMessage('The contents of the response could not be determined');
69
70
        $m = ValidGigyaResponseMiddleware::middleware();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
71
72
        $f = $m($h);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $f. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
73
        $f(new Request('GET', 'https://foo.com'), [])->wait();
74
    }
75
76 View Code Duplication
    public function testInvalidBody()
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...
77
    {
78
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
79
            function (RequestInterface $request, array $options) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
80
                return new Response(200, [], TestFixtures::getFixture('invalid_json'));
81
            },
82
        ]);
83
84
        $this->expectException(UnknownResponseException::class);
85
        $this->expectExceptionMessage('The contents of the response could not be determined. Could not decode the body');
86
87
        $m = ValidGigyaResponseMiddleware::middleware();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
88
89
        $f = $m($h);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $f. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
90
        $f(new Request('GET', 'https://foo.com'), [])->wait();
91
    }
92
93
    public function testUnknownResponseContainsTheOriginalResponse()
94
    {
95
        $response = new Response(200, [], TestFixtures::getFixture('invalid_json'));
96
        $h = new MockHandler([
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $h. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
97
            function (RequestInterface $request, array $options) use ($response) {
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
98
                return $response;
99
            },
100
        ]);
101
102
        $m = ValidGigyaResponseMiddleware::middleware();
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $m. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
103
104
        $f = $m($h);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $f. Configured minimum length is 2.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
105
        try {
106
            $f(new Request('GET', 'https://foo.com'), [])->wait();
107
        } catch (UnknownResponseException $e) {
108
            $this->assertSame($response, $e->getResponse());
109
        }
110
    }
111
}
112