Skip to content

Commit 36a1a36

Browse files
committed
Init
1 parent c8e6aac commit 36a1a36

File tree

5 files changed

+181
-9
lines changed

5 files changed

+181
-9
lines changed

.cs.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
->setFinder(PhpCsFixer\Finder::create()
3838
->in(__DIR__ . '/src')
3939
->in(__DIR__ . '/tests')
40-
->in(__DIR__ . '/config')
41-
->in(__DIR__ . '/public')
4240
->name('*.php')
4341
->ignoreDotFiles(true)
4442
->ignoreVCS(true));

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "selective/basepath",
33
"type": "library",
4-
"description": "A URL base path detector for Slim 3 and 4",
4+
"description": "A URL base path detector for Slim 4",
55
"keywords": [
66
"basepath",
77
"slim",

src/BasePathDetector.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,21 @@ class BasePathDetector
1212
*/
1313
private $server = [];
1414

15+
/**
16+
* @var string The PHP_SAPI value
17+
*/
18+
private $phpSapi = '';
19+
1520
/**
1621
* The constructor.
1722
*
1823
* @param array $server The SERVER data to use
24+
* @param string $phpSapi The PHP_SAPI value
1925
*/
20-
public function __construct(array $server)
26+
public function __construct(array $server, string $phpSapi)
2127
{
2228
$this->server = $server;
29+
$this->phpSapi = $phpSapi;
2330
}
2431

2532
/**
@@ -29,13 +36,17 @@ public function __construct(array $server)
2936
*/
3037
public function getBasePath(): string
3138
{
39+
// For apache
40+
if ($this->phpSapi === 'apache2handler') {
41+
return $this->getBasePathFromApache($this->server);
42+
}
43+
3244
// For built-in server
33-
if (PHP_SAPI === 'cli-server') {
45+
if ($this->phpSapi === 'cli-server') {
3446
return $this->getBasePathFromBuiltIn($this->server);
3547
}
3648

37-
// For apache
38-
return $this->getBasePathFromApache($this->server);
49+
return '';
3950
}
4051

4152
/**

tests/ApacheTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace Selective\BasePath\Test;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Selective\BasePath\BasePathDetector;
7+
8+
/**
9+
* Test.
10+
*/
11+
class ApacheTest extends TestCase
12+
{
13+
/**
14+
* @var array
15+
*/
16+
private $server;
17+
18+
/**
19+
* Set Up.
20+
*
21+
* @return void
22+
*/
23+
protected function setUp(): void
24+
{
25+
$this->server = [
26+
'REQUEST_METHOD' => 'GET',
27+
'REQUEST_SCHEME' => 'http',
28+
'HTTP_HOST' => 'localhost',
29+
'SERVER_PORT' => '80',
30+
'REQUEST_URI' => '/',
31+
'SERVER_PROTOCOL' => 'HTTP/1.1',
32+
'SCRIPT_NAME' => '',
33+
'REQUEST_TIME_FLOAT' => microtime(true),
34+
'REQUEST_TIME' => microtime(),
35+
];
36+
}
37+
38+
/**
39+
* Create instance.
40+
*
41+
* @return BasePathDetector The detector
42+
*/
43+
private function createInstance(): BasePathDetector
44+
{
45+
return new BasePathDetector($this->server, 'apache2handler');
46+
}
47+
48+
/**
49+
* Test.
50+
*
51+
* @return void
52+
*/
53+
public function testDefault(): void
54+
{
55+
$detector = $this->createInstance();
56+
$basePath = $detector->getBasePath();
57+
58+
static::assertSame('', $basePath);
59+
}
60+
61+
/**
62+
* Test.
63+
*
64+
* @return void
65+
*/
66+
public function testUnknownServer(): void
67+
{
68+
$detector = new BasePathDetector($this->server, '');
69+
$basePath = $detector->getBasePath();
70+
71+
static::assertSame('', $basePath);
72+
}
73+
74+
/**
75+
* Test.
76+
*
77+
* @return void
78+
*/
79+
public function testSubdirectory(): void
80+
{
81+
$this->server['REQUEST_URI'] = '/public';
82+
83+
$detector = $this->createInstance();
84+
$basePath = $detector->getBasePath();
85+
86+
static::assertSame('/public', $basePath);
87+
}
88+
89+
/**
90+
* Test.
91+
*
92+
* @return void
93+
*/
94+
public function testWithoutRequestUri(): void
95+
{
96+
unset($this->server['REQUEST_URI']);
97+
98+
$detector = $this->createInstance();
99+
$basePath = $detector->getBasePath();
100+
101+
static::assertSame('', $basePath);
102+
}
103+
}

tests/InternalServerTest.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,79 @@
33
namespace Selective\BasePath\Test;
44

55
use PHPUnit\Framework\TestCase;
6+
use Selective\BasePath\BasePathDetector;
67

78
/**
89
* Test.
910
*/
1011
class InternalServerTest extends TestCase
1112
{
13+
/**
14+
* @var array
15+
*/
16+
private $server;
17+
18+
/**
19+
* Set Up.
20+
*
21+
* @return void
22+
*/
23+
protected function setUp(): void
24+
{
25+
$this->server = [
26+
'REQUEST_METHOD' => 'GET',
27+
'REQUEST_SCHEME' => 'http',
28+
'HTTP_HOST' => 'localhost',
29+
'SERVER_PORT' => '80',
30+
'REQUEST_URI' => '/',
31+
'SERVER_PROTOCOL' => 'HTTP/1.1',
32+
'SCRIPT_NAME' => '',
33+
'REQUEST_TIME_FLOAT' => microtime(true),
34+
'REQUEST_TIME' => microtime(),
35+
];
36+
}
37+
38+
/**
39+
* Create instance.
40+
*
41+
* @return BasePathDetector The detector
42+
*/
43+
private function createInstance(): BasePathDetector
44+
{
45+
return new BasePathDetector($this->server, 'cli-server');
46+
}
47+
48+
/**
49+
* Test.
50+
*
51+
* @return void
52+
*/
53+
public function testDefault(): void
54+
{
55+
$detector = $this->createInstance();
56+
$basePath = $detector->getBasePath();
57+
58+
static::assertSame('', $basePath);
59+
60+
$this->server['SCRIPT_NAME'] = '/index.php';
61+
$detector = $this->createInstance();
62+
$basePath = $detector->getBasePath();
63+
64+
static::assertSame('', $basePath);
65+
}
66+
1267
/**
1368
* Test.
1469
*
1570
* @return void
1671
*/
17-
public function testInternalServer(): void
72+
public function testSubdirectory(): void
1873
{
19-
$this->assertTrue(true);
74+
$this->server['SCRIPT_NAME'] = '/public/index.php';
75+
76+
$detector = $this->createInstance();
77+
$basePath = $detector->getBasePath();
78+
79+
static::assertSame('/public', $basePath);
2080
}
2181
}

0 commit comments

Comments
 (0)