SetupInfoTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Test\Unit;
  7. use \Magento\Framework\App\SetupInfo;
  8. class SetupInfoTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * A default fixture
  12. *
  13. * @var array
  14. */
  15. private static $fixture = ['DOCUMENT_ROOT' => '/doc/root', 'SCRIPT_FILENAME' => '/doc/root/dir/file.php'];
  16. /**
  17. * @param array $server
  18. * @param string $expectedError
  19. * @dataProvider constructorExceptionsDataProvider
  20. */
  21. public function testConstructorExceptions($server, $expectedError)
  22. {
  23. $this->expectException('\InvalidArgumentException');
  24. $this->expectExceptionMessage($expectedError);
  25. new SetupInfo($server);
  26. }
  27. /**
  28. * @return array
  29. */
  30. public function constructorExceptionsDataProvider()
  31. {
  32. $docRootErr = 'DOCUMENT_ROOT variable is unavailable.';
  33. $projectRootErr = 'Project root cannot be automatically detected.';
  34. return [
  35. [[], $docRootErr],
  36. [['DOCUMENT_ROOT' => ''], $docRootErr],
  37. [['DOCUMENT_ROOT' => '/foo'], $projectRootErr],
  38. [['DOCUMENT_ROOT' => '/foo', 'SCRIPT_FILENAME' => ''], $projectRootErr],
  39. ];
  40. }
  41. /**
  42. * @param array $server
  43. * @param string $expected
  44. * @dataProvider getUrlDataProvider
  45. */
  46. public function testGetUrl($server, $expected)
  47. {
  48. $info = new SetupInfo($server);
  49. $this->assertEquals($expected, $info->getUrl());
  50. }
  51. /**
  52. * @return array
  53. */
  54. public function getUrlDataProvider()
  55. {
  56. return [
  57. [
  58. self::$fixture,
  59. '/setup/'
  60. ],
  61. [
  62. self::$fixture + [SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => 'install'],
  63. '/install/',
  64. ],
  65. [
  66. self::$fixture + [SetupInfo::PARAM_NOT_INSTALLED_URL => 'http://example.com/'],
  67. 'http://example.com/',
  68. ],
  69. ];
  70. }
  71. /**
  72. * @param array $server
  73. * @param string $expected
  74. * @dataProvider getProjectUrlDataProvider
  75. */
  76. public function testGetProjectUrl($server, $expected)
  77. {
  78. $info = new SetupInfo($server);
  79. $this->assertEquals($expected, $info->getProjectUrl());
  80. }
  81. /**
  82. * @return array
  83. */
  84. public function getProjectUrlDataProvider()
  85. {
  86. return [
  87. [self::$fixture, ''],
  88. [self::$fixture + ['HTTP_HOST' => ''], ''],
  89. [
  90. ['DOCUMENT_ROOT' => '/foo/bar', 'SCRIPT_FILENAME' => '/other/baz.php', 'HTTP_HOST' => 'example.com'],
  91. 'http://example.com/'
  92. ],
  93. [self::$fixture + ['HTTP_HOST' => 'example.com'], 'http://example.com/dir/'],
  94. [
  95. ['DOCUMENT_ROOT' => '/foo/bar', 'SCRIPT_FILENAME' => '/foo/bar/baz.php', 'HTTP_HOST' => 'example.com'],
  96. 'http://example.com/'
  97. ],
  98. ];
  99. }
  100. /**
  101. * @param array $server
  102. * @param string $projectRoot
  103. * @param string $expected
  104. * @dataProvider getDirDataProvider
  105. */
  106. public function testGetDir($server, $projectRoot, $expected)
  107. {
  108. $info = new SetupInfo($server);
  109. $this->assertEquals($expected, $info->getDir($projectRoot));
  110. }
  111. /**
  112. * @return array
  113. */
  114. public function getDirDataProvider()
  115. {
  116. return [
  117. [
  118. self::$fixture,
  119. '/test/root',
  120. '/test/root/setup',
  121. ],
  122. [
  123. self::$fixture,
  124. '/test/root/',
  125. '/test/root/setup',
  126. ],
  127. [
  128. self::$fixture + [SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '/install/'],
  129. '/test/',
  130. '/test/install',
  131. ],
  132. ];
  133. }
  134. /**
  135. * @param array $server
  136. * @param bool $expected
  137. * @dataProvider isAvailableDataProvider
  138. */
  139. public function testIsAvailable($server, $expected)
  140. {
  141. $info = new SetupInfo($server);
  142. $this->assertEquals($expected, $info->isAvailable());
  143. }
  144. /**
  145. * @return array
  146. */
  147. public function isAvailableDataProvider()
  148. {
  149. $server = ['DOCUMENT_ROOT' => __DIR__, 'SCRIPT_FILENAME' => __FILE__];
  150. return [
  151. 'root = doc root, but no "setup" sub-directory' => [
  152. $server, // it will look for "setup/" sub-directory, but won't find anything
  153. false
  154. ],
  155. 'root = doc root, nonexistent sub-directory' => [
  156. $server + [SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => 'nonexistent'],
  157. false
  158. ],
  159. 'root = doc root, existent sub-directory' => [
  160. $server + [SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files'],
  161. true
  162. ],
  163. 'root within doc root, existent sub-directory' => [
  164. [
  165. 'DOCUMENT_ROOT' => dirname(__DIR__),
  166. 'SCRIPT_FILENAME' => __FILE__,
  167. SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files'
  168. ],
  169. true
  170. ],
  171. 'root outside of doc root, existent sub-directory' => [
  172. [
  173. 'DOCUMENT_ROOT' => __DIR__,
  174. 'SCRIPT_FILENAME' => dirname(dirname(__DIR__)) . '/foo.php',
  175. SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => basename(__DIR__)
  176. ],
  177. false
  178. ],
  179. 'root within doc root, existent sub-directory, trailing slash' => [
  180. [
  181. 'DOCUMENT_ROOT' => dirname(__DIR__) . DIRECTORY_SEPARATOR,
  182. 'SCRIPT_FILENAME' => __FILE__,
  183. SetupInfo::PARAM_NOT_INSTALLED_URL_PATH => '_files'
  184. ],
  185. true
  186. ],
  187. 'root within doc root + pub, existent sub-directory' => [
  188. [
  189. 'DOCUMENT_ROOT' => __DIR__ . '/_files/pub/',
  190. 'SCRIPT_FILENAME' => __DIR__ . '/_files/pub/index.php',
  191. ],
  192. true
  193. ],
  194. ];
  195. }
  196. }