public function testNormalize() { $obj = new GetSetDummy(); $object = new \stdClass(); $obj->setFoo('foo'); $obj->setBar('bar'); $obj->setBaz(true); $obj->setCamelCase('camelcase'); $obj->setObject($object); $this->serializer ->expects($this->once()) ->method('normalize') ->with($object, 'any') ->will($this->returnValue('string_object')) ; $this->assertEquals( array( 'foo' => 'foo', 'bar' => 'bar', 'baz' => true, 'fooBar' => 'foobar', 'camelCase' => 'camelcase', 'object' => 'string_object', ), $this->normalizer->normalize($obj, 'any') ); }
/** * @expectedException \Symfony\Component\Serializer\Exception\CircularReferenceException */ public function testCircularNormalize() { $this->normalizer->setCircularReferenceLimit(1); $this->serializer->expects($this->once())->method('normalize')->will($this->returnCallback(function ($data, $format, $context) { $this->normalizer->normalize($data['qux'], $format, $context); return 'string_object'; })); $this->assertEquals('string_object', $this->normalizer->normalize(new JsonSerializableDummy())); }
/** * Tests the normalize() method with account context passed. * * @covers ::normalize */ public function testNormalizeWithAccountContext() { $mock_account = $this->getMock('Drupal\\Core\\Session\\AccountInterface'); $context = ['account' => $mock_account]; $this->serializer->expects($this->any())->method('normalize')->with($this->containsOnlyInstancesOf('Drupal\\Core\\Field\\FieldItemListInterface'), 'test_format', $context)->will($this->returnValue('test')); // The mock account should get passed directly into the access() method on // field items from $context['account']. $definitions = array('field_1' => $this->createMockFieldListItem(TRUE, $mock_account), 'field_2' => $this->createMockFieldListItem(FALSE, $mock_account)); $content_entity_mock = $this->createMockForContentEntity($definitions); $normalized = $this->contentEntityNormalizer->normalize($content_entity_mock, 'test_format', $context); $this->assertArrayHasKey('field_1', $normalized); $this->assertEquals('test', $normalized['field_1']); $this->assertArrayNotHasKey('field_2', $normalized); }
protected function validateRetrieveApiCall($method, $uri, array $urlParameters, $statusCode, $type, $transformedResult, array $serializerMap = array()) { $rawResponse = 'the-server-response'; $response = $this->getMockBuilder('\\Psr\\Http\\Message\\ResponseInterface')->getMock(); $response->expects($this->any())->method('getStatusCode')->willReturn($statusCode); $response->expects($this->any())->method('getHeader')->with('Content-Type')->willReturn(array('application/json')); $response->expects($this->any())->method('getBody')->willReturn($rawResponse); $request = $this->validateRequest($method, $uri, $urlParameters); if (404 === $statusCode) { $this->requestHandler->expects($this->once())->method('executeRequest')->with($request)->willThrowException(new NotFoundException('Not found')); } else { $this->requestHandler->expects($this->once())->method('executeRequest')->with($request)->willReturn($response); } $this->validateSerializer($serializerMap); if ($statusCode < 400) { $this->serializer->expects($this->once())->method('deserialize')->with($rawResponse, 'Xabbuh\\XApi\\Model\\' . $type, 'json')->willReturn($transformedResult); } }
public function testSupportsInvalidArray() { $this->serializer->expects($this->any())->method('supportsDenormalization')->will($this->returnValue(false)); $this->assertFalse($this->denormalizer->supportsDenormalization(array(array('foo' => 'one', 'bar' => 'two'), array('foo' => 'three', 'bar' => 'four')), __NAMESPACE__ . '\\InvalidClass[]')); }