|
6 | 6 | use Illuminate\Support\Facades\Cache; |
7 | 7 | use Mockery; |
8 | 8 | use PHPUnit\Framework\Attributes\Test; |
| 9 | +use Statamic\Facades\StaticCache; |
9 | 10 | use Statamic\StaticCaching\Cacher; |
| 11 | +use Statamic\StaticCaching\NoCache\RegionNotFound; |
10 | 12 | use Statamic\StaticCaching\NoCache\Session; |
11 | 13 | use Statamic\StaticCaching\NoCache\StringRegion; |
12 | 14 | use Tests\FakesContent; |
@@ -148,6 +150,39 @@ public function it_restores_from_cache() |
148 | 150 | $this->assertEquals('http://localhost/cp', $cascade['cp_url']); |
149 | 151 | } |
150 | 152 |
|
| 153 | + #[Test] |
| 154 | + public function it_serializes_and_unserializes_regions_through_cache() |
| 155 | + { |
| 156 | + $session = new Session('http://localhost/test'); |
| 157 | + |
| 158 | + $region = $session->pushRegion('the contents', ['foo' => 'bar'], '.html'); |
| 159 | + |
| 160 | + $cached = StaticCache::cacheStore()->get('nocache::region.'.$region->key()); |
| 161 | + $this->assertIsString($cached, 'Region should be stored as a serialized string, not an object.'); |
| 162 | + |
| 163 | + $retrieved = $session->region($region->key()); |
| 164 | + |
| 165 | + $this->assertInstanceOf(StringRegion::class, $retrieved); |
| 166 | + $this->assertEquals($region->key(), $retrieved->key()); |
| 167 | + $this->assertEquals(['foo' => 'bar'], $retrieved->context()); |
| 168 | + } |
| 169 | + |
| 170 | + #[Test] |
| 171 | + public function it_throws_region_not_found_when_cached_region_is_an_incomplete_class() |
| 172 | + { |
| 173 | + $session = new Session('http://localhost/test'); |
| 174 | + |
| 175 | + $region = $session->pushRegion('the contents', ['foo' => 'bar'], '.html'); |
| 176 | + |
| 177 | + // Simulate what happens when serializable_classes enforcement |
| 178 | + // turns a cached Region object into __PHP_Incomplete_Class. |
| 179 | + StaticCache::cacheStore()->forever('nocache::region.'.$region->key(), new \__PHP_Incomplete_Class); |
| 180 | + |
| 181 | + $this->expectException(RegionNotFound::class); |
| 182 | + |
| 183 | + $session->region($region->key()); |
| 184 | + } |
| 185 | + |
151 | 186 | #[Test] |
152 | 187 | public function a_singleton_is_bound_in_the_container() |
153 | 188 | { |
|
0 commit comments