Skip to content

Commit 4fffc2f

Browse files
Merge pull request #39 from Mohammadreza-73/0.x
[0.x] Improve code coverage
2 parents 616603e + 520e324 commit 4fffc2f

8 files changed

Lines changed: 119 additions & 46 deletions

File tree

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
"Tests\\": "tests/"
2525
}
2626
},
27+
"require": {
28+
"illuminate/config": "^12.35"
29+
},
2730
"require-dev": {
2831
"friendsofphp/php-cs-fixer": "^3.87",
2932
"mockery/mockery": "^1.6",

src/FileCache.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@
66

77
class FileCache
88
{
9+
private string $cacheDir;
10+
911
public function __construct()
1012
{
13+
$this->cacheDir = dirname(__DIR__).'/cache/';
1114
$this->ensureCacheDir();
1215
}
1316

@@ -66,8 +69,10 @@ public function set(string $url, array $content): bool
6669

6770
private function ensureCacheDir(): void
6871
{
69-
if (! file_exists(__DIR__.'/cache/')) {
70-
mkdir('cache', 0755, true);
72+
clearstatcache(true, $this->cacheDir);
73+
74+
if (! mkdir($this->cacheDir, 0755, true) && ! is_dir($this->cacheDir)) {
75+
throw new RuntimeException("Cache directory {$this->cacheDir} could not be created");
7176
}
7277
}
7378
}

src/ProxyServer.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function handleRequest(): void
3333
}
3434

3535
// Check filter URL
36-
if (config('cache', 'filter_url_status')) {
36+
if (config('cache', 'filter_url_enable')) {
3737
if ($this->checkFilterUrl($url) === false) {
3838
$this->sendError('Domain not allowed', Response::HTTP_FORBIDDEN);
3939

@@ -91,7 +91,7 @@ private function checkFilterUrl(string $url): bool
9191
return true;
9292
}
9393

94-
if (in_array($host, config('cache', 'blacklist'))) {
94+
if (in_array($host, config('cache', 'filter_url_list'))) {
9595
return false;
9696
}
9797

@@ -171,7 +171,7 @@ private function makeRequest(string $url): array
171171
$body = substr($response, $header_size);
172172

173173
// Check content length
174-
if (strlen($body) > config('cache', 'max_content_length')) {
174+
if (strlen($body) > config('cache', 'file.max_content_length')) {
175175
return [
176176
'success' => false,
177177
'error' => Response::$statusTexts[Response::HTTP_REQUEST_ENTITY_TOO_LARGE],

src/config/cache.php

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,67 @@
11
<?php
22

33
return [
4-
'type' => 'file', // or Redis
5-
'ttl' => 3600, // an hour
6-
'timeout' => 30, // seconds
7-
'max_content_length' => 10485760, // 10MB
8-
9-
'filter_url_status' => true,
10-
'blacklist' => [
11-
'zoomit.ir',
4+
/**
5+
* --------------------------
6+
* Default Driver
7+
* --------------------------
8+
*
9+
* Handle caches with difference strategies,
10+
* like: `file`, `database`, `redis`
11+
*/
12+
'driver' => 'file',
13+
14+
/**
15+
* --------------------------
16+
* Time to Live
17+
* --------------------------
18+
*
19+
* This parameter is cache time to live in seconds for outdate cache
20+
* data and cache new data.
21+
*/
22+
'ttl' => 3600,
23+
24+
/**
25+
* --------------------------
26+
* Request time out
27+
* --------------------------
28+
*
29+
* This parameter indicates request time out in seconds
30+
* to the origin URL.
31+
*/
32+
'timeout' => 30,
33+
34+
/**
35+
* --------------------------
36+
* Filter URL
37+
* --------------------------
38+
* This parameter enable/disable filter origin URL.
39+
*
40+
*/
41+
'filter_url_enable' => true,
42+
43+
/**
44+
* --------------------------
45+
* List of filterd URL
46+
* --------------------------
47+
* This parameter idicates filterd URL.
48+
*
49+
*/
50+
'filter_url_list' => [
51+
// 'malicious_url.com',
52+
],
53+
54+
/**
55+
* --------------------------
56+
* File caching driver
57+
* --------------------------
58+
* This driver caches the content of the origin into the file.
59+
*
60+
*/
61+
'file' => [
62+
/**
63+
* File content limitation
64+
*/
65+
'max_content_length' => 10485760, // 10MB
1266
],
1367
];

src/helpers/functions.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use Illuminate\Config\Repository;
4+
35
if (! function_exists('base_path')) {
46
function base_path(string $path = ''): string
57
{
@@ -31,22 +33,33 @@ function dd(mixed ...$vars): void
3133
}
3234
}
3335

34-
if (! function_exists('config')) {
35-
function config(string $fileName, ?string $index = null): mixed
36+
if (! function_exists('config_file')) {
37+
function config_file(string $fileName): mixed
3638
{
3739
$fileName = $fileName . '.php';
3840

3941
if (! file_exists(config_path($fileName))) {
4042
throw new Exception("Config file `$fileName` not found.");
4143
}
4244

43-
$config = require config_path($fileName);
45+
return require config_path($fileName);
46+
}
47+
}
48+
49+
if (! function_exists('config')) {
50+
function config(string $fileName, $key = null, $default = null)
51+
{
52+
$config = new Repository(config_file($fileName));
53+
54+
if (is_null($key)) {
55+
return $config->all();
56+
}
4457

45-
if ($index === null) {
46-
return $config;
58+
if (is_array($key)) {
59+
return $config->set($key);
4760
}
4861

49-
return $config[$index] ?? null;
62+
return $config->get($key, $default);
5063
}
5164
}
5265

tests/Unit/CacheConfigTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
});
1515

1616
test('config file expected values', function () {
17-
expect(config('cache', 'type'))->toBeString();
17+
expect(config('cache', 'driver'))->toBeString();
1818
expect(config('cache', 'ttl'))->toBeInt();
1919
expect(config('cache', 'timeout'))->toBeInt();
20-
expect(config('cache', 'max_content_length'))->toBeInt();
21-
expect(config('cache', 'filter_url_status'))->toBeBool();
22-
expect(config('cache', 'blacklist'))->toBeArray();
20+
expect(config('cache', 'filter_url_enable'))->toBeBool();
21+
expect(config('cache', 'filter_url_list'))->toBeArray();
22+
expect(config('cache', 'file'))->toBeArray();
23+
expect(config('cache', 'file.max_content_length'))->toBeInt();
2324
});

tests/Unit/FileCacheTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Clean up cache directory before each test
77
*/
88
beforeEach(function () {
9-
$cacheDir = base_path('cache/');
9+
$cacheDir = cache_path();
1010

1111
if (file_exists($cacheDir)) {
1212
array_map('unlink', glob($cacheDir . '/*'));
@@ -17,7 +17,7 @@
1717
});
1818

1919
afterAll(function () {
20-
$cacheDir = base_path('cache/');
20+
$cacheDir = cache_path();
2121

2222
if (file_exists($cacheDir)) {
2323
array_map('unlink', glob($cacheDir . '/*'));
@@ -43,8 +43,10 @@
4343
test('does not throw exception if cache directory already exists', function () {
4444
$cacheDir = cache_path();
4545

46-
expect(fn () => new Phoxy\FileCache())->not->toThrow(Exception::class);
46+
expect(is_dir($cacheDir))->toBeTrue();
4747
expect(file_exists($cacheDir))->toBeTrue();
48+
49+
expect(fn () => $this->fileCache)->not->toThrow(Exception::class);
4850
});
4951
});
5052

tests/Unit/RunServerTest.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
use Phoxy\ProxyServer;
44

5-
describe('ProxyServer', function () {
6-
beforeEach(function () {
7-
$this->proxy = new ProxyServer();
8-
});
5+
beforeEach(function () {
6+
$cacheDir = cache_path();
97

10-
afterEach(function () {
11-
Mockery::close();
12-
});
8+
if (file_exists($cacheDir)) {
9+
array_map('unlink', glob($cacheDir . '/*'));
10+
rmdir($cacheDir);
11+
}
12+
13+
$this->proxy = new ProxyServer();
14+
});
15+
16+
afterEach(function () {
17+
Mockery::close();
18+
});
1319

20+
describe('ProxyServer', function () {
1421
test('instantiates successfully', function () {
1522
expect($this->proxy)->toBeInstanceOf(ProxyServer::class);
1623
});
@@ -22,6 +29,7 @@
2229
test('calls handleRequest without errors', function () {
2330
/** @var Phoxy\ProxyServer|Mockery\MockInterface $mock */
2431
$mock = Mockery::mock(ProxyServer::class)->makePartial();
32+
$_GET['url'] = urlencode('example.com?url=site.com');
2533

2634
expect(fn () => $mock->handleRequest())->not->toThrow(Exception::class);
2735
});
@@ -33,17 +41,4 @@
3341

3442
$mock->handleRequest();
3543
});
36-
37-
/**
38-
* Performance Test
39-
*/
40-
test('handles request within reasonable time', function () {
41-
$start = microtime(true);
42-
$this->proxy->handleRequest();
43-
$end = microtime(true);
44-
45-
$executionTime = $end - $start;
46-
47-
expect($executionTime)->toBeLessThan(2.0); // 2 Seconds
48-
});
4944
});

0 commit comments

Comments
 (0)