Completed
Push — master ( 8b4718...61d59f )
by Oleg
02:27
created

testSetApiVersion_WrongVer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
1
<?php
2
namespace OptimizelyPHPTest;
3
4
use PHPUnit_Framework_TestCase;
5
use WebMarketingROI\OptimizelyPHP\OptimizelyApiClient;
6
use WebMarketingROI\OptimizelyPHP\Service\v2\Experiments;
7
use WebMarketingROI\OptimizelyPHP\Exception;
8
9
class OptimizelyApiClientTest extends PHPUnit_Framework_TestCase
10
{
11
    public function testCreateClient()
12
    {
13
        $authCredentials = array(
14
            'access_token' => '1234567890'
15
        );
16
        $client = new OptimizelyApiClient($authCredentials, 'v2');
17
        
18
        $this->assertNotNull($client);
19
    }    
20
    
21
    /**
22
     * @expectedException WebMarketingROI\OptimizelyPHP\Exception
23
     */
24
    public function testCreateClientWrongCredentialFormat()
25
    {
26
        $authCredentials = '1234567';
27
        
28
        $client = new OptimizelyApiClient($authCredentials);        
0 ignored issues
show
Documentation introduced by
$authCredentials is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$client is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
29
    }
30
    
31
    /**
32
     * @expectedException Exception
33
     */
34
    public function testCreateClientWrongApiVersion()
35
    {
36
        $authCredentials = array(
37
            'access_token' => '1234567890'
38
        );
39
        $client = new OptimizelyApiClient($authCredentials, 'v1');        
0 ignored issues
show
Unused Code introduced by
$client is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
40
    }
41
    
42
    public function testCallService()
43
    {
44
        $authCredentials = array(
45
            'access_token' => '1234567890'
46
        );
47
        $client = new OptimizelyApiClient($authCredentials);        
48
        
49
        $experiments = $client->experiments();
0 ignored issues
show
Documentation Bug introduced by
The method experiments does not exist on object<WebMarketingROI\O...HP\OptimizelyApiClient>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
50
        
51
        $this->assertNotNull($experiments);
52
        $this->assertTrue($experiments instanceof Experiments);
53
    }
54
    
55
    /**
56
     * @expectedException Exception
57
     */
58
    public function testSendApiRequestThrowsException()
59
    {
60
        $authCredentials = array(
61
            'access_token' => '1234567890'
62
        );
63
        $client = new OptimizelyApiClient($authCredentials);        
64
        
65
        // Access token is not a real one, so it should throw an exception
66
        $response = $client->sendApiRequest('/experiments');
67
        print_r($response);
68
    }
69
    
70
    public function testGetApiVersion()
71
    {
72
        $authCredentials = array(
73
            'access_token' => '1234567890'
74
        );
75
        $client = new OptimizelyApiClient($authCredentials);        
76
        
77
        $apiVersion = $client->getApiVersion();
78
        $this->assertEquals('v2', $apiVersion);
79
        
80
        $client->setApiVersion('v2');
81
        $this->assertEquals('v2', $client->getApiVersion());
82
    }
83
    
84
    /**
85
     * @expectedException Exception
86
     */
87
    public function testSetApiVersion_WrongVer()
88
    {
89
        $authCredentials = array(
90
            'access_token' => '1234567890'
91
        );
92
        $client = new OptimizelyApiClient($authCredentials);        
93
        
94
        $client->setApiVersion('v1');        
95
    }
96
    
97
    public function testSetAuthCredentials()
98
    {
99
        $authCredentials = array(
100
            'client_id' => '34523242342423424',
101
            'client_secret' => '24352342413131234636',
102
            'refresh_token' => '0345932524',
103
            'access_token' => '1234567890',            
104
        );
105
        $client = new OptimizelyApiClient($authCredentials);        
106
        
107
        $this->assertEquals($authCredentials, $client->getAuthCredentials());
108
        
109
        $authCredentials2 = array(
110
            'access_token' => '4252023452342134'
111
        );
112
        
113
        $client->setAuthCredentials($authCredentials2);
114
        
115
        $this->assertEquals($authCredentials2, $client->getAuthCredentials());        
116
    }
117
    
118
    /**
119
     * @expectedException Exception
120
     */
121
    public function testSetAuthCredentials_InvalidArg()
122
    {
123
        $client = new OptimizelyApiClient(1234);        
0 ignored issues
show
Documentation introduced by
1234 is of type integer, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Unused Code introduced by
$client is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
124
    }
125
    
126
    /**
127
     * @expectedException Exception
128
     */
129
    public function testSetAuthCredentials_EmptyArray()
130
    {
131
        $client = new OptimizelyApiClient(array());        
0 ignored issues
show
Unused Code introduced by
$client is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
132
    }
133
    
134
    public function testGetAccessToken()
135
    {
136
        $authCredentials = array(
137
            'access_token' => '1234567890',
138
            'access_token_timestamp' => time(),
139
            'expires_in' => 7200,
140
            'token_type' => 'bearer'
141
        );
142
        $client = new OptimizelyApiClient($authCredentials);        
143
        
144
        $accessToken = $client->getAccessToken();
145
        $this->assertEquals('1234567890', $accessToken['access_token']);
146
        $this->assertEquals('bearer', $accessToken['token_type']);
147
    }
148
    
149
    public function testGetAccessTokenByRefreshToken()
150
    {
151
        $authCredentials = array(
152
            'client_id' => '23423423423424242423424',
153
            'client_secret' => '2323424234242424234',
154
            'refresh_token' => '24242424244523452gf234',
155
            'access_token' => '1234567890234234234234',
156
            'access_token_timestamp' => time() - 8000,
157
            'expires_in' => 7200,
158
            'token_type' => 'bearer'
159
        );
160
        $client = new OptimizelyApiClient($authCredentials);        
161
        
162
        // Issuing sendApiRequest() method will try to get access token with 
163
        // refresh token, since the provided access token is invalid.
164
        try {
165
            $result = $client->sendApiRequest('/projects');        
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
166
        }
167
        catch (Exception $e) {
168
            $this->assertEquals(Exception::CODE_API_ERROR, $e->getCode());
169
            $this->assertEquals(400, $e->getHttpCode());            
170
        }
171
    }
172
    
173
    public function testGetDiagnosticsInfo()
174
    {
175
        $authCredentials = array(
176
            'access_token' => '1234567890'
177
        );
178
        $client = new OptimizelyApiClient($authCredentials);        
179
        
180
        $diagInfo = $client->getDiagnosticsInfo();
181
        $this->assertTrue(is_array($diagInfo));
182
    }
183
}
184