Passed
Pull Request — main (#11)
by Jesús
02:37
created

LightningFeature::bootstrapGacela()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 0
loc 12
rs 9.9332
c 1
b 0
f 0
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\Http\Domain\HttpClientInterface;
15
use PhpLightning\Http\HttpDependencyProvider;
16
use PhpLightning\Lightning;
17
use PHPUnit\Framework\TestCase;
18
19
final class LightningFeature extends TestCase
20
{
21
    public function test_ln_bits_feature(): void
22
    {
23
        $this->bootstrapGacela();
24
        $this->mockLnPaymentRequest();
25
26
        $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...
27
28
        self::assertEquals([
29
            'pr' => 'lnbc10u1pjzh489...CUSTOM PAYMENT REQUEST',
30
            'status' => 'OK',
31
            'successAction' => [
32
                'tag' => 'message',
33
                'message' => 'Payment received!',
34
            ],
35
            'routes' => [],
36
            'disposable' => false,
37
        ], json_decode($json, true));
38
    }
39
40
    private function bootstrapGacela(): void
41
    {
42
        Gacela::bootstrap(__DIR__, static function (GacelaConfig $config): void {
43
            $config->resetInMemoryCache();
44
            $config->addAppConfigKeyValues(
45
                (new LightningConfig())
46
                    ->setSendableRange(1_000, 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...
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...
47
                    ->addBackend(
48
                        (new LnBitsBackendConfig())
49
                            ->setApiEndpoint('http://localhost:5000')
50
                            ->setApiKey('XYZ'),
51
                    )->jsonSerialize(),
52
            );
53
        });
54
    }
55
56
    private function mockLnPaymentRequest(): void
57
    {
58
        AnonymousGlobal::overrideExistingResolvedClass(
59
            HttpDependencyProvider::class,
60
            new class() extends AbstractDependencyProvider {
61
                public function provideModuleDependencies(Container $container): void
62
                {
63
                    $container->set(
64
                        HttpDependencyProvider::HTTP_CLIENT,
65
                        static fn () => new class() implements HttpClientInterface {
66
                            public function post(string $url, array $options = []): string
67
                            {
68
                                return json_encode([
69
                                    'payment_request' => 'lnbc10u1pjzh489...CUSTOM PAYMENT REQUEST',
70
                                ], JSON_THROW_ON_ERROR);
71
                            }
72
                        },
73
                    );
74
                }
75
            },
76
        );
77
    }
78
}
79