forked from codeigniter4/CodeIgniter4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBootstrapFCPATHTest.php
More file actions
110 lines (93 loc) · 3.07 KB
/
BootstrapFCPATHTest.php
File metadata and controls
110 lines (93 loc) · 3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <[email protected]>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\Test;
use PHPUnit\Framework\Attributes\Group;
/**
* This test confirms that the bootstrap.php
* will set the correct FCPATH regardless of the current directory
*
* It writes a file in the temp directory that loads the bootstrap file
* then compares its echo FCPATH; to the correct FCPATH returned
* from correctFCPATH();
*
* @internal
*/
#[Group('Others')]
final class BootstrapFCPATHTest extends CIUnitTestCase
{
private string $currentDir = __DIR__;
private string $dir1 = '/tmp/dir1';
private string $file1 = '/tmp/dir1/testFile.php';
protected function setUp(): void
{
parent::setUp();
$this->deleteFiles();
$this->deleteDirectories();
$this->buildDirectories();
$this->writeFiles();
}
protected function tearDown(): void
{
parent::tearDown();
$this->deleteFiles();
$this->deleteDirectories();
}
public function testSetFCPATH(): void
{
$result1 = $this->readOutput($this->file1);
$correctPath = $this->correctFCPATH();
$this->assertSame($correctPath, $result1);
}
private function correctFCPATH(): string
{
return realpath(__DIR__ . '/../../../public') . DIRECTORY_SEPARATOR;
}
private function buildDirectories(): void
{
mkdir($this->dir1, 0777, true);
}
private function deleteDirectories(): void
{
// these need to be executed in reverse order: dir 2 in inside dir1
if (is_dir($this->dir1)) {
rmdir($this->dir1);
}
}
private function writeFiles(): void
{
file_put_contents($this->file1, $this->fileContents());
chmod($this->file1, 0777);
}
private function deleteFiles(): void
{
if (is_file($this->file1)) {
unlink($this->file1);
}
}
private function fileContents(): string
{
$fileContents = '';
$fileContents .= '<?php' . PHP_EOL;
$fileContents .= "define('HOMEPATH', '" . $this->currentDir . "' . '/../../../');" . PHP_EOL;
$fileContents .= "define('CONFIGPATH', '" . $this->currentDir . "' . '/../../../app/Config/');" . PHP_EOL;
$fileContents .= "define('PUBLICPATH', '" . $this->currentDir . "' . '/../../../public/');" . PHP_EOL;
$fileContents .= "include_once '" . $this->currentDir . "' . '/../../../vendor/autoload.php';" . PHP_EOL;
$fileContents .= "include_once '" . $this->currentDir . "' . '/../../../system/Test/bootstrap.php';" . PHP_EOL;
$fileContents .= '// return value of FCPATH' . PHP_EOL;
return $fileContents . 'echo FCPATH;' . PHP_EOL;
}
private function readOutput(string $file): false|string
{
ob_start();
system('php -f ' . $file);
return ob_get_clean();
}
}