Passed
Branch develop (84afed)
by Paulius
02:35
created

TestCase::createSigning()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 31
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 0
1
<?php
2
namespace Dokobit\Gateway\Tests\Integration;
3
4
use Dokobit\Gateway\Client;
5
use Dokobit\Gateway\Query\File\Upload;
6
use Dokobit\Gateway\Query\Signing\Create;
7
use Dokobit\Gateway\Result\File\UploadResult;
8
use Dokobit\Gateway\Result\Signing\CreateResult;
9
10
/**
11
 * Base test case
12
 */
13
use PHPUnit\Framework\TestCase as BaseTestCase;
14
15
class TestCase extends BaseTestCase
16
{
17
    const SIGNER1_ID = 'Signer1';
18
    const SIGNER2_ID = 'Signer2';
19
20
    /** @var Client */
21
    protected $client;
22
23
    /** @var string */
24
    protected $fileToken;
25
26
    /** @var string */
27
    protected $signingToken;
28
29
    /** @var string */
30
    protected $signerToken;
31
32
    protected function setUp(): void
33
    {
34
        $params = [
35
            'apiKey' => SANDBOX_API_KEY,
0 ignored issues
show
Bug introduced by
The constant Dokobit\Gateway\Tests\Integration\SANDBOX_API_KEY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
36
            'sandbox' => true,
37
        ];
38
39
        if (defined('SANDBOX_URL')) {
40
            $params['sandboxUrl'] = SANDBOX_URL;
0 ignored issues
show
Bug introduced by
The constant Dokobit\Gateway\Tests\Integration\SANDBOX_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
41
        }
42
43
        $log = null;
44
        // Uncomment to enable request/response debugging
45
        // $log = new \Monolog\Logger('test');
46
        // $log->pushHandler(new \Monolog\Handler\StreamHandler('php://stdout'));
47
48
        $this->client = Client::create($params, $log);
49
    }
50
51
    /**
52
     * Upload an unsigned document file and set $this->fileToken to its token
53
     */
54
    protected function uploadFile()
55
    {
56
        /** @var UploadResult $result */
57
        $result = $this->client->get(new Upload(
58
            __DIR__ . '/../data/document.pdf'
59
        ));
60
61
        if ('ok' !== $result->getStatus() || empty($result->getToken())) {
62
            throw new \RuntimeException('Failed to upload file.');
63
        }
64
65
        $this->fileToken = $result->getToken();
66
    }
67
68
    /**
69
     * Create a signing and set $this->signingToken to its token
70
     */
71
    protected function createSigning()
72
    {
73
        if (empty($this->fileToken)) {
74
            $this->uploadFile();
75
        }
76
77
        /** @var CreateResult $result */
78
        $result = $this->client->get(new Create(
79
            'pdf',
80
            'Test signing',
81
            [
82
                [
83
                    'token' => $this->fileToken,
84
                ],
85
            ],
86
            [
87
                [
88
                    'id' => self::SIGNER1_ID,
89
                    'name' => 'Kraft',
90
                    'surname' => 'Lawrence',
91
                    'signing_purpose' => 'signature',
92
                ],
93
            ]
94
        ));
95
96
        if ('ok' !== $result->getStatus() || empty($result->getToken())) {
97
            throw new \RuntimeException('Failed to create signing.');
98
        }
99
100
        $this->signingToken = $result->getToken();
101
        $this->signerToken = $result->getSigners()[self::SIGNER1_ID];
102
    }
103
104
    protected function sign($dtbs, $key)
105
    {
106
        openssl_sign(base64_decode($dtbs), $signatureValue, $key, OPENSSL_ALGO_SHA256);
107
108
        return base64_encode($signatureValue);
109
    }
110
}
111