Completed
Pull Request — master (#10)
by thomas
06:48
created

GetsSignedRequestTest::testGetsSignedRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 61
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 61
rs 9.5147
c 1
b 0
f 0
cc 1
eloc 39
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bip70\Test\Client\GuzzleClient;
6
7
use Bip70\Client\GuzzleHttpClient;
8
use Bip70\Client\MIMEType;
9
use Bip70\Client\PaymentRequestInfo;
10
use Bip70\Protobuf\Proto\PaymentDetails;
11
use Bip70\X509\PKIType;
12
use Bip70\X509\RequestSigner;
13
use Bip70\X509\RequestValidation;
14
use Bip70\X509\TrustStoreLoader;
15
use GuzzleHttp\Handler\MockHandler;
16
use GuzzleHttp\HandlerStack;
17
use GuzzleHttp\Middleware;
18
use GuzzleHttp\Psr7\Response;
19
use PHPUnit\Framework\TestCase;
20
use Psr\Http\Message\RequestInterface;
21
use Psr\Http\Message\ResponseInterface;
22
use Sop\CryptoEncoding\PEM;
23
use Sop\CryptoEncoding\PEMBundle;
24
use Sop\CryptoTypes\Asymmetric\PrivateKeyInfo;
25
use X509\Certificate\CertificateBundle;
26
use X509\CertificationPath\PathValidation\PathValidationConfig;
27
use X509\CertificationPath\PathValidation\PathValidationResult;
28
29
class GetsSignedRequestTest extends TestCase
30
{
31
    public function testGetsSignedRequest()
32
    {
33
        $keySigned = PrivateKeyInfo::fromPEM(PEM::fromFile(__DIR__ . "/../../../data/testnet-only-cert-not-valid.key"));
34
        $certs = CertificateBundle::fromPEMBundle(PEMBundle::fromFile(__DIR__ . "/../../../data/testnet-only-cert-not-valid.cabundle.pem"));
35
        $certSigned = $certs->all()[0];
36
        $certBundle = new CertificateBundle(...array_slice($certs->all(), 1));
37
38
        $now = 1509692666;
39
        $details = new PaymentDetails();
40
        $details->setTime($now);
41
42
        $requestSigner = new RequestSigner();
43
        $request = $requestSigner->sign($details, PKIType::X509_SHA256, $keySigned, $certSigned, $certBundle);
44
        $serializedRequest = $request->serialize();
45
46
        $container = [];
47
        $history = Middleware::history($container);
48
49
        $handler = new MockHandler([
50
            // response to our GET PAYMENTREQUEST request
51
            new Response(200, [
52
                'Content-Type' => [
53
                    'application/bitcoin-paymentrequest',
54
                ]
55
            ], $serializedRequest)
56
        ]);
57
58
        $stack = HandlerStack::create($handler);
59
        $stack->push($history);
60
61
        $mockClient = new \GuzzleHttp\Client([
62
            'handler' => $stack,
63
        ]);
64
65
        $client = new GuzzleHttpClient($mockClient);
66
67
        $requestUrl = "https://development.bip70.local/invoice/1";
68
69
        // 10/12/2017 ish
70
        $dtnow = new \DateTimeImmutable();
71
        $dtnow = $dtnow->setTimestamp($now);
72
73
        $validationConfig = new PathValidationConfig($dtnow, 10);
0 ignored issues
show
Security Bug introduced by
It seems like $dtnow defined by $dtnow->setTimestamp($now) on line 71 can also be of type false; however, X509\CertificationPath\P...onConfig::__construct() does only seem to accept object<DateTimeImmutable>, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
74
        $validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem());
75
        $data = $client->getRequest($requestUrl, $validator);
76
77
        $this->assertInstanceOf(PaymentRequestInfo::class, $data);
78
        $this->assertCount(1, $container);
79
        $this->assertInstanceOf(PathValidationResult::class, $data->pathValidation());
80
81
        /** @var RequestInterface $req1 */
82
        $req1 = $container[0]['request'];
83
        $this->assertEquals($requestUrl, $req1->getUri());
84
        $this->assertTrue($req1->hasHeader('Accept'));
85
        $this->assertEquals(MIMEType::PAYMENT_REQUEST, $req1->getHeader('Accept')[0]);
86
87
        /** @var ResponseInterface $res1 */
88
        $res1 = $container[0]['response'];
89
        $res1->getBody()->rewind();
90
        $this->assertEquals($serializedRequest, $res1->getBody()->getContents());
91
    }
92
}
93