CliTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\Framework\Console;
  8. use Magento\Framework\App\DeploymentConfig;
  9. use Magento\Framework\App\DeploymentConfig\FileReader;
  10. use Magento\Framework\App\DeploymentConfig\Writer;
  11. use Magento\Framework\App\Filesystem\DirectoryList;
  12. use Magento\Framework\Config\File\ConfigFilePool;
  13. use Magento\Framework\Filesystem;
  14. use Magento\Framework\ObjectManagerInterface;
  15. use Magento\TestFramework\Helper\Bootstrap;
  16. class CliTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * @var ObjectManagerInterface
  20. */
  21. private $objectManager;
  22. /**
  23. * @var Filesystem
  24. */
  25. private $filesystem;
  26. /**
  27. * @var ConfigFilePool
  28. */
  29. private $configFilePool;
  30. /**
  31. * @var FileReader
  32. */
  33. private $reader;
  34. /**
  35. * @var Writer
  36. */
  37. private $writer;
  38. /**
  39. * @var array
  40. */
  41. private $envConfig;
  42. /**
  43. * @inheritdoc
  44. */
  45. protected function setUp()
  46. {
  47. $this->objectManager = Bootstrap::getObjectManager();
  48. $this->configFilePool = $this->objectManager->get(ConfigFilePool::class);
  49. $this->filesystem = $this->objectManager->get(Filesystem::class);
  50. $this->reader = $this->objectManager->get(FileReader::class);
  51. $this->writer = $this->objectManager->get(Writer::class);
  52. $this->envConfig = $this->reader->load(ConfigFilePool::APP_ENV);
  53. }
  54. /**
  55. * @inheritdoc
  56. */
  57. protected function tearDown()
  58. {
  59. $this->filesystem->getDirectoryWrite(DirectoryList::CONFIG)->writeFile(
  60. $this->configFilePool->getPath(ConfigFilePool::APP_ENV),
  61. "<?php\n return array();\n"
  62. );
  63. $this->writer->saveConfig([ConfigFilePool::APP_ENV => $this->envConfig], true);
  64. }
  65. /**
  66. * Checks that settings from env.php config file are applied
  67. * to created application instance.
  68. *
  69. * @param bool $isPub
  70. * @param array $params
  71. * @dataProvider documentRootIsPubProvider
  72. */
  73. public function testDocumentRootIsPublic($isPub, $params)
  74. {
  75. $config = include __DIR__ . '/_files/env.php';
  76. $config['directories']['document_root_is_pub'] = $isPub;
  77. $this->writer->saveConfig([ConfigFilePool::APP_ENV => $config], true);
  78. $cli = new Cli();
  79. $cliReflection = new \ReflectionClass($cli);
  80. $serviceManagerProperty = $cliReflection->getProperty('serviceManager');
  81. $serviceManagerProperty->setAccessible(true);
  82. $serviceManager = $serviceManagerProperty->getValue($cli);
  83. $deploymentConfig = $this->objectManager->get(DeploymentConfig::class);
  84. $serviceManager->setAllowOverride(true);
  85. $serviceManager->setService(DeploymentConfig::class, $deploymentConfig);
  86. $serviceManagerProperty->setAccessible(false);
  87. $documentRootResolver = $cliReflection->getMethod('documentRootResolver');
  88. $documentRootResolver->setAccessible(true);
  89. self::assertEquals($params, $documentRootResolver->invoke($cli));
  90. }
  91. /**
  92. * Provides document root setting and expecting
  93. * properties for object manager creation.
  94. *
  95. * @return array
  96. */
  97. public function documentRootIsPubProvider(): array
  98. {
  99. return [
  100. [true, [
  101. 'MAGE_DIRS' => [
  102. 'pub' => ['uri' => ''],
  103. 'media' => ['uri' => 'media'],
  104. 'static' => ['uri' => 'static'],
  105. 'upload' => ['uri' => 'media/upload']
  106. ]
  107. ]],
  108. [false, []]
  109. ];
  110. }
  111. }