Skip to content

Commit 05c80ad

Browse files
committed
Add Test support
1 parent dec8955 commit 05c80ad

File tree

4 files changed

+120
-54
lines changed

4 files changed

+120
-54
lines changed

tests/framework/RestServiceTest.php

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,75 +8,59 @@
88
require_once RELATIVE_PATH . 'framework/Controller.php';
99
require_once RELATIVE_PATH . 'framework/RestService.php';
1010

11-
class RestServiceStub extends \framework\RestService
11+
class ModelStub extends framework\Model
1212
{
1313
public function __construct()
1414
{
15-
// Skip parent constructor to avoid HTTP header operations during tests.
15+
// Do not call parent constructor to avoid DB connection
1616
}
1717
}
1818

19-
final class RestServiceTest extends TestCase
19+
class ViewStub extends framework\View
2020
{
21-
private function getProperty(object $object, string $property)
21+
public function __construct()
2222
{
23-
$reflection = new ReflectionClass($object);
24-
$prop = $reflection->getProperty($property);
25-
$prop->setAccessible(true);
23+
$template = 'Template Stub';
24+
$this->replaceTpl($template);
2625

27-
return $prop;
2826
}
27+
}
28+
class RestServiceStub extends \framework\RestService
29+
{
2930

30-
private function callPrivate(object $object, string $method, array $args = [])
31+
public function __construct($view = null, $model = null)
3132
{
32-
$reflection = new ReflectionClass($object);
33-
$methodRef = $reflection->getMethod($method);
34-
$methodRef->setAccessible(true);
35-
36-
return $methodRef->invokeArgs($object, $args);
33+
parent::__construct(new ViewStub(), new ModelStub());
3734
}
3835

36+
}
37+
38+
final class RestServiceTest extends TestCase
39+
{
40+
3941
public function testAllowMethodTracksAllowedOperations(): void
4042
{
4143
$service = new RestServiceStub();
42-
4344
$service->allowMethod('fetch');
44-
45-
$allowed = $this->getProperty($service, 'allowedMethods')->getValue($service);
45+
$allowed = $service->getAllowedMethods();
4646
$this->assertContains('fetch', $allowed);
4747
}
4848

4949
public function testAddCorsStoresAllowedOrigins(): void
5050
{
5151
$service = new RestServiceStub();
52-
5352
$service->addCORS('https://example.com');
54-
$origins = $this->getProperty($service, 'accessControlAllowOrigins')->getValue($service);
55-
53+
$origins = $service->getAccessControlAllowOrigins();
5654
$this->assertSame(['https://example.com'], $origins);
5755
}
5856

5957
public function testHttpPostRequestReturnsCurrentResult(): void
6058
{
6159
$service = new RestServiceStub();
62-
$resultProperty = $this->getProperty($service, 'result');
63-
$resultProperty->setValue($service, ['status' => 'ok']);
64-
65-
$this->assertSame(['status' => 'ok'], $service->httpPostRequest('method', []));
60+
$service->allowMethod('fetch');
61+
$resultProperty = $service->httpPostRequest('fetch', 1);
62+
$expected = ['message:' => 'Web MVC REST Service.', 'status:' => 'ok'];
63+
$this->assertSame($expected, $resultProperty['body_data:']);
6664
}
6765

68-
public function testSwitchActionMergesOperationResult(): void
69-
{
70-
$service = new RestServiceStub();
71-
$resultProperty = $this->getProperty($service, 'result');
72-
$resultProperty->setValue($service, ['status' => 'ok']);
73-
$methodProperty = $this->getProperty($service, 'HTTPRequestMethod');
74-
$methodProperty->setValue($service, 'DELETE');
75-
76-
$this->callPrivate($service, 'switchAction', ['remove', ['id' => 10]]);
77-
78-
$updated = $resultProperty->getValue($service);
79-
$this->assertSame('DELETE', $updated['rest_operation']);
80-
$this->assertSame('ok', $updated['status']);
81-
}
8266
}

tests/framework/TestHelper.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?php
2-
$projectRoot = dirname(__DIR__, 4);
2+
$projectRoot = dirname(__DIR__, 2);
33

44
if (!defined('RELATIVE_PATH')) {
55
define('RELATIVE_PATH', $projectRoot . DIRECTORY_SEPARATOR);
@@ -117,3 +117,15 @@
117117
if (!isset($_SESSION)) {
118118
$_SESSION = [];
119119
}
120+
if (!function_exists('getallheaders')) {
121+
function getallheaders()
122+
{
123+
$headers = [];
124+
foreach ($_SERVER as $name => $value) {
125+
if (substr($name, 0, 5) == 'HTTP_') {
126+
$headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value;
127+
}
128+
}
129+
return $headers;
130+
}
131+
}

tests/framework/UserTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,64 @@
55
require_once __DIR__ . '/TestHelper.php';
66
require_once RELATIVE_PATH . 'framework/Model.php';
77
require_once RELATIVE_PATH . 'framework/MySqlRecord.php';
8+
require_once RELATIVE_PATH . 'framework/BeanUser.php';
89
require_once RELATIVE_PATH . 'framework/User.php';
910

11+
1012
class UserStub extends \framework\User
1113
{
14+
private $id;
15+
private $email;
16+
private $password;
17+
private $role;
18+
private $token;
19+
private $tokenTimeStamp;
20+
21+
public function getId()
22+
{
23+
return $this->id;
24+
}
25+
26+
public function getEmail()
27+
{
28+
return $this->email;
29+
}
30+
31+
public function getPassword()
32+
{
33+
return $this->password;
34+
}
35+
36+
public function getRole()
37+
{
38+
return $this->role;
39+
}
40+
41+
public function getToken()
42+
{
43+
return $this->token;
44+
}
45+
46+
public function getTokenTimeStamp()
47+
{
48+
return $this->tokenTimeStamp;
49+
}
50+
51+
public function isLogged(): bool
52+
{
53+
return $this->id !== null && $this->email !== null && $this->password !== null;
54+
}
55+
56+
public function validateToken(int $expirationHours = 8): bool
57+
{
58+
if ($this->token === null || $this->tokenTimeStamp === null) {
59+
return false;
60+
}
61+
$tokenTime = strtotime($this->tokenTimeStamp);
62+
$currentTime = time();
63+
return ($currentTime - $tokenTime) <= ($expirationHours * 3600);
64+
}
65+
1266
public function __construct()
1367
{
1468
// Skip parent constructor to avoid database connection and session dependencies.

tests/framework/ViewTest.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,26 @@
44

55
require_once __DIR__ . '/TestHelper.php';
66
require_once RELATIVE_PATH . 'framework/View.php';
7+
require_once RELATIVE_PATH . 'framework/exceptions/MVCException.php';
8+
require_once RELATIVE_PATH . 'framework/exceptions/NotInitializedViewException.php';
9+
require_once RELATIVE_PATH . 'framework/exceptions/TemplateNotFoundException.php';
10+
require_once RELATIVE_PATH . 'framework/exceptions/VariableNotFoundException.php';
711

812
class TestableView extends \framework\View
913
{
1014
public function __construct($content)
1115
{
12-
$this->tpl = $content;
13-
$this->blocks = [];
14-
$this->currentBlock = '';
16+
parent::__construct();
17+
$this->replaceTpl($content);
1518
}
19+
1620
}
1721

1822
final class ViewTest extends TestCase
1923
{
2024
public function testSetVarReplacesPlaceholderInTemplate(): void
2125
{
26+
$template = 'Hello {NAME}';
2227
$view = new TestableView('Hello {NAME}');
2328
$view->setVar('NAME', 'World');
2429

@@ -27,28 +32,39 @@ public function testSetVarReplacesPlaceholderInTemplate(): void
2732

2833
public function testHideRemovesSpecificBlock(): void
2934
{
30-
$template = '<!-- BEGIN content -->Visible<!-- END content -->';
35+
$template = '<!-- BEGIN content --> Visible <!-- END content -->More Content';
3136
$view = new TestableView($template);
32-
3337
$view->hide('content');
34-
3538
$this->assertStringNotContainsString('Visible', $view->parse());
3639
}
3740

38-
public function testSetVarTemplatePathInjectsSiteUrl(): void
39-
{
40-
$view = new TestableView('Path: {TEMPLATE_PATH}');
41-
42-
$view->setVarTemplatePath();
43-
44-
$this->assertSame('Path: ' . SITEURL . '/', $view->parse());
45-
}
46-
4741
public function testReplaceTplOverridesContent(): void
4842
{
4943
$view = new TestableView('Original');
5044
$view->replaceTpl('New Content');
5145

5246
$this->assertSame('New Content', $view->parse());
5347
}
48+
49+
public function testOpenBlockThenParseThenCloseThenShow(): void
50+
{
51+
52+
$users = array(
53+
array('UserName' => 'Mark', 'UserEmail' => 'mark@email.com'),
54+
array('UserName' => 'Elen', 'UserEmail' => 'elen@email.com'),
55+
array('UserName' => 'John', 'UserEmail' => 'john@email.com')
56+
);
57+
$template = '<!-- BEGIN Users -->[{UserName} {UserEmail}]<!-- END Users -->';
58+
$expected = '<!-- BEGIN Users -->[Mark mark@email.com][Elen elen@email.com][John john@email.com]<!-- END Users -->';
59+
$view = new TestableView($template);
60+
$view->openBlock("Users");
61+
foreach ($users as $user) {
62+
$view->setVar("UserName", $user["UserName"]);
63+
$view->setVar("UserEmail", $user["UserEmail"]);
64+
$view->parseCurrentBlock();
65+
}
66+
$view->setBlock();
67+
$this->assertSame($expected, $view->parse());
68+
}
69+
5470
}

0 commit comments

Comments
 (0)