Completed
Push — master ( 28d05b...16bc73 )
by Raphaël
02:44
created

DetectAppointmentsChangingsServiceTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 6
dl 0
loc 136
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
B personalAppointment() 0 36 1
A setUp() 0 7 1
A testDetectChangings() 0 9 1
A testGetListChangings() 0 16 1
A testDetectDeleteOrUpdated() 0 23 1
A testDetectAppointmentsChangings() 0 17 1
1
<?php
2
3
namespace Wabel\CertainAPI\Services;
4
5
6
use PHPUnit\Framework\TestCase;
7
use Wabel\CertainAPI\CertainApiClient;
8
use Wabel\CertainAPI\CertainApiService;
9
use Wabel\CertainAPI\Ressources\AppointmentsCertain;
10
11
class DetectAppointmentsChangingsServiceTest extends TestCase
12
{
13
    /**
14
     * @var CertainApiService
15
     */
16
    private $certainApiService;
17
18
    /**
19
     * @var array[]
20
     */
21
    private $certainAppointmentsList;
22
23
    /**
24
     * @var AppointmentsCertain
25
     */
26
    private $appointmentsCertain;
27
28
    /**
29
     * Personal Object to test
30
     * @return array
31
     */
32
    private function personalAppointment(){
33
        return [
34
                    "appointmentId"=> 52,
35
                    "startDate"=> "2015-16-07 11:00:00",
36
                    "endDate"=> "2015-16-07 11:30:00",
37
                    "eventCode"=> "eventCodeXYZ",
38
                    "pkEventId"=> "pkEventIdXYZ",
39
                    "location"=> "locationXYZ",
40
                    "status"=> "statusXYZ",
41
                    "appointmentType"=> "appointmentTypeXYZ",
42
                    "appointmentRating"=> [
43
                        2,
44
                        1
45
                    ],
46
                    "appointmentSource"=> "AME",
47
                    "registration"=> [
48
                        "regCode"=> "regCodeXYZ",
49
                        "name"=> "nameXYZ",
50
                        "jobTitle"=> "jobTitleXYZ",
51
                        "organization"=> "organizationXYZ",
52
                        "attendeeType"=> "attendeeTypeXYZ",
53
                        "city"=> "cityXYZ",
54
                        "state"=> "stateXYZ"
55
                    ],
56
                    "targetRegistration"=> [
57
                        "regCode"=> "regCodeXYZ",
58
                        "name"=> "nameXYZ",
59
                        "jobTitle"=> "jobTitleXYZ",
60
                        "organization"=> "organizationXYZ",
61
                        "attendeeType"=> "attendeeTypeXYZ",
62
                        "city"=> "cityXYZ",
63
                        "state"=> "stateXYZ"
64
                    ],
65
                    "calendar"=> null
66
                ];
67
    }
68
69
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
70
    {
71
        $certainApiClient =  new CertainApiClient(null,$GLOBALS['username'],$GLOBALS['password'],$GLOBALS['accountCode']);
72
        $this->certainApiService  = new CertainApiService($certainApiClient);
73
        $this->appointmentsCertain = new AppointmentsCertain($this->certainApiService);
74
        $this->certainAppointmentsList = $this->appointmentsCertain->get($GLOBALS['eventCode'],['start_index'=>0,'max_results'=>10])->getResults()->appointments;
75
    }
76
77
    public function testDetectChangings(){
78
        $detectService = new DetectAppointmentsChangingsService($this->appointmentsCertain);
79
        $oldAppointments = $this->certainAppointmentsList;
80
        $currentAppoiments = $oldAppointments;
81
        unset($currentAppoiments[0]);
82
        unset($currentAppoiments[1]);
83
        $hasChanged = $detectService->hasChanged($oldAppointments,$currentAppoiments);
84
        $this->assertTrue($hasChanged);
85
    }
86
87
    public function testGetListChangings(){
88
        $detectService = new DetectAppointmentsChangingsService($this->appointmentsCertain);
89
        $oldAppointments = $this->certainAppointmentsList;
90
        $currentAppoiments = $oldAppointments;
91
        $delete1 = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($currentAppoiments[0]);
92
        $delete2 = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($currentAppoiments[1]);
93
        $delete3 = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($currentAppoiments[4]);
94
        unset($currentAppoiments[0]);
95
        unset($currentAppoiments[1]);
96
        unset($currentAppoiments[4]);
97
        $changings = $detectService->getListChangings($oldAppointments,$currentAppoiments);
98
        $this->assertEquals(3,count($changings));
99
        $this->assertContains($delete1,$changings);
100
        $this->assertContains($delete2,$changings);
101
        $this->assertContains($delete3,$changings);
102
    }
103
104
    public function testDetectDeleteOrUpdated(){
105
        $detectService = new DetectAppointmentsChangingsService($this->appointmentsCertain);
106
        $oldAppointments = $this->certainAppointmentsList;
107
        $oldAppointments[10] = $this->personalAppointment();
108
        $currentAppoiments = $oldAppointments;
109
        $delete1 = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($currentAppoiments[1]);
110
        $delete2 = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($currentAppoiments[4]);
111
        unset($currentAppoiments[1]);
112
        unset($currentAppoiments[4]);
113
        $update1 = $currentAppoiments[10];
114
        $currentAppoiments[10]["startDate"] = "2015-17-07 12:00:00";
115
        $currentAppoiments[10]["endDate"] = "2015-17-07 12:30:00";
116
        $changings = $detectService->getListChangings($oldAppointments,$currentAppoiments);
117
        $listDetected = $detectService->detectDeleteOrUpdated($currentAppoiments,$changings);
118
        $this->assertEquals(3,count($changings));
119
        $this->assertArrayHasKey('deleted',$listDetected);
120
        $this->assertArrayHasKey('updated',$listDetected);
121
        $this->assertEquals(2,count($listDetected['deleted']));
122
        $this->assertEquals(1,count($listDetected['updated']));
123
        $this->assertContains($delete1,$listDetected['deleted']);
124
        $this->assertContains($delete2,$listDetected['deleted']);
125
        $this->assertContains($update1,$listDetected['updated']);
126
    }
127
128
    public function testDetectAppointmentsChangings(){
129
        $detectService = new DetectAppointmentsChangingsService($this->appointmentsCertain);
130
        $oldAppointments = $this->certainAppointmentsList;
131
        $oldAppointments[10] = $this->personalAppointment();
132
        $currentAppoiments = $oldAppointments;
133
        $delete1 = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($currentAppoiments[1]);
134
        $delete2 = DetectAppointmentsChangingsService::recursiveArrayObjectToFullArray($currentAppoiments[4]);
135
        unset($currentAppoiments[1]);
136
        unset($currentAppoiments[4]);
137
        $update1 = $currentAppoiments[10];
138
        $currentAppoiments[10]["startDate"] = "2015-17-07 12:00:00";
139
        $currentAppoiments[10]["endDate"] = "2015-17-07 12:30:00";
140
        $listDetected = $detectService->detectAppointmentsChangings($oldAppointments,$currentAppoiments);
141
        $this->assertContains($delete1,$listDetected['deleted']);
142
        $this->assertContains($delete2,$listDetected['deleted']);
143
        $this->assertContains($update1,$listDetected['updated']);
144
    }
145
146
}