Skip to content

Commit 94a6626

Browse files
committed
Add Test support
1 parent 2817dc1 commit 94a6626

File tree

1 file changed

+22
-38
lines changed

1 file changed

+22
-38
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
}

0 commit comments

Comments
 (0)