GetsUnsignedRequestTest::testGetsUnsignedRequest()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 9.2
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bip70\Test\Client;
6
7
use Bip70\Client\GuzzleHttpClient;
8
use Bip70\Client\MIMEType;
9
use Bip70\Client\NetworkConfig\BitcoinNetworkConfig;
10
use Bip70\Client\PaymentRequestInfo;
11
use Bip70\X509\RequestValidation;
12
use Bip70\X509\TrustStoreLoader;
13
use GuzzleHttp\Handler\MockHandler;
14
use GuzzleHttp\HandlerStack;
15
use GuzzleHttp\Middleware;
16
use GuzzleHttp\Psr7\Response;
17
use PHPUnit\Framework\TestCase;
18
use Psr\Http\Message\RequestInterface;
19
use X509\CertificationPath\PathValidation\PathValidationConfig;
20
21
class GetsUnsignedRequestTest extends TestCase
22
{
23
    public function testGetsUnsignedRequest()
24
    {
25
        $container = [];
26
        $history = Middleware::history($container);
27
28
        $handler = new MockHandler([
29
            // response to our GET PAYMENTREQUEST request
30
            new Response(200, [
31
                'Content-Type' => [
32
                    'application/bitcoin-paymentrequest',
33
                ]
34
            ], '')
35
        ]);
36
37
        $stack = HandlerStack::create($handler);
38
        $stack->push($history);
39
40
        $mockClient = new \GuzzleHttp\Client([
41
            'handler' => $stack,
42
        ]);
43
44
        $client = new GuzzleHttpClient($mockClient);
45
46
        $requestUrl = "https://development.bip70.local/invoice/1";
47
48
        // 10/12/2017 ish
49
        $now = new \DateTimeImmutable();
50
        $now = $now->setTimestamp(1509692666);
51
52
        $networkConfig = new BitcoinNetworkConfig();
53
54
        $validationConfig = new PathValidationConfig($now, 10);
55
        $validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem());
56
        $data = $client->getRequest($requestUrl, $validator, $networkConfig);
57
58
        $this->assertInstanceOf(PaymentRequestInfo::class, $data);
59
        $this->assertCount(1, $container);
60
61
        /** @var RequestInterface $req1 */
62
        $req1 = $container[0]['request'];
63
        $this->assertEquals($requestUrl, $req1->getUri());
64
        $this->assertTrue($req1->hasHeader('Accept'));
65
        $this->assertEquals($networkConfig->getPaymentRequestMimeType(), $req1->getHeader('Accept')[0]);
66
        $this->assertNull($data->pathValidation());
67
    }
68
}
69