| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the PHP Translation package. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * (c) PHP Translation team <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please view the LICENSE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * file that was distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | namespace Translation\SymfonyStorage\Tests\Unit\Loader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Symfony\Component\HttpKernel\Kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Translation\Exception\InvalidResourceException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Translation\MessageCatalogue; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use Translation\SymfonyStorage\Loader\XliffLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author Tobias Nyholm <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | class XliffLoaderTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @expectedException \Symfony\Component\Translation\Exception\InvalidResourceException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 28 |  |  |     public function testEmptyContent() | 
            
                                                                        
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  |         $loader = new XliffLoader(); | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |         $loader->extractFromContent(' ', new MessageCatalogue('en'), 'messages'); | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     public function testInvalidContent() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $loader = new XliffLoader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         try { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $loader->extractFromContent('Foobar', new MessageCatalogue('en'), 'messages'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         } catch (InvalidResourceException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |             $invalidArgument = $e->getPrevious(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |             $this->assertNotNull($invalidArgument); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             $this->assertContains('[ERROR 4] Start tag expected', $invalidArgument->getMessage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $this->fail('XliffLoader must throw exception on invalid XML'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 50 |  | View Code Duplication |     public function testXliff12() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         if (Kernel::VERSION_ID < 20800) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $this->markTestSkipped('Symfony <2.8 is not supported. '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $content = file_get_contents(__DIR__.'/../../Fixtures/single-file/messages.en.xlf'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         $catalogue = new MessageCatalogue('en'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         (new XliffLoader())->extractFromContent($content, $catalogue, 'messages'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         $this->assertTrue($catalogue->defines('test_0')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->assertTrue($catalogue->defines('test_1')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 63 |  | View Code Duplication |     public function testXliff20() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if (Kernel::VERSION_ID < 20800) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $this->markTestSkipped('Symfony <2.8 is not supported. '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $content = <<<'XML' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | <xliff xmlns="urn:oasis:names:tc:xliff:document:2.0" version="2.0" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  srcLang="en-US" trgLang="sv"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  <file id="f1" original="Example"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |   <skeleton href="Example"/> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |   <unit id="1"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |    <segment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     <source>key0</source> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     <target>Foo</target> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |    </segment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |   </unit> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |   <unit id="2"> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |    <segment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     <source>key1</source> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     <target>Bar</target> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |    </segment> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |   </unit> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  </file> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | </xliff> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | XML; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $catalogue = new MessageCatalogue('en'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         (new XliffLoader())->extractFromContent($content, $catalogue, 'messages'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->assertTrue($catalogue->defines('key0')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $this->assertTrue($catalogue->defines('key1')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     public function testXliff20Meta() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         if (Kernel::VERSION_ID < 20800) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |             $this->markTestSkipped('Symfony <2.8 is not supported. '); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $content = file_get_contents(__DIR__.'/../../Fixtures/meta.en.xlf'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $catalogue = new MessageCatalogue('en'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         (new XliffLoader())->extractFromContent($content, $catalogue, 'messages'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $this->assertTrue($catalogue->defines('foo')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $metadata = $catalogue->getMetadata('foo'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $this->assertNotEmpty($metadata); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->assertCount(3, $metadata['notes']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->assertEquals('state', $metadata['notes'][0]['category']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->assertEquals('new', $metadata['notes'][0]['content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->assertEquals('approved', $metadata['notes'][1]['category']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->assertEquals('true', $metadata['notes'][1]['content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->assertEquals('section', $metadata['notes'][2]['category']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $this->assertEquals('1', $metadata['notes'][2]['priority']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $this->assertEquals('user login', $metadata['notes'][2]['content']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 121 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 122 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.