public function testFindCurrencyByIso() { $data = [['name' => 'US Dollar', 'iso_code' => 'USD', 'convert_nominal' => 1, 'convert_rate' => 80, 'sort_order' => 1, 'format_string' => '$ #', 'thousands_sep' => '.', 'dec_point' => ',', 'is_main' => 0, 'intl_formatting' => 1], 'Euro' => ['name' => 'Euro', 'iso_code' => 'EUR', 'convert_rate' => 90, 'format_string' => '€ #', 'is_main' => 1]]; $storage = (new Currency())->getStorage(); FunctionalHelper::flushStorage($storage); $this->assertFileNotExists($storage); CurrencyStorageHelper::generateStorage($data, $storage, Currency::className()); $c = CurrencyHelper::findCurrencyByIso('USD'); $this->assertEquals('US Dollar', $c->name); return $c; }
/** * Using PHPUnit with static variables are painfully. Because of it this single test executes in different Class then others. */ public function testGetMainCurrencyNotExists() { $data = [['name' => 'US Dollar', 'iso_code' => 'USD', 'convert_nominal' => 1, 'convert_rate' => 62.83, 'sort_order' => 1, 'format_string' => '$ #', 'thousands_sep' => '.', 'dec_point' => ',', 'is_main' => 0], 'Euro' => ['name' => 'Euro', 'iso_code' => 'EUR', 'convert_rate' => 71.31999999999999, 'format_string' => '€ #', 'is_main' => 0]]; $storage = (new Currency())->getStorage(); FunctionalHelper::flushStorage($storage); $this->assertFileNotExists($storage); CurrencyStorageHelper::generateStorage($data, $storage, Currency::className()); $this->assertFileExists($storage); $c = CurrencyHelper::getMainCurrency(); $this->assertNull($c); FunctionalHelper::flushCurrencyStorage(); }
/** * @expectedException \yii\base\InvalidParamException */ public function testRemoveFromStorageWrongModel() { CurrencyStorageHelper::removeFromStorage('im a Currency model, btc!'); }
/** * Saves model data * * @return bool */ public function save() { $this->beforeSave($this->isNewItem()); return CurrencyStorageHelper::updateStorage($this, static::$storage); }
/** * Loads Currency[] & CurrencyRateProvider[] from associated storage files and * generates storage files if them not exists yet. Using $loadDefaults storage can be generated empty or * with default data * * @param $className * @param bool $ignoreCache * @param bool $loadDefaults * @throws InvalidParamException * @return mixed */ public function getData($className, $ignoreCache = false, $loadDefaults = true) { if (false === class_exists($className)) { throw new InvalidParamException(Yii::t('dotplant.currencies', 'Class "{className}" not found!', ['className' => $className])); } /** @var BaseFileModel $model */ $model = new $className(); if (false === $model instanceof BaseFileModel) { throw new InvalidParamException(Yii::t('dotplant.currencies', 'Class "{className}" must be an instance of "BaseFileModel" !', ['className' => $className])); } if (0 === count(self::$items[$className]) || true === $ignoreCache) { $canLoad = false; $storage = $model->getStorage(); if (true === file_exists($storage) && is_readable($storage)) { $canLoad = true; } else { $data = []; if (true === $loadDefaults) { $data = self::$defaults[$className]; } $canLoad = CurrencyStorageHelper::generateStorage($data, $storage, $className); } if (true === $canLoad) { self::$items[$className] = (include $storage); } else { Yii::$app->session->setFlash('error', Yii::t('dotplant.currencies', 'Unable to write "{storage}" file.', ['storage' => $storage])); } } return self::$items[$className]; }