| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * This file is part of the Superdesk Web Publisher Templates System. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Copyright 2015 Sourcefabric z.ú. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @copyright 2015 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | namespace SWP\Component\TemplatesSystem\Tests\Twig\Node; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use Doctrine\Common\Cache\ArrayCache; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use SWP\Component\TemplatesSystem\Gimme\Context\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use SWP\Component\TemplatesSystem\Gimme\Factory\MetaFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use SWP\Component\TemplatesSystem\Gimme\Loader\ArticleLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use SWP\Component\TemplatesSystem\Gimme\Loader\ChainLoader; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use SWP\Component\TemplatesSystem\Twig\Extension\GimmeExtension; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use SWP\Component\TemplatesSystem\Twig\Node\GimmeNode; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 25 |  |  | class GimmeNodeTest extends \Twig_Test_NodeTestCase | 
            
                                                                        
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 27 |  | View Code Duplication |     public function testConstructor() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         $annotation = new \Twig_Node([new \Twig_Node_Expression_AssignName('article', 1)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         $parameters = new \Twig_Node_Expression_Array([], 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $body = new \Twig_Node_Text('', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $node = new GimmeNode($annotation, $parameters, $body, 1, 'gimme'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $this->assertEquals($annotation, $node->getNode('annotation')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $this->assertEquals($parameters, $node->getNode('parameters')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $this->assertEquals($body, $node->getNode('body')); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 36 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 38 |  | View Code Duplication |     public function getTests() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $annotation1 = new \Twig_Node([new \Twig_Node_Expression_AssignName('article', 1)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         $parameters1 = new \Twig_Node_Expression_Array([], 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $body1 = new \Twig_Node_Text('Test body', 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         $node1 = new GimmeNode($annotation1, $parameters1, $body1, 1, 'gimme'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $annotation2 = new \Twig_Node([new \Twig_Node_Expression_AssignName('article', 2)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $body2 = new \Twig_Node_Text('Test body', 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $node2 = new GimmeNode($annotation2, null, $body2, 2, 'gimme'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $annotation3 = new \Twig_Node([new \Twig_Node_Expression_AssignName('article', 3)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $parameters3 = new \Twig_Node_Expression_Array([new \Twig_Node_Expression_Constant('foo', 1), new \Twig_Node_Expression_Constant(true, 1)], 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $body3 = new \Twig_Node_Text('Test body', 3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $node3 = new GimmeNode($annotation3, $parameters3, $body3, 3, 'gimme'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             [$node1, <<<'EOF' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | // line 1 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | $swpMetaLoader3 = $this->env->getExtension('swp_gimme')->getLoader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | $context["article"] = $swpMetaLoader3->load("article", array()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | if ($context["article"] !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     echo "Test body"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | unset($context["article"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | EOF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             [$node2, <<<'EOF' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | // line 2 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | $swpMetaLoader4 = $this->env->getExtension('swp_gimme')->getLoader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | $context["article"] = $swpMetaLoader4->load("article", null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | if ($context["article"] !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     echo "Test body"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | unset($context["article"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | EOF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             [$node3, <<<'EOF' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | // line 3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | $swpMetaLoader5 = $this->env->getExtension('swp_gimme')->getLoader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | $context["article"] = $swpMetaLoader5->load("article", array("foo" => true)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | if ($context["article"] !== false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     echo "Test body"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | unset($context["article"]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  | EOF | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         ]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 86 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     public function testTemplateString() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         $loader = new \Twig_Loader_Array([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             'clear_gimme' => '{% gimme article %}{{ article.title }}{% endgimme %}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |             'gimme_with_parameters' => '{% gimme article with {id: 1} %}{{ article.title }}{% endgimme %}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $metaLoader = new ChainLoader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         $context = new Context(new ArrayCache()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $metaLoader->addLoader(new ArticleLoader(__DIR__, new MetaFactory($context))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $twig = new \Twig_Environment($loader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         $twig->addExtension(new GimmeExtension($context, $metaLoader)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->assertEquals($twig->render('clear_gimme'), 'New article'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->assertEquals($twig->render('gimme_with_parameters'), 'New article'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     public function testBrokenTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $loader = new \Twig_Loader_Array([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             'error_gimme' => '{% gimme article {id: 1} %}{{ article.title }}{% endgimme %}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $metaLoader = new ChainLoader(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $context = new Context(new ArrayCache()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $metaLoader->addLoader(new ArticleLoader(__DIR__, new MetaFactory($context))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $twig = new \Twig_Environment($loader); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $twig->addExtension(new GimmeExtension($context, $metaLoader)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->expectException(\Twig_Error_Syntax::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         $twig->render('error_gimme'); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 117 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     protected function tearDown() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $reflection = new \ReflectionObject($this); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         foreach ($reflection->getProperties() as $prop) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             if (!$prop->isStatic() && 0 !== strpos($prop->getDeclaringClass()->getName(), 'PHPUnit_')) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 $prop->setAccessible(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 $prop->setValue($this, null); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 128 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 129 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |  | 
            
                        
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.