ZohoClientTest::testDao()   C
last analyzed

Complexity

Conditions 7
Paths 64

Size

Total Lines 133
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 133
rs 6.4589
c 0
b 0
f 0
cc 7
eloc 39
nc 64
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
class ZohoClientTest 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...
4
{
5
    public function getClient()
0 ignored issues
show
Coding Style introduced by
getClient 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...
6
    {
7
        return new \Wabel\Zoho\CRM\ZohoClient($GLOBALS['auth_token']);
8
    }
9
10
    public function testGetModules()
11
    {
12
        $zohoClient = $this->getClient();
13
14
        $modules = $zohoClient->getModules();
15
16
        $found = false;
17
        foreach ($modules->getRecords() as $record) {
18
            if ($record['pl'] == 'Leads') {
19
                $found = true;
20
            }
21
        }
22
23
        $this->assertTrue($found);
24
    }
25
26
    public function testGetFields()
27
    {
28
        $zohoClient = $this->getClient();
29
30
        $fields = $zohoClient->getFields('Leads');
31
32
        $this->assertArrayHasKey('Lead Owner', $fields->getRecords()['Lead Information']);
33
    }
34
35
    /**
36
     * @throws Exception
37
     * @throws \Wabel\Zoho\CRM\Exception\ZohoCRMResponseException
38
     */
39
    public function testDao()
40
    {
41
        require_once __DIR__.'/generated/Contact.php';
42
        require_once __DIR__.'/generated/ContactZohoDao.php';
43
44
        $contactZohoDao = new \TestNamespace\ContactZohoDao($this->getClient());
45
46
        // First, let's clean up the records (in case a previous test did not run correctly).
47
        $records = $contactZohoDao->searchRecords('(First Name:TestMultiplePoolUser)');
48
        foreach ($records as $record) {
49
            $contactZohoDao->delete($record->getZohoId());
50
        }
51
52
//
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% 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...
53
//        $lastName = uniqid("Test");
54
//        $email = $lastName."@test.com";
55
//
56
//        $contactBean = new \TestNamespace\Contact();
57
//        $contactBean->setFirstName("Testuser");
58
//        $contactBean->setLastName($lastName);
59
//        // Testing special characters.
60
//        $contactBean->setTitle("M&M's épatant");
61
//
62
//        $contactZohoDao->save($contactBean);
63
//
64
//        $this->assertNotEmpty($contactBean->getZohoId(), "ZohoID must be set in the bean after save.");
65
//
66
//        // Second save (to verify the updateRecords method).
67
//        $contactBean->setEmail($email);
68
//        $contactZohoDao->save($contactBean);
69
//        $record = $contactZohoDao->getById($contactBean->getZohoId());
70
//        $this->assertEquals($record[0]->getEmail(), $email);
71
//
72
//        $multipleContact = [];
73
//        // Now, let's test multiple saves under 100.
74
//        for($i = 0; $i < 98; $i++) {
75
//            $multipleContact["contact"][$i] = new \TestNamespace\Contact();
76
//            $multipleContact["lastName"][$i] = uniqid("Test");
77
//            $multipleContact["email"][$i] = $multipleContact["lastName"][$i]."@test.com";
78
//            $multipleContact["contact"][$i]->setLastName($multipleContact["lastName"][$i]);
79
//            $multipleContact["contact"][$i]->setFirstName("TestMultipleUser");
80
//        }
81
//        $contactZohoDao->save($multipleContact["contact"]);
82
//
83
//
84
//        for($i = 0; $i < 98; $i++) {
85
//            $multipleContact["contact"][$i]->setEmail($multipleContact["email"][$i]);
86
//        }
87
//        $contactZohoDao->save($multipleContact["contact"]);
88
89
        // Now, let's test multiple saves over 100.
90
        $multiplePoolContact = [];
91
        for ($i = 0; $i < 302; ++$i) {
92
            $multiplePoolContact['contact']['key'.$i] = new \TestNamespace\Contact();
93
            $multiplePoolContact['lastName']['key'.$i] = uniqid('Test');
94
            $multiplePoolContact['email']['key'.$i] = $multiplePoolContact['lastName']['key'.$i].'@test.com';
95
            $multiplePoolContact['contact']['key'.$i]->setLastName($multiplePoolContact['lastName']['key'.$i]);
96
            $multiplePoolContact['contact']['key'.$i]->setFirstName('TestMultiplePoolUser');
97
        }
98
        $contactZohoDao->save($multiplePoolContact['contact']);
99
100
        for ($i = 0; $i < 302; ++$i) {
101
            $multiplePoolContact['contact']['key'.$i]->setEmail($multiplePoolContact['email']['key'.$i]);
102
        }
103
        $beforePoolMultiple = new DateTime();
0 ignored issues
show
Unused Code introduced by
$beforePoolMultiple 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...
104
        $contactZohoDao->save($multiplePoolContact['contact']);
105
106
        for ($i = 0; $i < 302; ++$i) {
107
            $this->assertNotNull($multiplePoolContact['contact']['key'.$i]->getZohoId());
108
        }
109
110
        // We need to wait for Zoho to index the record.
111
        sleep(120);
112
113
        // Test if the unique Contact has been well saved and is deleted
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
114
//        $records = $contactZohoDao->searchRecords("(Last Name:$lastName)");
115
//
116
//        $this->assertCount(1, $records);
117
//        foreach ($records as $record) {
118
//            $this->assertInstanceOf("\\TestNamespace\\Contact", $record);
119
//            $this->assertEquals("Testuser", $record->getFirstName());
120
//            $this->assertEquals($lastName, $record->getLastName());
121
//            $this->assertEquals($email, $record->getEmail());
122
//            $this->assertEquals("M&M's épatant", $record->getTitle());
123
//        }
124
//
125
//        $contactZohoDao->delete($contactBean->getZohoId());
126
//
127
//        $records = $contactZohoDao->searchRecords("(Last Name:$lastName)");
128
//        $this->assertCount(0, $records);
129
//
130
//        // Test if the 98 Contacts has been well saved and are deleted
131
//        $records = $contactZohoDao->searchRecords("(First Name:TestMultipleUser)");
132
//
133
//        $this->assertCount(98, $records);
134
//        foreach ($records as $key=>$record) {
135
//            $this->assertInstanceOf("\\TestNamespace\\Contact", $record);
136
//            $this->assertEquals("TestMultipleUser", $record->getFirstName());
137
//            $contactZohoDao->delete($record->getZohoId());
138
//        }
139
//
140
//        $records = $contactZohoDao->searchRecords("(First Name:TestMultipleUser)");
141
//        $this->assertCount(0, $records);
142
143
        $records = $contactZohoDao->searchRecords('(First Name:TestMultiplePoolUser)');
144
        $this->assertCount(302, $records);
145
146
        $modifiedTime = $records[0]->getModifiedTime()->sub(DateInterval::createFromDateString('10 seconds'));
147
148
        // Test if the 302 Contacts has been well saved and are deleted
149
        //$records = $contactZohoDao->getRecords("Modified Time", "asc", $modifiedTime);
0 ignored issues
show
Unused Code Comprehensibility introduced by
65% 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...
150
        $records = $contactZohoDao->getRecords(null, null, $modifiedTime);
151
152
        $beforeDeleteTime = new DateTimeImmutable();
153
154
        $this->assertCount(302, $records);
155
        foreach ($records as $key => $record) {
156
            $this->assertInstanceOf('\\TestNamespace\\Contact', $record);
157
            $this->assertEquals('TestMultiplePoolUser', $record->getFirstName());
158
            $this->assertContains($record->getLastName(), $multiplePoolContact['lastName']);
159
            $this->assertContains($record->getEmail(), $multiplePoolContact['email']);
160
            $contactZohoDao->delete($record->getZohoId());
161
        }
162
163
        $records2 = $contactZohoDao->searchRecords('(First Name:TestMultiplePoolUser)');
164
        $this->assertCount(0, $records2);
165
166
        $deletedRecords = $contactZohoDao->getDeletedRecordIds($beforeDeleteTime->sub(new DateInterval('PT3M')));
167
        // Let's check that each deleted record is present in the deleted record list.
168
        foreach ($records as $record) {
169
            $this->assertContains($record->getZohoId(), $deletedRecords);
170
        }
171
    }
172
173
    /**
174
     * @throws Exception
175
     * @throws \Wabel\Zoho\CRM\Exception\ZohoCRMResponseException
176
     */
177
    public function testDaoUpdateException()
178
    {
179
        require_once __DIR__.'/generated/Contact.php';
180
        require_once __DIR__.'/generated/ContactZohoDao.php';
181
182
        $contactZohoDao = new \TestNamespace\ContactZohoDao($this->getClient());
183
184
        // Now, let's try to update records that do not exist.
185
        $notExistingRecords = array();
186
187
        $contact1 = new \TestNamespace\Contact();
188
        $contact1->setZohoId('123495843958439432');
189
        $contact1->setLastName('I DONT EXIST');
190
        $contact1->setFirstName('I DONT EXIST');
191
        $contact1->setEmail('[email protected]');
192
        $notExistingRecords[] = $contact1;
193
194
        $contact2 = new \TestNamespace\Contact();
195
        $contact2->setZohoId('54349584395439432');
196
        $contact2->setLastName('I DONT EXIST AT ALL');
197
        $contact2->setFirstName('I DONT EXIST AT ALL');
198
        $contact2->setEmail('[email protected]');
199
        $notExistingRecords[] = $contact2;
200
201
        $updateExceptionTriggered = false;
202
        try {
203
            $contactZohoDao->updateRecords($notExistingRecords);
204
        } catch (\Wabel\Zoho\CRM\Exception\ZohoCRMUpdateException $updateException) {
205
            $updateExceptionTriggered = true;
206
            $failedBeans = $updateException->getFailedBeans();
207
            $this->assertTrue($failedBeans->offsetExists($contact1));
208
            $this->assertTrue($failedBeans->offsetExists($contact2));
209
            $innerException = $failedBeans->offsetGet($contact1);
210
            $this->assertEquals('401.2', $innerException->getZohoCode());
211
            $this->assertEquals(2, $failedBeans->count());
212
        }
213
214
        $this->assertTrue($updateExceptionTriggered);
215
    }
216
217
    protected function tearDown()
218
    {
219
        if (class_exists('\TestNamespace\ContactZohoDao')) {
220
            $contactZohoDao = new \TestNamespace\ContactZohoDao($this->getClient());
221
            // Let's end by removing past inserted clients:
222
            $pastContacts = $contactZohoDao->searchRecords('(First Name:TestMultiplePoolUser)');
223
            foreach ($pastContacts as $pastContact) {
224
                $contactZohoDao->delete($pastContact->getZohoId());
225
            }
226
        }
227
    }
228
}
229