Page MenuHomeDevCentral

No OneTemporary

diff --git a/src/OS/CurrentOS.php b/src/OS/CurrentOS.php
new file mode 100644
index 0000000..f957704
--- /dev/null
+++ b/src/OS/CurrentOS.php
@@ -0,0 +1,16 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\OS;
+
+class CurrentOS {
+
+ public static function isWindows () : bool {
+ return PHP_OS === 'CYGWIN' || self::isPureWindows();
+ }
+
+ public static function isPureWindows () : bool {
+ return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
+ }
+
+}
diff --git a/src/OS/CurrentProcess.php b/src/OS/CurrentProcess.php
new file mode 100644
index 0000000..8d87faa
--- /dev/null
+++ b/src/OS/CurrentProcess.php
@@ -0,0 +1,34 @@
+<?php
+declare(strict_types=1);
+
+namespace Keruald\OmniTools\OS;
+
+class CurrentProcess {
+
+ /**
+ * Determines if the current process, ie the PHP interpreter,
+ * runs as root on UNIX systems or in elevated mode on Windows.
+ *
+ * Cygwin processes are considered as Windows processes.
+ */
+ public static function isPrivileged () : bool {
+ if (CurrentOS::isWindows()) {
+ // `net session` is known to only work as privileged process.
+ // To wrap in cmd allows to avoid /dev/null for Cygwin,
+ // or $null when invoked from PowerShell. NUL: will always be used.
+ exec('cmd /C "net session >NUL 2>&1"', $_, $exitCode);
+
+ return $exitCode === 0;
+ }
+
+ if (!function_exists('posix_geteuid')) {
+ // POSIX PHP functions aren't always available, e.g. on FreeBSD
+ // In such cases, `id` will probably be available.
+ return trim(shell_exec('id -u')) === '0';
+ }
+
+ /** @noinspection PhpComposerExtensionStubsInspection */
+ return posix_geteuid() === 0;
+ }
+
+}
diff --git a/tests/Reflection/CodeFileTest.php b/tests/Reflection/CodeFileTest.php
index 22500f2..67df93f 100644
--- a/tests/Reflection/CodeFileTest.php
+++ b/tests/Reflection/CodeFileTest.php
@@ -1,72 +1,85 @@
<?php
declare(strict_types=1);
namespace Keruald\OmniTools\Tests\Reflection;
+use Keruald\OmniTools\OS\CurrentProcess;
use Keruald\OmniTools\Reflection\CodeFile;
use Keruald\OmniTools\Tests\WithData;
use PHPUnit\Framework\TestCase;
use Acme\MockLib\Bar; // a mock class in a mock namespace to test include
class CodeFileTest extends TestCase {
use WithData;
/**
* @var CodeFile
*/
private $validCodeFile;
/**
* @var CodeFile
*/
private $notExistingCodeFile;
public function setUp () : void {
$file = $this->getDataPath("MockLib/Bar.php");
$this->validCodeFile = CodeFile::from($file);
$this->notExistingCodeFile = CodeFile::from("/notexisting");
}
public function testCanInclude () : void {
$this->assertTrue($this->validCodeFile->canBeIncluded());
$this->assertFalse($this->notExistingCodeFile->canBeIncluded());
}
public function testCanBeIncludedWhenFileModeForbidsReading () : void {
+ if (CurrentProcess::isPrivileged()) {
+ $this->markTestSkipped(
+ "This test requires non-root access to run properly."
+ );
+ }
+
$file = $this->getNonReadableFile();
$this->assertFalse(CodeFile::From($file)->canBeIncluded());
unlink($file);
}
public function testTryInclude () : void {
$this->assertTrue($this->validCodeFile->tryInclude());
$this->assertTrue(class_exists(Bar::class));
$this->assertFalse($this->notExistingCodeFile->tryInclude());
}
public function testIsReadable () : void {
$this->assertTrue($this->validCodeFile->isReadable());
}
public function testIsReadableWhenFileModeForbidsReading () : void {
+ if (CurrentProcess::isPrivileged()) {
+ $this->markTestSkipped(
+ "This test requires non-root access to run properly."
+ );
+ }
+
$file = $this->getNonReadableFile();
$this->assertFalse(CodeFile::From($file)->isReadable());
unlink($file);
}
private function getNonReadableFile () : string {
$file = tempnam(sys_get_temp_dir(), "testCodeFile");
chmod($file, 0);
return $file;
}
}

File Metadata

Mime Type
text/x-diff
Expires
Fri, Sep 19, 03:09 (1 d, 11 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2990660
Default Alt Text
(4 KB)

Event Timeline