Completed
Push — master ( e13577...86f857 )
by Harry
15s
created

ValidGigyaResponseSubscriberTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 121
Duplicated Lines 24.79 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 11
c 4
b 2
f 0
lcom 1
cbo 7
dl 30
loc 121
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A tearDown() 0 4 1
A testInstanceOf() 0 4 1
A testGetEvents() 0 7 1
A testValidResponse() 0 10 1
A testNullResponseWillThrowAnException() 0 13 1
A testMissingFieldWillThrowAnException() 15 15 1
A testNoBodyWillFail() 0 15 1
A testInvalidBody() 15 15 1
A testUnknownResponseContainsTheOriginalResponse() 0 14 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\ValidGigyaResponseSubscriber;
20
use GuzzleHttp\Event\CompleteEvent;
21
use GuzzleHttp\Event\RequestEvents;
22
use GuzzleHttp\Event\SubscriberInterface;
23
use Mockery as m;
24
25
class ValidGigyaResponseSubscriberTest extends TestCase
26
{
27
    /**
28
     * @var ValidGigyaResponseSubscriber
29
     */
30
    private $validator;
31
32
    public function setUp()
33
    {
34
        $this->validator = new ValidGigyaResponseSubscriber();
35
    }
36
37
    public function tearDown()
38
    {
39
        $this->validator = null;
40
    }
41
42
    public function testInstanceOf()
43
    {
44
        static::assertInstanceOf(SubscriberInterface::class, $this->validator);
45
    }
46
47
    public function testGetEvents()
48
    {
49
        static::assertEquals(
50
            ['complete' => ['onComplete', RequestEvents::VERIFY_RESPONSE]],
51
            $this->validator->getEvents()
52
        );
53
    }
54
55
    /**
56
     * @throws \Graze\Gigya\Exception\UnknownResponseException
57
     */
58
    public function testValidResponse()
59
    {
60
        $completeEvent = m::mock(CompleteEvent::class);
61
        $response      = m::mock('GuzzleHttp\Message\ResponseInterface');
62
        $completeEvent->shouldReceive('getResponse')
63
                      ->andReturn($response);
64
        $response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('accounts.search_simple'));
65
66
        $this->validator->onComplete($completeEvent, 'name');
0 ignored issues
show
Unused Code introduced by
The call to ValidGigyaResponseSubscriber::onComplete() has too many arguments starting with 'name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
67
    }
68
69
    public function testNullResponseWillThrowAnException()
70
    {
71
        $completeEvent = m::mock(CompleteEvent::class);
72
        $completeEvent->shouldReceive('getResponse')
73
                      ->andReturn(null);
74
75
        static::setExpectedException(
76
            'Graze\Gigya\Exception\UnknownResponseException',
77
            'The contents of the response could not be determined. No response provided'
78
        );
79
80
        $this->validator->onComplete($completeEvent, 'name');
0 ignored issues
show
Unused Code introduced by
The call to ValidGigyaResponseSubscriber::onComplete() has too many arguments starting with 'name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
81
    }
82
83 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...
84
    {
85
        $completeEvent = m::mock(CompleteEvent::class);
86
        $response      = m::mock('GuzzleHttp\Message\ResponseInterface');
87
        $completeEvent->shouldReceive('getResponse')
88
                      ->andReturn($response);
89
        $response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('missing_field'));
90
91
        static::setExpectedException(
92
            'Graze\Gigya\Exception\UnknownResponseException',
93
            "The contents of the response could not be determined. Missing required field: 'statusReason'"
94
        );
95
96
        $this->validator->onComplete($completeEvent, 'name');
0 ignored issues
show
Unused Code introduced by
The call to ValidGigyaResponseSubscriber::onComplete() has too many arguments starting with 'name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
97
    }
98
99
    public function testNoBodyWillFail()
100
    {
101
        $completeEvent = m::mock(CompleteEvent::class);
102
        $response      = m::mock('GuzzleHttp\Message\ResponseInterface');
103
        $completeEvent->shouldReceive('getResponse')
104
                      ->andReturn($response);
105
        $response->shouldReceive('getBody')->andReturn('');
106
107
        static::setExpectedException(
108
            'Graze\Gigya\Exception\UnknownResponseException',
109
            'The contents of the response could not be determined'
110
        );
111
112
        $this->validator->onComplete($completeEvent, 'name');
0 ignored issues
show
Unused Code introduced by
The call to ValidGigyaResponseSubscriber::onComplete() has too many arguments starting with 'name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
113
    }
114
115 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...
116
    {
117
        $completeEvent = m::mock(CompleteEvent::class);
118
        $response      = m::mock('GuzzleHttp\Message\ResponseInterface');
119
        $completeEvent->shouldReceive('getResponse')
120
                      ->andReturn($response);
121
        $response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('invalid_json'));
122
123
        static::setExpectedException(
124
            'Graze\Gigya\Exception\UnknownResponseException',
125
            'The contents of the response could not be determined. Could not decode the body'
126
        );
127
128
        $this->validator->onComplete($completeEvent, 'name');
0 ignored issues
show
Unused Code introduced by
The call to ValidGigyaResponseSubscriber::onComplete() has too many arguments starting with 'name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
129
    }
130
131
    public function testUnknownResponseContainsTheOriginalResponse()
132
    {
133
        $completeEvent = m::mock(CompleteEvent::class);
134
        $response      = m::mock('GuzzleHttp\Message\ResponseInterface');
135
        $completeEvent->shouldReceive('getResponse')
136
                      ->andReturn($response);
137
        $response->shouldReceive('getBody')->andReturn(TestFixtures::getFixture('invalid_json'));
138
139
        try {
140
            $this->validator->onComplete($completeEvent, 'name');
0 ignored issues
show
Unused Code introduced by
The call to ValidGigyaResponseSubscriber::onComplete() has too many arguments starting with 'name'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
141
        } catch (UnknownResponseException $e) {
142
            static::assertSame($response, $e->getResponse());
143
        }
144
    }
145
}
146