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

GetsUnsignedRequestTest::testGetsUnsignedRequest()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 43
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 26
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\PaymentRequestInfo;
10
use Bip70\X509\RequestValidation;
11
use Bip70\X509\TrustStoreLoader;
12
use GuzzleHttp\Handler\MockHandler;
13
use GuzzleHttp\HandlerStack;
14
use GuzzleHttp\Middleware;
15
use GuzzleHttp\Psr7\Response;
16
use PHPUnit\Framework\TestCase;
17
use Psr\Http\Message\RequestInterface;
18
use X509\CertificationPath\PathValidation\PathValidationConfig;
19
20
class GetsUnsignedRequestTest extends TestCase
21
{
22
    public function testGetsUnsignedRequest()
23
    {
24
        $container = [];
25
        $history = Middleware::history($container);
26
27
        $handler = new MockHandler([
28
            // response to our GET PAYMENTREQUEST request
29
            new Response(200, [
30
                'Content-Type' => [
31
                    'application/bitcoin-paymentrequest',
32
                ]
33
            ], '')
34
        ]);
35
36
        $stack = HandlerStack::create($handler);
37
        $stack->push($history);
38
39
        $mockClient = new \GuzzleHttp\Client([
40
            'handler' => $stack,
41
        ]);
42
43
        $client = new GuzzleHttpClient($mockClient);
44
45
        $requestUrl = "https://development.bip70.local/invoice/1";
46
47
        // 10/12/2017 ish
48
        $now = new \DateTimeImmutable();
49
        $now = $now->setTimestamp(1509692666);
50
51
        $validationConfig = new PathValidationConfig($now, 10);
0 ignored issues
show
Security Bug introduced by
It seems like $now defined by $now->setTimestamp(1509692666) on line 49 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...
52
        $validator = new RequestValidation($validationConfig, TrustStoreLoader::fromSystem());
53
        $data = $client->getRequest($requestUrl, $validator);
54
55
        $this->assertInstanceOf(PaymentRequestInfo::class, $data);
56
        $this->assertCount(1, $container);
57
58
        /** @var RequestInterface $req1 */
59
        $req1 = $container[0]['request'];
60
        $this->assertEquals($requestUrl, $req1->getUri());
61
        $this->assertTrue($req1->hasHeader('Accept'));
62
        $this->assertEquals(MIMEType::PAYMENT_REQUEST, $req1->getHeader('Accept')[0]);
63
        $this->assertNull($data->pathValidation());
64
    }
65
66
}
67