You are here

class CurrencyTest in Currency 8.3

Same name in this branch
  1. 8.3 tests/src/Kernel/CurrencyTest.php \Drupal\Tests\currency\Kernel\CurrencyTest
  2. 8.3 tests/src/Unit/Entity/CurrencyTest.php \Drupal\Tests\currency\Unit\Entity\CurrencyTest
  3. 8.3 tests/src/Unit/Plugin/views/filter/CurrencyTest.php \Drupal\Tests\currency\Unit\Plugin\views\filter\CurrencyTest
  4. 8.3 tests/src/Unit/Plugin/views/field/CurrencyTest.php \Drupal\Tests\currency\Unit\Plugin\views\field\CurrencyTest

@coversDefaultClass \Drupal\currency\Plugin\views\field\Currency

@group Currency

Hierarchy

Expanded class hierarchy of CurrencyTest

File

tests/src/Unit/Plugin/views/field/CurrencyTest.php, line 19

Namespace

Drupal\Tests\currency\Unit\Plugin\views\field
View source
class CurrencyTest extends UnitTestCase {

  /**
   * The currency storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $currencyStorage;

  /**
   * The plugin configuration.
   *
   * @var mixed[]
   */
  protected $pluginConfiguration;

  /**
   * The plugin definiton.
   *
   * @var mixed[]
   */
  protected $pluginDefinition;

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

  /**
   * The class under test.
   *
   * @var \Drupal\currency\Plugin\views\field\Currency
   */
  protected $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->pluginConfiguration = [
      'currency_method' => 'label',
    ];
    $plugin_id = $this
      ->randomMachineName();
    $this->pluginDefinition = [];
    $this->currencyStorage = $this
      ->createMock(EntityStorageInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->sut = new Currency($this->pluginConfiguration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->currencyStorage);
  }

  /**
   * @covers ::__construct
   */
  public function testConstructWithoutMethod() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $plugin_id = $this
      ->randomMachineName();
    $this->sut = new Currency([], $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->currencyStorage);
  }

  /**
   * @covers ::__construct
   */
  public function testConstructWithNonExistentMethod() {
    $this
      ->expectException(\InvalidArgumentException::class);
    $configuration = [
      'currency_method' => $this
        ->randomMachineName(),
    ];
    $plugin_id = $this
      ->randomMachineName();
    $this->sut = new Currency($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->currencyStorage);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  function testCreate() {
    $entity_type_manager = $this
      ->createMock(EntityTypeManagerInterface::class);
    $entity_type_manager
      ->expects($this
      ->atLeastOnce())
      ->method('getStorage')
      ->with('currency')
      ->willReturn($this->currencyStorage);
    $container = $this
      ->createMock(ContainerInterface::class);
    $map = [
      [
        'entity_type.manager',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $entity_type_manager,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = Currency::create($container, $this->pluginConfiguration, '', $this->pluginDefinition);
    $this
      ->assertInstanceOf(Currency::class, $sut);
  }

  /**
   * @covers ::render
   */
  function testRenderWithExistingCurrency() {
    $currency_code = $this
      ->randomMachineName();
    $currency_method_return_value = $this
      ->randomMachineName();
    $field_alias = $this
      ->randomMachineName();
    $this->sut->field_alias = $field_alias;
    $result_row = new ResultRow([
      $field_alias => $currency_code,
    ]);
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $currency
      ->expects($this
      ->atLeastOnce())
      ->method($this->pluginConfiguration['currency_method'])
      ->willReturn($currency_method_return_value);
    $this->currencyStorage
      ->expects($this
      ->atLeastOnce())
      ->method('load')
      ->with($currency_code)
      ->willReturn($currency);
    $this
      ->assertSame($currency_method_return_value, $this->sut
      ->render($result_row));
  }

  /**
   * @covers ::render
   */
  function testRenderWithNonExistingCurrency() {
    $currency_code = $this
      ->randomMachineName();
    $field_alias = $this
      ->randomMachineName();
    $this->sut->field_alias = $field_alias;
    $result_row = new ResultRow([
      $field_alias => $currency_code,
    ]);
    $this->currencyStorage
      ->expects($this
      ->atLeastOnce())
      ->method('load')
      ->with($currency_code)
      ->willReturn(NULL);
    $this
      ->assertInstanceOf(MarkupInterface::class, $this->sut
      ->render($result_row));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CurrencyTest::$currencyStorage protected property The currency storage.
CurrencyTest::$pluginConfiguration protected property The plugin configuration.
CurrencyTest::$pluginDefinition protected property The plugin definiton.
CurrencyTest::$stringTranslation protected property The string translator.
CurrencyTest::$sut protected property The class under test.
CurrencyTest::setUp public function Overrides UnitTestCase::setUp
CurrencyTest::testConstructWithNonExistentMethod public function @covers ::__construct
CurrencyTest::testConstructWithoutMethod public function @covers ::__construct
CurrencyTest::testCreate function @covers ::create @covers ::__construct
CurrencyTest::testRenderWithExistingCurrency function @covers ::render
CurrencyTest::testRenderWithNonExistingCurrency function @covers ::render
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.
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.