You are here

class AmountTest in Currency 8.3

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

@group Currency

Hierarchy

Expanded class hierarchy of AmountTest

File

tests/src/Unit/Plugin/views/field/AmountTest.php, line 28

Namespace

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

  /**
   * The config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $configFactory;

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

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $moduleHandler;

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

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $renderer;

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

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

  /**
   * The Views display handler.
   *
   * @param \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $viewsDisplayHandler;

  /**
   * The Views view executable.
   *
   * @param \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $viewsViewExecutable;

  /**
   * The Views query.
   *
   * @var \Drupal\views\Plugin\views\query\Sql
   */
  protected $viewsQuery;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $configuration = [];
    $plugin_id = $this
      ->randomMachineName();
    $this->pluginDefinition = [
      'group' => $this
        ->randomMachineName(),
      'title' => $this
        ->randomMachineName(),
    ];
    $this->configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $this->currencyStorage = $this
      ->createMock(EntityStorageInterface::class);
    $this->moduleHandler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->renderer = $this
      ->createMock(RendererInterface::class);
    $this->stringTranslation = $this
      ->getStringTranslationStub();
    $this->viewsDisplayHandler = $this
      ->getMockBuilder(DisplayPluginBase::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->viewsQuery = $this
      ->getMockBuilder(Sql::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->viewsViewExecutable = $this
      ->getMockBuilder(ViewExecutable::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->viewsViewExecutable->display_handler = $this->viewsDisplayHandler;
    $this->viewsViewExecutable->query = $this->viewsQuery;
    $container = new ContainerBuilder();
    $container
      ->set('config.factory', $this->configFactory);
    $container
      ->set('renderer', $this->renderer);
    \Drupal::setContainer($container);
    $this->sut = new Amount($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->moduleHandler, $this->renderer, $this->currencyStorage);
    $this->sut
      ->init($this->viewsViewExecutable, $this->viewsDisplayHandler);
  }

  /**
   * @covers ::create
   * @covers ::__construct
   */
  public 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,
      ],
      [
        'module_handler',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->moduleHandler,
      ],
      [
        'renderer',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->renderer,
      ],
      [
        'string_translation',
        ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE,
        $this->stringTranslation,
      ],
    ];
    $container
      ->expects($this
      ->any())
      ->method('get')
      ->willReturnMap($map);
    $sut = Amount::create($container, [], '', $this->pluginDefinition);
    $this
      ->assertInstanceOf(Amount::class, $sut);
  }

  /**
   * @covers ::defineOptions
   */
  public function testDefineOptions() {
    foreach ($this->sut
      ->defineOptions() as $option) {
      $this
        ->assertIsArray($option);
      $this
        ->assertTrue(array_key_exists('default', $option) || array_key_exists('contains', $option));
    }
  }

  /**
   * @covers ::buildOptionsForm
   */
  public function testBuildOptionsForm() {
    $this->viewsDisplayHandler
      ->expects($this
      ->atLeastOnce())
      ->method('getFieldLabels')
      ->willReturn([]);
    $this->viewsDisplayHandler
      ->expects($this
      ->atLeastOnce())
      ->method('getHandlers')
      ->with('argument')
      ->willReturn([]);
    $views_settings_config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $views_settings_config
      ->expects($this
      ->atLeastOnce())
      ->method('get')
      ->with('field_rewrite_elements')
      ->willReturn([]);
    $this->configFactory
      ->expects($this
      ->atLeastOnce())
      ->method('get')
      ->with('views.settings')
      ->willReturn($views_settings_config);
    $container = new ContainerBuilder();
    $container
      ->set('config.factory', $this->configFactory);
    \Drupal::setContainer($container);
    $form = [];
    $form_state = new FormState();
    $this->sut
      ->buildOptionsForm($form, $form_state);
    foreach ($form as $element) {
      $this
        ->assertIsArray($element);
    }
  }

  /**
   * @covers ::defaultDefinition
   */
  function testDefaultDefinition() {
    $method = new \ReflectionMethod($this->sut, 'defaultDefinition');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertIsArray($method
      ->invoke($this->sut));
  }

  /**
   * @covers ::getAmount
   */
  function testGetAmount() {
    $amount = mt_rand();
    $field_alias = $this
      ->randomMachineName();
    $this->sut->field_alias = $field_alias;
    $result_row = new ResultRow([
      $field_alias => $amount,
    ]);
    $method = new \ReflectionMethod($this->sut, 'getAmount');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($amount, $method
      ->invoke($this->sut, $result_row));
  }

  /**
   * @covers ::getCurrency
   */
  function testGetCurrencyWithoutLoadableCurrencies() {
    $this
      ->expectException(RuntimeException::class);
    $this->currencyStorage
      ->expects($this
      ->atLeastOnce())
      ->method('load')
      ->willReturn(NULL);
    $result_row = new ResultRow();
    $method = new \ReflectionMethod($this->sut, 'getCurrency');
    $method
      ->setAccessible(TRUE);
    $method
      ->invoke($this->sut, $result_row);
  }

  /**
   * @covers ::getCurrency
   */
  function testGetCurrencyWithFallbackCurrency() {
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $map = [
      [
        'XXX',
        $currency,
      ],
    ];
    $this->currencyStorage
      ->expects($this
      ->atLeastOnce())
      ->method('load')
      ->willReturnMap($map);
    $result_row = new ResultRow();
    $method = new \ReflectionMethod($this->sut, 'getCurrency');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($currency, $method
      ->invoke($this->sut, $result_row));
  }

  /**
   * @covers ::getCurrency
   */
  function testGetCurrencyWithFixedCurrency() {
    $currency_code = $this
      ->randomMachineName();
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $map = [
      [
        $currency_code,
        $currency,
      ],
    ];
    $this->currencyStorage
      ->expects($this
      ->atLeastOnce())
      ->method('load')
      ->willReturnMap($map);
    $result_row = new ResultRow();
    $configuration = [];
    $plugin_id = $this
      ->randomMachineName();
    $this->pluginDefinition['currency_code'] = $currency_code;
    $this->sut = new Amount($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->moduleHandler, $this->renderer, $this->currencyStorage);
    $this->sut
      ->init($this->viewsViewExecutable, $this->viewsDisplayHandler);
    $method = new \ReflectionMethod($this->sut, 'getCurrency');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($currency, $method
      ->invoke($this->sut, $result_row));
  }

  /**
   * @covers ::getCurrency
   */
  function testGetCurrencyWithCurrencyCodeField() {
    $currency_code = $this
      ->randomMachineName();
    $currency_code_field = $this
      ->randomMachineName();
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $map = [
      [
        $currency_code,
        $currency,
      ],
    ];
    $this->currencyStorage
      ->expects($this
      ->atLeastOnce())
      ->method('load')
      ->willReturnMap($map);
    $field_alias = $this
      ->randomMachineName();
    $result_row = new ResultRow([
      $field_alias => $currency_code,
    ]);
    $configuration = [];
    $plugin_id = $this
      ->randomMachineName();
    $this->pluginDefinition['currency_code_field'] = $currency_code_field;
    $this->sut = new Amount($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->moduleHandler, $this->renderer, $this->currencyStorage);
    $this->sut
      ->init($this->viewsViewExecutable, $this->viewsDisplayHandler);
    $this->sut->aliases['currency_code_field'] = $field_alias;
    $method = new \ReflectionMethod($this->sut, 'getCurrency');
    $method
      ->setAccessible(TRUE);
    $this
      ->assertSame($currency, $method
      ->invoke($this->sut, $result_row));
  }

  /**
   * @covers ::render
   *
   * @dataProvider providerTestRender
   *
   * @depends testGetAmount
   * @depends testGetCurrencyWithoutLoadableCurrencies
   * @depends testGetCurrencyWithFallbackCurrency
   * @depends testGetCurrencyWithFixedCurrency
   * @depends testGetCurrencyWithCurrencyCodeField
   */
  function testRender($round) {
    $amount = mt_rand();
    $formatted_amount = $this
      ->randomMachineName();
    $field_alias = $this
      ->randomMachineName();
    $currency_code = $this
      ->randomMachineName();
    $currency = $this
      ->createMock(CurrencyInterface::class);
    $currency
      ->expects($this
      ->atLeastOnce())
      ->method('formatAmount')
      ->with($amount, $round)
      ->willReturn($formatted_amount);
    $this->currencyStorage
      ->expects($this
      ->atLeastOnce())
      ->method('load')
      ->with($currency_code)
      ->willReturn($currency);
    $result_row = new ResultRow([
      $field_alias => $amount,
    ]);
    $configuration = [];
    $plugin_id = $this
      ->randomMachineName();
    $this->pluginDefinition['currency_code'] = $currency_code;
    $options = [
      'currency_round' => $round,
    ];
    $this->sut = new Amount($configuration, $plugin_id, $this->pluginDefinition, $this->stringTranslation, $this->moduleHandler, $this->renderer, $this->currencyStorage);
    $this->sut
      ->init($this->viewsViewExecutable, $this->viewsDisplayHandler, $options);
    $this->sut->field_alias = $field_alias;
    $this
      ->assertSame($formatted_amount, $this->sut
      ->render($result_row));
  }

  /**
   * Provides data to self::testRender().
   */
  public function providerTestRender() {
    return [
      [
        TRUE,
      ],
      [
        FALSE,
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmountTest::$configFactory protected property The config factory.
AmountTest::$currencyStorage protected property The currency storage.
AmountTest::$moduleHandler protected property The module handler.
AmountTest::$pluginDefinition protected property The plugin definiton.
AmountTest::$renderer protected property The renderer.
AmountTest::$stringTranslation protected property The string translator.
AmountTest::$sut protected property The class under test.
AmountTest::$viewsDisplayHandler protected property The Views display handler.
AmountTest::$viewsQuery protected property The Views query.
AmountTest::$viewsViewExecutable protected property The Views view executable.
AmountTest::providerTestRender public function Provides data to self::testRender().
AmountTest::setUp public function Overrides UnitTestCase::setUp
AmountTest::testBuildOptionsForm public function @covers ::buildOptionsForm
AmountTest::testCreate public function @covers ::create @covers ::__construct
AmountTest::testDefaultDefinition function @covers ::defaultDefinition
AmountTest::testDefineOptions public function @covers ::defineOptions
AmountTest::testGetAmount function @covers ::getAmount
AmountTest::testGetCurrencyWithCurrencyCodeField function @covers ::getCurrency
AmountTest::testGetCurrencyWithFallbackCurrency function @covers ::getCurrency
AmountTest::testGetCurrencyWithFixedCurrency function @covers ::getCurrency
AmountTest::testGetCurrencyWithoutLoadableCurrencies function @covers ::getCurrency
AmountTest::testRender 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.