| Conditions | 7 |
| Paths | 64 |
| Total Lines | 133 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
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:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 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 | // |
||
| 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(); |
||
| 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 |
||
| 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); |
||
| 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 | |||
| 229 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.