BaseResponseTest.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\AppFramework\OCS;
  8. use OC\AppFramework\OCS\BaseResponse;
  9. class ArrayValue implements \JsonSerializable {
  10. private $array;
  11. public function __construct(array $array) {
  12. $this->array = $array;
  13. }
  14. public function jsonSerialize(): mixed {
  15. return $this->array;
  16. }
  17. }
  18. class BaseResponseTest extends \Test\TestCase {
  19. public function testToXml(): void {
  20. /** @var BaseResponse $response */
  21. $response = $this->createMock(BaseResponse::class);
  22. $writer = new \XMLWriter();
  23. $writer->openMemory();
  24. $writer->setIndent(false);
  25. $writer->startDocument();
  26. $data = [
  27. 'hello' => 'hello',
  28. 'information' => [
  29. '@test' => 'some data',
  30. 'someElement' => 'withAttribute',
  31. ],
  32. 'value without key',
  33. 'object' => new \stdClass(),
  34. ];
  35. $this->invokePrivate($response, 'toXml', [$data, $writer]);
  36. $writer->endDocument();
  37. $this->assertEquals(
  38. "<?xml version=\"1.0\"?>\n<hello>hello</hello><information test=\"some data\"><someElement>withAttribute</someElement></information><element>value without key</element><object/>\n",
  39. $writer->outputMemory(true)
  40. );
  41. }
  42. public function testToXmlJsonSerializable(): void {
  43. /** @var BaseResponse $response */
  44. $response = $this->createMock(BaseResponse::class);
  45. $writer = new \XMLWriter();
  46. $writer->openMemory();
  47. $writer->setIndent(false);
  48. $writer->startDocument();
  49. $data = [
  50. 'hello' => 'hello',
  51. 'information' => new ArrayValue([
  52. '@test' => 'some data',
  53. 'someElement' => 'withAttribute',
  54. ]),
  55. 'value without key',
  56. 'object' => new \stdClass(),
  57. ];
  58. $this->invokePrivate($response, 'toXml', [$data, $writer]);
  59. $writer->endDocument();
  60. $this->assertEquals(
  61. "<?xml version=\"1.0\"?>\n<hello>hello</hello><information test=\"some data\"><someElement>withAttribute</someElement></information><element>value without key</element><object/>\n",
  62. $writer->outputMemory(true)
  63. );
  64. }
  65. }