Completed
Push — issue#666 ( 0f58f5...3afa36 )
by Guilherme
03:38
created

HttpMocker::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 18
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 28
rs 8.8571
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\RemoteClaimsBundle\Tests\Http;
12
13
use GuzzleHttp\Client;
14
use GuzzleHttp\Subscriber\History;
15
use GuzzleHttp\Subscriber\Mock;
16
use LoginCidadao\RemoteClaimsBundle\Tests\Parser\RemoteClaimParserTest;
17
18
class HttpMocker
19
{
20
    /** @var History */
21
    private $history;
22
23
    /** @var mixed */
24
    private $data;
25
26
    /** @var Client */
27
    private $client;
28
29
    /**
30
     * HttpMocker constructor.
31
     * @param mixed $data Remote Claim metadata
32
     * @param string|null $expectedUri if a Discovery call is expected, this parameter MUST be the Claim's URI
33
     */
34
    public function __construct($data = null, $expectedUri = null)
35
    {
36
        $this->history = new History();
37
        $this->data = $data !== null ? $data : RemoteClaimParserTest::$claimMetadata;
38
39
        $this->client = new Client();
40
41
        $metadata = json_encode($this->data);
42
        $length = strlen($metadata);
43
44
        $responses = ["HTTP/1.1 200 OK\r\n\Content-Length: {$length}\r\n\r\n{$metadata}"];
45
46
        if ($expectedUri !== null) {
47
            $discoveryResponse = json_encode([
48
                'subject' => $data['claim_name'],
49
                'links' => [
50
                    ['rel' => 'http://openid.net/specs/connect/1.0/claim', 'href' => $expectedUri],
51
                ],
52
            ]);
53
            $discoveryLen = strlen($discoveryResponse);
54
            array_unshift($responses,
55
                "HTTP/1.1 200 OK\r\n\Content-Length: {$discoveryLen}\r\n\r\n{$discoveryResponse}");
56
        }
57
58
        $mock = new Mock($responses);
59
        $this->client->getEmitter()->attach($this->history);
60
        $this->client->getEmitter()->attach($mock);
61
    }
62
63
    /**
64
     * @return Client
65
     */
66
    public function getClient()
67
    {
68
        return $this->client;
69
    }
70
71
    /**
72
     * @return History
73
     */
74
    public function getHistory()
75
    {
76
        return $this->history;
77
    }
78
}
79