diff --git a/CHANGELOG.md b/CHANGELOG.md index 3b9d65e..05a8247 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Fixed +- sending/scheduling smses in large amount ### Added - PHP-8.3 support diff --git a/src/Feature/Sms/Bag/ScheduleSmssBag.php b/src/Feature/Sms/Bag/ScheduleSmssBag.php index b6fda24..6d2afc3 100644 --- a/src/Feature/Sms/Bag/ScheduleSmssBag.php +++ b/src/Feature/Sms/Bag/ScheduleSmssBag.php @@ -29,7 +29,7 @@ #[\AllowDynamicProperties] class ScheduleSmssBag { - /** @var array */ + /** @var array|string */ public $to; /** @var DateTimeInterface */ diff --git a/src/Feature/Sms/Bag/SendSmssBag.php b/src/Feature/Sms/Bag/SendSmssBag.php index 53dc09b..5a48c72 100644 --- a/src/Feature/Sms/Bag/SendSmssBag.php +++ b/src/Feature/Sms/Bag/SendSmssBag.php @@ -29,7 +29,7 @@ #[\AllowDynamicProperties] class SendSmssBag { - /** @var array */ + /** @var array|string */ public $to; /** @var string */ diff --git a/src/Feature/Sms/SmsHttpFeature.php b/src/Feature/Sms/SmsHttpFeature.php index 10dea8b..b2e2299 100644 --- a/src/Feature/Sms/SmsHttpFeature.php +++ b/src/Feature/Sms/SmsHttpFeature.php @@ -110,6 +110,8 @@ public function sendFlashSmsToGroup(SendSmsToGroupBag $sendSmsToGroupBag): array public function sendSmss(SendSmssBag $sendSmssBag): array { + $sendSmssBag->to = implode(',', $sendSmssBag->to); + return array_map( [$this->dataFactoryProvider->provideSmsFactory(), 'createFromObject'], $this->makeRequest($sendSmssBag)->list @@ -118,6 +120,8 @@ public function sendSmss(SendSmssBag $sendSmssBag): array public function sendFlashSmss(SendSmssBag $sendSmssBag): array { + $sendSmssBag->to = implode(',', $sendSmssBag->to); + return array_map( [$this->dataFactoryProvider->provideSmsFactory(), 'createFromObject'], $this->makeRequest($sendSmssBag)->list @@ -146,6 +150,7 @@ public function scheduleSms(ScheduleSmsBag $scheduleSmsBag): Sms public function scheduleSmss(ScheduleSmssBag $scheduleSmssBag): array { + $scheduleSmssBag->to = implode(',', $scheduleSmssBag->to); $scheduleSmssBag->dateValidate = true; return array_map( diff --git a/src/Infrastructure/RequestMapper/LegacyRequestMapper.php b/src/Infrastructure/RequestMapper/LegacyRequestMapper.php index c59e314..b881e30 100644 --- a/src/Infrastructure/RequestMapper/LegacyRequestMapper.php +++ b/src/Infrastructure/RequestMapper/LegacyRequestMapper.php @@ -34,9 +34,7 @@ private function createRequest( array $builtInParameters, array $userParameters ): Request { - $builtInParameters['format'] = 'json'; - - $parameters = new QueryParametersData($builtInParameters, $userParameters); + $parameters = new QueryParametersData(['format' => 'json'] + $builtInParameters, $userParameters); return new Request($method, $path, $this->queryFormatter->format($parameters)); } diff --git a/tests/Fixture/PhoneNumberFixture.php b/tests/Fixture/PhoneNumberFixture.php index 2ab2941..9f06e1f 100644 --- a/tests/Fixture/PhoneNumberFixture.php +++ b/tests/Fixture/PhoneNumberFixture.php @@ -20,4 +20,9 @@ public static function anyValidMobile(): string { return (string)((int)self::$validMobile + self::$i++); } + + public static function xValidMobile(int $x): array + { + return array_map(function () {return self::anyValidMobile();}, range(1, $x)); + } } diff --git a/tests/Integration/Feature/Sms/SmsFeatureTest.php b/tests/Integration/Feature/Sms/SmsFeatureTest.php index e78a420..c5051f8 100644 --- a/tests/Integration/Feature/Sms/SmsFeatureTest.php +++ b/tests/Integration/Feature/Sms/SmsFeatureTest.php @@ -82,12 +82,12 @@ public function it_should_send_flash_sms() public function it_should_send_smss() { $smsFeature = self::$smsapiService->smsFeature(); - $sendSmssBag = $this->givenSmssToSend(); + $sendSmssBag = $this->givenSmssToSend(2); $sendSmssBag->test = true; $results = $smsFeature->sendSmss($sendSmssBag); - $this->assertCount(count($sendSmssBag->to), $results); + $this->assertCount(2, $results); } /** @@ -96,12 +96,12 @@ public function it_should_send_smss() public function it_should_send_flash_smss() { $smsFeature = self::$smsapiService->smsFeature(); - $sendSmssBag = $this->givenSmssToSend(); + $sendSmssBag = $this->givenSmssToSend(2); $sendSmssBag->test = true; $results = $smsFeature->sendFlashSmss($sendSmssBag); - $this->assertCount(count($sendSmssBag->to), $results); + $this->assertCount(2, $results); } /** @@ -110,7 +110,7 @@ public function it_should_send_flash_smss() public function it_should_not_receive_content_details_for_smss() { $smsFeature = self::$smsapiService->smsFeature(); - $sendSmsesBag = $this->givenSmssToSend(); + $sendSmsesBag = $this->givenSmssToSend(2); $sendSmsesBag->test = true; /** @var Sms[] $results */ @@ -142,12 +142,12 @@ public function it_should_schedule_sms() public function it_should_schedule_smss() { $smsFeature = self::$smsapiService->smsFeature(); - $scheduleSmssBag = $this->givenSmssToSchedule(); + $scheduleSmssBag = $this->givenSmssToSchedule(2); $scheduleSmssBag->test = true; $results = $smsFeature->scheduleSmss($scheduleSmssBag); - $this->assertCount(count($scheduleSmssBag->to), $results); + $this->assertCount(2, $results); } /** @@ -171,7 +171,7 @@ public function it_should_schedule_flash_sms() public function it_should_delete_scheduled_smss() { $smsFeature = self::$smsapiService->smsFeature(); - $scheduleSmssBag = $this->givenSmssToSchedule(); + $scheduleSmssBag = $this->givenSmssToSchedule(2); $results = $smsFeature->scheduleSmss($scheduleSmssBag); $smsIds = array_map(function (Sms $sms) { @@ -190,12 +190,9 @@ private function givenSmsToSend(): SendSmsBag return SendSmsBag::withMessage($someReceiver, 'some message'); } - private function givenSmssToSend(): SendSmssBag + private function givenSmssToSend(int $x): SendSmssBag { - $receivers = [ - PhoneNumberFixture::anyValidMobile(), - PhoneNumberFixture::anyValidMobile(), - ]; + $receivers = PhoneNumberFixture::xValidMobile($x); return SendSmssBag::withMessage($receivers, 'some message'); } @@ -206,13 +203,10 @@ private function givenSmsToSchedule(): ScheduleSmsBag return ScheduleSmsBag::withMessage($someDate, $someReceiver, 'some message'); } - private function givenSmssToSchedule(): ScheduleSmssBag + private function givenSmssToSchedule(int $x): ScheduleSmssBag { $someDate = new DateTime('+1 day noon'); - $receivers = [ - PhoneNumberFixture::anyValidMobile(), - PhoneNumberFixture::anyValidMobile(), - ]; + $receivers = PhoneNumberFixture::xValidMobile($x); return ScheduleSmssBag::withMessage($someDate, $receivers, 'some message'); } } diff --git a/tests/Unit/Infrastructure/RequestMapper/LegacyRequestMapperTest.php b/tests/Unit/Infrastructure/RequestMapper/LegacyRequestMapperTest.php index 63f910f..9b5246a 100644 --- a/tests/Unit/Infrastructure/RequestMapper/LegacyRequestMapperTest.php +++ b/tests/Unit/Infrastructure/RequestMapper/LegacyRequestMapperTest.php @@ -25,10 +25,69 @@ public function init() /** * @test */ - public function it_should_create_post_request_with_parameters() + public function it_should_use_path_as_request_uri() { $path = 'anyPath'; + $request = $this->mapper->map($path, []); + + $this->assertEquals($path, $request->getUri()); + } + + /** + * @test + */ + public function it_should_send_request_as_post() + { + $request = $this->mapper->map('anyPath', []); + + $this->assertEquals(RequestHttpMethod::POST, $request->getMethod()); + } + + /** + * @test + */ + public function it_should_always_set_format_json_parameter() + { + $builtInParameters = []; + $userParameters = []; + + $request = $this->mapper->map('anyPath', $builtInParameters, $userParameters); + + $this->assertEquals('format=json', $request->getBody()); + } + + /** + * @test + */ + public function it_should_prepend_format_parameter_to_built_in_parameters_when_none() + { + $builtInParameters = []; + $userParameters = ['any2' => 'any']; + + $request = $this->mapper->map('anyPath', $builtInParameters, $userParameters); + + $this->assertEquals('format=json&any2=any', $request->getBody()); + } + + /** + * @test + */ + public function it_should_prepend_format_parameter_to_built_in_parameters_when_set() + { + $builtInParameters = ['any1' => 'any']; + $userParameters = []; + + $request = $this->mapper->map('anyPath', $builtInParameters, $userParameters); + + $this->assertEquals('format=json&any1=any', $request->getBody()); + } + + /** + * @test + */ + public function it_should_merge_both_built_in_and_user_parameters() + { $builtInParameters = [ 'any1' => 'any', ]; @@ -36,11 +95,8 @@ public function it_should_create_post_request_with_parameters() 'any2' => 'any', ]; - $request = $this->mapper->map($path, $builtInParameters, $userParameters); - - $this->assertEquals($path, $request->getUri()); - $this->assertEquals(RequestHttpMethod::POST, $request->getMethod()); + $request = $this->mapper->map('anyPath', $builtInParameters, $userParameters); - $this->assertEquals('any1=any&format=json&any2=any', $request->getBody()); + $this->assertEquals('format=json&any1=any&any2=any', $request->getBody()); } }