1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PhpLightningTest\Feature; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\AbstractDependencyProvider; |
8
|
|
|
use Gacela\Framework\Bootstrap\GacelaConfig; |
9
|
|
|
use Gacela\Framework\ClassResolver\GlobalInstance\AnonymousGlobal; |
10
|
|
|
use Gacela\Framework\Container\Container; |
11
|
|
|
use Gacela\Framework\Gacela; |
12
|
|
|
use PhpLightning\Config\Backend\LnBitsBackendConfig; |
13
|
|
|
use PhpLightning\Config\LightningConfig; |
14
|
|
|
use PhpLightning\Invoice\InvoiceDependencyProvider; |
15
|
|
|
use PhpLightning\Invoice\InvoiceFacade; |
16
|
|
|
use PhpLightningTest\Feature\Fake\FakeHttpApi; |
17
|
|
|
use PHPUnit\Framework\TestCase; |
18
|
|
|
|
19
|
|
|
final class InvoiceFacadeTest extends TestCase |
20
|
|
|
{ |
21
|
|
|
private InvoiceFacade $facade; |
22
|
|
|
|
23
|
|
|
protected function setUp(): void |
24
|
|
|
{ |
25
|
|
|
$this->facade = new InvoiceFacade(); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function test_get_get_callback_url(): void |
29
|
|
|
{ |
30
|
|
|
$this->bootstrapGacela(); |
31
|
|
|
$this->mockLnPaymentRequest(); |
32
|
|
|
|
33
|
|
|
$json = $this->facade->getCallbackUrl('username'); |
34
|
|
|
|
35
|
|
|
self::assertEquals([ |
36
|
|
|
'callback' => 'https://callback.url/receiver', |
37
|
|
|
'maxSendable' => 10_000, |
|
|
|
|
38
|
|
|
'minSendable' => 1_000, |
|
|
|
|
39
|
|
|
'metadata' => '[["text/plain","Pay to [email protected]"],["text/identifier","[email protected]"]]', |
40
|
|
|
'tag' => 'payRequest', |
41
|
|
|
'commentAllowed' => false, |
42
|
|
|
], $json); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function test_ln_bits_feature(): void |
46
|
|
|
{ |
47
|
|
|
$this->bootstrapGacela(); |
48
|
|
|
$this->mockLnPaymentRequest(); |
49
|
|
|
|
50
|
|
|
$json = $this->facade->generateInvoice('username', 2_000, 'lnbits'); |
|
|
|
|
51
|
|
|
|
52
|
|
|
self::assertEquals([ |
53
|
|
|
'pr' => 'lnbc10u1pjzh489...fake payment_request', |
54
|
|
|
'status' => 'OK', |
55
|
|
|
'successAction' => [ |
56
|
|
|
'tag' => 'message', |
57
|
|
|
'message' => 'Payment received!', |
58
|
|
|
], |
59
|
|
|
'routes' => [], |
60
|
|
|
'disposable' => false, |
61
|
|
|
'reason' => '', |
62
|
|
|
], $json); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
private function bootstrapGacela(): void |
66
|
|
|
{ |
67
|
|
|
Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void { |
68
|
|
|
$config->resetInMemoryCache(); |
69
|
|
|
$config->addAppConfigKeyValues( |
70
|
|
|
(new LightningConfig()) |
|
|
|
|
71
|
|
|
->setCallbackUrl('https://callback.url/receiver') |
72
|
|
|
->setDomain('domain.com') |
73
|
|
|
->setReceiver('receiver') |
74
|
|
|
->setSendableRange(1_000, 10_000) |
|
|
|
|
75
|
|
|
->addBackend( |
76
|
|
|
(new LnBitsBackendConfig()) |
|
|
|
|
77
|
|
|
->setApiEndpoint('http://localhost:5000') |
78
|
|
|
->setApiKey('XYZ'), |
79
|
|
|
)->jsonSerialize(), |
80
|
|
|
); |
81
|
|
|
}); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function mockLnPaymentRequest(): void |
85
|
|
|
{ |
86
|
|
|
AnonymousGlobal::overrideExistingResolvedClass( |
87
|
|
|
InvoiceDependencyProvider::class, |
88
|
|
|
new class() extends AbstractDependencyProvider { |
89
|
|
|
public function provideModuleDependencies(Container $container): void |
90
|
|
|
{ |
91
|
|
|
$container->set(InvoiceDependencyProvider::HTTP_API, static fn () => new FakeHttpApi()); |
92
|
|
|
} |
93
|
|
|
}, |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|