StorefrontAnalyzer.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Mtf\Troubleshooting;
  7. use Magento\Mtf\ObjectManagerInterface;
  8. use Magento\Mtf\Troubleshooting\Helper\UrlAnalyzer;
  9. use Magento\Mtf\Util\Protocol\CurlInterface;
  10. use Magento\Mtf\Util\Protocol\CurlTransport;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. /**
  14. * Analyze URL specified in the phpunit.xml.
  15. */
  16. class StorefrontAnalyzer extends \Symfony\Component\Console\Command\Command
  17. {
  18. /**
  19. * HTTP CURL Adapter.
  20. *
  21. * @var CurlTransport
  22. */
  23. private $curlTransport;
  24. /**
  25. * Object manager instance.
  26. *
  27. * @var ObjectManagerInterface
  28. */
  29. private $objectManager;
  30. /**
  31. * Url analyzer helper.
  32. *
  33. * @var UrlAnalyzer
  34. */
  35. private $urlAnalyzer;
  36. /**
  37. * @param ObjectManagerInterface $objectManager
  38. * @param UrlAnalyzer $urlAnalyzer
  39. * @param CurlTransport $curlTransport
  40. */
  41. public function __construct(
  42. ObjectManagerInterface $objectManager,
  43. UrlAnalyzer $urlAnalyzer,
  44. CurlTransport $curlTransport
  45. ) {
  46. parent::__construct();
  47. $this->objectManager = $objectManager;
  48. $this->urlAnalyzer = $urlAnalyzer;
  49. $this->curlTransport = $curlTransport;
  50. }
  51. /**
  52. * Configure command.
  53. *
  54. * @return void
  55. */
  56. protected function configure()
  57. {
  58. parent::configure();
  59. $this->setName('troubleshooting:check-magento-storefront')
  60. ->setDescription('Check that app_frontend_url config is correct and Magento installed.');
  61. }
  62. /**
  63. * Execute command.
  64. *
  65. * @param InputInterface $input
  66. * @param OutputInterface $output
  67. * @return void
  68. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  69. */
  70. protected function execute(InputInterface $input, OutputInterface $output)
  71. {
  72. \PHPUnit\Util\Configuration::getInstance(MTF_PHPUNIT_FILE)->handlePHPConfiguration();
  73. $output = $this->objectManager->create(
  74. \Magento\Mtf\Console\Output::class,
  75. ['output' => $output]
  76. );
  77. $output->writeln("Verifying Magento Storefront...");
  78. $storefrontUrlAnalyzerMessages = $this->runStorefrontUrlAnalyzer();
  79. if (isset($storefrontUrlAnalyzerMessages['error']) === false) {
  80. $output->outputMessages($this->urlAnalyzer->fixLastSlash('app_frontend_url'));
  81. $output->outputMessages($this->urlAnalyzer->checkDomain($_ENV['app_frontend_url']));
  82. } else {
  83. $output->outputMessages($storefrontUrlAnalyzerMessages);
  84. }
  85. $output->writeln("Storefront verification finished.");
  86. }
  87. /**
  88. * Run Storefront url analyzer check.
  89. *
  90. * @return array
  91. */
  92. private function runStorefrontUrlAnalyzer()
  93. {
  94. $messages = [];
  95. if (!isset($_ENV['app_frontend_url'])) {
  96. $messages['error'][] = 'app_frontend_url parameter is absent in the phpunit.xml file. '
  97. . 'Please, copy file from phpunit.xml.dist.';
  98. return $messages;
  99. }
  100. $url = $_ENV['app_frontend_url'];
  101. try {
  102. $this->curlTransport->write($url, [], CurlInterface::GET);
  103. $response = $this->curlTransport->read();
  104. if (strpos($response, 'Home Page') === false) {
  105. $messages['error'][] = 'Magento seems not installed. Check your Magento instance.';
  106. }
  107. } catch (\Exception $e) {
  108. $messages['error'][] = $e->getMessage();
  109. }
  110. $this->curlTransport->close();
  111. return $messages;
  112. }
  113. }