Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send smss coma separated #129

Merged
merged 4 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/Feature/Sms/Bag/ScheduleSmssBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#[\AllowDynamicProperties]
class ScheduleSmssBag
{
/** @var array */
/** @var array|string */
public $to;

/** @var DateTimeInterface */
Expand Down
2 changes: 1 addition & 1 deletion src/Feature/Sms/Bag/SendSmssBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#[\AllowDynamicProperties]
class SendSmssBag
{
/** @var array */
/** @var array|string */
public $to;

/** @var string */
Expand Down
5 changes: 5 additions & 0 deletions src/Feature/Sms/SmsHttpFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions src/Infrastructure/RequestMapper/LegacyRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Fixture/PhoneNumberFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
30 changes: 12 additions & 18 deletions tests/Integration/Feature/Sms/SmsFeatureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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 */
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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) {
Expand All @@ -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');
}

Expand All @@ -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');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,78 @@ 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',
];
$userParameters = [
'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());
}
}