Completed
Pull Request — develop (#24)
by Ben
06:57 queued 04:18
created

BridgeTest   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 139
Duplicated Lines 75.54 %

Coupling/Cohesion

Components 2
Dependencies 10

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 10
dl 105
loc 139
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testErroneousStatusCode() 18 18 3
A testErrorOnReturnBinary() 15 15 2
A testSearch() 12 12 1
A testSearchCompressedResponse() 12 12 1
A testSearchRefinements() 13 13 1
A testMultipleFailingCalls() 19 19 2
A testSomeFailingCalls() 16 16 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
use GroupByInc\API\AbstractBridge;
4
use GroupByInc\API\Bridge;
5
use GroupByInc\API\Model\RefinementsResult;
6
use GroupByInc\API\Model\Results;
7
use GroupByInc\API\Query;
8
use GroupByInc\API\Util\StringUtils;
9
use Httpful\Request;
10
use Httpful\Response;
11
12
class BridgeTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
{
14
    const CLIENT_KEY = 'randomkey';
15
    const HOST = 'localhost';
16
    const PORT = 8080;
17
    const SEARCH_URL = 'http://localhost:8080/search';
18
    const REFINEMENT_SEARCH_URL = 'http://localhost:8080/search/refinements';
19
    const HEADERS = "Status 200\r\nContent-Type:application/json\n";
20
    // Must match expected bridge json
21
    const TEST_QUERY = '{"clientKey":"randomkey","sort":[],"fields":[],"orFields":[],"refinements":[],"customUrlParams":[],"skip":0,"pageSize":10,"pruneRefinements":true,"wildcardSearchEnabled":false}';
22
    const TEST_RESPONSE = '{"query":"foobar","pageInfo":{"recordStart":14,"recordEnd":23}}';
23
24 View Code Duplication
    public function testErroneousStatusCode()
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...
25
    {
26
        $bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT);
27
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0)
28
            ->thenReturn(new Response('{"foo":"bar"}', 'Status 400', Request::post('')));
29
30
        $query = new Query();
31
        try {
32
            /** @var Bridge $bridge */
33
            $bridge->search($query);
34
            $this->fail("Should have thrown exception here");
35
        } catch (RuntimeException $e) {
36
            if (strpos($e->getMessage(), '404 Not Found') !== false) {
37
                $this->fail("Expected status code 400, found 404");
38
            }
39
            // Should throw exception
40
        }
41
    }
42
43 View Code Duplication
    public function testErrorOnReturnBinary()
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...
44
    {
45
        $bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT);
46
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0)
47
            ->thenReturn(new Response('{"foo":"bar"}', "Status 200\r\nContent-Type:application/bson\n", Request::post('')));
48
49
        $query = new Query();
50
        try {
51
            /** @var Bridge $bridge */
52
            $bridge->search($query);
53
            $this->fail("Should have thrown exception here");
54
        } catch (RuntimeException $e) {
55
            // Should throw exception
56
        }
57
    }
58
59 View Code Duplication
    public function testSearch()
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
        $bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT);
62
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0)
63
            ->thenReturn(new Response('{"query":"foobar"}', self::HEADERS, Request::post('')));
64
65
        $query = new Query();
66
        /** @var Bridge $bridge */
67
        /** @var Results $results */
68
        $results = $bridge->search($query);
0 ignored issues
show
Bug introduced by
The method search() does not seem to exist on object<Phake_IMock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
        $this->assertEquals('foobar', $results->getQuery());
70
    }
71
72 View Code Duplication
    public function testSearchCompressedResponse()
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...
73
    {
74
        $bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT);
75
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0)
76
            ->thenReturn(new Response('{"query":"foobar"}', self::HEADERS . "Content-Encoding:gzip\n", Request::post('')));
77
78
        $query = new Query();
79
        /** @var Bridge $bridge */
80
        /** @var Results $results */
81
        $results = $bridge->search($query);
0 ignored issues
show
Bug introduced by
The method search() does not seem to exist on object<Phake_IMock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
82
        $this->assertEquals('foobar', $results->getQuery());
83
    }
84
85 View Code Duplication
    public function testSearchRefinements()
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...
86
    {
87
        $refinementsQuery = '{"originalQuery":' . self::TEST_QUERY . ',"navigationName":"height"}';
88
        $bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT);
89
        Phake::when($bridge)->execute(self::REFINEMENT_SEARCH_URL, $refinementsQuery, 0)
90
            ->thenReturn(new Response('{"navigation":{"name":"foobar"}}', self::HEADERS, Request::post('')));
91
92
        $query = new Query();
93
        /** @var Bridge $bridge */
94
        /** @var RefinementsResult $results */
95
        $results = $bridge->refinements($query, "height");
0 ignored issues
show
Bug introduced by
The method refinements() does not seem to exist on object<Phake_IMock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
96
        $this->assertEquals('foobar', $results->getNavigation()->getName());
97
    }
98
99 View Code Duplication
    public function testMultipleFailingCalls()
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...
100
    {
101
        $bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT);
102
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0)
103
            ->thenThrow(new Exception('the request failed!'));
104
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 1)
105
            ->thenThrow(new Exception('the request failed!'));
106
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 2)
107
            ->thenThrow(new Exception('the request failed!'));
108
109
        $query = new Query();
110
        /** @var Bridge $bridge */
111
        try {
112
            $bridge->search($query);
113
            $this->fail('expected a "retry failed" exceptions but got none');
114
        } catch (Exception $e) {
115
            $this->assertTrue(StringUtils::endsWith($e->getMessage(), 'failed after ' . AbstractBridge::MAX_TRIES . ' tries'));
116
        }
117
    }
118
119 View Code Duplication
    public function testSomeFailingCalls()
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...
120
    {
121
        $bridge = Phake::partialMock('GroupByInc\API\Bridge', self::CLIENT_KEY, self::HOST, self::PORT);
122
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 0)
123
            ->thenThrow(new Exception('the request failed!'));
124
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 1)
125
            ->thenThrow(new Exception('the request failed!'));
126
        Phake::when($bridge)->execute(self::SEARCH_URL, self::TEST_QUERY, 2)
127
            ->thenReturn(new Response('{"query":"foobar"}', self::HEADERS, Request::post('')));
128
129
        $query = new Query();
130
        /** @var Bridge $bridge */
131
        /** @var Results $results */
132
        $results = $bridge->search($query);
0 ignored issues
show
Bug introduced by
The method search() does not seem to exist on object<Phake_IMock>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
133
        $this->assertNotNull($results);
134
    }
135
136
//    public function testClusterSearch()
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
137
//    {
138
//        $http = Mockery::mock('API\Util\AbstractRequest');
139
//        $http->shouldReceive('setOption');
140
//        $http->shouldReceive('getHttpStatusCode')->andReturn(200);
141
//        $http->shouldReceive('execute')->andReturn(true);
142
//        $http->shouldReceive('getHttpContentType')->andReturn('application/json');
143
//        //create fake bridge with empty query, assign the mock http session, try to search, should not get exception
144
//        $myBridge = new Bridge('randomkey', 'somewhere', 9050);
145
//        $myQuery = new Query();
146
//        /** @noinspection PhpParamsInspection */
147
//        $myBridge->setSessionCluster($http);
148
//        $myBridge->searchCluster($myQuery);
149
//    }
150
}
151