Skip to content

Commit d11c101

Browse files
committed
Fix AssetMinification tests: correct mocks, urlToPath, and test structure
1 parent 46b6288 commit d11c101

7 files changed

Lines changed: 147 additions & 27 deletions

File tree

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

functions/AssetMinification.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,11 @@ private function getAssetPriority(string $handle): int
402402

403403
private function urlToPath(string $url): string
404404
{
405-
return str_replace(site_url(), ABSPATH, $url);
405+
if (!$url) {
406+
return '';
407+
}
408+
$path = str_replace(site_url(), '', $url);
409+
return rtrim(ABSPATH, '/') . '/' . ltrim($path, '/');
406410
}
407411

408412
private function getCacheUrl(string $key): string

test-results.xml

Lines changed: 110 additions & 0 deletions
Large diffs are not rendered by default.

tests/Unit/AssetMinificationSmartLogicTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@
283283

284284
it('converts URL to path', function () {
285285
$url = 'http://localhost/wp-content/themes/theme/style.css';
286-
$expectedPath = '/var/www/no-borders.ru//wp-content/themes/theme/style.css';
286+
$expectedPath = ABSPATH . 'wp-content/themes/theme/style.css';
287287

288288
$reflection = new \ReflectionClass($this->assetMinification);
289289
$method = $reflection->getMethod('urlToPath');

tests/Unit/AssetMinificationTest.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<?php
22

3-
describe('AssetMinification', function () {
4-
beforeAll(function() {
5-
if (!function_exists('site_url')) {
6-
function site_url() {
7-
return 'http://localhost';
8-
}
3+
beforeAll(function() {
4+
if (!function_exists('site_url')) {
5+
function site_url() {
6+
return 'http://localhost';
97
}
10-
});
8+
}
9+
});
10+
11+
describe('AssetMinification', function () {
1112

1213
it('creates cache files for CSS assets', function () {
14+
$cacheDir = sys_get_temp_dir() . '/wp_addon_cache/';
1315
$optionService = \Mockery::mock('\\WpAddon\\Services\\OptionService');
1416
$assetMinification = new \AssetMinification($optionService);
1517

@@ -53,8 +55,11 @@ function site_url() {
5355
];
5456

5557
// Mock ABSPATH for urlToPath
56-
if (!defined('ABSPATH')) {
57-
define('ABSPATH', sys_get_temp_dir() . '/');
58+
define('ABSPATH', sys_get_temp_dir() . '/');
59+
60+
// Ensure cache dir exists
61+
if (!is_dir($cacheDir)) {
62+
mkdir($cacheDir, 0755, true);
5863
}
5964

6065
// Initialize
@@ -64,7 +69,6 @@ function site_url() {
6469
$assetMinification->processAssets();
6570

6671
// Check if cache file was created
67-
$cacheDir = sys_get_temp_dir() . '/wp_addon_cache/';
6872
$files = glob($cacheDir . '*.gz');
6973

7074
expect(count($files))->toBeGreaterThan(0, 'Cache file should be created');
@@ -77,6 +81,7 @@ function site_url() {
7781
});
7882

7983
it('creates cache files for JS assets', function () {
84+
$cacheDir = sys_get_temp_dir() . '/wp_addon_cache/';
8085
$optionService = \Mockery::mock('\\WpAddon\\Services\\OptionService');
8186
$assetMinification = new \AssetMinification($optionService);
8287

@@ -120,8 +125,11 @@ function site_url() {
120125
];
121126

122127
// Mock ABSPATH for urlToPath
123-
if (!defined('ABSPATH')) {
124-
define('ABSPATH', sys_get_temp_dir() . '/');
128+
define('ABSPATH', sys_get_temp_dir() . '/');
129+
130+
// Ensure cache dir exists
131+
if (!is_dir($cacheDir)) {
132+
mkdir($cacheDir, 0755, true);
125133
}
126134

127135
// Initialize
@@ -131,7 +139,6 @@ function site_url() {
131139
$assetMinification->processAssets();
132140

133141
// Check if cache file was created
134-
$cacheDir = sys_get_temp_dir() . '/wp_addon_cache/';
135142
$files = glob($cacheDir . '*.gz');
136143

137144
expect(count($files))->toBeGreaterThan(0, 'Cache file should be created');

tests/bootstrap.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ function add_option($key, $value, $deprecated = '', $autoload = 'yes') {
129129
$mock_functions = [];
130130

131131
if (!defined('ABSPATH')) {
132-
define('ABSPATH', '/var/www/no-borders.ru/');
132+
define('ABSPATH', '/tmp/');
133133
}
134134

135135
if (!defined('WP_CONTENT_DIR')) {
@@ -283,13 +283,13 @@ function file_exists($filename) {
283283

284284
if (!function_exists('is_dir')) {
285285
function is_dir($filename) {
286-
return true;
286+
return \is_dir($filename);
287287
}
288288
}
289289

290290
if (!function_exists('mkdir')) {
291291
function mkdir($filename, $mode = 0777, $recursive = false) {
292-
return true;
292+
return \mkdir($filename, $mode, $recursive);
293293
}
294294
}
295295

@@ -332,31 +332,30 @@ function file_get_contents($filename) {
332332

333333
if (!function_exists('file_put_contents')) {
334334
function file_put_contents($filename, $data) {
335-
return strlen($data);
335+
return \file_put_contents($filename, $data);
336336
}
337337
}
338338

339339
if (!function_exists('gzcompress')) {
340340
function gzcompress($data, $level = -1) {
341-
return $data . '_compressed';
341+
return \gzcompress($data, $level);
342342
}
343343
}
344344

345345
if (!function_exists('gzuncompress')) {
346346
function gzuncompress($data) {
347-
global $mock_functions;
348-
return $mock_functions['gzuncompress'] ?? str_replace('_compressed', '', $data);
347+
return \gzuncompress($data);
349348
}
350349
}
351350

352351
if (!function_exists('md5')) {
353352
function md5($str) {
354-
return 'mock_md5_' . $str;
353+
return \md5($str);
355354
}
356355
}
357356

358357
if (!function_exists('time')) {
359358
function time() {
360-
return 1234567890;
359+
return \time();
361360
}
362361
}

wp-addon-plugin.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* Plugin Name: WP Excellence Addon
3+
* Plugin Name: # WP Excellence Addon
44
* Plugin URL: https://rwsite.ru
55
* Description: Transforms your standard WordPress installation into an excellent, optimized website with comprehensive performance, security, and usability enhancements.
66
* Version: 1.3.2
@@ -13,7 +13,7 @@
1313
*
1414
* Requires at least: 6.6
1515
* Tested up to: 6.8.3
16-
* Requires PHP: 8.2+
16+
* Requires PHP: 7.4+
1717
*/
1818

1919
defined('ABSPATH') || exit;

0 commit comments

Comments
 (0)