AmountFormatterManagerTest.php in Currency 8.3
File
tests/src/Unit/Plugin/Currency/AmountFormatter/AmountFormatterManagerTest.php
View source
<?php
namespace Drupal\Tests\currency\Unit\Plugin\Currency\AmountFormatter;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Config\Config;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterInterface;
use Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterManager;
use Drupal\Tests\UnitTestCase;
use Zend\Stdlib\ArrayObject;
class AmountFormatterManagerTest extends UnitTestCase {
protected $cache;
protected $configFactory;
protected $discovery;
protected $factory;
protected $moduleHandler;
public $sut;
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);
}
public function testGetFallbackPluginId() {
$plugin_id = $this
->randomMachineName();
$plugin_configuration = array(
$this
->randomMachineName(),
);
$this
->assertIsString($this->sut
->getFallbackPluginId($plugin_id, $plugin_configuration));
}
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());
}
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)));
}
public function testGetDefaultPlugin() {
$namespaces = new ArrayObject();
$default_plugin_id = $this
->randomMachineName();
$formatter = $this
->createMock(AmountFormatterInterface::class);
$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()));
}
}