appConfig = $this->createStub(AppConfig::class); $this->settingsData = new SettingsData($this->appConfig); } /** * jsonSerialize returns all four expected keys sourced from AppConfig. */ public function testJsonSerializeReturnsAllExpectedKeys(): void { $formats = ["docx" => ["mime" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document"]]; $this->appConfig->method("formatsSetting")->willReturn($formats); $this->appConfig->method("getSameTab")->willReturn(true); $this->appConfig->method("getEnableSharing")->willReturn(false); $this->appConfig->method("getDisableDownload")->willReturn(true); $result = $this->settingsData->jsonSerialize(); $this->assertArrayHasKey("formats", $result); $this->assertArrayHasKey("sameTab", $result); $this->assertArrayHasKey("enableSharing", $result); $this->assertArrayHasKey("disableDownload", $result); } /** * jsonSerialize passes AppConfig values through unchanged. */ public function testJsonSerializeReflectsAppConfigValues(): void { $formats = ["xlsx" => ["mime" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]]; $this->appConfig->method("formatsSetting")->willReturn($formats); $this->appConfig->method("getSameTab")->willReturn(false); $this->appConfig->method("getEnableSharing")->willReturn(true); $this->appConfig->method("getDisableDownload")->willReturn(false); $result = $this->settingsData->jsonSerialize(); $this->assertSame($formats, $result["formats"]); $this->assertFalse($result["sameTab"]); $this->assertTrue($result["enableSharing"]); $this->assertFalse($result["disableDownload"]); } /** * The object is JSON-encodable and produces a valid JSON string. */ public function testObjectIsJsonEncodable(): void { $this->appConfig->method("formatsSetting")->willReturn([]); $this->appConfig->method("getSameTab")->willReturn(false); $this->appConfig->method("getEnableSharing")->willReturn(false); $this->appConfig->method("getDisableDownload")->willReturn(false); $json = json_encode($this->settingsData); $this->assertIsString($json); $this->assertNotFalse($json); $decoded = json_decode($json, true); $this->assertIsArray($decoded); } }