| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Sepia\Test; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Sepia\PoParser\Catalog; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Sepia\PoParser\PoCompiler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Sepia\PoParser\PoReader\FileHandler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class WriteTest extends AbstractFixtureTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     public function test_write() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |         $faker = \Faker\Factory::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |         $catalogSource = new Catalog(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         // Normal Entry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         $entry = Catalog\EntryFactory::createFromArray(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |             'msgid' => 'string.1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |             'msgstr' => 'translation.1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |             'msgctxt' => 'context.1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'reference' => array('src/views/forms.php:44'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'tcomment' => array('translator comment'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             'ccomment' => array('code comment'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |             'flags' => array('1', '2', '3') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $previousEntry = Catalog\EntryFactory::createFromArray(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |            'msgid' => 'previous.string.1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |            'msgctxt' => 'previous.context.1' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $entry->setPreviousEntry($previousEntry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $catalogSource->addEntry($entry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         // Obsolete entry | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $entry = Catalog\EntryFactory::createFromArray(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |             'msgid' => 'obsolete.1', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'msgstr' => $faker->paragraph(5), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'msgctxt' => 'obsolete.context', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |             'obsolete' => true | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $catalogSource->addEntry($entry); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $this->saveCatalog($catalogSource); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $catalog = $this->parseFile('temp.po'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->assertPoFile($catalogSource, $catalog); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @throws \Exception | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 51 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |     protected function saveCatalog(Catalog $catalog) | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |         $fileHandler = new FileHandler($this->resourcesPath.'temp.po'); | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $compiler = new PoCompiler(); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $fileHandler->save( | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |             $compiler->compile($catalog), | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             $this->resourcesPath.'temp.po' | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     private function assertPoFile(Catalog $catalogSource, Catalog $catalogNew) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         foreach ($catalogSource->getEntries() as $entry) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $entryWritten = $catalogNew->getEntry($entry->getMsgId(), $entry->getMsgCtxt()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->assertNotNull($entryWritten, 'Entry not found:'.$entry->getMsgId().','.$entry->getMsgCtxt()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $this->assertEquals($entry->getMsgStr(), $entryWritten->getMsgStr()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             $this->assertEquals($entry->getMsgCtxt(), $entryWritten->getMsgCtxt()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             $this->assertEquals($entry->getFlags(), $entryWritten->getFlags()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             $this->assertEquals($entry->isObsolete(), $entryWritten->isObsolete()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             if ($entry->isObsolete() === true) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                 $this->assertEmpty($entryWritten->getReference()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                 $this->assertEmpty($entryWritten->getTranslatorComments()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 $this->assertEmpty($entryWritten->getDeveloperComments()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                 $this->assertEquals($entry->getReference(), $entryWritten->getReference()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                 $this->assertEquals($entry->getDeveloperComments(), $entryWritten->getDeveloperComments()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 $this->assertEquals($entry->getTranslatorComments(), $entryWritten->getTranslatorComments()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     public function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         parent::tearDown(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if (file_exists($this->resourcesPath.'temp.po')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         //    unlink($this->resourcesPath.'temp.po'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 94 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  |  |