Test Failed
Push — main ( 117601...63d9b1 )
by Chema
11:24
created

InvoiceFacadeTest::test_ln_bits_feature()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 13
nc 1
nop 0
dl 0
loc 18
c 0
b 0
f 0
cc 1
rs 9.8333
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\LightningConfig;
13
use PhpLightning\Invoice\InvoiceDependencyProvider;
14
use PhpLightning\Invoice\InvoiceFacade;
15
use PhpLightningTest\Feature\Fake\FakeHttpApi;
16
use PHPUnit\Framework\TestCase;
17
18
final class InvoiceFacadeTest extends TestCase
19
{
20
    private InvoiceFacade $facade;
21
22
    protected function setUp(): void
23
    {
24
        $this->facade = new InvoiceFacade();
25
    }
26
27
    public function test_get_get_callback_url(): void
28
    {
29
        $this->bootstrapGacela();
30
        $this->mockLnPaymentRequest();
31
32
        $json = $this->facade->getCallbackUrl('bob');
33
34
        self::assertEquals([
35
            'callback' => 'https://callback.url/receiver',
36
            'maxSendable' => 10_000,
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting ',' or ']' on line 36 at column 31
Loading history...
37
            'minSendable' => 1_000,
38
            'metadata' => '[["text/plain","Pay to [email protected]"],["text/identifier","[email protected]"]]',
39
            'tag' => 'payRequest',
40
            'commentAllowed' => false,
41
        ], $json);
42
    }
43
44
    public function test_ln_bits_feature(): void
45
    {
46
        $this->bootstrapGacela();
47
        $this->mockLnPaymentRequest();
48
49
        $json = $this->facade->generateInvoice('alice', 2_000, 'lnbits');
50
51
        self::assertEquals([
52
            'pr' => 'lnbc10u1pjzh489...fake payment_request',
53
            'status' => 'OK',
54
            'successAction' => [
55
                'tag' => 'message',
56
                'message' => 'Payment received!',
57
            ],
58
            'routes' => [],
59
            'disposable' => false,
60
            'reason' => '',
61
        ], $json);
62
    }
63
64
    private function bootstrapGacela(): void
65
    {
66
        Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void {
67
            $config->resetInMemoryCache();
68
            $config->addAppConfigKeyValues(
69
                (new LightningConfig())
70
                    ->setCallbackUrl('https://callback.url/receiver')
71
                    ->setDomain('domain.com')
72
                    ->setReceiver('receiver')
73
                    ->setSendableRange(1_000, 10_000)
74
                    ->addBackendsFile(__DIR__ . DIRECTORY_SEPARATOR . 'nostr.json')
75
                    ->jsonSerialize(),
76
            );
77
        });
78
    }
79
80
    private function mockLnPaymentRequest(): void
81
    {
82
        AnonymousGlobal::overrideExistingResolvedClass(
83
            InvoiceDependencyProvider::class,
84
            new class() extends AbstractDependencyProvider {
85
                public function provideModuleDependencies(Container $container): void
86
                {
87
                    $container->set(InvoiceDependencyProvider::HTTP_API, static fn () => new FakeHttpApi());
88
                }
89
            },
90
        );
91
    }
92
}
93