You are here

CurrencyDeleteFormTest.php in Currency 8.3

File

tests/src/Unit/Entity/Currency/CurrencyDeleteFormTest.php
View source
<?php

namespace Drupal\Tests\currency\Unit\Entity\Currency;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\currency\Entity\Currency\CurrencyDeleteForm;
use Drupal\currency\Entity\CurrencyInterface;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * @coversDefaultClass \Drupal\currency\Entity\Currency\CurrencyDeleteForm
 *
 * @group Currency
 */
class CurrencyDeleteFormTest extends UnitTestCase {

  /**
   * The currency.
   *
   * @var \Drupal\currency\Entity\CurrencyInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currency;

  /**
   * The string translator.
   *
   * @var \Drupal\Core\StringTranslation\TranslationInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $stringTranslation;

  /**
   * The messenger.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $messenger;

  /**
   * The class under test.
   *
   * @var \Drupal\currency\Entity\Currency\CurrencyDeleteForm
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->currency = $this
      ->createMock(CurrencyInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->messenger = $this
      ->createMock(MessengerInterface::class);
    $this->sut = new CurrencyDeleteForm($this->stringTranslation);
    $this->sut
      ->setEntity($this->currency);
    $this->sut
      ->setMessenger($this->messenger);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $container = $this
      ->createMock(ContainerInterface::class);
    $container
      ->expects($this
      ->once())
      ->method('get')
      ->with('string_translation')
      ->willReturn($this->stringTranslation);
    $sut = CurrencyDeleteForm::create($container);
    $this
      ->assertInstanceOf(CurrencyDeleteForm::class, $sut);
  }

  /**
   * @covers ::getQuestion
   */
  function testGetQuestion() {
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $this->sut
      ->getQuestion());
  }

  /**
   * @covers ::getConfirmText
   */
  function testGetConfirmText() {
    $this
      ->assertInstanceOf(TranslatableMarkup::class, $this->sut
      ->getConfirmText());
  }

  /**
   * @covers ::getCancelUrl
   */
  function testGetCancelUrl() {
    $url = $this->sut
      ->getCancelUrl();
    $this
      ->assertInstanceOf(Url::class, $url);
    $this
      ->assertSame('entity.currency.collection', $url
      ->getRouteName());
  }

  /**
   * @covers ::submitForm
   */
  function testSubmitForm() {
    $this->currency
      ->expects($this
      ->once())
      ->method('delete');
    $form = array();
    $form_state = $this
      ->createMock(FormStateInterface::class);
    $form_state
      ->expects($this
      ->once())
      ->method('setRedirectUrl');
    $this->sut
      ->submitForm($form, $form_state);
  }

}