FacadeTest::generateRandomResourceId()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright 2017 OpenStack Foundation
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 * http://www.apache.org/licenses/LICENSE-2.0
8
 * Unless required by applicable law or agreed to in writing, software
9
 * distributed under the License is distributed on an "AS IS" BASIS,
10
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
 * See the License for the specific language governing permissions and
12
 * limitations under the License.
13
 **/
14
15
use OutlookRestClient\IOutlookRestClient;
16
use OutlookRestClient\Facade\OutlookRestClient;
17
use OutlookRestClient\Facade\Requests\CalendarVO;
18
use OutlookRestClient\Facade\Requests\EventVO;
19
use OutlookRestClient\Facade\Requests\LocationVO;
20
use OutlookRestClient\Facade\Requests\AddressVO;
21
/**
22
 * Class FacadeTest
23
 */
24
final class FacadeTest 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...
25
{
26
    /**
27
     * @var IOutlookRestClient
28
     */
29
    private static $client;
30
31
    private static function generateRandomResourceId(){
32
        return md5(uniqid(mt_rand(), true));
33
    }
34
35
    public static function setUpBeforeClass()
36
    {
37
        $dotenv = new Dotenv\Dotenv(__DIR__.'/../');
38
        $dotenv->load();
39
        self::$client = new OutlookRestClient();
40
        self::$client->setTokenCallback(function($access_token){
0 ignored issues
show
Unused Code introduced by
The parameter $access_token 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...
41
            echo "renewed accesss token!";
42
        });
43
        $access_token = <<< JSON
44
{"token_type":"Bearer","scope":"https:\/\/outlook.office.com\/User.Read https:\/\/outlook.office.com\/Calendars.ReadWrite","ext_expires_in":0,"id_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IjFMVE16YWtpaGlSbGFfOHoyQkVKVlhlV01xbyJ9.eyJ2ZXIiOiIyLjAiLCJpc3MiOiJodHRwczovL2xvZ2luLm1pY3Jvc29mdG9ubGluZS5jb20vOTE4ODA0MGQtNmM2Ny00YzViLWIxMTItMzZhMzA0YjY2ZGFkL3YyLjAiLCJzdWIiOiJBQUFBQUFBQUFBQUFBQUFBQUFBQUFGbzdMX1c4Y0FyM0ZyWWtSVmhJd3dRIiwiYXVkIjoiNDU5Mzg1MzUtYzk2Ni00NGI5LTlmOGYtMmMzZDQzMmRkMzAyIiwiZXhwIjoxNTAxNzc0Mjc4LCJpYXQiOjE1MDE2ODc1NzgsIm5iZiI6MTUwMTY4NzU3OCwibmFtZSI6IlNlYmFzdGlhbiBNYXJjZXQiLCJwcmVmZXJyZWRfdXNlcm5hbWUiOiJ0ZXN0MTU3NUBob3RtYWlsLmNvbSIsIm9pZCI6IjAwMDAwMDAwLTAwMDAtMDAwMC0zZjk1LTkxNzI5ZDQ5MGQ4OSIsInRpZCI6IjkxODgwNDBkLTZjNjctNGM1Yi1iMTEyLTM2YTMwNGI2NmRhZCIsImFpbyI6IkRjZmdYUndYSVU0UEw3NUdBQ2VYNml1MGhFQm9yVk4qVFpJVFNVSWNhSXVTMVg3a29tOU1BczBNVCo4NGlOWnQqN3RyR05tT1MyaVlBNTJXNHRCbWU1MDhHYW42azZlaDNEUGVDWEI0OEshUiJ9.kF06NqO4o30l8wZr6ESn7Aw-s0JYA_awHtrQw-nS15M6VtvR0aa9aHi2wprZQWkHWo0YjukPY8GnSwibH7WHEw-CAWoE0AH38EmE1Qw_pMDaH4c23DpS1icwE4jgrwI-DRh98gi2orcLWCNdNmm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dagpJi+dPQJTzYIhZjl+6HPiifhaX5pb6ETMNSyIrIJIwHHSxf1hyfk6KfZknp5Qlrw03\/qQTqt+2rtvTV7CjGgXc6iyhRWCTjlJuyWSA1VPzKb0MeHk1eXiI558\/TsmJWQjprIou0HPBlWbl2rYbMOgbvbyedpbYovC2Yg5IyDm8tVOXlxTFOgnxCgI=","refresh_token":"MCcRLzpTOFvb26kzx2rvZbYpY6kJ1RgdTW7XdWoeTVfNqMGc1gOeHzY2I43cp04zvktAgd!0ryOXuw!RPAUqWy8yntWMuICY5vZC!lCTbnEcln8EnH8ILmgCnPx*fKoMzwp9r2RyEvKywZuNZuAYZ2LeEngz5jZqmQqiHSj38gwZc4aWOePmKKqOHnkGGKZHNwP2SYletg!9lWTmA0QwR8KJBI7hhrt*1fUS7Hdtk6AAJdVH2XtHL3fdK1RynMlASvO6vccW7O2wssp6h031aUyAKCElka2Z3bReARDUA9Pzp9mXHDd6hz6do3!m3zcezrydPHDwzkH02p*ADCppcoXloJAwbD!mXIKh7X0W!dg3ffSZhuRdctsVgtT9LbEB5KEkXcpBcKR6nfAKh5yLzSTusk1FkxClShuAaTi2eqZFvrMtbUqMbKPwiWLehv!Y9I*60DLfMmSciRDMRCv2skqhuXVzqS9e0X483*F1YH!lN","expires":1501691484}
45
JSON;
46
        self::$client->setAccessToken(json_decode($access_token, true));
47
    }
48
49
    public static function tearDownAfterClass()
50
    {
51
        // do sth after the last test
52
    }
53
54
    public function testCreateCalendar($name = null){
55
        if(empty($name)) $name = self::generateRandomResourceId();
56
        $res = self::$client->createCalendar(new CalendarVO($name));
57
        $id = $res->getId();
58
        $this->assertTrue(!empty($id));
59
        echo printf("new calendar id %s", $id).PHP_EOL;
60
        return $id;
61
    }
62
63
    public function testDeleteCalendar(){
64
        $id  = $this->testCreateCalendar();
65
        // https://stackoverflow.com/questions/31923669/office-365-unified-api-error-when-deleting-a-calendar
66
        // https://stackoverflow.com/questions/44597230/office365-calendar-rest-api-cannot-delete-calendars
67
        //self::$client->updateCalendar($id, new CalendarVO("deleted!"));
0 ignored issues
show
Unused Code Comprehensibility introduced by
77% 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...
68
        $res = self::$client->deleteCalendar($id);
69
70
        $this->assertTrue($res);
71
    }
72
73
    public function testAddNewEvent(){
74
        $id  = $this->testCreateCalendar();
75
76
        $res = self::$client->createEvent($id, new EventVO(
77
            "test event",
78
            "test event body",
79
            new DateTime('2017-11-01 09:00:00'),
80
            new DateTime('2017-11-01 10:30:00'),
81
            new DateTimeZone('Australia/Sydney'),
82
            new LocationVO(
83
                "Boston Marriott Copley Place",
84
                new AddressVO(
85
                    "110 Huntington Av",
86
                    "Boston",
87
                    "MA",
88
                    "USA",
89
                    "02116"
90
                ),
91
                "42.3471832",
92
                "-71.0778024"
93
            )
94
        ));
95
96
        $this->assertTrue($res instanceof \OutlookRestClient\Facade\Responses\EventResponse);
97
        $this->assertTrue(!empty($res->getId()));
98
99
        return $res->getId();
100
    }
101
102
    public function testGetCalendars(){
103
        $res = self::$client->getCalendars();
104
        $this->assertTrue(count($res->getEntries()) > 0);
0 ignored issues
show
Bug introduced by
The method getEntries does only exist in OutlookRestClient\Facade...endarCollectionResponse, but not in OutlookRestClient\Facade\Responses\ErrorResponse.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
105
    }
106
107
    public function testDeleteNewEvent(){
108
        $id  = $this->testAddNewEvent();
109
110
        $res = self::$client->deleteEvent($id);
111
112
        $this->assertTrue($res);
113
    }
114
}