Completed
Push — master ( f2d606...ed3e7f )
by Oleg
02:59
created

PlanTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testGet() 0 38 1
A testIntegration() 0 16 2
1
<?php
2
namespace OptimizelyPHPTest\Service\v2;
3
4
use OptimizelyPHPTest\Service\v2\BaseServiceTest;
5
use WebMarketingROI\OptimizelyPHP\OptimizelyApiClient;
6
use WebMarketingROI\OptimizelyPHP\Exception;
7
use WebMarketingROI\OptimizelyPHP\Result;
8
use WebMarketingROI\OptimizelyPHP\Resource\v2\Plan as PlanResource;
9
use WebMarketingROI\OptimizelyPHP\Service\v2\Plan;
10
11
class PlanTest extends BaseServiceTest
12
{
13
    public function testGet()
14
    {
15
        // Mock 'OptimizelyApiClient' object to avoid real API calls
16
        $optimizelyApiClientMock = $this->getMockBuilder('\WebMarketingROI\OptimizelyPHP\OptimizelyApiClient')
17
                            ->disableOriginalConstructor()
18
                            ->getMock();
19
                
20
        $result = new Result(array(
21
                            'account_id' => '54321',
22
                            'plan_name' => 'premium',
23
                            'status' => 'active',
24
                            'unit_of_measurement' => 'Impressions',
25
                            'product_usages' => array(
26
                                array(
27
                                    'allocation_term_in_months' => 6,
28
                                    'end_time' => '2019-12-31',
29
                                    'last_update_time' => '2019-06-01',
30
                                    'overage_cents_per_visitor' => 50,
31
                                    'product_name' => 'Product Name',
32
                                    'projects' => array(12345 => 10),
33
                                    'start_time' => '2019-06-01',
34
                                    'usage' => 1,
35
                                    'usage_allowance' => 5,
36
                                ),
37
                            ),                     
38
                        ), 200);
39
        
40
        $optimizelyApiClientMock->method('sendApiRequest')
41
                    ->willReturn($result);
42
        
43
        $planService = new Plan($optimizelyApiClientMock);
44
        
45
        $result = $planService->get();
46
        $plan = $result->getPayload();
47
        
48
        $this->assertTrue($plan instanceof PlanResource);
49
        $this->assertTrue($plan->getPlanName()=='premium');        
50
    }
51
    
52
    public function testIntegration()
53
    {
54
        if (!getenv('OPTIMIZELY_PHP_TEST_INTEGRATION')) 
55
            $this->markTestSkipped('OPTIMIZELY_PHP_TEST_INTEGRATION env var is not set');
56
        
57
        $credentials = $this->loadCredentialsFromFile();
58
        
59
        $optimizelyClient = new OptimizelyApiClient($credentials, 'v2');
0 ignored issues
show
Bug introduced by
It seems like $credentials defined by $this->loadCredentialsFromFile() on line 57 can also be of type null; however, WebMarketingROI\Optimize...piClient::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
        $this->assertTrue($optimizelyClient!=null);
61
       
62
        // Retrieve plan        
63
        $result = $optimizelyClient->plan()->get();        
0 ignored issues
show
Documentation Bug introduced by
The method plan 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...
64
        $plan = $result->getPayload();
65
        
66
        $this->assertTrue($plan instanceof PlanResource);
67
    }
68
}
69
70
71