summaryrefslogtreecommitdiff
path: root/vendor/guzzle/guzzle/tests/Guzzle/Tests/Service/Command/LocationVisitor/Request/JsonVisitorTest.php
blob: ea6782f5352049f68a6e0093f944be8f82102bdc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

namespace Guzzle\Tests\Service\Command\LocationVisitor\Request;

use Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor as Visitor;

/**
 * @covers Guzzle\Service\Command\LocationVisitor\Request\JsonVisitor
 * @covers Guzzle\Service\Command\LocationVisitor\Request\AbstractRequestVisitor::resolveRecursively
 */
class JsonVisitorTest extends AbstractVisitorTestCase
{
    public function testVisitsLocation()
    {
        $visitor = new Visitor();
        // Test after when no body query values were found
        $visitor->after($this->command, $this->request);

        $param = $this->getNestedCommand('json')->getParam('foo');
        $visitor->visit($this->command, $this->request, $param->setSentAs('test'), '123');
        $visitor->visit($this->command, $this->request, $param->setSentAs('test2'), 'abc');
        $visitor->after($this->command, $this->request);
        $this->assertEquals('{"test":"123","test2":"abc"}', (string) $this->request->getBody());
    }

    public function testAddsJsonHeader()
    {
        $visitor = new Visitor();
        $visitor->setContentTypeHeader('application/json-foo');
        $param = $this->getNestedCommand('json')->getParam('foo');
        $visitor->visit($this->command, $this->request, $param->setSentAs('test'), '123');
        $visitor->after($this->command, $this->request);
        $this->assertEquals('application/json-foo', (string) $this->request->getHeader('Content-Type'));
    }

    public function testRecursivelyBuildsJsonBodies()
    {
        $command = $this->getCommand('json');
        $request = $command->prepare();
        $this->assertEquals('{"Foo":{"test":{"baz":true,"Jenga_Yall!":"HELLO"},"bar":123}}', (string) $request->getBody());
    }

    public function testAppliesFiltersToAdditionalProperties()
    {
        $command = $this->getCommand('json');
        $command->set('foo', array('not_set' => 'abc'));
        $request = $command->prepare();
        $result = json_decode($request->getBody(), true);
        $this->assertEquals('ABC', $result['Foo']['not_set']);
    }

    public function testAppliesFiltersToArrayItemValues()
    {
        $command = $this->getCommand('json');
        $command->set('arr', array('a', 'b'));
        $request = $command->prepare();
        $result = json_decode($request->getBody(), true);
        $this->assertEquals(array('A', 'B'), $result['arr']);
    }
}