| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace SergeyNezbritskiy\Tests\Integration\Models\ContactPerson; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Exception; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use SergeyNezbritskiy\NovaPoshta\Models\ContactPerson; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use SergeyNezbritskiy\NovaPoshta\Models\Counterparty; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use SergeyNezbritskiy\NovaPoshta\NovaPoshtaApiException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use SergeyNezbritskiy\NovaPoshta\Tests\UsesConnectionTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use function count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class CrudTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     use UsesConnectionTrait; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      * Counterparty Ref | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     private const COUNTERPARTY_REF = '98d078a0-e096-11e6-8ba8-005056881c6b'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private ContactPerson $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     private Counterparty $counterpartyModel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected function setUp(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $connection = $this->getConnection(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $this->model = new ContactPerson($connection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $this->counterpartyModel = new Counterparty($connection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @throws NovaPoshtaApiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @throws Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     public function testContactPersonCrud(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $sfx = $this->randomString(5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $counterparty = $this->counterpartyModel->savePrivatePerson([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |             'FirstName' => 'Іван' . $sfx, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             'MiddleName' => 'Іванович' . $sfx, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             'LastName' => 'Іванов' . $sfx, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             'Email' => 'ivan.ivanov.' . substr(uniqid(), 0, 5) . '@nova.poshta.test', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             'Phone' => rand(380000000000, 380000999999), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |             'CounterpartyProperty' => 'Recipient', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         if (filter_var(getenv('CLEAR_CONTACT_PERSONS'), FILTER_VALIDATE_BOOL)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             $this->clearContactPersons($counterparty); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         //create counterparty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $contactPersonData = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             'FirstName' => 'Петро' . $sfx, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             'MiddleName' => 'Петрович' . $sfx, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             'LastName' => 'Петров' . $sfx, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |             'Email' => 'petro.petrov.' . substr(uniqid(), 0, 5) . '@nova.poshta.test', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             'Phone' => rand(380000000000, 380000999999), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $contactPerson = $this->model->save($counterparty['Ref'], $contactPersonData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         $this->assertContactPerson($counterparty, $contactPerson, ['Email', 'MiddleName']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         //update contact person | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $contactPersonData['Ref'] = $contactPerson['Ref']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $contactPersonData['MiddleName'] = 'Сидорович' . $sfx; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         $contactPersonUpdated = $this->model->update($counterparty['Ref'], $contactPersonData); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $this->assertContactPerson($counterparty, $contactPersonUpdated, ['Ref', 'MiddleName', 'Email']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         //delete contact person | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         $this->model->delete($contactPerson['Ref']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         $this->assertContactPersonMissing($counterparty['Ref']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         //delete counterparty contact person | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->model->delete($contactPerson['Ref']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param array $counterparty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param array $expectedResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param array $compareAttributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @throws NovaPoshtaApiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     private function assertContactPerson(array $counterparty, array $expectedResult, array $compareAttributes): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $page = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             $contactPersons = $this->counterpartyModel->getCounterpartyContactPersons($counterparty['Ref'], $page++); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             foreach ($contactPersons as $contactPerson) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 if ($this->ensureContactPerson($contactPerson, $expectedResult, $compareAttributes)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                     $this->assertTrue(true, 'Imitate that we did an assertion'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } while (count($contactPersons) > 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->fail('Contact person not found'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param string $ref | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @throws NovaPoshtaApiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     private function assertContactPersonMissing(string $ref): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $page = 1; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $result = $this->counterpartyModel->getCounterpartyContactPersons($ref, $page++); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             foreach ($result as $contactPerson) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |                 if ($this->ensureContactPerson($contactPerson, ['Ref' => $ref], ['Ref'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |                     $this->fail('Assert failed that counterparty `' . $ref . '` has been deleted'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } while (count($result) > 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->assertTrue(true, 'Imitate that we did an assertion'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @param array $contactPerson | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      * @param array $expectedResult | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @param array $compareAttributes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     private function ensureContactPerson(array $contactPerson, array $expectedResult, array $compareAttributes): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         foreach ($compareAttributes as $attribute) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             if ($contactPerson[$attribute] !== $expectedResult[$attribute]) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @throws Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |     private function randomString($length = 10): string | 
            
                                                                        
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |         $characters = 'абвгґдеєжзиіїйклмнопрстуфхцчшщьюяАБВГҐДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯ'; | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |         $charactersLength = mb_strlen($characters); | 
            
                                                                        
                            
            
                                    
            
            
                | 145 |  |  |         $randomString = ''; | 
            
                                                                        
                            
            
                                    
            
            
                | 146 |  |  |         for ($i = 0; $i < $length; $i++) { | 
            
                                                                        
                            
            
                                    
            
            
                | 147 |  |  |             $symbolPosition = random_int(0, $charactersLength - 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 148 |  |  |             $randomString .= mb_substr($characters, $symbolPosition, 1); | 
            
                                                                        
                            
            
                                    
            
            
                | 149 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 150 |  |  |         return $randomString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param array $counterparty | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @throws NovaPoshtaApiException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     private function clearContactPersons(array $counterparty): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |             $result = $this->counterpartyModel->getCounterpartyContactPersons($counterparty['Ref']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             foreach ($result as $contactPerson) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |                 $this->model->delete($contactPerson['Ref']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |                 $template = 'Contact person %s %s has been deleted' . PHP_EOL; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 echo sprintf($template, $contactPerson['FirstName'], $contactPerson['LastName']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |         } while (count($result) > 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 169 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 170 |  |  |  |