'bar']); $command['bar'] = 'test'; $request = new Request('POST', 'http://httbin.org'); $param = new Parameter(['name' => 'foo']); $location->visit($command, $request, $param); $param = new Parameter(['name' => 'bar']); $location->visit($command, $request, $param); $operation = new Operation(); $request = $location->after($command, $request, $operation); $xml = $request->getBody()->getContents(); $this->assertEquals('' . "\n" . 'bartest' . "\n", $xml); $header = $request->getHeader('Content-Type'); $this->assertArraySubset([0 => 'application/xml'], $header); } /** * @group RequestLocation */ public function testCreatesBodyForEmptyDocument() { $location = new XmlLocation(); $command = new Command('foo', ['foo' => 'bar']); $request = new Request('POST', 'http://httbin.org'); $operation = new Operation([ 'data' => ['xmlAllowEmpty' => true] ]); $request = $location->after($command, $request, $operation); $xml = $request->getBody()->getContents(); $this->assertEquals('' . "\n" . '' . "\n", $xml); $header = $request->getHeader('Content-Type'); $this->assertArraySubset([0 => 'application/xml'], $header); } /** * @group RequestLocation */ public function testAddsAdditionalParameters() { $location = new XmlLocation('xml', 'test'); $command = new Command('foo', ['foo' => 'bar']); $request = new Request('POST', 'http://httbin.org'); $param = new Parameter(['name' => 'foo']); $command['foo'] = 'bar'; $location->visit($command, $request, $param); $operation = new Operation([ 'additionalParameters' => [ 'location' => 'xml' ] ]); $command['bam'] = 'boo'; $request = $location->after($command, $request, $operation); $xml = $request->getBody()->getContents(); $this->assertEquals('' . "\n" . 'barbarboo' . "\n", $xml); $header = $request->getHeader('Content-Type'); $this->assertArraySubset([0 => 'test'], $header); } /** * @group RequestLocation */ public function testAllowsXmlEncoding() { $location = new XmlLocation(); $operation = new Operation([ 'data' => ['xmlEncoding' => 'UTF-8'] ]); $command = new Command('foo', ['foo' => 'bar']); $request = new Request('POST', 'http://httbin.org'); $param = new Parameter(['name' => 'foo']); $command['foo'] = 'bar'; $location->visit($command, $request, $param); $request = $location->after($command, $request, $operation); $xml = $request->getBody()->getContents(); $this->assertEquals('' . "\n" . 'bar' . "\n", $xml); } public function xmlProvider() { return [ [ [ 'data' => [ 'xmlRoot' => [ 'name' => 'test', 'namespaces' => 'http://foo.com' ] ], 'parameters' => [ 'Foo' => [ 'location' => 'xml', 'type' => 'string' ], 'Baz' => [ 'location' => 'xml', 'type' => 'string' ] ] ], [ 'Foo' => 'test', 'Baz' => 'bar' ], 'testbar' ], // Ensure that the content-type is not added [ [ 'parameters' => [ 'Foo' => [ 'location' => 'xml', 'type' => 'string' ] ] ], [], '' ], // Test with adding attributes and no namespace [ [ 'data' => [ 'xmlRoot' => [ 'name' => 'test' ] ], 'parameters' => [ 'Foo' => [ 'location' => 'xml', 'type' => 'string', 'data' => ['xmlAttribute' => true] ] ] ], [ 'Foo' => 'test', 'Baz' => 'bar' ], '' ], // Test adding with an array [ [ 'parameters' => [ 'Foo' => [ 'location' => 'xml', 'type' => 'string' ], 'Baz' => [ 'type' => 'array', 'location' => 'xml', 'items' => [ 'type' => 'numeric', 'sentAs' => 'Bar' ] ] ] ], ['Foo' => 'test', 'Baz' => [1, 2]], 'test12' ], // Test adding an object [ [ 'parameters' => [ 'Foo' => ['location' => 'xml', 'type' => 'string'], 'Baz' => [ 'type' => 'object', 'location' => 'xml', 'properties' => [ 'Bar' => ['type' => 'string'], 'Bam' => [] ] ] ] ], [ 'Foo' => 'test', 'Baz' => [ 'Bar' => 'abc', 'Bam' => 'foo' ] ], 'testabcfoo' ], // Add an array that contains an object [ [ 'parameters' => [ 'Baz' => [ 'type' => 'array', 'location' => 'xml', 'items' => [ 'type' => 'object', 'sentAs' => 'Bar', 'properties' => ['A' => [], 'B' => []] ] ] ] ], ['Baz' => [ [ 'A' => '1', 'B' => '2' ], [ 'A' => '3', 'B' => '4' ] ]], '1234' ], // Add an object of attributes [ [ 'parameters' => [ 'Foo' => [ 'location' => 'xml', 'type' => 'string' ], 'Baz' => [ 'type' => 'object', 'location' => 'xml', 'properties' => [ 'Bar' => [ 'type' => 'string', 'data' => [ 'xmlAttribute' => true ] ], 'Bam' => [] ] ] ] ], [ 'Foo' => 'test', 'Baz' => [ 'Bar' => 'abc', 'Bam' => 'foo' ] ], 'testfoo' ], // Check order doesn't matter [ [ 'parameters' => [ 'Foo' => [ 'location' => 'xml', 'type' => 'string' ], 'Baz' => [ 'type' => 'object', 'location' => 'xml', 'properties' => [ 'Bar' => [ 'type' => 'string', 'data' => [ 'xmlAttribute' => true ] ], 'Bam' => [] ] ] ] ], [ 'Foo' => 'test', 'Baz' => [ 'Bam' => 'foo', 'Bar' => 'abc' ] ], 'testfoo' ], // Add values with custom namespaces [ [ 'parameters' => [ 'Foo' => [ 'location' => 'xml', 'type' => 'string', 'data' => [ 'xmlNamespace' => 'http://foo.com' ] ] ] ], ['Foo' => 'test'], 'test' ], // Add attributes with custom namespace prefix [ [ 'parameters' => [ 'Wrap' => [ 'type' => 'object', 'location' => 'xml', 'properties' => [ 'Foo' => [ 'type' => 'string', 'sentAs' => 'xsi:baz', 'data' => [ 'xmlNamespace' => 'http://foo.com', 'xmlAttribute' => true ] ] ] ], ] ], ['Wrap' => [ 'Foo' => 'test' ]], '' ], // Add nodes with custom namespace prefix [ [ 'parameters' => [ 'Wrap' => [ 'type' => 'object', 'location' => 'xml', 'properties' => [ 'Foo' => [ 'type' => 'string', 'sentAs' => 'xsi:Foo', 'data' => [ 'xmlNamespace' => 'http://foobar.com' ] ] ] ], ] ], ['Wrap' => [ 'Foo' => 'test' ]], 'test' ], [ [ 'parameters' => [ 'Foo' => [ 'location' => 'xml', 'type' => 'string', 'data' => [ 'xmlNamespace' => 'http://foo.com' ] ] ] ], ['Foo' => '

This is a title

'], 'This is a title]]>' ], // Flat array at top level [ [ 'parameters' => [ 'Bars' => [ 'type' => 'array', 'data' => ['xmlFlattened' => true], 'location' => 'xml', 'items' => [ 'type' => 'object', 'sentAs' => 'Bar', 'properties' => [ 'A' => [], 'B' => [] ] ] ], 'Boos' => [ 'type' => 'array', 'data' => ['xmlFlattened' => true], 'location' => 'xml', 'items' => [ 'sentAs' => 'Boo', 'type' => 'string' ] ] ] ], [ 'Bars' => [ ['A' => '1', 'B' => '2'], ['A' => '3', 'B' => '4'] ], 'Boos' => ['test', '123'] ], '1234test123' ], // Nested flat arrays [ [ 'parameters' => [ 'Delete' => [ 'type' => 'object', 'location' => 'xml', 'properties' => [ 'Items' => [ 'type' => 'array', 'data' => ['xmlFlattened' => true], 'items' => [ 'type' => 'object', 'sentAs' => 'Item', 'properties' => [ 'A' => [], 'B' => [] ] ] ] ] ] ] ], [ 'Delete' => [ 'Items' => [ ['A' => '1', 'B' => '2'], ['A' => '3', 'B' => '4'] ] ] ], '1234' ], // Test adding root node attributes after nodes [ [ 'data' => [ 'xmlRoot' => [ 'name' => 'test' ] ], 'parameters' => [ 'Foo' => ['location' => 'xml', 'type' => 'string'], 'Baz' => ['location' => 'xml', 'type' => 'string', 'data' => ['xmlAttribute' => true]], ] ], ['Foo' => 'test', 'Baz' => 'bar'], 'test' ], ]; } /** * @param array $operation * @param array $input * @param string $xml * @dataProvider xmlProvider * @group RequestLocation */ public function testSerializesXml(array $operation, array $input, $xml) { $container = []; $history = Middleware::history($container); $mock = new MockHandler([new Response(200)]); $stack = new HandlerStack($mock); $stack->push($history); $operation['uri'] = 'http://httpbin.org'; $client = new GuzzleClient( new Client(['handler' => $stack]), new Description([ 'operations' => [ 'foo' => $operation ] ]) ); $command = $client->getCommand('foo', $input); $client->execute($command); $this->assertCount(1, $container); foreach ($container as $transaction) { /** @var Request $request */ $request = $transaction['request']; if (empty($input)) { if ($request->hasHeader('Content-Type')) { $this->assertArraySubset([0 => ''], $request->getHeader('Content-Type')); } } else { $this->assertArraySubset([0 => 'application/xml'], $request->getHeader('Content-Type')); } $body = str_replace(["\n", ""], '', (string) $request->getBody()); $this->assertEquals($xml, $body); } } }