SmsTestTrait.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /*
  3. * This file is part of ibrand/laravel-sms.
  4. *
  5. * (c) iBrand <https://www.ibrand.cc>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace iBrand\Sms\Test;
  11. use DB;
  12. use iBrand\Sms\Sms as SmsClass;
  13. use iBrand\Sms\Storage\CacheStorage;
  14. use Overtrue\EasySms\EasySms;
  15. use Overtrue\EasySms\Gateways\YuntongxunGateway;
  16. use Overtrue\EasySms\PhoneNumber;
  17. use Overtrue\EasySms\Support\Config;
  18. use Sms;
  19. trait SmsTestTrait
  20. {
  21. public function testStorage()
  22. {
  23. Sms::setStorage(new CacheStorage());
  24. $storage = Sms::getStorage();
  25. $this->assertEquals(CacheStorage::class, get_class($storage));
  26. }
  27. /**
  28. * Test key.
  29. */
  30. public function testKey()
  31. {
  32. $key = md5('ibrand.sms.18988888888');
  33. Sms::setKey('18988888888');
  34. $this->assertEquals($key, Sms::getKey());
  35. }
  36. /**
  37. * Test send method.
  38. */
  39. public function testSend()
  40. {
  41. //1. test send.
  42. $result = Sms::send('18988888888');
  43. $this->assertTrue($result);
  44. //2. test need create new code.
  45. $result = Sms::send('18988888888');
  46. $this->assertTrue($result);
  47. //3. test use old code.
  48. $this->app['config']->set('ibrand.sms.code.maxAttempts', 1);
  49. $result = Sms::send('18988888888');
  50. $this->assertTrue($result);
  51. }
  52. /**
  53. * Test canSend method.
  54. */
  55. public function testCanSend()
  56. {
  57. $result = Sms::canSend('18999999999');
  58. $this->assertTrue($result);
  59. Sms::send('18999999999');
  60. $result = Sms::canSend('18999999999');
  61. $this->assertFalse($result);
  62. }
  63. /**
  64. * Test checkCode method.
  65. */
  66. public function testCheckCode()
  67. {
  68. Sms::send('1897777777');
  69. $code = Sms::getCodeFromStorage();
  70. $result = Sms::checkCode('1897777777', $code->code);
  71. $this->assertTrue($result);
  72. Sms::send('1897777776');
  73. $code = Sms::getCodeFromStorage();
  74. $result = Sms::checkCode('1897777776', '12345');
  75. $this->assertFalse($result);
  76. }
  77. public function testBadGateway()
  78. {
  79. //1. test does not exist gateway.
  80. $storage = config('ibrand.sms.storage', CacheStorage::class);
  81. $this->app['config']->set('ibrand.sms.easy_sms.default.gateways', ['bad_gateway']);
  82. $sms = new SmsClass(new EasySms(config('ibrand.sms.easy_sms')), new $storage());
  83. $result = $sms->send('18988888888');
  84. $this->assertFalse($result);
  85. }
  86. public function testSendUseDbLog()
  87. {
  88. $this->app['config']->set('ibrand.sms.dblog', true);
  89. $result = Sms::send('18988888888');
  90. $this->assertTrue($result);
  91. //check database
  92. $result = DB::table('laravel_sms_log')->where('mobile', '18988888888')->first();
  93. $this->assertNotNull($result);
  94. $this->assertEquals('18988888888', $result->mobile);
  95. $this->assertEquals(1, $result->is_sent);
  96. }
  97. public function testSendByUserDefined()
  98. {
  99. $data = [
  100. 'content' => '【your app signature】亲爱的用户,您的验证码是%s。有效期为%s分钟,请尽快验证。',
  101. 'template' => 'SMS_802xxx',
  102. 'data' => [
  103. 'code' => mt_rand(10000, 99999),
  104. ],
  105. ];
  106. $result = Sms::send('18988888888', $data);
  107. $this->assertTrue($result);
  108. $gateways = ['xxx'];
  109. $result = Sms::send('18988888888', $data, $gateways);
  110. $this->assertFalse($result);
  111. $gateways = ['errorlog'];
  112. $result = Sms::send('18988888888', $data, $gateways);
  113. $this->assertTrue($result);
  114. }
  115. public function testYunTongXun()
  116. {
  117. $yuntongxun = [
  118. 'is_sub_account' => false,
  119. 'account_sid' => 'mock-account-sid',
  120. 'account_token' => 'mock-account-token',
  121. 'app_id' => 'mock-app-id',
  122. ];
  123. $gateways = config('ibrand.sms.easy_sms.gateways');
  124. $gateways = array_merge($gateways, ['yuntongxun' => $yuntongxun]);
  125. config(['ibrand.sms.easy_sms.gateways' => $gateways]);
  126. $storage = config('ibrand.sms.storage', CacheStorage::class);
  127. $storage = new $storage();
  128. $easySms = new EasySms(config('ibrand.sms.easy_sms'));
  129. $sms = new SmsClass($easySms, $storage);
  130. $code = $sms->getNewCode('18188888888');
  131. $config = [
  132. 'debug' => false,
  133. 'is_sub_account' => false,
  134. 'account_sid' => 'mock-account-sid',
  135. 'account_token' => 'mock-account-token',
  136. 'app_id' => 'mock-app-id',
  137. ];
  138. $gateway = \Mockery::mock(YuntongxunGateway::class . '[request]', [$config])->shouldAllowMockingProtectedMethods();
  139. $gateway->shouldReceive('request')->with(
  140. 'post',
  141. \Mockery::on(function ($api) {
  142. return 0 === strpos($api, 'https://app.cloopen.com:8883/2013-12-26/Accounts/mock-account-sid/SMS/TemplateSMS?sig=');
  143. }),
  144. \Mockery::on(function ($params) use ($code) {
  145. return $params['json'] == [
  146. 'to' => '18188888888',
  147. 'templateId' => 5589,
  148. 'appId' => 'mock-app-id',
  149. 'datas' => [$code->code, 'mock-data-2'],
  150. ] && 'application/json' == $params['headers']['Accept']
  151. && 'application/json;charset=utf-8' == $params['headers']['Content-Type'];
  152. })
  153. )->andReturn([
  154. 'statusCode' => YuntongxunGateway::SUCCESS_CODE,
  155. ], [
  156. 'statusCode' => 100,
  157. ])->once();
  158. $config = new Config($config);
  159. $message = new CustomMessage($code->code);
  160. $this->assertSame([
  161. 'statusCode' => YuntongxunGateway::SUCCESS_CODE,
  162. ], $gateway->send(new PhoneNumber(18188888888), $message, $config));
  163. $smsMockery = \Mockery::mock(SmsClass::class . '[send]', [$easySms, $storage]);
  164. $smsMockery->shouldReceive('send')->with('18188888888', $message, ['yuntongxun'])->andReturn(
  165. true
  166. )->once();
  167. $result = $smsMockery->send('18188888888', $message, ['yuntongxun']);
  168. $this->assertTrue($result);
  169. }
  170. }