diff --git a/src/Contact/ContactService.php b/src/Contact/ContactService.php index 6b3c468..eb1b187 100644 --- a/src/Contact/ContactService.php +++ b/src/Contact/ContactService.php @@ -205,7 +205,7 @@ private function prepareParams(AddContactCommand $addContactCommand, ContactCust unset($params['name']); } - if (!empty($addContactCommand->getDayOfCycle())) { + if (null !== $addContactCommand->getDayOfCycle()) { $params['dayOfCycle'] = $addContactCommand->getDayOfCycle(); } diff --git a/tests/Generator.php b/tests/Generator.php index 7402026..202343f 100644 --- a/tests/Generator.php +++ b/tests/Generator.php @@ -232,9 +232,10 @@ private static function createHistoricalOrderCollection() /** * @param string $name + * @param int $dayOfCycle * @return AddContactCommand */ - public static function createAddContactCommand($name = 'Adam Kowalski') + public static function createAddContactCommand($name = 'Adam Kowalski', $dayOfCycle = 3) { $customFieldCollection = new ContactCustomFieldsCollection(); $customFieldCollection->add(new ContactCustomField('id_1', 'value_1')); @@ -244,7 +245,7 @@ public static function createAddContactCommand($name = 'Adam Kowalski') 'adam.kowalski@getresponse.com', $name, 'contactListId', - 3, + $dayOfCycle, $customFieldCollection, 'origin' ); diff --git a/tests/Unit/Domain/Contact/ContactServiceTest.php b/tests/Unit/Domain/Contact/ContactServiceTest.php index 1b36d5d..5280b6d 100644 --- a/tests/Unit/Domain/Contact/ContactServiceTest.php +++ b/tests/Unit/Domain/Contact/ContactServiceTest.php @@ -8,9 +8,9 @@ use GrShareCode\Contact\ContactNotFoundException; use GrShareCode\Contact\ContactService; use GrShareCode\GetresponseApiClient; +use GrShareCode\GetresponseApiException; use GrShareCode\Tests\Generator; use GrShareCode\Tests\Unit\BaseTestCase; -use PHPUnit\Framework\TestCase; /** * Class ContactServiceTest @@ -75,7 +75,7 @@ public function shouldThrowExceptionWhenClientNotExists() * @dataProvider validAddContactProvider * @param $addContactCommand * @param $params - * @throws \GrShareCode\GetresponseApiException + * @throws GetresponseApiException */ public function shouldCreateContact($addContactCommand, $params) { @@ -289,6 +289,83 @@ public function shouldCreateContactWithOriginIfCustomFieldNotExists() $contactService->createContact($addContactCommand); } + /** + * @test + */ + public function shouldCreateContactWithDayOfCycleZero() + { + $customerName = 'Adam Kowalski'; + $dayOfCycle = 0; + + $params = [ + 'name' => $customerName, + 'email' => 'adam.kowalski@getresponse.com', + 'campaign' => [ + 'campaignId' => 'contactListId' + ], + 'dayOfCycle' => $dayOfCycle, + 'customFieldValues' => [ + ['customFieldId' => 'id_1', 'value' => ['value_1']], + ['customFieldId' => 'id_2', 'value' => ['value_2']], + ['customFieldId' => 'cx4R', 'value' => ['origin']] + ] + ]; + + $this->getResponseApiClientMock + ->expects($this->once()) + ->method('createContact') + ->with($params); + + $this->getResponseApiClientMock + ->expects($this->once()) + ->method('getCustomFieldByName') + ->with('origin') + ->willReturn(['customFieldId' => 'cx4R']); + + $addContactCommand = Generator::createAddContactCommand($customerName, $dayOfCycle); + + $contactService = new ContactService($this->getResponseApiClientMock); + $contactService->createContact($addContactCommand); + } + + /** + * @test + */ + public function shouldCreateContactWithNoDayOfCycle() + { + $customerName = 'Adam Kowalski'; + $dayOfCycle = null; + + $params = [ + 'name' => $customerName, + 'email' => 'adam.kowalski@getresponse.com', + 'campaign' => [ + 'campaignId' => 'contactListId' + ], + 'customFieldValues' => [ + ['customFieldId' => 'id_1', 'value' => ['value_1']], + ['customFieldId' => 'id_2', 'value' => ['value_2']], + ['customFieldId' => 'cx4R', 'value' => ['origin']] + ] + ]; + + $this->getResponseApiClientMock + ->expects($this->once()) + ->method('createContact') + ->with($params); + + $this->getResponseApiClientMock + ->expects($this->once()) + ->method('getCustomFieldByName') + ->with('origin') + ->willReturn(['customFieldId' => 'cx4R']); + + $addContactCommand = Generator::createAddContactCommand($customerName, $dayOfCycle); + + $contactService = new ContactService($this->getResponseApiClientMock); + $contactService->createContact($addContactCommand); + } + /** * @test */ @@ -351,6 +428,7 @@ public function shouldUnsubscribeContact() * @param string $email * @param string $origin * @param string $invalidOrigin + * @throws GetresponseApiException */ public function shouldNotUnsubscribeContact($email, $origin, $invalidOrigin) {