Skip to content

Commit

Permalink
1. debug 模式下不验证手机号规则。
Browse files Browse the repository at this point in the history
2. 完善单元测试
3. fix CS.
  • Loading branch information
chenbidepro committed Feb 2, 2018
1 parent e1f1279 commit 621f9b1
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 34 deletions.
36 changes: 26 additions & 10 deletions src/Sms.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,22 @@ public function setStorage(StorageInterface $storage)
*/
public function send($to)
{
$this->setKey($to);
try {

//1. get code from storage.
$code = $this->getCodeFromStorage();
$this->setKey($to);

if ($this->needNewCode($code)) {
$code = $this->getNewCode($to);
}
//1. get code from storage.
$code = $this->getCodeFromStorage();

$validMinutes = (int)config('ibrand.sms.code.validMinutes', 5);
if ($this->needNewCode($code)) {
$code = $this->getNewCode($to);
}

$validMinutes = (int)config('ibrand.sms.code.validMinutes', 5);

$message = new CodeMessage($code->code, $validMinutes);

$message = new CodeMessage($code->code, $validMinutes);

try {
$results = $this->easySms->send($to, $message);

foreach ($results as $key => $value) {
Expand All @@ -105,13 +107,27 @@ public function send($to)
return true;
}
}
} catch (NoGatewayAvailableException $e) {

} catch (NoGatewayAvailableException $noGatewayAvailableException) {
return false;
} catch (\Exception $exception) {
return false;
}

return false;
}


/**
* check china mobile.
* @param $to
* @return false|int
*/
public function verifyMobile($to)
{
return preg_match('/^(?=\d{11}$)^1(?:3\d|4[57]|5[^4\D]|66|7[^249\D]|8\d|9[89])\d{8}$/', $to);
}

/**
* @param $to
*
Expand Down
3 changes: 3 additions & 0 deletions src/SmsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ public function postSendCode()
{
$mobile = request('mobile');

if(!config('app.debug') && !Sms::verifyMobile($mobile)){
return response()->json(['success' => false, 'message' => '无效手机号码']);
}

if (!Sms::canSend($mobile)) {
return response()->json(['success' => false, 'message' => '每60秒发送一次']);
Expand Down
16 changes: 9 additions & 7 deletions tests/SessionSmsTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/2/2
* Time: 14:04

/*
* This file is part of ibrand/laravel-sms.
*
* (c) iBrand <https://www.ibrand.cc>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace iBrand\Sms\Test;


use iBrand\Sms\Storage\SessionStorage;

class SessionSmsTest extends \Orchestra\Testbench\TestCase
Expand Down Expand Up @@ -42,4 +44,4 @@ protected function getEnvironmentSetUp($app)
$app['config']->set('ibrand.sms', require __DIR__.'/../config/config.php');
$app['config']->set('ibrand.sms.storage', SessionStorage::class);
}
}
}
31 changes: 19 additions & 12 deletions tests/SmsControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2018/2/2
* Time: 14:29

/*
* This file is part of ibrand/laravel-sms.
*
* (c) iBrand <https://www.ibrand.cc>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace iBrand\Sms\Test;


class SmsControllerTest extends \Orchestra\Testbench\TestCase
{
/**
Expand Down Expand Up @@ -36,27 +38,32 @@ protected function getPackageAliases($app)
public function testPostSendCode()
{
//1. test success mobile.
$response = $this->post('sms/verify-code',['mobile'=>'18988885555']);
$response = $this->post('sms/verify-code', ['mobile' => '18973305743']);

$response
->assertStatus(200)
->assertJson(['success' => true, 'message' => '短信发送成功']);

//2. test repeat in 60 seconds.

$response = $this->post('sms/verify-code',['mobile'=>'18988885555']);
$response = $this->post('sms/verify-code', ['mobile' => '18973305743']);

$response
->assertStatus(200)
->assertJson(['success' => false, 'message' => '每60秒发送一次']);

//3. test invalid mobile.
$response = $this->post('sms/verify-code', ['mobile' => '10000000000']);

$response
->assertStatus(200)
->assertJson(['success' => false, 'message' => '无效手机号码']);
}

public function testInfo()
{
$response= $this->get('sms/info?mobile=18988885555');
$response = $this->get('sms/info?mobile=18988885555');

$response
->assertStatus(200);
}

}
}
17 changes: 12 additions & 5 deletions tests/SmsTestTrait.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
<?php

/*
* This file is part of ibrand/laravel-sms.
*
* (c) iBrand <https://www.ibrand.cc>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace iBrand\Sms\Test;

use iBrand\Sms\Storage\CacheStorage;
use Sms;

trait SmsTestTrait
{

public function testStorage()
{
Sms::setStorage(new CacheStorage());

$storage = Sms::getStorage();

$this->assertEquals(CacheStorage::class, get_class($storage));
$this->assertSame(CacheStorage::class, get_class($storage));
}

/**
Expand All @@ -24,15 +32,14 @@ public function testKey()
{
$key = md5('ibrand.sms.18988888888');
Sms::setKey('18988888888');
$this->assertEquals($key, Sms::getKey());
$this->assertSame($key, Sms::getKey());
}

/**
* Test send method.
*/
public function testSend()
{

//1. test send.
$result = Sms::send('18988888888');
$this->assertTrue($result);
Expand Down Expand Up @@ -77,4 +84,4 @@ public function testCheckCode()

$this->assertFalse($result);
}
}
}

0 comments on commit 621f9b1

Please sign in to comment.