RuntimeTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php declare(strict_types=1);
  2. /*
  3. * This file is part of sebastian/environment.
  4. *
  5. * (c) Sebastian Bergmann <sebastian@phpunit.de>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace SebastianBergmann\Environment;
  11. use const PHP_SAPI;
  12. use const PHP_VERSION;
  13. use PHPUnit\Framework\TestCase;
  14. /**
  15. * @covers \SebastianBergmann\Environment\Runtime
  16. */
  17. final class RuntimeTest extends TestCase
  18. {
  19. /**
  20. * @var \SebastianBergmann\Environment\Runtime
  21. */
  22. private $env;
  23. protected function setUp(): void
  24. {
  25. $this->env = new Runtime;
  26. }
  27. /**
  28. * @requires extension xdebug
  29. */
  30. public function testCanCollectCodeCoverageWhenXdebugExtensionIsEnabled(): void
  31. {
  32. $this->assertTrue($this->env->canCollectCodeCoverage());
  33. }
  34. /**
  35. * @requires extension pcov
  36. */
  37. public function testCanCollectCodeCoverageWhenPcovExtensionIsEnabled(): void
  38. {
  39. $this->assertTrue($this->env->canCollectCodeCoverage());
  40. }
  41. public function testCanCollectCodeCoverageWhenRunningOnPhpdbg(): void
  42. {
  43. $this->markTestSkippedWhenNotRunningOnPhpdbg();
  44. $this->assertTrue($this->env->canCollectCodeCoverage());
  45. }
  46. public function testBinaryCanBeRetrieved(): void
  47. {
  48. $this->assertNotEmpty($this->env->getBinary());
  49. }
  50. /**
  51. * @requires PHP
  52. */
  53. public function testIsHhvmReturnsFalseWhenRunningOnPhp(): void
  54. {
  55. $this->assertFalse($this->env->isHHVM());
  56. }
  57. /**
  58. * @requires PHP
  59. */
  60. public function testIsPhpReturnsTrueWhenRunningOnPhp(): void
  61. {
  62. $this->markTestSkippedWhenRunningOnPhpdbg();
  63. $this->assertTrue($this->env->isPHP());
  64. }
  65. /**
  66. * @requires extension pcov
  67. */
  68. public function testPCOVCanBeDetected(): void
  69. {
  70. $this->assertTrue($this->env->hasPCOV());
  71. }
  72. public function testPhpdbgCanBeDetected(): void
  73. {
  74. $this->markTestSkippedWhenNotRunningOnPhpdbg();
  75. $this->assertTrue($this->env->hasPHPDBGCodeCoverage());
  76. }
  77. /**
  78. * @requires extension xdebug
  79. */
  80. public function testXdebugCanBeDetected(): void
  81. {
  82. $this->markTestSkippedWhenRunningOnPhpdbg();
  83. $this->assertTrue($this->env->hasXdebug());
  84. }
  85. public function testNameAndVersionCanBeRetrieved(): void
  86. {
  87. $this->assertNotEmpty($this->env->getNameWithVersion());
  88. }
  89. public function testGetNameReturnsPhpdbgWhenRunningOnPhpdbg(): void
  90. {
  91. $this->markTestSkippedWhenNotRunningOnPhpdbg();
  92. $this->assertSame('PHPDBG', $this->env->getName());
  93. }
  94. /**
  95. * @requires PHP
  96. */
  97. public function testGetNameReturnsPhpdbgWhenRunningOnPhp(): void
  98. {
  99. $this->markTestSkippedWhenRunningOnPhpdbg();
  100. $this->assertSame('PHP', $this->env->getName());
  101. }
  102. public function testNameAndCodeCoverageDriverCanBeRetrieved(): void
  103. {
  104. $this->assertNotEmpty($this->env->getNameWithVersionAndCodeCoverageDriver());
  105. }
  106. /**
  107. * @requires PHP
  108. */
  109. public function testGetVersionReturnsPhpVersionWhenRunningPhp(): void
  110. {
  111. $this->assertSame(PHP_VERSION, $this->env->getVersion());
  112. }
  113. /**
  114. * @requires PHP
  115. */
  116. public function testGetVendorUrlReturnsPhpDotNetWhenRunningPhp(): void
  117. {
  118. $this->assertSame('https://secure.php.net/', $this->env->getVendorUrl());
  119. }
  120. public function testGetCurrentSettingsReturnsEmptyDiffIfNoValuesArePassed(): void
  121. {
  122. $this->assertSame([], (new Runtime)->getCurrentSettings([]));
  123. }
  124. /**
  125. * @requires extension xdebug
  126. */
  127. public function testGetCurrentSettingsReturnsCorrectDiffIfXdebugValuesArePassed(): void
  128. {
  129. $this->assertIsArray((new Runtime)->getCurrentSettings(['xdebug.mode']));
  130. $this->assertArrayHasKey('xdebug.mode', (new Runtime)->getCurrentSettings(['xdebug.mode']));
  131. }
  132. public function testGetCurrentSettingsWillSkipSettingsThatIsNotSet(): void
  133. {
  134. $this->assertSame([], (new Runtime)->getCurrentSettings(['allow_url_include']));
  135. }
  136. private function markTestSkippedWhenNotRunningOnPhpdbg(): void
  137. {
  138. if ($this->isRunningOnPhpdbg()) {
  139. return;
  140. }
  141. $this->markTestSkipped('PHPDBG is required.');
  142. }
  143. private function markTestSkippedWhenRunningOnPhpdbg(): void
  144. {
  145. if (!$this->isRunningOnPhpdbg()) {
  146. return;
  147. }
  148. $this->markTestSkipped('Cannot run on PHPDBG');
  149. }
  150. private function isRunningOnPhpdbg(): bool
  151. {
  152. return PHP_SAPI === 'phpdbg';
  153. }
  154. }