@@ -523,15 +523,35 @@ public function scrollToTopOfPage()
523523 */
524524 public function magentoCLI ($ command , $ timeout = null , $ arguments = null )
525525 {
526- $ magentoBinary = realpath (MAGENTO_BP . DIRECTORY_SEPARATOR . 'bin ' . DIRECTORY_SEPARATOR . 'magento ' );
527- $ valid = $ this ->validateCommand ($ magentoBinary , $ command );
528- // execute from shell when running tests from web root -- excluding cron
529- //TODO: investigate why cron:run hangs with shell execution on pipeline
530- if ($ valid && strpos ($ command , self ::COMMAND_CRON_RUN ) === false ) {
531- return $ this ->shellExecMagentoCLI ($ magentoBinary , $ command , $ timeout , $ arguments );
532- } else {
533- return $ this ->curlExecMagentoCLI ($ command , $ timeout , $ arguments );
534- }
526+ // Remove index.php if it's present in url
527+ $ baseUrl = rtrim (
528+ str_replace ('index.php ' , '' , rtrim ($ this ->config ['url ' ], '/ ' )),
529+ '/ '
530+ );
531+
532+ $ apiURL = UrlFormatter::format (
533+ $ baseUrl . '/ ' . ltrim (getenv ('MAGENTO_CLI_COMMAND_PATH ' ), '/ ' ),
534+ false
535+ );
536+
537+ $ restExecutor = new WebapiExecutor ();
538+ $ executor = new CurlTransport ();
539+ $ executor ->write (
540+ $ apiURL ,
541+ [
542+ 'token ' => $ restExecutor ->getAuthToken (),
543+ getenv ('MAGENTO_CLI_COMMAND_PARAMETER ' ) => $ command ,
544+ 'arguments ' => $ arguments ,
545+ 'timeout ' => $ timeout ,
546+ ],
547+ CurlInterface::POST ,
548+ []
549+ );
550+ $ response = $ executor ->read ();
551+ $ restExecutor ->close ();
552+ $ executor ->close ();
553+
554+ return $ response ;
535555 }
536556
537557 /**
@@ -839,133 +859,4 @@ public function makeScreenshot($name = null)
839859 $ this ->debug ("Screenshot saved to $ screenName " );
840860 AllureHelper::addAttachmentToCurrentStep ($ screenName , 'Screenshot ' );
841861 }
842-
843- /**
844- * Takes given $command and executes it against bin/magento executable. Returns stdout output from the command.
845- *
846- * @param string $magentoBinary
847- * @param string $command
848- * @param integer $timeout
849- * @param string $arguments
850- *
851- * @throws \RuntimeException
852- * @return string
853- */
854- private function shellExecMagentoCLI ($ magentoBinary , $ command , $ timeout , $ arguments ): string
855- {
856- $ php = PHP_BINDIR ? PHP_BINDIR . DIRECTORY_SEPARATOR . 'php ' : 'php ' ;
857- $ fullCommand = $ php . ' -f ' . $ magentoBinary . ' ' . $ command . ' ' . $ arguments ;
858- $ process = Process::fromShellCommandline (escapeshellcmd ($ fullCommand ), MAGENTO_BP );
859- $ process ->setIdleTimeout ($ timeout );
860- $ process ->setTimeout (0 );
861- try {
862- $ process ->run ();
863- $ output = $ process ->getOutput ();
864- if (!$ process ->isSuccessful ()) {
865- $ failureOutput = $ process ->getErrorOutput ();
866- if (!empty ($ failureOutput )) {
867- $ output = $ failureOutput ;
868- }
869- }
870- if (empty ($ output )) {
871- $ output = "CLI did not return output. " ;
872- }
873- } catch (ProcessTimedOutException $ exception ) {
874- $ output = "CLI command timed out, no output available. " ;
875- }
876-
877- if ($ this ->checkForFilePath ($ output )) {
878- $ output = "CLI output suppressed, filepath detected in output. " ;
879- }
880-
881- $ exitCode = $ process ->getExitCode ();
882-
883- if ($ exitCode !== 0 ) {
884- throw new \RuntimeException ($ process ->getErrorOutput ());
885- }
886- return $ output ;
887- }
888-
889- /**
890- * Takes given $command and executes it against exposed MTF CLI entry point. Returns response from server.
891- *
892- * @param string $command
893- * @param integer $timeout
894- * @param string $arguments
895- *
896- * @return string
897- * @throws TestFrameworkException
898- */
899- private function curlExecMagentoCLI ($ command , $ timeout , $ arguments ): string
900- {
901- // Remove index.php if it's present in url
902- $ baseUrl = rtrim (
903- str_replace ('index.php ' , '' , rtrim ($ this ->config ['url ' ], '/ ' )),
904- '/ '
905- );
906-
907- $ apiURL = UrlFormatter::format (
908- $ baseUrl . '/ ' . ltrim (getenv ('MAGENTO_CLI_COMMAND_PATH ' ), '/ ' ),
909- false
910- );
911-
912- $ restExecutor = new WebapiExecutor ();
913- $ executor = new CurlTransport ();
914- $ executor ->write (
915- $ apiURL ,
916- [
917- 'token ' => $ restExecutor ->getAuthToken (),
918- getenv ('MAGENTO_CLI_COMMAND_PARAMETER ' ) => $ command ,
919- 'arguments ' => $ arguments ,
920- 'timeout ' => $ timeout ,
921- ],
922- CurlInterface::POST ,
923- []
924- );
925- $ response = $ executor ->read ();
926- $ restExecutor ->close ();
927- $ executor ->close ();
928-
929- return $ response ;
930- }
931-
932- /**
933- * Checks magento list of CLI commands for given $command. Does not check command parameters, just base command.
934- *
935- * @param string $magentoBinary
936- * @param string $command
937- *
938- * @return boolean
939- */
940- private function validateCommand ($ magentoBinary , $ command )
941- {
942- exec ($ magentoBinary . ' list ' , $ commandList );
943- // Trim list of commands after first whitespace
944- $ commandList = array_map ([$ this , 'trimAfterWhitespace ' ], $ commandList );
945- return in_array ($ this ->trimAfterWhitespace ($ command ), $ commandList );
946- }
947-
948- /**
949- * Returns given string trimmed of everything after the first found whitespace.
950- *
951- * @param string $string
952- *
953- * @return string
954- */
955- private function trimAfterWhitespace ($ string )
956- {
957- return strtok ($ string , ' ' );
958- }
959-
960- /**
961- * Detects file path in string.
962- *
963- * @param string $string
964- *
965- * @return boolean
966- */
967- private function checkForFilePath ($ string )
968- {
969- return preg_match ('/\/[\S]+\// ' , $ string );
970- }
971862}
0 commit comments