Skip to content

Commit

Permalink
connect/tests: validate some responses + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pryznar committed Nov 22, 2015
1 parent 26f5cac commit 4542184
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
45 changes: 38 additions & 7 deletions src/SmsConnect.php
Expand Up @@ -36,9 +36,9 @@ public function getInbox()
$this->authData['action'] = self::ACTION_INBOX;

$requestUrl = $this->getRequestUrl($this->authData);
$response = $this->makeRequest($requestUrl);
$response = $this->getRequest($requestUrl);

return $this->convertToArray($response);
return $response;
}


Expand All @@ -54,9 +54,9 @@ public function sendSms($number, $text)
$this->authData['message'] = urlencode($text);

$requestUrl = $this->getRequestUrl($this->authData);
$response = $this->makeRequest($requestUrl);
$response = $this->getRequest($requestUrl);

return $this->convertToArray($response);
return $response;
}


Expand Down Expand Up @@ -112,7 +112,7 @@ protected function getSalt($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
$string = '';
for ($p = 0; $p < $length; $p++) {
$string .= $characters[mt_rand(0, strlen($characters))];
$string .= $characters[mt_rand(0, strlen($characters - 1))];
}

return $string;
Expand All @@ -131,10 +131,41 @@ protected function makeRequest($url)
CURLOPT_URL => $url,
CURLOPT_USERAGENT => self::USER_AGENT,
));
$resp = curl_exec($curl);
$response = curl_exec($curl);
curl_close($curl);

return $resp;
$response = $this->convertToArray($response);

return $response;
}


/**
* @param string $url
* @return array
*/
protected function getRequest($url)
{
$response = $this->makeRequest($url);
$this->validateResponse($response);

return $response;
}


/**
* @param array $response
*/
protected function validateResponse($response)
{
if (isset($response['err'])) {
if ($response['err'] === '2' || $response['err'] === '3') {
throw new MemberAccessException('Incorrect login or password');
}
if ($response['err'] === '11') {
throw new InvalidArgumentException('Empty sms text');
}
}
}


Expand Down
28 changes: 26 additions & 2 deletions tests/cases/SmsConnectTest.phpt
@@ -1,10 +1,34 @@
<?php

use Neogate\SmsConnect\SmsConnect;
use Tester\Assert;

$container = require __DIR__ . '/../bootstrap.php';


// empty auth
Assert::exception(function() {
new \Neogate\SmsConnect\SmsConnect(NULL, NULL);
}, 'InvalidArgumentException', 'Empty login');
new SmsConnect(NULL, NULL);
}, 'Neogate\SmsConnect\InvalidArgumentException', 'Empty login');


// Incorrect login or password
$smsConnect = Mockery::mock(SmsConnect::class)
->makePartial()
->shouldAllowMockingProtectedMethods();
$smsConnect->shouldReceive('makeRequest')->andReturn(['err' => '3']);

Assert::exception(function() use ($smsConnect) {
$smsConnect->getInbox();
}, 'Neogate\SmsConnect\MemberAccessException', 'Incorrect login or password');


// Empty sms text
$smsConnect = Mockery::mock(SmsConnect::class)
->makePartial()
->shouldAllowMockingProtectedMethods();
$smsConnect->shouldReceive('makeRequest')->andReturn(['err' => '11']);

Assert::exception(function() use ($smsConnect) {
$smsConnect->sendSms('+420602111111', '');
}, 'Neogate\SmsConnect\InvalidArgumentException', 'Empty sms text');

0 comments on commit 4542184

Please sign in to comment.