|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace PhpLightningTest\Unit\Invoice\Domain\LnAddress; |
|
6
|
|
|
|
|
7
|
|
|
use PhpLightning\Http\HttpFacadeInterface; |
|
8
|
|
|
use PhpLightning\Invoice\Domain\BackendInvoice\BackendInvoiceInterface; |
|
9
|
|
|
use PhpLightning\Invoice\Domain\LnAddress\InvoiceGenerator; |
|
10
|
|
|
use PHPUnit\Framework\TestCase; |
|
11
|
|
|
|
|
12
|
|
|
final class InvoiceGeneratorTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
private const BACKEND = 'lnbits'; |
|
15
|
|
|
|
|
16
|
|
|
public function test_unknown_backend(): void |
|
17
|
|
|
{ |
|
18
|
|
|
$invoiceFacade = $this->createStub(BackendInvoiceInterface::class); |
|
19
|
|
|
$invoiceFacade->method('requestInvoice')->willReturn(['status' => 'ERROR', 'reason' => 'some reason']); |
|
20
|
|
|
|
|
21
|
|
|
$httpFacade = $this->createStub(HttpFacadeInterface::class); |
|
22
|
|
|
$httpFacade->method('get')->willReturn(null); |
|
23
|
|
|
|
|
24
|
|
|
$invoice = new InvoiceGenerator($invoiceFacade, $httpFacade, 'ln@address'); |
|
25
|
|
|
$actual = $invoice->generateInvoice(123456, 'unknown?'); |
|
26
|
|
|
|
|
27
|
|
|
self::assertSame([ |
|
28
|
|
|
'status' => 'ERROR', |
|
29
|
|
|
'reason' => 'some reason', |
|
30
|
|
|
], $actual); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function test_successful_payment_request_with_amount(): void |
|
34
|
|
|
{ |
|
35
|
|
|
$invoiceFacade = $this->createStub(BackendInvoiceInterface::class); |
|
36
|
|
|
$invoiceFacade->method('requestInvoice')->willReturn([ |
|
37
|
|
|
'status' => 'OK', |
|
38
|
|
|
'pr' => 'any payment_request', |
|
39
|
|
|
]); |
|
40
|
|
|
|
|
41
|
|
|
$httpFacade = $this->createStub(HttpFacadeInterface::class); |
|
42
|
|
|
$httpFacade->method('get')->willReturn( |
|
43
|
|
|
json_encode([ |
|
44
|
|
|
'payment_request' => 'any payment_request', |
|
45
|
|
|
], JSON_THROW_ON_ERROR), |
|
46
|
|
|
); |
|
47
|
|
|
|
|
48
|
|
|
$invoice = new InvoiceGenerator($invoiceFacade, $httpFacade, 'ln@address'); |
|
49
|
|
|
$actual = $invoice->generateInvoice(123456, self::BACKEND); |
|
50
|
|
|
|
|
51
|
|
|
self::assertSame([ |
|
52
|
|
|
'pr' => 'any payment_request', |
|
53
|
|
|
'status' => 'OK', |
|
54
|
|
|
'successAction' => [ |
|
55
|
|
|
'tag' => 'message', |
|
56
|
|
|
'message' => 'Payment received!', |
|
57
|
|
|
], |
|
58
|
|
|
'routes' => [], |
|
59
|
|
|
'disposable' => false, |
|
60
|
|
|
], $actual); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function test_invalid_amount(): void |
|
64
|
|
|
{ |
|
65
|
|
|
$invoiceFacade = $this->createStub(BackendInvoiceInterface::class); |
|
66
|
|
|
$invoiceFacade->method('requestInvoice')->willReturn([]); |
|
67
|
|
|
|
|
68
|
|
|
$httpFacade = $this->createStub(HttpFacadeInterface::class); |
|
69
|
|
|
$httpFacade->method('get')->willReturn(null); |
|
70
|
|
|
|
|
71
|
|
|
$invoice = new InvoiceGenerator($invoiceFacade, $httpFacade, 'ln@address'); |
|
72
|
|
|
$actual = $invoice->generateInvoice(100, self::BACKEND); |
|
73
|
|
|
|
|
74
|
|
|
self::assertSame([ |
|
75
|
|
|
'status' => 'ERROR', |
|
76
|
|
|
'reason' => 'Amount is not between minimum and maximum sendable amount', |
|
77
|
|
|
], $actual); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|