public function testToArray() { $tmxFile = new Application_Util_TmxFile(); $tmxFile->setVariantSegment('test_translation_unit', 'de', 'Testübersetzung'); $tmxFile->setVariantSegment('test_translation_unit', 'en', 'Test translation'); $tmxFile->save(APPLICATION_PATH . DIRECTORY_SEPARATOR . $this->tmxTarget); foreach ($this->contentFiles as $contentFile) { file_put_contents(APPLICATION_PATH . DIRECTORY_SEPARATOR . $this->contentBasepath . DIRECTORY_SEPARATOR . $contentFile, 'Test Data'); } $array = $this->object->toArray(); foreach ($this->contentFiles as $lang => $contentFile) { // $this->assertTrue $this->assertEquals($contentFile, $array[$lang]['file']['filename'], "Expected file '{$contentFile}' in array."); $this->assertEquals('Test Data', $array[$lang]['file']['contents']); } $this->assertTrue(isset($array['de']['key']['test_translation_unit']), "Expected translation unit 'test_translation_unit'"); $this->assertTrue(isset($array['en']['key']['test_translation_unit']), "Expected translation unit 'test_translation_unit'"); $this->assertEquals('Testübersetzung', $array['de']['key']['test_translation_unit']); $this->assertEquals('Test translation', $array['en']['key']['test_translation_unit']); }
/** * Store all tmx and content that has been set. * This is performed as a transaction, reverting * changes already written if one write fails. * * @return bool Returns true on success, false on failure. */ public function store() { $result = true; $tmxBackup = $savedContent = array(); try { if (!empty($this->_tmxContents)) { $this->verifyWriteAccess($this->_tmxTarget); $tmxFile = new Application_Util_TmxFile(); if (is_file($this->_tmxTarget)) { $tmxFile->load($this->_tmxTarget); // backup in case a write operation fails $tmxBackup = $tmxFile->toArray(); } $tmxFile->fromArray($this->_tmxContents); $result = $tmxFile->save($this->_tmxTarget); if (!$result) { throw new Setup_Model_Exception("Saving File '{$this->_tmxTarget}' failed"); } } foreach ($this->_contentSources as $filename => $contents) { if (!is_null($contents)) { $this->verifyWriteAccess($filename); // backup stored content to restore in case a write operation fails $savedContent[$filename] = file_get_contents($filename); $result = false !== file_put_contents($filename, $contents) && $result; if (!$result) { throw new Setup_Model_Exception("Saving File '{$filename}' failed"); } } } } catch (Setup_Model_Exception $se) { if (!empty($tmxBackup)) { $tmxFile = new Application_Util_TmxFile(); $tmxFile->fromArray($tmxBackup); $tmxFile->save($this->_tmxTarget); } if (!empty($savedContent)) { foreach ($savedContent as $filename => $contents) { file_put_contents($filename, $contents); } } $this->log($se->getMessage()); $result = false; } return $result; }