| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Drupal\DrupalExtension\Context; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Behat\Behat\Context\TranslatableContext; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Provides step definitions for interacting directly with Drush commands. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class DrushContext extends RawDrupalContext implements TranslatableContext { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |    * Keep track of drush output. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |    * @var string|boolean | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |   protected $drushOutput; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |    * {@inheritDoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |   public static function getTranslationResources() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     return glob(__DIR__ . '/../../../../i18n/*.xliff'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |    * Return the most recent drush command output. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |    * @return string | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 30 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  |   public function readDrushOutput() { | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  |     if (!isset($this->drushOutput)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  |       throw new \RuntimeException('No drush output was found.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     } | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     return $this->drushOutput; | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |    * @Given I run drush :command | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |   public function assertDrushCommand($command) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     if (!$this->drushOutput = $this->getDriver('drush')->$command()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |        $this->drushOutput = TRUE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |    * @Given I run drush :command :arguments | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |   public function assertDrushCommandWithArgument($command, $arguments) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     $this->drushOutput = $this->getDriver('drush')->$command($this->fixStepArgument($arguments)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     if (!isset($this->drushOutput)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |       $this->drushOutput = TRUE; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |    * @Then drush output should contain :output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |   public function assertDrushOutput($output) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 61 |  | View Code Duplication |     if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) === FALSE) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |       throw new \Exception(sprintf("The last drush command output did not contain '%s'.\nInstead, it was:\n\n%s'", $output, $this->drushOutput)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |    * @Then drush output should match :regex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |   public function assertDrushOutputMatches($regex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     if (!preg_match($regex, (string) $this->readDrushOutput())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |       throw new \Exception(sprintf("The pattern %s was not found anywhere in the drush output.\nOutput:\n\n%s", $regex, $this->drushOutput)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |    * @Then drush output should not contain :output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |   public function drushOutputShouldNotContain($output) { | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 79 |  | View Code Duplication |     if (strpos((string) $this->readDrushOutput(), $this->fixStepArgument($output)) !== FALSE) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         throw new \Exception(sprintf("The last drush command output did contain '%s' although it should not.\nOutput:\n\n%s'", $output, $this->drushOutput)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |    * @Then print last drush output | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |   public function printLastDrushOutput() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     echo $this->readDrushOutput(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |    * Returns fixed step argument (with \\" replaced back to "). | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |    * @param string $argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |    * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |   protected function fixStepArgument($argument) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     return str_replace('\\"', '"', $argument); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 102 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 103 |  |  |  | 
            
                        
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.