BaseResponseTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Daniel Kesselberg <mail@danielkesselberg.de>
  5. *
  6. * @author 2020 Daniel Kesselberg <mail@danielkesselberg.de>
  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. */
  24. namespace Test\AppFramework\Middleware;
  25. use OC\AppFramework\OCS\BaseResponse;
  26. class ArrayValue implements \JsonSerializable {
  27. private $array;
  28. public function __construct(array $array) {
  29. $this->array = $array;
  30. }
  31. public function jsonSerialize(): mixed {
  32. return $this->array;
  33. }
  34. }
  35. class BaseResponseTest extends \Test\TestCase {
  36. public function testToXml(): void {
  37. /** @var BaseResponse $response */
  38. $response = $this->createMock(BaseResponse::class);
  39. $writer = new \XMLWriter();
  40. $writer->openMemory();
  41. $writer->setIndent(false);
  42. $writer->startDocument();
  43. $data = [
  44. 'hello' => 'hello',
  45. 'information' => [
  46. '@test' => 'some data',
  47. 'someElement' => 'withAttribute',
  48. ],
  49. 'value without key',
  50. 'object' => new \stdClass(),
  51. ];
  52. $this->invokePrivate($response, 'toXml', [$data, $writer]);
  53. $writer->endDocument();
  54. $this->assertEquals(
  55. "<?xml version=\"1.0\"?>\n<hello>hello</hello><information test=\"some data\"><someElement>withAttribute</someElement></information><element>value without key</element><object/>\n",
  56. $writer->outputMemory(true)
  57. );
  58. }
  59. public function testToXmlJsonSerializable(): void {
  60. /** @var BaseResponse $response */
  61. $response = $this->createMock(BaseResponse::class);
  62. $writer = new \XMLWriter();
  63. $writer->openMemory();
  64. $writer->setIndent(false);
  65. $writer->startDocument();
  66. $data = [
  67. 'hello' => 'hello',
  68. 'information' => new ArrayValue([
  69. '@test' => 'some data',
  70. 'someElement' => 'withAttribute',
  71. ]),
  72. 'value without key',
  73. 'object' => new \stdClass(),
  74. ];
  75. $this->invokePrivate($response, 'toXml', [$data, $writer]);
  76. $writer->endDocument();
  77. $this->assertEquals(
  78. "<?xml version=\"1.0\"?>\n<hello>hello</hello><information test=\"some data\"><someElement>withAttribute</someElement></information><element>value without key</element><object/>\n",
  79. $writer->outputMemory(true)
  80. );
  81. }
  82. }