Passed
Pull Request — master (#41)
by Darío
06:34 queued 03:02
created

itCanChangeTheBodyPreviouslyAsJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace EasyHttp\LayerContracts\Tests\Unit\Common;
4
5
use EasyHttp\LayerContracts\Common\SecurityContext;
6
use EasyHttp\LayerContracts\Tests\TestCase;
7
use EasyHttp\LayerContracts\Tests\Unit\Example\ClientRequest;
8
9
class ClientRequestTest extends TestCase
10
{
11
    /**
12
     * @test
13
     */
14
    public function itSetsInitialProperties()
15
    {
16
        $method = 'POST';
17
        $url = $this->faker->url;
18
19
        $request = new ClientRequest($method, $url);
20
21
        $this->assertSame($method, $request->getMethod());
22
        $this->assertSame($url, $request->getUri());
23
        $this->assertFalse($request->hasHeaders());
24
        $this->assertEmpty($request->getBody());
25
        $this->assertFalse($request->hasJson());
26
        $this->assertFalse($request->hasQuery());
27
        $this->assertFalse($request->hasSecurityContext());
28
        $this->assertFalse($request->isSSL());
29
        $this->assertFalse($request->hasBasicAuth());
30
    }
31
32
    /**
33
     * @test
34
     */
35
    public function itCanChangeItsData()
36
    {
37
        $request = new ClientRequest('POST', $this->faker->url);
38
39
        $method = 'GET';
40
        $url = $this->faker->url;
41
        $request->setMethod($method);
42
        $request->setUri($url);
43
        $request->setJson(['foo' => 'bar']);
44
        $request->setQuery(['bar' => 'baz']);
45
        $request->setTimeout(20);
46
        $request->setHeaders(['a' => 'b']);
47
        $request->setHeader('auth', 'xdsG56');
48
        $request->setBasicAuth('user', 'pass');
49
        $security = new SecurityContext();
50
        $request->setSecurityContext($security);
51
        $request->ssl(true);
52
53
        $this->assertSame($method, $request->getMethod());
54
        $this->assertSame($url, $request->getUri());
55
        $this->assertSame(['foo' => 'bar'], $request->getJson());
56
        $this->assertTrue($request->hasJson());
57
        $this->assertSame(['bar' => 'baz'], $request->getQuery());
58
        $this->assertTrue($request->hasQuery());
59
        $this->assertSame(20, $request->getTimeout());
60
        $this->assertSame('xdsG56', $request->getHeader('auth'));
61
        $this->assertSame(['a' => 'b', 'auth' => 'xdsG56'], $request->getHeaders());
62
        $this->assertTrue($request->hasHeaders());
63
        $this->assertSame(['user', 'pass'], $request->getBasicAuth());
64
        $this->assertTrue($request->hasBasicAuth());
65
        $this->assertTrue($request->hasSecurityContext());
66
        $this->assertTrue($request->isSSL());
67
    }
68
69
    /**
70
     * @test
71
     */
72
    public function itCanSetTheBodyAsJson()
73
    {
74
        $request = new ClientRequest('POST', $this->faker->url);
75
76
        $request->setJson(['foo' => 'bar']);
77
78
        $this->assertSame(['foo' => 'bar'], $request->getJson());
79
        $this->assertSame('{"foo":"bar"}', $request->getBody());
80
    }
81
82
    /**
83
     * @test
84
     */
85
    public function itCannotParseWrontJsonValues()
86
    {
87
        $request = new ClientRequest('POST', $this->faker->url);
88
89
        $request->setJson(["\xc3" => "bar"]);
90
91
        $this->assertSame('', $request->getBody());
92
    }
93
94
    /**
95
     * @test
96
     */
97
    public function itCanChangeTheBodyPreviouslyAsJson()
98
    {
99
        $request = new ClientRequest('POST', $this->faker->url);
100
101
        $request->setJson(['foo' => 'bar']);
102
        $request->setBody('Not Found');
103
104
        $this->assertSame([], $request->getJson());
105
        $this->assertSame('Not Found', $request->getBody());
106
    }
107
108
    /**
109
     * @test
110
     */
111
    public function itCanChangeTheBodyPreviouslyAsPlainText()
112
    {
113
        $request = new ClientRequest('POST', $this->faker->url);
114
115
        $request->setBody('Not Found');
116
        $request->setJson(['foo' => 'bar']);
117
118
        $this->assertSame(['foo' => 'bar'], $request->getJson());
119
        $this->assertSame('{"foo":"bar"}', $request->getBody());
120
    }
121
122
    /**
123
     * @test
124
     */
125
    public function itHasBodyForJsonAssignment()
126
    {
127
        $request = new ClientRequest('POST', $this->faker->url);
128
129
        $this->assertFalse($request->hasBody());
130
131
        $request->setJson(['foo' => 'bar']);
132
133
        $this->assertTrue($request->hasBody());
134
    }
135
136
    /**
137
     * @test
138
     */
139
    public function itHasBodyWhenItIsAssigned()
140
    {
141
        $request = new ClientRequest('POST', $this->faker->url);
142
143
        $this->assertFalse($request->hasBody());
144
145
        $request->setBody('Not Found');
146
147
        $this->assertTrue($request->hasBody());
148
    }
149
}
150