|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace RESTAPI\Tests; |
| 4 | + |
| 5 | +use RESTAPI\Core\ModelCache; |
| 6 | +use RESTAPI\Core\ModelSet; |
| 7 | +use RESTAPI\Core\TestCase; |
| 8 | +use RESTAPI\Models\DNSResolverHostOverrideAlias; |
| 9 | +use RESTAPI\Models\FirewallAlias; |
| 10 | +use RESTAPI\Models\RESTAPISettings; |
| 11 | + |
| 12 | +/** |
| 13 | + * Tests for the RESTAPI\Core\ModelCache class. These tests ensure that the central ModelCache object correctly |
| 14 | + * caches and retrieves Model objects as expected. |
| 15 | + */ |
| 16 | +class APICoreModelCacheTestCase extends TestCase { |
| 17 | + /** |
| 18 | + * Ensures that the ModelCache is always a singleton instance. |
| 19 | + */ |
| 20 | + public function test_model_cache_singleton_instance(): void { |
| 21 | + $instance_1 = ModelCache::get_instance(); |
| 22 | + $instance_2 = ModelCache::get_instance(); |
| 23 | + |
| 24 | + $this->assert_equals($instance_1, $instance_2); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Ensures the has_modelset() method correctly identifies if a given Model class is cached in the ModelCache. |
| 29 | + */ |
| 30 | + public function test_model_cache_has_modelset(): void { |
| 31 | + # Obtain and clear the ModelCache |
| 32 | + $model_cache = ModelCache::get_instance(); |
| 33 | + $model_cache::clear(); |
| 34 | + |
| 35 | + # Ensure the ModelCache does not have a cached ModelSet for the MockModelClass |
| 36 | + $this->assert_is_false($model_cache::has_modelset('MockModelClass')); |
| 37 | + |
| 38 | + # Populate the ModelCache with a mock ModelSet for testing |
| 39 | + ModelCache::$cache['MockModelClass'] = new ModelSet(); |
| 40 | + $this->assert_is_true($model_cache::has_modelset('MockModelClass')); |
| 41 | + |
| 42 | + # Clear the ModelCache and ensure the ModelSet is removed |
| 43 | + $model_cache::clear(); |
| 44 | + $this->assert_is_false($model_cache::has_modelset('MockModelClass')); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Ensures the cache_modelset() correctly caches a ModelSet in the ModelCache. |
| 49 | + */ |
| 50 | + public function test_model_cache_cache_modelset(): void { |
| 51 | + # Obtain and clear the ModelCache |
| 52 | + $model_cache = ModelCache::get_instance(); |
| 53 | + $model_cache::clear(); |
| 54 | + |
| 55 | + # Create a mock ModelSet to cache |
| 56 | + $mock_model_set = new ModelSet(); |
| 57 | + |
| 58 | + # Cache the mock ModelSet in the ModelCache |
| 59 | + $model_cache::cache_modelset('MockModelClass', $mock_model_set); |
| 60 | + |
| 61 | + # Ensure the ModelSet was cached correctly |
| 62 | + $this->assert_is_true($model_cache::has_modelset('MockModelClass')); |
| 63 | + $this->assert_equals($model_cache::fetch_modelset('MockModelClass'), $mock_model_set); |
| 64 | + |
| 65 | + # Clear the ModelCache |
| 66 | + $model_cache::clear(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Ensures the fetch_modelset() method correctly retrieves a cached ModelSet from the ModelCache. |
| 71 | + */ |
| 72 | + public function test_model_cache_fetch_nonexisting_modelset_throws_error(): void { |
| 73 | + # Obtain and clear the ModelCache |
| 74 | + $model_cache = ModelCache::get_instance(); |
| 75 | + $model_cache::clear(); |
| 76 | + |
| 77 | + # Ensure fetching a non-cached ModelSet throws an error |
| 78 | + $this->assert_throws_response( |
| 79 | + response_id: 'MODEL_CACHE_MODELSET_NOT_FOUND', |
| 80 | + code: 404, |
| 81 | + callable: function () { |
| 82 | + ModelCache::fetch_modelset('MockModelClass'); |
| 83 | + }, |
| 84 | + ); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Ensures Models must be many-enabled to be indexed in the ModelCache. |
| 89 | + */ |
| 90 | + public function test_model_index_must_be_many_enabled(): void { |
| 91 | + # Ensure attempting to index a non-many enabled Model throws an error |
| 92 | + $this->assert_throws_response( |
| 93 | + response_id: 'MODEL_CACHE_INDEX_FIELD_ON_NON_MANY_MODEL', |
| 94 | + code: 500, |
| 95 | + callable: function () { |
| 96 | + ModelCache::index_modelset_by_field(model_class: RESTAPISettings::get_class_fqn(), index_field: 'id'); |
| 97 | + }, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Ensures we cannot index a cached ModelSet by a non-unique field. |
| 103 | + */ |
| 104 | + public function test_model_index_field_must_be_unique(): void { |
| 105 | + # Ensure indexing by id is always allowed |
| 106 | + $this->assert_does_not_throw(function () { |
| 107 | + ModelCache::index_modelset_by_field(model_class: FirewallAlias::get_class_fqn(), index_field: 'id'); |
| 108 | + }); |
| 109 | + |
| 110 | + # Ensure attempting to index the Model by the non-unique field throws an error |
| 111 | + $this->assert_throws_response( |
| 112 | + response_id: 'MODEL_CACHE_INDEX_FIELD_NOT_UNIQUE', |
| 113 | + code: 500, |
| 114 | + callable: function () { |
| 115 | + ModelCache::index_modelset_by_field(model_class: FirewallAlias::get_class_fqn(), index_field: 'descr'); |
| 116 | + }, |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * Ensures we cannot index a cached ModelSet for a Model that has a parent model class. |
| 122 | + */ |
| 123 | + public function test_model_index_field_cannot_have_parent_model_class(): void { |
| 124 | + # Ensure attempting to index the Model by the non-unique field throws an error |
| 125 | + $this->assert_throws_response( |
| 126 | + response_id: 'MODEL_CACHE_INDEX_FIELD_ON_PARENTED_MODEL', |
| 127 | + code: 500, |
| 128 | + callable: function () { |
| 129 | + ModelCache::index_modelset_by_field( |
| 130 | + model_class: DNSResolverHostOverrideAlias::get_class_fqn(), |
| 131 | + index_field: 'id', |
| 132 | + ); |
| 133 | + }, |
| 134 | + ); |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Ensures the index_modelset_by_field() method correctly indexes a cached ModelSet by the specified field. This |
| 139 | + * test also ensures the has_model() and fetch_model() methods work as expected for indexed Model objects. |
| 140 | + */ |
| 141 | + public function test_model_index_modelset_by_field(): void { |
| 142 | + # Create FirewallAlias model objects to test with |
| 143 | + $alias1 = new FirewallAlias(data: ['name' => 'host_alias', 'descr' => 'First alias', 'type' => 'host']); |
| 144 | + $alias2 = new FirewallAlias(data: ['name' => 'network_alias', 'descr' => 'Second alias', 'type' => 'network']); |
| 145 | + $alias3 = new FirewallAlias(data: ['name' => 'port_alias', 'descr' => 'Third alias', 'type' => 'port']); |
| 146 | + $alias1->create(); |
| 147 | + $alias2->create(); |
| 148 | + $alias3->create(); |
| 149 | + |
| 150 | + # Index the FirewallAlias ModelSet by the 'name' field |
| 151 | + ModelCache::index_modelset_by_field(model_class: FirewallAlias::get_class_fqn(), index_field: 'name'); |
| 152 | + |
| 153 | + # Ensure the ModelCache has the indexed Models |
| 154 | + $this->assert_is_true( |
| 155 | + ModelCache::has_model( |
| 156 | + model_class: FirewallAlias::get_class_fqn(), |
| 157 | + index_field: 'name', |
| 158 | + index_value: 'host_alias', |
| 159 | + ), |
| 160 | + ); |
| 161 | + $this->assert_is_true( |
| 162 | + ModelCache::has_model( |
| 163 | + model_class: FirewallAlias::get_class_fqn(), |
| 164 | + index_field: 'name', |
| 165 | + index_value: 'network_alias', |
| 166 | + ), |
| 167 | + ); |
| 168 | + $this->assert_is_true( |
| 169 | + ModelCache::has_model( |
| 170 | + model_class: FirewallAlias::get_class_fqn(), |
| 171 | + index_field: 'name', |
| 172 | + index_value: 'port_alias', |
| 173 | + ), |
| 174 | + ); |
| 175 | + |
| 176 | + # Fetch the indexed Models and ensure they are correct |
| 177 | + $fetched_alias1 = ModelCache::fetch_model( |
| 178 | + model_class: FirewallAlias::get_class_fqn(), |
| 179 | + index_field: 'name', |
| 180 | + index_value: 'host_alias', |
| 181 | + ); |
| 182 | + $this->assert_equals($fetched_alias1->name->value, 'host_alias'); |
| 183 | + $fetched_alias2 = ModelCache::fetch_model( |
| 184 | + model_class: FirewallAlias::get_class_fqn(), |
| 185 | + index_field: 'name', |
| 186 | + index_value: 'network_alias', |
| 187 | + ); |
| 188 | + $this->assert_equals($fetched_alias2->name->value, 'network_alias'); |
| 189 | + $fetched_alias3 = ModelCache::fetch_model( |
| 190 | + model_class: FirewallAlias::get_class_fqn(), |
| 191 | + index_field: 'name', |
| 192 | + index_value: 'port_alias', |
| 193 | + ); |
| 194 | + $this->assert_equals($fetched_alias3->name->value, 'port_alias'); |
| 195 | + |
| 196 | + # Clean up the created aliases |
| 197 | + FirewallAlias::delete_all(); |
| 198 | + } |
| 199 | +} |
0 commit comments