InitialStateServiceTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace Test;
  24. use JsonSerializable;
  25. use OC\AppFramework\Bootstrap\Coordinator;
  26. use OC\InitialStateService;
  27. use OCP\IServerContainer;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. use Psr\Log\LoggerInterface;
  30. use stdClass;
  31. use function json_encode;
  32. class InitialStateServiceTest extends TestCase {
  33. /** @var InitialStateService */
  34. private $service;
  35. /** @var MockObject|LoggerInterface|(LoggerInterface&MockObject) */
  36. protected $logger;
  37. protected function setUp(): void {
  38. parent::setUp();
  39. $this->logger = $this->createMock(LoggerInterface::class);
  40. $this->service = new InitialStateService(
  41. $this->logger,
  42. $this->createMock(Coordinator::class),
  43. $this->createMock(IServerContainer::class)
  44. );
  45. }
  46. public function staticData(): array {
  47. return [
  48. ['string'],
  49. [23],
  50. [2.3],
  51. [new class implements JsonSerializable {
  52. public function jsonSerialize(): int {
  53. return 3;
  54. }
  55. }],
  56. ];
  57. }
  58. /**
  59. * @dataProvider staticData
  60. */
  61. public function testStaticData(mixed $value): void {
  62. $this->service->provideInitialState('test', 'key', $value);
  63. $data = $this->service->getInitialStates();
  64. $this->assertEquals(
  65. ['test-key' => json_encode($value)],
  66. $data
  67. );
  68. }
  69. public function testValidDataButFailsToJSONEncode(): void {
  70. $this->logger->expects($this->once())
  71. ->method('error');
  72. $this->service->provideInitialState('test', 'key', ['upload' => INF]);
  73. $data = $this->service->getInitialStates();
  74. $this->assertEquals(
  75. [],
  76. $data
  77. );
  78. }
  79. public function testStaticButInvalidData(): void {
  80. $this->logger->expects($this->once())
  81. ->method('warning');
  82. $this->service->provideInitialState('test', 'key', new stdClass());
  83. $data = $this->service->getInitialStates();
  84. $this->assertEquals(
  85. [],
  86. $data
  87. );
  88. }
  89. /**
  90. * @dataProvider staticData
  91. */
  92. public function testLazyData(mixed $value): void {
  93. $this->service->provideLazyInitialState('test', 'key', function () use ($value) {
  94. return $value;
  95. });
  96. $data = $this->service->getInitialStates();
  97. $this->assertEquals(
  98. ['test-key' => json_encode($value)],
  99. $data
  100. );
  101. }
  102. }