View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\sms\Unit\Event;
use Drupal\Tests\UnitTestCase;
use Drupal\Tests\sms\Functional\SmsFrameworkTestTrait;
use Drupal\sms\Event\RecipientGatewayEvent;
use Drupal\sms\Entity\SmsGatewayInterface;
class RecipientGatewayEventTest extends UnitTestCase {
use SmsFrameworkTestTrait;
public function testSortFunction() {
$number = $this
->randomPhoneNumbers()[0];
$event = $this
->createEvent($number);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_first');
$event
->addGateway($gateway, 100);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_second');
$event
->addGateway($gateway, 200);
$sorted = $event
->getGatewaysSorted();
$this
->assertEquals(2, count($sorted));
$this
->assertEquals('gateway_second', $sorted[0]
->id());
$this
->assertEquals('gateway_first', $sorted[1]
->id());
}
public function testRecipientConstructor() {
$number = $this
->randomPhoneNumbers()[0];
$event = $this
->createEvent($number);
$this
->assertEquals($number, $event
->getRecipient(), 'Constructor recipient is set');
}
public function testRecipient() {
$event = $this
->createEvent($this
->randomPhoneNumbers()[0]);
$number = $this
->randomPhoneNumbers()[0];
$event
->setRecipient($number);
$this
->assertEquals($number, $event
->getRecipient(), 'Recipient is set via setRecipient');
}
public function testGetGateways() {
$event = $this
->createEvent($this
->randomPhoneNumbers()[0]);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_1');
$event
->addGateway($gateway, 200);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_2');
$event
->addGateway($gateway, 400);
$gateways = $event
->getGateways();
$this
->assertEquals('gateway_1', $gateways[0][0]
->id());
$this
->assertEquals(200, $gateways[0][1]);
$this
->assertEquals('gateway_2', $gateways[1][0]
->id());
$this
->assertEquals(400, $gateways[1][1]);
}
public function testGatewayRemove() {
$event = $this
->createEvent($this
->randomPhoneNumbers()[0]);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_1');
$event
->addGateway($gateway, 200);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_1');
$event
->addGateway($gateway, 400);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_2');
$event
->addGateway($gateway, 600);
$this
->assertEquals(3, count($event
->getGateways()), 'There are three gateways.');
$event
->removeGateway('gateway_1', 400);
$this
->assertEquals(2, count($event
->getGateways()), 'One gateways was removed.');
}
public function testGatewayRemoveAllSameId() {
$event = $this
->createEvent($this
->randomPhoneNumbers()[0]);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_1');
$event
->addGateway($gateway, 200);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_1');
$event
->addGateway($gateway, 400);
$gateway = $this
->createMock(SmsGatewayInterface::class);
$gateway
->expects($this
->any())
->method('id')
->willReturn('gateway_2');
$event
->addGateway($gateway, 600);
$this
->assertEquals(3, count($event
->getGateways()), 'There are three gateways.');
$event
->removeGateway('gateway_1');
$this
->assertEquals(1, count($event
->getGateways()), 'Two gateways were removed.');
}
public function createEvent($recipient) {
return new RecipientGatewayEvent($recipient);
}
}