Skip to content

Commit

Permalink
Merge pull request #10 from enricodias/dev
Browse files Browse the repository at this point in the history
auto formatting files
  • Loading branch information
enricodias authored Aug 8, 2021
2 parents 2c1af5b + 67fda55 commit baa5a06
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 42 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ $SmsDev->setNumberValidation(false); // disables phone number validation

Note that SmsDev will charge you for messages sent to invalid numbers.


### Receiving SMS messages

Get unread messages in a specific date interval:
Expand Down Expand Up @@ -130,5 +129,5 @@ This class solves this problem by automatically correcting dates both in search

## TODO

- Check the status of sent messages.
- Send multiple SMS messages.
- Check the status of sent messages.
- Send multiple SMS messages.
40 changes: 20 additions & 20 deletions src/SmsDev.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

/**
* SmsDev
*
*
* Send and receive SMS using SmsDev.com.br
*
*
* @see https://www.smsdev.com.br/ SMSDev API.
*
*
* @author Enrico Dias <enrico@enricodias.com>
*/
class SmsDev
Expand Down Expand Up @@ -55,7 +55,7 @@ class SmsDev
* Query string to be sent to the API as a search filter.
*
* The default 'status' = 1 will return all received messages.
*
*
* @var array
*/
private $_query = [
Expand Down Expand Up @@ -83,7 +83,7 @@ public function __construct($apiKey = '')

/**
* Send an SMS message.
*
*
* This method does not guarantee that the recipient received the massage since the message delivery is async.
*
* @param int $number Recipient's number.
Expand All @@ -96,7 +96,7 @@ public function send($number, $message, $refer = null)
$this->_result = [];

if ($this->_numberValidation === true) {

try {

$number = $this->validatePhoneNumber($number);
Expand Down Expand Up @@ -239,7 +239,7 @@ public function dateBetween($dateFrom, $dateTo)

/**
* Query the API for received messages using search filters.
*
*
* @see SmsDev::$_query Search filters.
* @see SmsDev::$_result API response.
*
Expand Down Expand Up @@ -274,11 +274,11 @@ public function fetch()

/**
* Parse the received messages in a more useful format with the fields date, number and message.
*
*
* The dates received by the API are converted to SmsDev::$_dateFormat.
*
* @see SmsDev::$_dateFormat Date format to be used in all date functions.
*
*
* @return array List of received messages.
*/
public function parsedMessages()
Expand Down Expand Up @@ -341,7 +341,7 @@ public function getBalance()
* Get the raw API response from the last response received.
*
* @see SmsDev::$_result Raw API response.
*
*
* @return array Raw API response.
*/
public function getResult()
Expand All @@ -353,10 +353,10 @@ public function getResult()
* Verifies if a phone number is valid
*
* @see https://github.com/giggsey/libphonenumber-for-php libphonenumber for PHP repository.
*
*
* @param int $number Phone number.
* @return int A valid mobile phone number.
*
*
* @throws \libphonenumber\NumberParseException If the number is not valid.
* @throws \Exception If the number is not a valid brazilian mobile number.
*/
Expand Down Expand Up @@ -384,12 +384,12 @@ private function validatePhoneNumber($number)

/**
* Convert a date to format supported by the API.
*
*
* The API requires the date format d/m/Y, but in this class any valid date format is supported.
* Since the API is always using the timezone America/Sao_Paulo, this function must also do timezone conversions.
*
* @see SmsDev::$_dateFormat Date format to be used in all date functions.
*
*
* @param string $key The filter key to be set as a search filter.
* @param string $date A valid date format.
* @return SmsDev Return itself for chaining.
Expand All @@ -399,7 +399,7 @@ private function parseDate($key, $date)
$parsedDate = \DateTime::createFromFormat($this->_dateFormat, $date);

if ($parsedDate !== false) {

$parsedDate->setTimezone($this->_apiTimeZone);

$this->_query[$key] = $parsedDate->format('d/m/Y');
Expand All @@ -424,13 +424,13 @@ private function makeRequest($request)
$response = $client->send($request);

} catch (\Exception $e) {

return false;

}

$response = json_decode($response->getBody(), true);

if (json_last_error() != JSON_ERROR_NONE || is_array($response) === false) return false;

$this->_result = $response;
Expand All @@ -443,11 +443,11 @@ private function makeRequest($request)
* This method is needed to test API calls in unit tests.
*
* @return object GuzzleHttp\Client instance.
*
*
* @codeCoverageIgnore
*/
protected function getGuzzleClient()
{
return new Client();
}
}
}
12 changes: 6 additions & 6 deletions tests/ApiRequestsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Test if the requests sent are compatible with the API specification.
*
*
* @see https://www.smsdev.com.br/ SMSDev API specification.
*/
final class ApiRequestsTest extends SmsDevMock
Expand Down Expand Up @@ -86,7 +86,7 @@ public function testFilterById()
->fetch();

$this->assertSame('/v1/inbox', $this->getRequestPath());

$query = $this->getRequestBody();

$this->assertEquals('', $query->key);
Expand Down Expand Up @@ -123,11 +123,11 @@ public function testFilterByDateBetween_InvalidDate()
$this->assertSame('/v1/inbox', $this->getRequestPath());

$query = $this->getRequestBody();

$this->assertEquals('', $query->key);
$this->assertObjectNotHasAttribute('date_from', $query);
$this->assertEquals('19/01/2019', $query->date_to);

$SmsDev = $this->getServiceMock();

$SmsDev->setDateFormat('Y-m-d')
Expand Down Expand Up @@ -179,7 +179,7 @@ public function testFilterByDate()

/**
* Test the timezone calculations in the date filters.
*
*
* Example: 2020-01-02 01:00:00 should be day 2020-01-01 in America/Sao_Paulo
*/
public function testTimezoneDate()
Expand All @@ -199,4 +199,4 @@ public function testTimezoneDate()

$this->assertSame('01/01/2020', $query->date_from);
}
}
}
18 changes: 9 additions & 9 deletions tests/ApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Test if the class can parse the API responses correctly.
*
*
* @see https://www.smsdev.com.br/ SMSDev API specification.
*/
final class ApiResponseTest extends SmsDevMock
Expand Down Expand Up @@ -62,7 +62,7 @@ public function sendDataProvider()
[ '118888100', 'Message', 'Refer', true, '{"situacao":"OK","codigo":"1","id":"645106333","refer": "Refer","descricao":"MENSAGEM NA FILA"}' ],
[ '11888810009', 'Message', 'Refer', true, '{"situacao":"OK","codigo":"1","id":"645106334","refer": "Refer","descricao":"MENSAGEM NA FILA"}' ],
[ 'abc', 'Message', 'Refer', false, '{"situacao":"ERRO","codigo":"402","refer": "Refer","descricao":"SEM NUMERO DESTINATARIO."}' ],
[ '', 'Message', 'Refer', false, '{"situacao":"ERRO","codigo":"402","refer": "Refer","descricao":"SEM NUMERO DESTINATARIO."}'],
[ '', 'Message', 'Refer', false, '{"situacao":"ERRO","codigo":"402","refer": "Refer","descricao":"SEM NUMERO DESTINATARIO."}'],

];
}
Expand All @@ -79,7 +79,7 @@ public function testFilterByUnread()
->fetch();

$parsedMessages = current($SmsDev->parsedMessages());

$this->assertSame('2018-01-19 13:35:14', $parsedMessages['date']); // UTC conversion
$this->assertSame('5511988887777', $parsedMessages['number']);
$this->assertSame('Resposta', $parsedMessages['message']);
Expand All @@ -102,15 +102,15 @@ public function testFilterByUnread_EmptyResponse()
public function testFilterById()
{
$apiResponse = '[{"situacao":"OK","data_read":"19\/01\/2018 11:35:14","telefone":"5511988887777","id":"","refer_id":"","nome":"","msg_sent":"","id_sms_read":"2515974","descricao":"Resposta"}]';

$SmsDev = $this->getServiceMock($apiResponse);

$SmsDev->setFilter()
->byId(2515974)
->fetch();

$result = current($SmsDev->getResult());

$this->assertSame(2515974, (int)$result['id_sms_read']);
}

Expand All @@ -123,7 +123,7 @@ public function testFilterById_EmptyResponse()
$SmsDev->setFilter()
->byId(2515974)
->fetch();

$this->assertEmpty($SmsDev->parsedMessages());
}

Expand All @@ -140,7 +140,7 @@ public function testFilterByDate()
->fetch();

$parsedMessages = current($SmsDev->parsedMessages());

$this->assertSame('1529418914', $parsedMessages['date']);
$this->assertSame('5511988887777', $parsedMessages['number']);
$this->assertSame('Resposta 1', $parsedMessages['message']);
Expand All @@ -153,7 +153,7 @@ public function testEmptyInbox()
$SmsDev = $this->getServiceMock($apiResponse);

$SmsDev->setDateFormat('Y-m-d H:i:s')->fetch();

$this->assertEmpty($SmsDev->parsedMessages());
}

Expand All @@ -169,4 +169,4 @@ public function testWrongApiKey()

$this->assertSame(false, $SmsDev->fetch());
}
}
}
4 changes: 2 additions & 2 deletions tests/PhoneNumberVerificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

/**
* Test the optional phone number verification
*
*
* @see https://github.com/giggsey/libphonenumber-for-php libphonenumber for PHP repository.
*/
final class PhoneNumberVerificationTest extends SmsDevMock
Expand Down Expand Up @@ -87,4 +87,4 @@ public function invalidPhoneProvider()
];
}

}
}
4 changes: 2 additions & 2 deletions tests/SmsDevMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ public function getServiceMock($apiResponse = '')
$stub = $this->getMockBuilder(SmsDev::class)
->setMethods(['getGuzzleClient'])
->getMock();

$stub->method('getGuzzleClient')->willReturn($client);

return $stub;
}

Expand Down

0 comments on commit baa5a06

Please sign in to comment.