| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Functional test for cache handling for non-existent translatable domains on Container build time. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * On Container build time, only existent Translatable domains are added to the Translator map. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * So they will not be loaded until the Container is rebuild. We fixed that by creating our own | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Translator and I18nCacheUtils. This test ensures that this works as expected for the user. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * IMPORTANT: In order for this test to stay relevant, the service definition for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * /external/translatable MUST NOT contain ANY fixtures as this would produce the domain at Container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * build time! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | namespace Graviton\I18nBundle\Tests\Controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use Graviton\TestBundle\Test\RestTestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use Symfony\Component\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Symfony\Component\Finder\Finder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @license  http://opensource.org/licenses/gpl-license.php GNU Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @link     http://swisscom.ch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | class ExternalTranslationDomainControllerTest extends RestTestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * setup function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @return void | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 31 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     public function setUp() | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |         $this->loadFixtures( | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |             array( | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |                 'Graviton\I18nBundle\DataFixtures\MongoDB\LoadLanguageData', | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |                 'Graviton\I18nBundle\DataFixtures\MongoDB\LoadMultiLanguageData', | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |                 'Graviton\I18nBundle\DataFixtures\MongoDB\LoadTranslatableData', | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |             ), | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             null, | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |             'doctrine_mongodb' | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |         // make sure we have no resource files for domain 'external' | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |         $this->cleanFiles(); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * cleanup actions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->cleanFiles(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * clean up our mess | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 63 |  | View Code Duplication |     private function cleanFiles() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $fs = new Filesystem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $finder = new Finder(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         $finder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             ->files() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             ->in(__DIR__.'/../../Resources/translations') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ->name('external.*.*'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         foreach ($finder as $file) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             $fs->remove($file->getRealPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * see if a non existent domain gets translated correctly. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * it must invalidate the cache and trigger a reload of the translations in order for this to work. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function testCacheUtils() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $resource = new \stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $resource->id = 'test'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $resource->myString = new \stdClass; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $resource->myString->en = 'The John'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         $resource->myString->de = 'Der Hans'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $resource->myString->fr = 'Le Jean'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $client = static::createRestClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $client->put( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             '/external/translatable/test', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             $resource, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             array('HTTP_ACCEPT_LANGUAGE' => 'en,de,fr') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->assertEquals(204, $client->getResponse()->getStatusCode()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         $client = static::createRestClient(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $client->request( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             'GET', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |             '/external/translatable/test', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             array(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             array('HTTP_ACCEPT_LANGUAGE' => 'en,de,fr') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $results = $client->getResults(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $this->assertEquals($resource->id, $results->id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->assertEquals($resource->myString->en, $results->myString->en); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $this->assertEquals($resource->myString->de, $results->myString->de); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->assertEquals($resource->myString->fr, $results->myString->fr); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 119 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 120 |  |  |  | 
            
                        
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.