Skip to content

Commit

Permalink
Miscellaneous Client Test Suite (#88)
Browse files Browse the repository at this point in the history
* Miscellaneous Client Test Suite
* InvoiceCheckoutHtml Result Class + Test
  • Loading branch information
utxo-one authored Nov 8, 2022
1 parent 15b8ff6 commit 52e96a0
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 19 deletions.
14 changes: 6 additions & 8 deletions src/Client/Miscellaneous.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

namespace BTCPayServer\Client;

use BTCPayServer\Result\InvoiceCheckoutHTML;
use BTCPayServer\Result\InvoiceCheckoutHtml;
use BTCPayServer\Result\LanguageCodeList;
use BTCPayServer\Result\PermissionMetadata;
use BTCPayServer\Result\PermissionMetadataList;

class Miscellaneous extends AbstractClient
{
public function getPermissionMetadata(): PermissionMetadata
public function getPermissionMetadata(): PermissionMetadataList
{
$url = $this->getBaseUrl() . '/misc/permissions';
$headers = $this->getRequestHeaders();
Expand All @@ -19,7 +19,7 @@ public function getPermissionMetadata(): PermissionMetadata
$response = $this->getHttpClient()->request($method, $url, $headers);

if ($response->getStatus() === 200) {
return new PermissionMetadata(
return new PermissionMetadataList(
json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR)
);
} else {
Expand Down Expand Up @@ -47,7 +47,7 @@ public function getLanguageCodes(): LanguageCodeList
public function getInvoiceCheckout(
string $invoiceId,
?string $lang
): InvoiceCheckoutHTML {
): InvoiceCheckoutHtml {
$url = $this->getBaseUrl() . '/i/' . urlencode($invoiceId);

//set language query parameter if passed
Expand All @@ -61,9 +61,7 @@ public function getInvoiceCheckout(
$response = $this->getHttpClient()->request($method, $url, $headers);

if ($response->getStatus() === 200) {
return new InvoiceCheckoutHTML(
json_decode($response->getBody(), true, 512, JSON_THROW_ON_ERROR)
);
return new InvoiceCheckoutHtml($response->getBody());
} else {
throw $this->getExceptionByStatusCode($method, $url, $response);
}
Expand Down
9 changes: 0 additions & 9 deletions src/Result/InvoiceCheckoutHTML.php

This file was deleted.

28 changes: 28 additions & 0 deletions src/Result/InvoiceCheckoutHtml.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace BTCPayServer\Result;

class InvoiceCheckoutHtml
{
/**
* @var string
*/
private $html;

public function __construct(string $html)
{
$this->html = $html;
}

public function getHtml(): string
{
return $this->html;
}

public function __toString()
{
return $this->html;
}
}
4 changes: 2 additions & 2 deletions src/Result/LanguageCodeList.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
class LanguageCodeList extends AbstractListResult
{
/**
* @return \BTCPayServer\Result\LanguageCode[]
* @return LanguageCode[]
*/
public function all(): array
{
$languageCodes = [];
foreach ($this->getData() as $languageCode) {
$languageCodes[] = new \BTCPayServer\Result\LanguageCode($languageCode);
$languageCodes[] = new LanguageCode($languageCode);
}
return $languageCodes;
}
Expand Down
20 changes: 20 additions & 0 deletions src/Result/PermissionMetadataList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace BTCPayServer\Result;

class PermissionMetadataList extends AbstractListResult
{
/**
* @return PermissionMetadata[]
*/
public function all(): array
{
$permissionMetadataList = [];
foreach ($this->getData() as $permissionMetadata) {
$permissionMetadataList[] = new PermissionMetadata($permissionMetadata);
}
return $permissionMetadataList;
}
}
6 changes: 6 additions & 0 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ public function testItSetsAllTheEnvironmentVariables(): void
$this->assertNotEmpty($this->storeId);
$this->assertNotEmpty($this->nodeUri);
}

public function dd($var): void
{
var_dump($var);
die();
}
}
68 changes: 68 additions & 0 deletions tests/MiscellaneousTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace BTCPayServer\Tests;

use BTCPayServer\Client\Invoice;
use BTCPayServer\Client\Miscellaneous;
use BTCPayServer\Result\InvoiceCheckoutHtml;
use BTCPayServer\Result\LanguageCode;
use BTCPayServer\Result\LanguageCodeList;
use BTCPayServer\Result\PermissionMetadata;
use BTCPayServer\Result\PermissionMetadataList;
use BTCPayServer\Util\PreciseNumber;

final class MiscellaneousTest extends BaseTest
{
private Miscellaneous $miscellaneousClient;

public function setUp(): void
{
parent::setUp();

$this->miscellaneousClient = new Miscellaneous($this->host, $this->apiKey);
}

public function testItCanGetPermissionMetadata(): void
{
$result = $this->miscellaneousClient->getPermissionMetadata();

$this->assertInstanceOf(PermissionMetadataList::class, $result);

foreach ($result->all() as $permissionMetadata) {
$this->assertInstanceOf(PermissionMetadata::class, $permissionMetadata);
$this->assertIsString($permissionMetadata->getName());
$this->assertIsArray($permissionMetadata->getIncluded());
}
}

public function testItCanGetLanguageCodes(): void
{
$result = $this->miscellaneousClient->getLanguageCodes();

$this->assertInstanceOf(LanguageCodeList::class, $result);

foreach ($result->all() as $languageCode) {
$this->assertInstanceOf(LanguageCode::class, $languageCode);
$this->assertIsString($languageCode->getCode());
$this->assertIsString($languageCode->getCurrentLanguage());
}
}

public function testItCanGetInvoiceCheckout(): void
{
$invoiceClient = new Invoice($this->host, $this->apiKey);

$invoice = $invoiceClient->createInvoice(
storeId: $this->storeId,
currency: 'SATS',
amount: PreciseNumber::parseString('1000'),
);

$result = $this->miscellaneousClient->getInvoiceCheckout($invoice->getId(), null);

$this->assertInstanceOf(InvoiceCheckoutHtml::class, $result);
$this->assertStringContainsString('<!DOCTYPE html>', $result->getHtml());
}
}

0 comments on commit 52e96a0

Please sign in to comment.