if (!isset($this->states[$appName])) {
$this->states[$appName] = [];
}
- $this->states[$appName][$key] = json_encode($data);
+ try {
+ $this->states[$appName][$key] = json_encode($data, JSON_THROW_ON_ERROR);
+ } catch (\JsonException $e) {
+ $this->logger->error('Invalid '. $key . ' data provided to provideInitialState by ' . $appName, ['exception' => $e]);
+ }
return;
}
use OC\AppFramework\Bootstrap\Coordinator;
use OCP\IServerContainer;
+use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use function json_encode;
use JsonSerializable;
class InitialStateServiceTest extends TestCase {
/** @var InitialStateService */
private $service;
+ /** @var MockObject|LoggerInterface|(LoggerInterface&MockObject) */
+ protected $logger;
protected function setUp(): void {
parent::setUp();
+ $this->logger = $this->createMock(LoggerInterface::class);
+
$this->service = new InitialStateService(
- $this->createMock(LoggerInterface::class),
+ $this->logger,
$this->createMock(Coordinator::class),
$this->createMock(IServerContainer::class)
);
}
- public function staticData() {
+ public function staticData(): array {
return [
['string'],
[23],
/**
* @dataProvider staticData
*/
- public function testStaticData($value) {
+ public function testStaticData(mixed $value): void {
$this->service->provideInitialState('test', 'key', $value);
$data = $this->service->getInitialStates();
);
}
- public function testStaticButInvalidData() {
+ public function testValidDataButFailsToJSONEncode(): void {
+ $this->logger->expects($this->once())
+ ->method('error');
+
+ $this->service->provideInitialState('test', 'key', ['upload' => INF]);
+ $data = $this->service->getInitialStates();
+
+ $this->assertEquals(
+ [],
+ $data
+ );
+ }
+
+ public function testStaticButInvalidData(): void {
+ $this->logger->expects($this->once())
+ ->method('warning');
+
$this->service->provideInitialState('test', 'key', new stdClass());
$data = $this->service->getInitialStates();
/**
* @dataProvider staticData
*/
- public function testLazyData($value) {
+ public function testLazyData(mixed $value): void {
$this->service->provideLazyInitialState('test', 'key', function () use ($value) {
return $value;
});