You are here

class RecipientGatewayEventTest in SMS Framework 8

Same name and namespace in other branches
  1. 2.x tests/src/Unit/Event/RecipientGatewayEventTest.php \Drupal\Tests\sms\Unit\Event\RecipientGatewayEventTest
  2. 2.1.x tests/src/Unit/Event/RecipientGatewayEventTest.php \Drupal\Tests\sms\Unit\Event\RecipientGatewayEventTest

Unit Tests for SmsMessage.

@group SMS Framework @coversDefaultClass \Drupal\sms\Event\RecipientGatewayEvent

Hierarchy

Expanded class hierarchy of RecipientGatewayEventTest

File

tests/src/Unit/Event/RecipientGatewayEventTest.php, line 16

Namespace

Drupal\Tests\sms\Unit\Event
View source
class RecipientGatewayEventTest extends UnitTestCase {
  use SmsFrameworkTestTrait;

  /**
   * Tests gateway sort.
   *
   * @covers ::getGatewaysSorted
   */
  public function testSortFunction() {
    $number = $this
      ->randomPhoneNumbers()[0];
    $event = $this
      ->createEvent($number);
    $gateway = $this
      ->getMock(SmsGatewayInterface::class);
    $gateway
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn('gateway_first');
    $event
      ->addGateway($gateway, 100);
    $gateway = $this
      ->getMock(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());
  }

  /**
   * Tests recipient constructor.
   *
   * @covers ::getRecipient
   */
  public function testRecipientConstructor() {
    $number = $this
      ->randomPhoneNumbers()[0];
    $event = $this
      ->createEvent($number);
    $this
      ->assertEquals($number, $event
      ->getRecipient(), 'Constructor recipient is set');
  }

  /**
   * Tests recipient methods.
   *
   * @covers ::getRecipient
   * @covers ::setRecipient
   */
  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');
  }

  /**
   * Tests gateway add and getter.
   *
   * @covers ::addGateway
   * @covers ::getGateways
   */
  public function testGetGateways() {
    $event = $this
      ->createEvent($this
      ->randomPhoneNumbers()[0]);
    $gateway = $this
      ->getMock(SmsGatewayInterface::class);
    $gateway
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn('gateway_1');
    $event
      ->addGateway($gateway, 200);
    $gateway = $this
      ->getMock(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]);
  }

  /**
   * Tests removing gateway with both ID and priority.
   *
   * @covers ::removeGateway
   */
  public function testGatewayRemove() {
    $event = $this
      ->createEvent($this
      ->randomPhoneNumbers()[0]);
    $gateway = $this
      ->getMock(SmsGatewayInterface::class);
    $gateway
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn('gateway_1');
    $event
      ->addGateway($gateway, 200);
    $gateway = $this
      ->getMock(SmsGatewayInterface::class);
    $gateway
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn('gateway_1');
    $event
      ->addGateway($gateway, 400);
    $gateway = $this
      ->getMock(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.');
  }

  /**
   * Tests removing gateways with same identifier.
   *
   * @covers ::removeGateway
   */
  public function testGatewayRemoveAllSameId() {
    $event = $this
      ->createEvent($this
      ->randomPhoneNumbers()[0]);
    $gateway = $this
      ->getMock(SmsGatewayInterface::class);
    $gateway
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn('gateway_1');
    $event
      ->addGateway($gateway, 200);
    $gateway = $this
      ->getMock(SmsGatewayInterface::class);
    $gateway
      ->expects($this
      ->any())
      ->method('id')
      ->willReturn('gateway_1');
    $event
      ->addGateway($gateway, 400);
    $gateway = $this
      ->getMock(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.');
  }

  /**
   * Create a new event for testing.
   *
   * @param string $recipient
   *   The recipient phone number.
   *
   * @return \Drupal\sms\Event\RecipientGatewayEvent
   *   A new recipient gateway event instance.
   */
  public function createEvent($recipient) {
    return new RecipientGatewayEvent($recipient);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
RecipientGatewayEventTest::createEvent public function Create a new event for testing.
RecipientGatewayEventTest::testGatewayRemove public function Tests removing gateway with both ID and priority.
RecipientGatewayEventTest::testGatewayRemoveAllSameId public function Tests removing gateways with same identifier.
RecipientGatewayEventTest::testGetGateways public function Tests gateway add and getter.
RecipientGatewayEventTest::testRecipient public function Tests recipient methods.
RecipientGatewayEventTest::testRecipientConstructor public function Tests recipient constructor.
RecipientGatewayEventTest::testSortFunction public function Tests gateway sort.
SmsFrameworkTestTrait::createEntityWithPhoneNumber protected function Creates an entity, and optionally adds phone numbers.
SmsFrameworkTestTrait::createMemoryGateway protected function Creates a memory gateway.
SmsFrameworkTestTrait::createMessageResult protected function Create a result and reports for a message.
SmsFrameworkTestTrait::getIncomingMessages protected function Get all messages received by a gateway.
SmsFrameworkTestTrait::getLastIncomingMessage protected function Get the last message sent to gateway.
SmsFrameworkTestTrait::getLastTestMessage public function Get the last SMS message sent to 'Memory' gateway.
SmsFrameworkTestTrait::getLastTestMessageReport protected function Gets the last SMS report for messages sent to 'Memory' gateway.
SmsFrameworkTestTrait::getLastVerification protected function Gets the last phone number verification that was created.
SmsFrameworkTestTrait::getTestMessageReport protected function Gets an SMS report for message with message ID.
SmsFrameworkTestTrait::getTestMessageReports protected function Gets all SMS reports for messages sent to 'Memory' gateway.
SmsFrameworkTestTrait::getTestMessages public function Get all SMS messages sent to a 'Memory' gateway.
SmsFrameworkTestTrait::randomPhoneNumbers protected function Generates random phone numbers for tests.
SmsFrameworkTestTrait::randomSmsMessage protected function Generates a random SMS message by the specified user.
SmsFrameworkTestTrait::resetIncomingMessages protected function Resets incoming messages stored in memory by gateway.
SmsFrameworkTestTrait::resetTestMessageReports protected function Resets the SMS reports stored in memory by 'Memory' gateway.
SmsFrameworkTestTrait::resetTestMessages public function Resets SMS messages stored in memory by 'Memory' gateway.
SmsFrameworkTestTrait::setFallbackGateway protected function Sets the fallback gateway.
SmsFrameworkTestTrait::verifyPhoneNumber protected function Forces verification of a phone number for an entity.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340