/** * Set the currency of the monetary value represented by this * object * * @param string|null $currency * @return $this */ public function setCurrency($currency) { if ($currency) { $currency = strtoupper($currency); Currency::getNumericCode($currency); // ensure currency support $this->currency = $currency; } else { $this->currency = null; } return $this; }
public function testSubUnitsLessThanOneShouldThrowUnexpectedValueException() { $refl = new \ReflectionClass(Currency::class); $property = $refl->getProperty('currencies'); $property->setAccessible(true); $originalCurrencies = $property->getValue(); $newCurrencies = ['ABC' => ['display_name' => 'ABC fake currency', 'numeric_code' => 123, 'default_fraction_digits' => 2, 'sub_unit' => -1]]; $property->setValue($newCurrencies); try { Currency::getSubUnit('ABC'); $this->fail(UnexpectedValueException::class . ' not thrown'); } catch (UnexpectedValueException $exc) { $this->assertInstanceOf(UnexpectedValueException::class, $exc); $this->assertSame(sprintf('The currency sub-units value must be greater than 1; "%s" given', $newCurrencies['ABC']['sub_unit']), $exc->getMessage()); } $property->setValue($originalCurrencies); $property->setAccessible(false); }