Failed Conditions
Push — master ( 9bceb4...42a7b7 )
by Guilherme
02:51 queued 10s
created

HttpMocker::__construct()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 4
nop 2
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
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\Handler\MockHandler;
15
use GuzzleHttp\HandlerStack;
16
use GuzzleHttp\Middleware;
17
use GuzzleHttp\Psr7\Request;
18
use GuzzleHttp\Psr7\Response;
19
use LoginCidadao\RemoteClaimsBundle\Tests\Parser\RemoteClaimParserTest;
20
21
class HttpMocker
22
{
23
    /** @var array */
24
    private $requests;
25
26
    /** @var array */
27
    private $responses;
28
29
    /** @var mixed */
30
    private $data;
31
32
    /** @var Client */
33
    private $client;
34
35
    /**
36
     * HttpMocker constructor.
37
     * @param mixed $data Remote Claim metadata
38
     * @param string|null $expectedUri if a Discovery call is expected, this parameter MUST be the Claim's URI
39
     * @param array|null $responses
40
     */
41
    public function __construct($data = null, $expectedUri = null, array $responses = null)
42
    {
43
        if (null === $responses) {
44
            $this->setDefaultResponse(json_encode($data));
45
        } else {
46
            $this->responses = $responses;
47
        }
48
49
        if ($expectedUri !== null) {
50
            $discoveryResponse = json_encode([
51
                'subject' => $data['claim_name'],
52
                'links' => [
53
                    ['rel' => 'http://openid.net/specs/connect/1.0/claim', 'href' => $expectedUri],
54
                ],
55
            ]);
56
            array_unshift($this->responses,
57
                new Response(200, ['Content-Length' => strlen($discoveryResponse)], $discoveryResponse));
58
        }
59
60
        $this->requests = [];
61
        $stack = HandlerStack::create(new MockHandler($this->responses));
62
        $stack->push(Middleware::history($this->requests));
63
64
        $this->data = $data !== null ? $data : RemoteClaimParserTest::$claimMetadata;
65
66
        $this->client = new Client(['handler' => $stack]);
67
    }
68
69
    private function setDefaultResponse(string $metadata)
70
    {
71
        $this->responses = [
72
            new Response(200, ['Content-Length' => strlen($metadata)], $metadata),
73
        ];
74
    }
75
76
    /**
77
     * @return Client
78
     */
79
    public function getClient()
80
    {
81
        return $this->client;
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getTransactions(): array
88
    {
89
        return $this->requests;
90
    }
91
92
    /**
93
     * @return Request[]
94
     */
95
    public function getRequests(): array
96
    {
97
        return array_column($this->getTransactions(), 'request');
98
    }
99
100
    /**
101
     * @return Response[]
102
     */
103
    public function getResponses(): array
104
    {
105
        return array_column($this->getTransactions(), 'response');
106
    }
107
}
108