2 declare(strict_types = 1);
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
48 protected function setUp():
void
50 $this->view = $this->getMockBuilder(JsonView::class)
51 ->setMethods([
'loadConfigurationFromYamlFile'])
53 $this->controllerContext = $this->createMock(ControllerContext::class);
54 $this->response = $this->createMock(Response::class);
55 $this->controllerContext->expects($this->any())->method(
'getResponse')->will($this->returnValue($this->response));
56 $this->view->setControllerContext($this->controllerContext);
67 $object = new \stdClass();
68 $object->value1 =
'foo';
71 $expected = [
'value1' =>
'foo',
'value2' => 1];
72 $output[] = [$object, $configuration, $expected,
'all direct child properties should be serialized'];
74 $configuration = [
'_only' => [
'value1']];
75 $expected = [
'value1' =>
'foo'];
76 $output[] = [$object, $configuration, $expected,
'if "only" properties are specified, only these should be serialized'];
78 $configuration = [
'_exclude' => [
'value1']];
79 $expected = [
'value2' => 1];
80 $output[] = [$object, $configuration, $expected,
'if "exclude" properties are specified, they should not be serialized'];
82 $object = new \stdClass();
83 $object->value1 = new \stdClass();
84 $object->value1->subvalue1 =
'Foo';
87 $expected = [
'value2' => 1];
88 $output[] = [$object, $configuration, $expected,
'by default, sub objects of objects should not be serialized.'];
90 $object = new \stdClass();
91 $object->value1 = [
'subarray' =>
'value'];
94 $expected = [
'value2' => 1];
95 $output[] = [$object, $configuration, $expected,
'by default, sub arrays of objects should not be serialized.'];
97 $object = [
'foo' =>
'bar', 1 =>
'baz',
'deep' => [
'test' =>
'value']];
99 $expected = [
'foo' =>
'bar', 1 =>
'baz',
'deep' => [
'test' =>
'value']];
100 $output[] = [$object, $configuration, $expected,
'associative arrays should be serialized deeply'];
102 $object = [
'foo',
'bar'];
104 $expected = [
'foo',
'bar'];
105 $output[] = [$object, $configuration, $expected,
'numeric arrays should be serialized'];
107 $nestedObject = new \stdClass();
108 $nestedObject->value1 =
'foo';
109 $object = [$nestedObject];
111 $expected = [[
'value1' =>
'foo']];
112 $output[] = [$object, $configuration, $expected,
'array of objects should be serialized'];
114 $properties = [
'foo' =>
'bar',
'prohibited' =>
'xxx'];
115 $nestedObject = $this->getMockBuilder($this->getUniqueId(
'Test'))
116 ->setMethods([
'getName',
'getPath',
'getProperties',
'getOther'])
118 $nestedObject->expects($this->any())->method(
'getName')->will($this->returnValue(
'name'));
119 $nestedObject->expects($this->any())->method(
'getPath')->will($this->returnValue(
'path'));
120 $nestedObject->expects($this->any())->method(
'getProperties')->will($this->returnValue($properties));
121 $nestedObject->expects($this->never())->method(
'getOther');
122 $object = $nestedObject;
124 '_only' => [
'name',
'path',
'properties'],
127 '_exclude' => [
'prohibited'],
134 'properties' => [
'foo' =>
'bar'],
136 $output[] = [$object, $configuration, $expected,
'descending into arrays should be possible'];
138 $nestedObject = new \stdClass();
139 $nestedObject->value1 =
'foo';
140 $value = new \SplObjectStorage();
141 $value->attach($nestedObject);
143 $expected = [[
'value1' =>
'foo']];
144 $output[] = [$value, $configuration, $expected,
'SplObjectStorage with objects should be serialized'];
146 $dateTimeObject = new \DateTime(
'2011-02-03T03:15:23',
new \DateTimeZone(
'UTC'));
148 $expected =
'2011-02-03T03:15:23+00:00';
149 $output[] = [$dateTimeObject, $configuration, $expected,
'DateTime object in UTC time zone could not be serialized.'];
151 $dateTimeObject = new \DateTime(
'2013-08-15T15:25:30',
new \DateTimeZone(
'America/Los_Angeles'));
153 $expected =
'2013-08-15T15:25:30-07:00';
154 $output[] = [$dateTimeObject, $configuration, $expected,
'DateTime object in America/Los_Angeles time zone could not be serialized.'];
167 public function testTransformValue($object, array $configuration, $expected,
string $description):
void
169 $jsonView = $this->getAccessibleMock(JsonView::class, [
'dummy'], [],
'',
false);
171 $actual = $jsonView->_call(
'transformValue', $object, $configuration);
173 $this->assertSame($expected, $actual, $description);
184 $dummyIdentifier =
'e4f40dfc-8c6e-4414-a5b1-6fd3c5cf7a53';
186 $object = new \stdClass();
187 $object->value1 = new \stdClass();
191 '_exposeObjectIdentifier' =>
true,
196 $expected = [
'value1' => [
'__identity' => $dummyIdentifier]];
197 $output[] = [$object, $configuration, $expected, $dummyIdentifier,
'boolean TRUE should result in __identity key'];
199 $configuration[
'_descend'][
'value1'][
'_exposedObjectIdentifierKey'] =
'guid';
200 $expected = [
'value1' => [
'guid' => $dummyIdentifier]];
201 $output[] = [$object, $configuration, $expected, $dummyIdentifier,
'string value should result in string-equal key'];
217 array $configuration,
219 string $dummyIdentifier,
222 $persistenceManagerMock = $this->getMockBuilder(PersistenceManager::class)
223 ->setMethods([
'getIdentifierByObject'])
225 $jsonView = $this->getAccessibleMock(JsonView::class, [
'dummy'], [],
'',
false);
226 $jsonView->_set(
'persistenceManager', $persistenceManagerMock);
228 $persistenceManagerMock->expects($this->once())->method(
'getIdentifierByObject')->with($object->value1)->will($this->returnValue($dummyIdentifier));
230 $actual = $jsonView->_call(
'transformValue', $object, $configuration);
232 $this->assertSame($expected, $actual, $description);
240 $className = $this->getUniqueId(
'DummyClass');
241 $namespace =
'TYPO3\CMS\Extbase\Tests\Unit\Mvc\View\\' . $className;
247 [
'value1' => [
'__class' => $namespace .
'\\' . $className]],
253 [
'value1' => [
'__class' => $className]],
273 ?
int $exposeClassNameSetting,
278 $fullyQualifiedClassName = $namespace .
'\\' . $className;
279 if (class_exists($fullyQualifiedClassName) ===
false) {
280 eval(
'namespace ' . $namespace .
'; class ' . $className .
' {}');
283 $object = new \stdClass();
284 $object->value1 =
new $fullyQualifiedClassName();
288 '_exposeClassName' => $exposeClassNameSetting,
292 $reflectionService = $this->getMockBuilder(\
TYPO3\CMS\
Extbase\Reflection\ReflectionService::class)
293 ->setMethods([
'getClassNameByObject' ])
295 $reflectionService->expects($this->any())->method(
'getClassNameByObject')->will($this->returnCallback(
function ($object) {
296 return get_class($object);
299 $jsonView = $this->getAccessibleMock(\
TYPO3\CMS\
Extbase\Mvc\View\JsonView::class, [
'dummy'], [],
'',
false);
300 $this->inject($jsonView,
'reflectionService', $reflectionService);
301 $actual = $jsonView->_call(
'transformValue', $object, $configuration);
302 $this->assertSame($expected, $actual);
310 $this->response->expects($this->once())->method(
'setHeader')->with(
'Content-Type',
'application/json');
312 $this->view->render();
320 $object = new \stdClass();
321 $object->foo =
'Foo';
322 $this->view->assign(
'value', $object);
324 $expectedResult =
'{"foo":"Foo"}';
325 $actualResult = $this->view->render();
326 $this->assertSame($expectedResult, $actualResult);
334 $array = [
'foo' =>
'Foo',
'bar' =>
'Bar'];
335 $this->view->assign(
'value', $array);
337 $expectedResult =
'{"foo":"Foo","bar":"Bar"}';
338 $actualResult = $this->view->render();
339 $this->assertSame($expectedResult, $actualResult);
348 $this->view->assign(
'value', $value);
350 $expectedResult =
'"Foo"';
351 $actualResult = $this->view->render();
352 $this->assertSame($expectedResult, $actualResult);
361 $this->view->assign(
'value', $value);
363 $actualResult = $this->view->render();
365 $expectedResult =
'"' . $value .
'"';
366 $this->assertSame($expectedResult, $actualResult);
375 'backslash' => [
'\\'],
376 'double quote' => [
'"'],
387 $this->view->assign(
'value', $character);
389 $actualResult = $this->view->render();
391 $expectedResult =
'"\\' . $character .
'"';
392 $this->assertSame($expectedResult, $actualResult);
401 $this->view->assign(
'foo', $value);
403 $expectedResult =
'null';
404 $actualResult = $this->view->render();
405 $this->assertSame($expectedResult, $actualResult);
414 ->assign(
'value',
'Value')
415 ->assign(
'someOtherVariable',
'Foo');
417 $expectedResult =
'"Value"';
418 $actualResult = $this->view->render();
419 $this->assertSame($expectedResult, $actualResult);
428 $this->view->assign(
'foo', $value);
429 $this->view->setVariablesToRender([
'foo']);
431 $expectedResult =
'"Foo"';
432 $actualResult = $this->view->render();
433 $this->assertSame($expectedResult, $actualResult);
442 ->assign(
'value',
'Value1')
443 ->assign(
'secondValue',
'Value2')
444 ->assign(
'someOtherVariable',
'Value3');
445 $this->view->setVariablesToRender([
'value',
'secondValue']);
447 $expectedResult =
'{"value":"Value1","secondValue":"Value2"}';
448 $actualResult = $this->view->render();
449 $this->assertSame($expectedResult, $actualResult);
457 $array = [
'foo' => [
'bar' =>
'Baz']];
458 $object = new \stdClass();
459 $object->foo =
'Foo';
462 ->assign(
'array', $array)
463 ->assign(
'object', $object)
464 ->assign(
'someOtherVariable',
'Value3');
465 $this->view->setVariablesToRender([
'array',
'object']);
467 $expectedResult =
'{"array":{"foo":{"bar":"Baz"}},"object":{"foo":"Foo"}}';
468 $actualResult = $this->view->render();
469 $this->assertSame($expectedResult, $actualResult);
477 $array = [[
'name' =>
'Foo',
'secret' =>
true], [
'name' =>
'Bar',
'secret' =>
true]];
479 $this->view->assign(
'value', $array);
480 $this->view->setConfiguration([
488 $expectedResult =
'[{"name":"Foo"},{"name":"Bar"}]';
489 $actualResult = $this->view->render();
490 $this->assertSame($expectedResult, $actualResult);
505 $this->view->assign(
'value', $array);
506 $this->view->setConfiguration([
510 '_exclude' => [
'secret']
515 $expectedResult =
'{"items":[{"name":"Foo"},{"name":"Bar"}]}';
516 $actualResult = $this->view->render();
517 self::assertSame($expectedResult, $actualResult);
525 $array = [[
'name' =>
'Foo',
'secret' =>
true], [
'name' =>
'Bar',
'secret' =>
true]];
527 $this->view->assign(
'value', $array);
528 $this->view->setConfiguration([
536 $expectedResult =
'[{"name":"Foo","secret":true},{"name":"Bar","secret":true}]';
537 $actualResult = $this->view->render();
538 $this->assertSame($expectedResult, $actualResult);