public function testValidator() { $method = self::getPrivateMethod('validator'); $obj = Sms::make()->to('18280000000'); $r = $method->invokeArgs($obj, []); $this->assertTrue($r); }
public function testAddAgentConfig() { Sms::agents('Log', []); $this->assertCount(1, Sms::getAgentsConfig()); Sms::agents('Luosimao', ['apikey' => '123']); $this->assertCount(2, Sms::getAgentsConfig()); Sms::agents(['Luosimao' => ['apikey' => '123'], 'YunPian' => ['apikey' => '123']]); $this->assertCount(3, Sms::getAgentsConfig()); }
public function testConfiguration() { $method = self::getPrivateMethod('configuration'); $obj = new Sms(false); $method->invokeArgs($obj, []); $config = (include __DIR__ . '/../src/config/phpsms.php'); $this->assertCount(count($config['scheme']), Sms::scheme()); $this->assertCount(count($config['scheme']), Sms::config()); }
/** * 简洁短信发送接口 * @param $to 接收人 * @param $content 短信内容 * @param $是否入队列, 未实现 * @return array * [ * 'success' => false, * 'info' => '', * 'code' => 0 * ]; * */ public function send($to, $content, $queue = true) { $this->trigger('beforeSend'); if ($this->template !== false) { $content = $this->translate($content); } $result = Sms::make()->to($to)->content($content)->send($queue); $this->trigger('afterSend'); return $result; }
/** * register service provider */ public function register() { //merge configs $this->mergeConfigFrom(__DIR__ . '/../config/phpsms.php', 'phpsms'); Sms::scheme(config('phpsms.scheme', [])); Sms::config(config('phpsms.agents', [])); $this->app->singleton('PhpSms', function () { return new Sms(false); }); }
/** * Register the service provider. * * @return void */ public function register() { $this->package('hardywen/phpsms-l4', null, realpath(__DIR__ . '/../src/')); $this->app->singleton('phpsms-l4', function ($app) { $config = $app->config->get('phpsms-l4::config'); Sms::enable(isset($config['enable']) ? $config['enable'] : []); Sms::agents(isset($config['agents']) ? $config['agents'] : []); return new Sms(false); }); }
/** * register service provider */ public function register() { //merge configs $this->mergeConfigFrom(__DIR__ . '/../config/phpsms.php', 'phpsms'); Sms::enable(config('phpsms.enable', [])); Sms::agents(config('phpsms.agents', [])); $this->app->singleton('PhpSms', function ($app) { return new Sms(); }); }
public function testUpdateAgentConfig() { $agent = Sms::getAgent('Luosimao'); $this->assertEquals('123', $agent->apikey); Sms::config('Luosimao', ['apikey' => '12345', 'data' => 'hello world']); $this->assertEquals('12345', $agent->apikey); $this->assertEquals('hello world', $agent->data); Sms::cleanConfig(); $this->assertEquals(null, $agent->apikey); $this->assertEquals(null, $agent->data); }
/** * get verify sms templates id * * @return array */ public function getVerifySmsTemplates() { $templates = []; $enableAgents = Sms::getEnableAgents(); $agentsConfig = Sms::getAgentsConfig(); foreach ($enableAgents as $name => $opts) { if (isset($agentsConfig["{$name}"])) { if (isset($agentsConfig["{$name}"]['verifySmsTemplateId'])) { $templates[$name] = $agentsConfig["{$name}"]['verifySmsTemplateId']; } } } return $templates; }
/** * bootstrap PhpSms */ protected function initPhpSms() { //export custom rule flag value define('CUSTOM_RULE', SmsManager::CUSTOM_RULE_FLAG); // define how to use queue $queueJob = config('laravel-sms.queueJob', 'App\\Jobs\\SendReminderSms'); Sms::queue(function ($sms, $data) use($queueJob) { if (!class_exists($queueJob)) { throw new LaravelSmsException("Class [{$queueJob}] does not exists."); } $this->dispatch(new $queueJob($sms)); return ['success' => true, 'after_push_to_queue' => true]; }); // before send hook // store sms data to database Sms::beforeSend(function ($task) { if (!config('laravel-sms.database_enable', false)) { return true; } $data = $task->data ?: []; $id = DB::table('laravel_sms')->insertGetId(['to' => $data['to'], 'temp_id' => json_encode($data['templates']), 'data' => json_encode($data['templateData']), 'content' => $data['content'], 'voice_code' => $data['voiceCode'], 'created_at' => date('Y-m-d H:i:s', time())]); $data['smsId'] = $id; $task->data($data); }); // after send hook // update sms data in database Sms::afterSend(function ($task, $result) { if (!config('laravel-sms.database_enable', false)) { return true; } // get time $microTime = $result['time']['finished_at']; $finishedAt = explode(' ', $microTime)[1]; // get sms id $data = $task->data; $smsId = isset($data['smsId']) ? $data['smsId'] : 0; // update database DB::beginTransaction(); $dbData = []; $dbData['updated_at'] = date('Y-m-d H:i:s', $finishedAt); $dbData['result_info'] = json_encode($result['logs']); if ($result['success']) { $dbData['sent_time'] = $finishedAt; } else { DB::table('laravel_sms')->where('id', $smsId)->increment('fail_times'); $dbData['last_fail_time'] = $finishedAt; } DB::table('laravel_sms')->where('id', $smsId)->update($dbData); DB::commit(); }); }
/** * bootstrap PhpSms */ protected function initPhpSms() { // define how to use queue Sms::queue(function ($sms, $data) { $this->dispatch(new SendReminderSms($sms)); return ['success' => true, 'after_push_to_queue' => true]; }); // before send hook // store sms data to database Sms::beforeSend(function ($task) { if (!config('laravel-sms.database_enable', false)) { return true; } $data = $task->data ?: []; $id = DB::table('sms')->insertGetId(['to' => $data['to'], 'temp_id' => json_encode($data['templates']), 'data' => $data['voiceCode'] ?: json_encode($data['templateData']), 'content' => $data['content'], 'created_at' => date('Y-m-d H:i:s', time())]); $data['smsId'] = $id; $task->data($data); }); // after send hook // update sms data in database Sms::afterSend(function ($task, $result) { if (!config('laravel-sms.database_enable', false)) { return true; } // get time $microTime = $result['time']['finished_at']; $finishedAt = explode(' ', $microTime)[1]; // get sms id $data = $task->data; $smsId = isset($data['smsId']) ? $data['smsId'] : 0; // update database DB::beginTransaction(); $dbData = []; $dbData['updated_at'] = date('Y-m-d H:i:s', $finishedAt); $dbData['result_info'] = json_encode($result['logs']); if ($result['success']) { $dbData['sent_time'] = $finishedAt; } else { DB::table('sms')->where('id', $smsId)->increment('fail_times'); $dbData['last_fail_time'] = $finishedAt; } DB::table('sms')->where('id', $smsId)->update($dbData); DB::commit(); }); }
public function testUseQueue() { $status = Sms::queue(); $this->assertFalse($status); //define how to use queue //way 1 Sms::queue(function ($sms, $data) { return 'in_queue_1'; }); $this->assertTrue(Sms::queue()); //define how to use queue //way 2 Sms::queue(false, function ($sms, $data) { return 'in_queue_2'; }); $this->assertFalse(Sms::queue()); //open queue Sms::queue(true); $this->assertTrue(Sms::queue()); //push sms to queue $result = self::$sms->send(); $this->assertEquals('in_queue_2', $result); //force send $result = self::$sms->send(true); $this->assertTrue($result['success']); $this->assertCount(1, $result['logs']); $this->assertEquals('Log', $result['logs'][0]['driver']); }
* after sent hook */ Sms::afterSend(function ($task, $results) { }); /** * manual set enable agents */ //Sms::enable([ // 'Log' => '1 backup', // 'Luosimao' => '3 backup' //]); /** * print config */ //var_dump(Sms::getAgents()); //var_dump(Sms::getConfig()); /** * define queue */ //Sms::queue(function(){ // var_dump('pushed to queue!'); // return 'yes'; //}); //Sms::queue(true); print_r('<hr>'); $result = Sms::make(['YunTongXun' => 21516])->to('18280345...')->data(['code' => '1111', 'length' => 10])->send(true); var_dump($result); print_r('<hr>'); $sms = new Sms(); $result2 = $sms->voice(111)->to(18280345349)->send(); var_dump($result2);