/** * Creates a currency entity from a currency from the repository. * * @param string $currency_code * * @return \Drupal\currency\Entity\CurrencyInterface * * @throws \InvalidArgumentException * Thrown when no currency with the given currency code exists. */ protected function createCurrencyFromRepository($currency_code) { /** @var \Drupal\currency\Entity\CurrencyInterface $currency_entity */ $currency_entity = $this->currencyStorage->create(); $currency_resource = $this->currencyResourceRepository->loadCurrency($currency_code); if (is_null($currency_resource)) { throw new \InvalidArgumentException(sprintf('No currency with currency code %s exists.', $currency_code)); } $currency_entity->setCurrencyCode($currency_resource->getCurrencyCode()); $currency_entity->setCurrencyNumber($currency_resource->getCurrencyNumber()); $currency_entity->setLabel($currency_resource->getLabel()); $currency_entity->setSign($currency_resource->getSign()); $currency_entity->setAlternativeSigns($currency_resource->getAlternativeSigns()); $currency_entity->setSubunits($currency_resource->getSubunits()); $currency_entity->setRoundingStep($currency_resource->getRoundingStep()); $currency_entity->setUsages($currency_resource->getUsages()); return $currency_entity; }
/** * @covers ::loadCurrency * * @dataProvider providerLoadCurrencyWithNonExistentCurrencies */ public function testLoadCurrencyWithNonExistentCurrencies($currencyCode) { $this->assertNull($this->sut->loadCurrency($currencyCode)); }
/** * @covers ::importCurrency * @covers ::createCurrencyFromRepository */ public function testImportCurrency() { $resource_repository = new ResourceRepository(); $currency_codes = $resource_repository->listCurrencies(); $currency_code = $currency_codes[array_rand($currency_codes)]; $currency = $this->getMock(CurrencyInterface::class); $this->currencyStorage->expects($this->atLeastOnce())->method('create')->with()->willReturn($currency); $this->sut->setConfigStorage($this->configStorage); $this->assertSame($currency, $this->sut->importCurrency($currency_code)); }