|
8 | 8 | require_once RELATIVE_PATH . 'framework/Controller.php'; |
9 | 9 | require_once RELATIVE_PATH . 'framework/RestService.php'; |
10 | 10 |
|
11 | | -class RestServiceStub extends \framework\RestService |
| 11 | +class ModelStub extends framework\Model |
12 | 12 | { |
13 | 13 | public function __construct() |
14 | 14 | { |
15 | | - // Skip parent constructor to avoid HTTP header operations during tests. |
| 15 | + // Do not call parent constructor to avoid DB connection |
16 | 16 | } |
17 | 17 | } |
18 | 18 |
|
19 | | -final class RestServiceTest extends TestCase |
| 19 | +class ViewStub extends framework\View |
20 | 20 | { |
21 | | - private function getProperty(object $object, string $property) |
| 21 | + public function __construct() |
22 | 22 | { |
23 | | - $reflection = new ReflectionClass($object); |
24 | | - $prop = $reflection->getProperty($property); |
25 | | - $prop->setAccessible(true); |
| 23 | + $template = 'Template Stub'; |
| 24 | + $this->replaceTpl($template); |
26 | 25 |
|
27 | | - return $prop; |
28 | 26 | } |
| 27 | +} |
| 28 | +class RestServiceStub extends \framework\RestService |
| 29 | +{ |
29 | 30 |
|
30 | | - private function callPrivate(object $object, string $method, array $args = []) |
| 31 | + public function __construct($view = null, $model = null) |
31 | 32 | { |
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()); |
37 | 34 | } |
38 | 35 |
|
| 36 | +} |
| 37 | + |
| 38 | +final class RestServiceTest extends TestCase |
| 39 | +{ |
| 40 | + |
39 | 41 | public function testAllowMethodTracksAllowedOperations(): void |
40 | 42 | { |
41 | 43 | $service = new RestServiceStub(); |
42 | | - |
43 | 44 | $service->allowMethod('fetch'); |
44 | | - |
45 | | - $allowed = $this->getProperty($service, 'allowedMethods')->getValue($service); |
| 45 | + $allowed = $service->getAllowedMethods(); |
46 | 46 | $this->assertContains('fetch', $allowed); |
47 | 47 | } |
48 | 48 |
|
49 | 49 | public function testAddCorsStoresAllowedOrigins(): void |
50 | 50 | { |
51 | 51 | $service = new RestServiceStub(); |
52 | | - |
53 | 52 | $service->addCORS('https://example.com'); |
54 | | - $origins = $this->getProperty($service, 'accessControlAllowOrigins')->getValue($service); |
55 | | - |
| 53 | + $origins = $service->getAccessControlAllowOrigins(); |
56 | 54 | $this->assertSame(['https://example.com'], $origins); |
57 | 55 | } |
58 | 56 |
|
59 | 57 | public function testHttpPostRequestReturnsCurrentResult(): void |
60 | 58 | { |
61 | 59 | $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:']); |
66 | 64 | } |
67 | 65 |
|
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 | | - } |
82 | 66 | } |
0 commit comments