GlobalFunctionsTest.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. declare(strict_types=1);
  3. namespace SPC\Tests\globals;
  4. use PHPUnit\Framework\TestCase;
  5. use Psr\Log\LogLevel;
  6. use SPC\exception\RuntimeException;
  7. use SPC\exception\WrongUsageException;
  8. use ZM\Logger\ConsoleLogger;
  9. /**
  10. * @internal
  11. */
  12. class GlobalFunctionsTest extends TestCase
  13. {
  14. private static $logger_cache;
  15. public static function setUpBeforeClass(): void
  16. {
  17. global $ob_logger;
  18. self::$logger_cache = $ob_logger;
  19. $ob_logger = new ConsoleLogger(LogLevel::ALERT);
  20. }
  21. public static function tearDownAfterClass(): void
  22. {
  23. global $ob_logger;
  24. $ob_logger = self::$logger_cache;
  25. }
  26. public function testIsAssocArray(): void
  27. {
  28. $this->assertTrue(is_assoc_array(['a' => 1, 'b' => 2]));
  29. $this->assertFalse(is_assoc_array([1, 2, 3]));
  30. }
  31. public function testLogger(): void
  32. {
  33. $this->assertInstanceOf('Psr\Log\LoggerInterface', logger());
  34. }
  35. /**
  36. * @throws WrongUsageException
  37. */
  38. public function testArch2Gnu(): void
  39. {
  40. $this->assertEquals('x86_64', arch2gnu('x86_64'));
  41. $this->assertEquals('x86_64', arch2gnu('x64'));
  42. $this->assertEquals('x86_64', arch2gnu('amd64'));
  43. $this->assertEquals('aarch64', arch2gnu('arm64'));
  44. $this->assertEquals('aarch64', arch2gnu('aarch64'));
  45. $this->expectException('SPC\exception\WrongUsageException');
  46. arch2gnu('armv7');
  47. }
  48. public function testQuote(): void
  49. {
  50. $this->assertEquals('"hello"', quote('hello'));
  51. $this->assertEquals("'hello'", quote('hello', "'"));
  52. }
  53. /**
  54. * @throws RuntimeException
  55. */
  56. public function testFPassthru(): void
  57. {
  58. if (PHP_OS_FAMILY === 'Windows') {
  59. $this->markTestSkipped('Windows not support f_passthru');
  60. }
  61. $this->assertEquals(null, f_passthru('echo ""'));
  62. $this->expectException('SPC\exception\RuntimeException');
  63. f_passthru('false');
  64. }
  65. public function testFPutenv(): void
  66. {
  67. $this->assertTrue(f_putenv('SPC_TEST_ENV=1'));
  68. $this->assertEquals('1', getenv('SPC_TEST_ENV'));
  69. }
  70. public function testShell(): void
  71. {
  72. if (PHP_OS_FAMILY === 'Windows') {
  73. $this->markTestSkipped('Windows not support shell');
  74. }
  75. $shell = shell();
  76. $this->assertInstanceOf('SPC\util\UnixShell', $shell);
  77. $this->assertInstanceOf('SPC\util\UnixShell', $shell->cd('/'));
  78. $this->assertInstanceOf('SPC\util\UnixShell', $shell->exec('echo ""'));
  79. $this->assertInstanceOf('SPC\util\UnixShell', $shell->setEnv(['SPC_TEST_ENV' => '1']));
  80. [$code, $out] = $shell->execWithResult('echo "_"');
  81. $this->assertEquals(0, $code);
  82. $this->assertEquals('_', implode('', $out));
  83. $this->expectException('SPC\exception\RuntimeException');
  84. $shell->exec('false');
  85. }
  86. }