Skip to content

Commit 1db2179

Browse files
author
Dmitry
committed
Migration to PHP 8, phpunit 10, restoration of functionality
1 parent 2982597 commit 1db2179

24 files changed

+672
-693
lines changed

src/Cache/Engine/FileCache.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function set($key, $data, $timeActual = 0)
2525
// Проверяем ключ на пустоту
2626

2727
if (empty($key) or preg_match('#..\/.\/#', $key)) {
28-
throw new Exception('Invalid cache name');
28+
throw new Exception('Invalid cache name: '. $key);
2929
}
3030

3131
// Создаем файл
@@ -60,10 +60,6 @@ public function get($key)
6060

6161
protected function loadData($path)
6262
{
63-
static $lastLoadedPath = null;
64-
if ($lastLoadedPath == $path) {
65-
return;
66-
}
6763
$isReadable = file_exists($path) && is_readable($path);
6864
if (!$isReadable) {
6965
throw new Exception(sprintf('Path %s not found or not readable', $path));
@@ -73,7 +69,7 @@ protected function loadData($path)
7369
throw new Exception('File restricted by security settings: ' . $path);
7470
}
7571
$data = file_get_contents($path);
76-
$this->loadedPath = $path;
72+
$this->lastLoadedPath = $path;
7773
$this->lastLoadedData = unserialize($data);
7874
}
7975

src/Controller/Controller.php

+10-5
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ public function render()
5656
}
5757

5858
/**
59-
* Called by dispatcher after action method was called
59+
* Ensurses that the response is created.
60+
* Ensures that the response is sent.
61+
6062
*/
6163
public function afterAction()
6264
{
@@ -73,7 +75,7 @@ public function afterAction()
7375
} else {
7476
}
7577
}
76-
//
78+
// Forcefully send response
7779
if (!$this->response->isSent()) {
7880
//
7981
$this->response->send();
@@ -94,7 +96,11 @@ protected function send( ) {
9496
$this->getDefaultHTTPResponse();
9597
//
9698
$this->response->setData( $this->view->render());
97-
}
99+
} else {
100+
// @todo probably to fix this behavior
101+
// so it the this->view is empty we actually do nothing and return a null
102+
// not sure if it a real problem now
103+
}
98104
}
99105
if ( !empty( $this->response )) {
100106
$this->response->send( );
@@ -105,7 +111,7 @@ public function set( $key, $value ) {
105111
if ( !empty( $this->view )) {
106112
$this->view->set( $key, $value );
107113
} else {
108-
throw new Exception('View not defined');
114+
throw new Exception('View not defined / the $view is empty');
109115
}
110116
}
111117

@@ -114,7 +120,6 @@ public function set( $key, $value ) {
114120
*/
115121
protected function getDefaultHTTPResponse( ) {
116122
if ( !empty( $this->response )) {
117-
//
118123
$this->response = new HttpResponse();
119124
}
120125
return $this->response;

src/Debug/ErrorRenderer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static function render( $errno, $errstr, $errfile = '', $errline = '' ) {
1818

1919
protected static function ignoreError( $errno ) {
2020
// Проверяем, возможно сейчас установлен режим игнорирования этого типа ошибки
21-
$currentErrorLevel = ini_get( 'error_reporting' );
21+
$currentErrorLevel = intval(ini_get( 'error_reporting' ));
2222
$result = !( $errno & $currentErrorLevel );
2323
return $result;
2424
}

src/Dispatcher/HttpRoute.php

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* @version: 1.0.1
45
*/
@@ -40,7 +41,7 @@ public function getUrlTemplate()
4041
*
4142
* @return bool
4243
*/
43-
public function test($request)
44+
public function test(\Faid\Request\Request $request): bool
4445
{
4546
parent::test($request);
4647
$regExp = $this->getRegexp();
@@ -57,33 +58,25 @@ protected function getRegExp()
5758

5859
protected function getRouteCallback()
5960
{
60-
$isControllerClass = !empty($this->controller) && class_exists($this->controller);
61-
$isControllerMethod = $isControllerClass && is_callable([$this->controller, $this->action]);
61+
6262
$isObjectController = !empty($this->controller) && is_object($this->controller);
6363
$isObjectControllerWithMethod = $isObjectController && is_callable([$this->controller, $this->action]);
6464
$isFunction = !empty($this->action) && is_callable($this->action);
6565
$isCallbackFunction = !empty($this->callback) && is_callable($this->callback);
6666

6767
if ($isObjectControllerWithMethod) {
6868
$callback = [$this->controller, $this->action];
69-
} elseif ($isControllerMethod) {
70-
$callback = [new $this->controller(), $this->action];
7169
} elseif ($isFunction) {
7270
$callback = $isFunction;
7371
} elseif ($isCallbackFunction) {
7472
$callback = $this->callback;
7573
}
7674

77-
if (empty( $callback) || !is_callable($callback)) {
75+
if (empty($callback) || !is_callable($callback)) {
7876
$error = sprintf('Route failed to dispatch. Callback not callable: %s', print_r($callback ?? $this, true));
7977
throw new RouteException($error);
8078
}
81-
// @todo To reintegrate with Extasy\Usecase
82-
// if ($this->controller instanceof \Faid\Controller\Controller) {
83-
// $this->controller->beforeAction($this->request);
84-
// }
8579
return $callback;
86-
8780
}
8881

8982
/**
@@ -92,15 +85,19 @@ protected function getRouteCallback()
9285
*/
9386
public function dispatch()
9487
{
95-
parent::dispatch();
9688
$callback = $this->getRouteCallback();
89+
90+
$isHasBeforeAction = is_array($callback) && is_object($callback[0]) && is_callable([$callback[0], 'beforeAction']);
91+
if ($isHasBeforeAction) {
92+
$callback[0]->beforeAction( $this->request );
93+
}
94+
9795
call_user_func($callback, $this->request, $this);
98-
// @todo To reintegrate that code
99-
// $isController = is_array( $callback ) && is_object( $callback[0]) && ( $callback[0] instanceof \Faid\Controller\Controller );
100-
// if ( $isController ) {
101-
// $controller = $callback[0];
102-
// $controller->afterAction( );
103-
// }
96+
97+
$isHasAfterAction = is_array($callback) && is_object($callback[0]) && is_callable([$callback[0], 'afterAction']);
98+
if ($isHasAfterAction) {
99+
$callback[0]->afterAction( $this->request );
100+
}
104101
}
105102

106103
public function buildUrl($data = [])
@@ -150,8 +147,6 @@ public function prepareRequest()
150147
}
151148
$this->request->set($params);
152149
}
153-
154150
}
155151
}
156152
}
157-
?>

src/Dispatcher/Route.php

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*/
66

77
namespace Faid\Dispatcher {
8-
class Route
8+
abstract class Route
99
{
1010
protected $isTestCalled = false;
1111

@@ -87,10 +87,9 @@ public function getCallback()
8787
*
8888
* @return bool
8989
*/
90-
public function test($request)
90+
public function test(\Faid\Request\Request $request): bool
9191
{
9292
$this->request = $request;
93-
$this->ready = false;
9493
return false;
9594
}
9695

@@ -105,13 +104,7 @@ public function prepareRequest()
105104
/**
106105
* @throws RouteException
107106
*/
108-
public function dispatch()
109-
{
110-
if ( !$this->isTestCalled ) {
111-
$this->isTestCalled = true;
112-
$this->test($this->request);
113-
}
114-
}
107+
abstract public function dispatch();
115108
}
116109
}
117110
?>

src/View/View.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class View extends StaticObservable
3434
public function __construct($filePath = null)
3535
{
3636

37-
$this->filePath = $this->getFilePath($filePath);
37+
$this->viewPath = $this->getFilePath($filePath);
3838
}
3939

4040
public function __isset($key)
@@ -109,7 +109,7 @@ public function getLayout()
109109
*/
110110
public function getPath()
111111
{
112-
return $this->filePath;
112+
return $this->viewPath;
113113
}
114114

115115
/**
@@ -181,7 +181,7 @@ public function render()
181181
} else {
182182
ob_start();
183183
}
184-
$content = $this->renderFile($this->viewVars, $this->filePath);
184+
$content = $this->renderFile($this->viewVars, $this->viewPath);
185185
if (!empty($this->layout)) {
186186
$vars = $this->viewVars;
187187
$vars['content_for_layout'] = $content;

tests/Cache/Engines/FileCacheTest.php

+18-26
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ public static function setUpBeforeClass(): void
2929
public function setUp(): void
3030
{
3131
$this->clearFiles();
32-
Configure::write(FileCache::ConfigurePath,
32+
Configure::write(
33+
FileCache::ConfigurePath,
3334
array(
3435
'BaseDir' => self::getPath()
35-
));
36+
)
37+
);
3638
}
3739

3840
public function tearDown(): void
@@ -59,25 +61,20 @@ protected function clearFiles()
5961
}
6062

6163
/**
62-
* @expectedException \Faid\Configure\ConfigureException
6364
*/
6465
public function testWithEmptyConfig()
6566
{
67+
$this->expectException(ConfigureException::class);
6668
Configure::write(FileCache::ConfigurePath, null);
6769
new FileCache();
6870
}
6971

70-
public function testAutoload()
71-
{
72-
new FileCache();
73-
}
74-
7572
/**
7673
* Создает кеш с пустым ключом
77-
* @expectedException Exception
7874
*/
7975
public function testSetWithEmptyKey()
8076
{
77+
$this->expectException(Exception::class);
8178
$instance = new FileCache();
8279
$instance->set('', array(1, 2, 3), self::CacheActualTime);
8380
}
@@ -91,18 +88,15 @@ public function testSet()
9188
$instance = new FileCache();
9289
$instance->set(self::KeyFixture, $fixture, self::CacheActualTime);
9390
$testPath = self::getPath() . (self::KeyFixture);
94-
if (!file_exists($testPath)) {
95-
die($testPath);
96-
$this->fail();
97-
}
91+
$this->assertTrue(file_exists($testPath), "File $testPath does not exist");
9892
}
9993

100-
/**
94+
/**t
10195
* Проверяет загрузку несуществующего кеша
102-
* @expectedException Exception
10396
*/
10497
public function testGetWithUknownKey()
10598
{
99+
$this->expectException(Exception::class);
106100
$instance = new FileCache();
107101
$instance->get(self::UnknownKeyFixture);
108102
}
@@ -112,6 +106,7 @@ public function testGetWithUknownKey()
112106
*/
113107
public function testGetSecurity()
114108
{
109+
$this->expectException(Exception::class);
115110
$instance = new FileCache();
116111
$instance->get('../FileCacheTest.php');
117112
}
@@ -131,16 +126,15 @@ public function testGet()
131126
// cover cached variant
132127
$data = $instance->get(self::KeyFixture);
133128
$this->assertEquals($data, $initialData);
134-
135129
}
136130

137131

138132
/**
139133
* Проверяем очистку кеша при несуществующем хеше
140-
* @expectedException Exception
141134
*/
142135
public function testClearWithUnknownKey()
143136
{
137+
$this->ExpectException(Exception::class);
144138
$instance = new FileCache();
145139
$instance->clear(self::UnknownKeyFixture);
146140
}
@@ -150,6 +144,7 @@ public function testClearWithUnknownKey()
150144
*/
151145
public function testClear()
152146
{
147+
$this->expectException(Faid\Cache\Exception::class);
153148
$key = 'test';
154149
$instance = new FileCache();
155150
$data = array(1, 2, 3, 4);
@@ -158,12 +153,9 @@ public function testClear()
158153

159154
$instance->clear($key);
160155

161-
try {
162-
$instance->get($key);
163-
$this->fail('Exception must be thrown');
164-
} catch (Exception $e) {
165-
166-
}
156+
$this->expectException(Exception::class);
157+
$instance->get($key);
158+
$this->fail('Exception must be thrown');
167159
}
168160

169161
public function testUnknownCacheIsActual()
@@ -192,15 +184,16 @@ public function testIsActual()
192184
}
193185

194186
/**
195-
* @expectedException Exception
187+
*
196188
*/
197189
public function testGetNotActualCache()
198190
{
191+
$this->expectException(Exception::class);
199192
$key = 'test';
200193
$time = 1;
201194
$instance = new FileCache();
202195
$instance->set($key, 'test', $time);
203-
sleep($time);
196+
sleep($time + 1);
204197
$instance->get($key);
205198
}
206199

@@ -215,6 +208,5 @@ public function testGetPersistentCache()
215208
$this->assertEquals($value, $instance->get($key));
216209
sleep(1);
217210
$this->assertEquals($value, $instance->get($key));
218-
219211
}
220212
}

0 commit comments

Comments
 (0)