SmsFrameworkProcessorTest.php in SMS Framework 2.1.x
File
tests/src/Kernel/SmsFrameworkProcessorTest.php
View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Kernel;
use Drupal\sms\Direction;
use Drupal\sms\Entity\SmsMessage;
use Drupal\sms\Exception\SmsException;
use Drupal\sms\Exception\SmsPluginReportException;
use Drupal\sms\Message\SmsMessageResult;
class SmsFrameworkProcessorTest extends SmsFrameworkKernelBase {
public static $modules = [
'sms',
'sms_test',
'sms_test_gateway',
'field',
'telephone',
'dynamic_entity_reference',
];
protected $smsStorage;
protected $smsProvider;
protected $gatewayMemory;
protected $gatewayOutgoingResult;
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('sms');
$this
->installEntitySchema('sms_result');
$this
->installEntitySchema('sms_report');
$this->gatewayMemory = $this
->createMemoryGateway();
$this->gatewayOutgoingResult = $this
->createMemoryGateway([
'plugin' => 'memory_outgoing_result',
]);
$this->smsStorage = $this->container
->get('entity_type.manager')
->getStorage('sms');
$this->smsProvider = $this->container
->get('sms.provider');
}
public function testIncomingNoResult() {
$sms_message = SmsMessage::create()
->setDirection(Direction::INCOMING)
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers())
->setGateway($this->gatewayMemory);
$this
->expectException(SmsPluginReportException::class);
$this
->expectExceptionMessage('Missing result for message.');
$this->smsProvider
->queue($sms_message);
}
public function testIncomingMissingReports() {
$result = new SmsMessageResult();
$sms_message = SmsMessage::create()
->setDirection(Direction::INCOMING)
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers())
->setGateway($this->gatewayMemory)
->setResult($result);
$recipient_count = count($sms_message
->getRecipients());
$this
->expectException(SmsPluginReportException::class);
$this
->expectExceptionMessage("Missing reports for {$recipient_count} recipient(s).");
$this->smsProvider
->queue($sms_message);
}
public function testOutgoingNoResult() {
$this
->setFallbackGateway($this->gatewayOutgoingResult);
\Drupal::state()
->set('sms_test_gateway.memory_outgoing_result.missing_result', TRUE);
$this->gatewayOutgoingResult
->setSkipQueue(TRUE)
->save();
$sms_message = SmsMessage::create()
->setDirection(Direction::OUTGOING)
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers());
$this
->expectException(SmsPluginReportException::class);
$this
->expectExceptionMessage('Missing result for message.');
$this->smsProvider
->queue($sms_message);
}
public function testOutgoingMissingReports() {
$this
->setFallbackGateway($this->gatewayOutgoingResult);
$delete_count = rand(1, 5);
\Drupal::state()
->set('sms_test_gateway.memory_outgoing_result.delete_reports', $delete_count);
$this->gatewayOutgoingResult
->setSkipQueue(TRUE)
->save();
$sms_message = SmsMessage::create()
->setDirection(Direction::OUTGOING)
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers($delete_count + 1));
$this
->expectException(SmsPluginReportException::class);
$this
->expectExceptionMessage("Missing reports for {$delete_count} recipient(s).");
$this->smsProvider
->queue($sms_message);
}
public function testIncomingMissingGateway() {
$sms_message = SmsMessage::create()
->setDirection(Direction::INCOMING)
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers());
$this
->expectException(SmsException::class);
$this
->expectExceptionMessage('Gateway not set on incoming message');
$this->smsProvider
->queue($sms_message);
}
public function testIncomingUnSupported() {
$gateway = $this
->createMemoryGateway([
'plugin' => 'capabilities_default',
]);
$sms_message = SmsMessage::create()
->setDirection(Direction::INCOMING)
->setMessage($this
->randomString())
->addRecipients($this
->randomPhoneNumbers())
->setGateway($gateway);
$this
->expectException(SmsException::class);
$this
->expectExceptionMessage("Gateway `" . $gateway
->id() . "` does not support incoming messages.");
$this->smsProvider
->queue($sms_message);
}
}