| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * it under the terms of the GNU Lesser General Public License as published by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * PAYONE Magento 2 Connector is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * GNU Lesser General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * You should have received a copy of the GNU Lesser General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * PHP version 5 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * @category  Payone | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @package   Payone_Magento2_Plugin | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @author    FATCHIP GmbH <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @copyright 2003 - 2017 Payone GmbH | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @link      http://www.payone.de | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | namespace Payone\Core\Test\Unit\Observer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use Magento\Catalog\Block\ShortcutButtons; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | use Magento\Framework\Event; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | use Magento\Framework\Event\Observer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | use Magento\Framework\View\LayoutInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | use Magento\Paypal\Block\Express\Shortcut; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | use Payone\Core\Helper\Payment; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | use Payone\Core\Observer\AddPaypalExpressButtons as ClassToTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | class AddPaypalExpressButtonTest extends \PHPUnit_Framework_TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @var ClassToTest | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     private $classToTest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @var ObjectManager | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     private $objectManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @var Payment|\PHPUnit_Framework_MockObject_MockObject | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private $paymentHelper; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                                                    
                                                                                                        
            
            
                | 55 |  | View Code Duplication |     protected function setUp() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         $this->objectManager = new ObjectManager($this); | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         $this->paymentHelper = $this->getMockBuilder(Payment::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         $this->classToTest = $this->objectManager->getObject(ClassToTest::class, [ | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |             'paymentHelper' => $this->paymentHelper | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |         ]); | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 66 |  | View Code Duplication |     public function testExecutePaypalInactive() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         $this->paymentHelper->method('isPayPalExpressActive')->willReturn(false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $result = $this->classToTest->execute($observer); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         $this->assertNull($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 75 |  | View Code Duplication |     public function testExecutePaypalActive() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         $shortcut = $this->getMockBuilder(Shortcut::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $layout = $this->getMockBuilder(LayoutInterface::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         $layout->method('createBlock')->willReturn($shortcut); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $shortcutButtons = $this->getMockBuilder(ShortcutButtons::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $shortcutButtons->method('getLayout')->willReturn($layout); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $event = $this->getMockBuilder(Event::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |             ->setMethods(['getContainer']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         $event->method('getContainer')->willReturn($shortcutButtons); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         $observer->method('getEvent')->willReturn($event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         /** @var Observer $observer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |         $this->classToTest->execute($observer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 100 |  | View Code Duplication |     public function testExecutePaypalActiveAddToCart() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $this->paymentHelper->method('isPayPalExpressActive')->willReturn(true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         $shortcut = $this->getMockBuilder(Shortcut::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         $layout = $this->getMockBuilder(LayoutInterface::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         $layout->method('createBlock')->willReturn($shortcut); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $shortcutButtons = $this->getMockBuilder(ShortcutButtons::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $shortcutButtons->method('getNameInLayout')->willReturn('addtocart.shortcut.buttons'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $shortcutButtons->method('getLayout')->willReturn($layout); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $event = $this->getMockBuilder(Event::class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             ->disableOriginalConstructor() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             ->setMethods(['getContainer']) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             ->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $event->method('getContainer')->willReturn($shortcutButtons); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |         $observer = $this->getMockBuilder(Observer::class)->disableOriginalConstructor()->getMock(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $observer->method('getEvent')->willReturn($event); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         /** @var Observer $observer */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $this->classToTest->execute($observer); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 125 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |  | 
            
                        
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.