You are here

class AmountFormatterManagerTest in Currency 8.3

@coversDefaultClass \Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterManager

@group Currency

Hierarchy

Expanded class hierarchy of AmountFormatterManagerTest

File

tests/src/Unit/Plugin/Currency/AmountFormatter/AmountFormatterManagerTest.php, line 21

Namespace

Drupal\Tests\currency\Unit\Plugin\Currency\AmountFormatter
View source
class AmountFormatterManagerTest extends UnitTestCase {

  /**
   * The cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $cache;

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

  /**
   * The plugin discovery.
   *
   * @var \Drupal\Component\Plugin\Discovery\DiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $discovery;

  /**
   * The plugin factory.
   *
   * @var \Drupal\Component\Plugin\Factory\DefaultFactory|\PHPUnit_Framework_MockObject_MockObject
   */
  protected $factory;

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

  /**
   * The class under test.
   *
   * @var \Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterManager
   */
  public $sut;

  /**
   * {@inheritdoc}
   */
  public function setUp() : void {
    $this->discovery = $this
      ->createMock(DiscoveryInterface::class);
    $this->factory = $this
      ->getMockBuilder(DefaultFactory::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->moduleHandler = $this
      ->createMock(ModuleHandlerInterface::class);
    $this->cache = $this
      ->createMock(CacheBackendInterface::class);
    $this->configFactory = $this
      ->createMock(ConfigFactoryInterface::class);
    $namespaces = new ArrayObject();
    $this->sut = new AmountFormatterManager($namespaces, $this->cache, $this->moduleHandler, $this->configFactory);
    $discovery_property = new \ReflectionProperty($this->sut, 'discovery');
    $discovery_property
      ->setAccessible(TRUE);
    $discovery_property
      ->setValue($this->sut, $this->discovery);
    $factory_property = new \ReflectionProperty($this->sut, 'factory');
    $factory_property
      ->setAccessible(TRUE);
    $factory_property
      ->setValue($this->sut, $this->factory);
  }

  /**
   * @covers ::getFallbackPluginId
   */
  public function testGetFallbackPluginId() {
    $plugin_id = $this
      ->randomMachineName();
    $plugin_configuration = array(
      $this
        ->randomMachineName(),
    );
    $this
      ->assertIsString($this->sut
      ->getFallbackPluginId($plugin_id, $plugin_configuration));
  }

  /**
   * @covers ::getDefaultPluginId
   */
  public function testGetDefaultPluginId() {
    $plugin_id = $this
      ->randomMachineName();
    $config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('get')
      ->with('plugin_id')
      ->willReturn($plugin_id);
    $this->configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('currency.amount_formatting')
      ->willReturn($config);
    $this
      ->assertSame($plugin_id, $this->sut
      ->getDefaultPluginId());
  }

  /**
   * @covers ::setDefaultPluginId
   */
  public function testSetDefaultPluginId() {
    $plugin_id = $this
      ->randomMachineName();
    $config = $this
      ->getMockBuilder(Config::class)
      ->disableOriginalConstructor()
      ->getMock();
    $config
      ->expects($this
      ->once())
      ->method('set')
      ->with('plugin_id', $plugin_id)
      ->will($this
      ->returnSelf());
    $config
      ->expects($this
      ->once())
      ->method('save');
    $this->configFactory
      ->expects($this
      ->once())
      ->method('get')
      ->with('currency.amount_formatting')
      ->willReturn($config);
    $this
      ->assertSame(spl_object_hash($this->sut), spl_object_hash($this->sut
      ->setDefaultPluginId($plugin_id)));
  }

  /**
   * @covers ::getDefaultPlugin
   */
  public function testGetDefaultPlugin() {
    $namespaces = new ArrayObject();
    $default_plugin_id = $this
      ->randomMachineName();
    $formatter = $this
      ->createMock(AmountFormatterInterface::class);

    /** @var \Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterManager|\PHPUnit_Framework_MockObject_MockObject $currency_amount_formatter_manager */
    $currency_amount_formatter_manager = $this
      ->getMockBuilder(AmountFormatterManager::class)
      ->setConstructorArgs(array(
      $namespaces,
      $this->cache,
      $this->moduleHandler,
      $this->configFactory,
    ))
      ->setMethods(array(
      'getDefaultPluginId',
      'createInstance',
    ))
      ->getMock();
    $currency_amount_formatter_manager
      ->expects($this
      ->once())
      ->method('getDefaultPluginId')
      ->willReturn($default_plugin_id);
    $currency_amount_formatter_manager
      ->expects($this
      ->once())
      ->method('createInstance')
      ->with($default_plugin_id)
      ->willReturn($formatter);
    $this
      ->assertSame(spl_object_hash($formatter), spl_object_hash($currency_amount_formatter_manager
      ->getDefaultPlugin()));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmountFormatterManagerTest::$cache protected property The cache backend.
AmountFormatterManagerTest::$configFactory protected property The config factory.
AmountFormatterManagerTest::$discovery protected property The plugin discovery.
AmountFormatterManagerTest::$factory protected property The plugin factory.
AmountFormatterManagerTest::$moduleHandler protected property The module handler.
AmountFormatterManagerTest::$sut public property The class under test.
AmountFormatterManagerTest::setUp public function Overrides UnitTestCase::setUp
AmountFormatterManagerTest::testGetDefaultPlugin public function @covers ::getDefaultPlugin
AmountFormatterManagerTest::testGetDefaultPluginId public function @covers ::getDefaultPluginId
AmountFormatterManagerTest::testGetFallbackPluginId public function @covers ::getFallbackPluginId
AmountFormatterManagerTest::testSetDefaultPluginId public function @covers ::setDefaultPluginId
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.