|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Test Error class. |
| 4 | + * |
| 5 | + * @package Microsub |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Microsub\Tests; |
| 9 | + |
| 10 | +use WP_UnitTestCase; |
| 11 | +use Microsub\Error; |
| 12 | + |
| 13 | +/** |
| 14 | + * Test Error class. |
| 15 | + * |
| 16 | + * @coversDefaultClass \Microsub\Error |
| 17 | + */ |
| 18 | +class Test_Error extends WP_UnitTestCase { |
| 19 | + |
| 20 | + /** |
| 21 | + * Test invalid_request returns 400 with message. |
| 22 | + * |
| 23 | + * @covers ::invalid_request |
| 24 | + * @covers ::response |
| 25 | + */ |
| 26 | + public function test_invalid_request_error() { |
| 27 | + $response = Error::invalid_request( 'Test error message' ); |
| 28 | + |
| 29 | + $this->assertInstanceOf( '\WP_REST_Response', $response ); |
| 30 | + $this->assertEquals( 400, $response->get_status() ); |
| 31 | + |
| 32 | + $data = $response->get_data(); |
| 33 | + $this->assertEquals( 'invalid_request', $data['error'] ); |
| 34 | + $this->assertEquals( 'Test error message', $data['error_description'] ); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Test unauthorized returns 401. |
| 39 | + * |
| 40 | + * @covers ::unauthorized |
| 41 | + */ |
| 42 | + public function test_unauthorized_error() { |
| 43 | + $response = Error::unauthorized(); |
| 44 | + |
| 45 | + $this->assertEquals( 401, $response->get_status() ); |
| 46 | + |
| 47 | + $data = $response->get_data(); |
| 48 | + $this->assertEquals( 'unauthorized', $data['error'] ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Test forbidden returns 403. |
| 53 | + * |
| 54 | + * @covers ::forbidden |
| 55 | + */ |
| 56 | + public function test_forbidden_error() { |
| 57 | + $response = Error::forbidden(); |
| 58 | + |
| 59 | + $this->assertEquals( 403, $response->get_status() ); |
| 60 | + |
| 61 | + $data = $response->get_data(); |
| 62 | + $this->assertEquals( 'forbidden', $data['error'] ); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test insufficient_scope returns 403 with message. |
| 67 | + * |
| 68 | + * @covers ::insufficient_scope |
| 69 | + */ |
| 70 | + public function test_insufficient_scope_error() { |
| 71 | + $response = Error::insufficient_scope( 'Requires read scope' ); |
| 72 | + |
| 73 | + $this->assertEquals( 403, $response->get_status() ); |
| 74 | + |
| 75 | + $data = $response->get_data(); |
| 76 | + $this->assertEquals( 'insufficient_scope', $data['error'] ); |
| 77 | + $this->assertEquals( 'Requires read scope', $data['error_description'] ); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Test not_found returns 404. |
| 82 | + * |
| 83 | + * @covers ::not_found |
| 84 | + */ |
| 85 | + public function test_not_found_error() { |
| 86 | + $response = Error::not_found(); |
| 87 | + |
| 88 | + $this->assertEquals( 404, $response->get_status() ); |
| 89 | + |
| 90 | + $data = $response->get_data(); |
| 91 | + $this->assertEquals( 'not_found', $data['error'] ); |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Test not_implemented returns 501. |
| 96 | + * |
| 97 | + * @covers ::not_implemented |
| 98 | + */ |
| 99 | + public function test_not_implemented_error() { |
| 100 | + $response = Error::not_implemented( 'Feature not available' ); |
| 101 | + |
| 102 | + $this->assertEquals( 501, $response->get_status() ); |
| 103 | + |
| 104 | + $data = $response->get_data(); |
| 105 | + $this->assertEquals( 'not_implemented', $data['error'] ); |
| 106 | + $this->assertEquals( 'Feature not available', $data['error_description'] ); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * Test server_error returns 500. |
| 111 | + * |
| 112 | + * @covers ::server_error |
| 113 | + */ |
| 114 | + public function test_server_error() { |
| 115 | + $response = Error::server_error(); |
| 116 | + |
| 117 | + $this->assertEquals( 500, $response->get_status() ); |
| 118 | + |
| 119 | + $data = $response->get_data(); |
| 120 | + $this->assertEquals( 'server_error', $data['error'] ); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Test generic response with custom error. |
| 125 | + * |
| 126 | + * @covers ::response |
| 127 | + */ |
| 128 | + public function test_generic_response() { |
| 129 | + $response = Error::response( 'custom_error', 'Custom description' ); |
| 130 | + |
| 131 | + $this->assertEquals( 400, $response->get_status() ); |
| 132 | + |
| 133 | + $data = $response->get_data(); |
| 134 | + $this->assertEquals( 'custom_error', $data['error'] ); |
| 135 | + $this->assertEquals( 'Custom description', $data['error_description'] ); |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Test response without description omits field. |
| 140 | + * |
| 141 | + * @covers ::response |
| 142 | + */ |
| 143 | + public function test_response_without_description() { |
| 144 | + $response = Error::response( 'invalid_request' ); |
| 145 | + |
| 146 | + $data = $response->get_data(); |
| 147 | + $this->assertEquals( 'invalid_request', $data['error'] ); |
| 148 | + $this->assertArrayNotHasKey( 'error_description', $data ); |
| 149 | + } |
| 150 | +} |
0 commit comments