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