PhpserverTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Phpserver;
  7. /**
  8. * @magentoAppIsolation enabled
  9. *
  10. * @magentoConfigFixture current_store web/secure/base_url http://127.0.0.1:8082/
  11. * @magentoConfigFixture current_store web/unsecure/base_link_url http://127.0.0.1:8082/
  12. * @magentoConfigFixture current_store web/secure/base_link_url http://127.0.0.1:8082/
  13. * @magentoConfigFixture current_store web/secure/use_in_frontend 0
  14. *
  15. * @magentoAppArea frontend
  16. */
  17. class PhpserverTest extends \PHPUnit\Framework\TestCase
  18. {
  19. const BASE_URL = '127.0.0.1:8082';
  20. private static $serverPid;
  21. /**
  22. * @var \Zend\Http\Client
  23. */
  24. private $httpClient;
  25. /**
  26. * Instantiate phpserver in the pub folder
  27. */
  28. public static function setUpBeforeClass()
  29. {
  30. if (!(defined('TRAVIS') && TRAVIS === true)) {
  31. self::markTestSkipped('Travis environment test');
  32. }
  33. $return = [];
  34. $baseDir = __DIR__ . '/../../../../../../';
  35. $command = sprintf(
  36. 'cd %s && php -S %s -t ./pub/ ./phpserver/router.php >/dev/null 2>&1 & echo $!',
  37. $baseDir,
  38. static::BASE_URL
  39. );
  40. exec($command, $return);
  41. static::$serverPid = (int) $return[0];
  42. }
  43. private function getUrl($url)
  44. {
  45. return sprintf('http://%s/%s', self::BASE_URL, ltrim($url, '/'));
  46. }
  47. public function setUp()
  48. {
  49. $this->httpClient = new \Zend\Http\Client(null, ['timeout' => 10]);
  50. }
  51. public function testServerHasPid()
  52. {
  53. $this->assertTrue(static::$serverPid > 0);
  54. }
  55. public function testServerResponds()
  56. {
  57. $this->httpClient->setUri($this->getUrl('/'));
  58. $response = $this->httpClient->send();
  59. $this->assertFalse($response->isClientError());
  60. }
  61. public function testStaticCssFile()
  62. {
  63. $this->httpClient->setUri($this->getUrl('/errors/default/css/styles.css'));
  64. $response = $this->httpClient->send();
  65. $this->assertFalse($response->isClientError());
  66. $this->assertStringStartsWith('text/css', $response->getHeaders()->get('Content-Type')->getMediaType());
  67. }
  68. public function testStaticImageFile()
  69. {
  70. $this->httpClient->setUri($this->getUrl('/errors/default/images/logo.gif'));
  71. $response = $this->httpClient->send();
  72. $this->assertFalse($response->isClientError());
  73. $this->assertStringStartsWith('image/gif', $response->getHeaders()->get('Content-Type')->getMediaType());
  74. }
  75. public static function tearDownAfterClass()
  76. {
  77. posix_kill(static::$serverPid, SIGKILL);
  78. }
  79. }