Passed
Push — main ( 087f6d...b5e930 )
by Chema
53s queued 13s
created

LightningFeature::test_get_get_callback_url()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 15
rs 9.9
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\Lightning;
16
use PhpLightningTest\Feature\Fake\FakeHttpApi;
17
use PHPUnit\Framework\TestCase;
18
19
final class LightningFeature extends TestCase
20
{
21
    public function test_get_get_callback_url(): void
22
    {
23
        $this->bootstrapGacela();
24
        $this->mockLnPaymentRequest();
25
26
        $json = Lightning::getCallbackUrl();
27
28
        self::assertEquals([
29
            'callback' => 'https://domain.com/receiver',
30
            'maxSendable' => 10_000,
0 ignored issues
show
Bug introduced by
The constant PhpLightningTest\Feature\10_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
31
            'minSendable' => 1_000,
0 ignored issues
show
Bug introduced by
The constant PhpLightningTest\Feature\1_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
32
            'metadata' => '[["text/plain","Pay to [email protected]"],["text/identifier","[email protected]"]]',
33
            'tag' => 'payRequest',
34
            'commentAllowed' => false,
35
        ], json_decode($json, true));
36
    }
37
38
    public function test_ln_bits_feature(): void
39
    {
40
        $this->bootstrapGacela();
41
        $this->mockLnPaymentRequest();
42
43
        $json = Lightning::generateInvoice(amount: 2_000);
0 ignored issues
show
Bug introduced by
The constant PhpLightningTest\Feature\2_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
44
45
        self::assertEquals([
46
            'pr' => 'lnbc10u1pjzh489...fake payment_request',
47
            'status' => 'OK',
48
            'successAction' => [
49
                'tag' => 'message',
50
                'message' => 'Payment received!',
51
            ],
52
            'routes' => [],
53
            'disposable' => false,
54
        ], json_decode($json, true));
55
    }
56
57
    private function bootstrapGacela(): void
58
    {
59
        Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void {
60
            $config->resetInMemoryCache();
61
            $config->addAppConfigKeyValues(
62
                (new LightningConfig())
63
                    ->setDomain('domain.com')
64
                    ->setReceiver('receiver')
65
                    ->setSendableRange(1_000, 10_000)
0 ignored issues
show
Bug introduced by
The constant PhpLightningTest\Feature\1_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant PhpLightningTest\Feature\10_000 was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
66
                    ->addBackend(
67
                        (new LnBitsBackendConfig())
68
                            ->setApiEndpoint('http://localhost:5000')
69
                            ->setApiKey('XYZ'),
70
                    )->jsonSerialize(),
71
            );
72
        });
73
    }
74
75
    private function mockLnPaymentRequest(): void
76
    {
77
        AnonymousGlobal::overrideExistingResolvedClass(
78
            InvoiceDependencyProvider::class,
79
            new class() extends AbstractDependencyProvider {
80
                public function provideModuleDependencies(Container $container): void
81
                {
82
                    $container->set(InvoiceDependencyProvider::HTTP_API, static fn () => new FakeHttpApi());
83
                }
84
            },
85
        );
86
    }
87
}
88