diff --git a/.github/ISSUE_TEMPLATE/apiary.yml b/.github/ISSUE_TEMPLATE/doc.yml similarity index 94% rename from .github/ISSUE_TEMPLATE/apiary.yml rename to .github/ISSUE_TEMPLATE/doc.yml index fcf5f7c..34b70c2 100644 --- a/.github/ISSUE_TEMPLATE/apiary.yml +++ b/.github/ISSUE_TEMPLATE/doc.yml @@ -1,6 +1,6 @@ -name: Apiary +name: WS API documentation description: Issue about documentation for ThePay.cz WS API -labels: ["Apiary"] +labels: ["OpenAPI"] body: - type: markdown attributes: diff --git a/.github/ISSUE_TEMPLATE/sdk.yml b/.github/ISSUE_TEMPLATE/sdk.yml index cf7bce6..4154518 100644 --- a/.github/ISSUE_TEMPLATE/sdk.yml +++ b/.github/ISSUE_TEMPLATE/sdk.yml @@ -20,8 +20,7 @@ body: options: - Production - Demo - - Apiary mock - - Apiary proxy + - OpenAPI mock validations: required: false - type: input diff --git a/.github/ISSUE_TEMPLATE/ws.yml b/.github/ISSUE_TEMPLATE/ws.yml index 7f7287b..6ed156a 100644 --- a/.github/ISSUE_TEMPLATE/ws.yml +++ b/.github/ISSUE_TEMPLATE/ws.yml @@ -20,8 +20,7 @@ body: options: - Production - Demo - - Apiary mock - - Apiary proxy + - OpenAPI mock validations: required: true - type: input diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a6d3c1f..8ab8d12 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,11 @@ jobs: name: "php 8.4" runs-on: ubuntu-latest container: "nofutur3/php-tests:8.4" + services: + openAPImock: + image: mockserver/mockserver + ports: + - 1080:1080 steps: - name: Checkout repository uses: actions/checkout@v4 @@ -20,6 +25,9 @@ jobs: - name: Run static analysis run: composer stan + - name: Create mock server from our production OpenAPI + run: curl -X PUT -d "{\"specUrlOrPayload\":\"https://gate.thepay.cz/openapi.yaml\"}" http://openAPImock:1080/mockserver/openapi + - name: Run tests run: composer test @@ -27,6 +35,11 @@ jobs: name: "php 8.3" runs-on: ubuntu-latest container: "nofutur3/php-tests:8.3" + services: + openAPImock: + image: mockserver/mockserver + ports: + - 1080:1080 steps: - name: Checkout repository uses: actions/checkout@v4 @@ -37,6 +50,9 @@ jobs: - name: Run static analysis run: composer stan + - name: Create mock server from our production OpenAPI + run: curl -X PUT -d "{\"specUrlOrPayload\":\"https://gate.thepay.cz/openapi.yaml\"}" http://openAPImock:1080/mockserver/openapi + - name: Run tests run: composer test @@ -44,6 +60,11 @@ jobs: name: "php 8.2 psr/http-message 2.0" runs-on: ubuntu-latest container: "nofutur3/php-tests:8.2" + services: + openAPImock: + image: mockserver/mockserver + ports: + - 1080:1080 steps: - name: Checkout repository uses: actions/checkout@v3 @@ -57,6 +78,9 @@ jobs: - name: Run static analysis run: composer stan + - name: Create mock server from our production OpenAPI + run: curl -X PUT -d "{\"specUrlOrPayload\":\"https://gate.thepay.cz/openapi.yaml\"}" http://openAPImock:1080/mockserver/openapi + - name: Run tests run: composer test @@ -64,6 +88,11 @@ jobs: name: "php 8.2 psr/http-message 1.0" runs-on: ubuntu-latest container: "nofutur3/php-tests:8.2" + services: + openAPImock: + image: mockserver/mockserver + ports: + - 1080:1080 steps: - name: Checkout repository uses: actions/checkout@v3 @@ -74,6 +103,9 @@ jobs: - name: Run static analysis run: composer stan + - name: Create mock server from our production OpenAPI + run: curl -X PUT -d "{\"specUrlOrPayload\":\"https://gate.thepay.cz/openapi.yaml\"}" http://openAPImock:1080/mockserver/openapi + - name: Run tests run: composer test @@ -81,6 +113,11 @@ jobs: name: "php 8.1" runs-on: ubuntu-latest container: "nofutur3/php-tests:8.1" + services: + openAPImock: + image: mockserver/mockserver + ports: + - 1080:1080 steps: - name: Checkout repository uses: actions/checkout@v3 @@ -91,6 +128,9 @@ jobs: - name: Run static analysis run: composer stan + - name: Create mock server from our production OpenAPI + run: curl -X PUT -d "{\"specUrlOrPayload\":\"https://gate.thepay.cz/openapi.yaml\"}" http://openAPImock:1080/mockserver/openapi + - name: Run tests run: composer test @@ -98,6 +138,11 @@ jobs: name: "php 8.0" runs-on: ubuntu-latest container: "nofutur3/php-tests:8.0" + services: + openAPImock: + image: mockserver/mockserver + ports: + - 1080:1080 steps: - name: Checkout repository uses: actions/checkout@v3 @@ -108,6 +153,9 @@ jobs: - name: Run static analysis run: composer stan + - name: Create mock server from our production OpenAPI + run: curl -X PUT -d "{\"specUrlOrPayload\":\"https://gate.thepay.cz/openapi.yaml\"}" http://openAPImock:1080/mockserver/openapi + - name: Run tests run: composer test @@ -115,6 +163,11 @@ jobs: name: "php 7.4" runs-on: ubuntu-latest container: "nofutur3/php-tests:7.4" + services: + openAPImock: + image: mockserver/mockserver + ports: + - 1080:1080 steps: # Deprecation example for future support removal. # @@ -130,6 +183,9 @@ jobs: - name: Run static analysis run: composer stan + - name: Create mock server from our production OpenAPI + run: curl -X PUT -d "{\"specUrlOrPayload\":\"https://gate.thepay.cz/openapi.yaml\"}" http://openAPImock:1080/mockserver/openapi + - name: Run tests run: composer test diff --git a/tests/BaseTestCase.php b/tests/BaseTestCase.php index 085888e..293e5a3 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -9,8 +9,8 @@ use PHPUnit\Framework\TestCase; use ThePay\ApiClient\Service\ApiService; use ThePay\ApiClient\Service\SignatureService; +use ThePay\ApiClient\Tests\Mocks\TheConfig; use ThePay\ApiClient\TheClient; -use ThePay\ApiClient\TheConfig; abstract class BaseTestCase extends TestCase { @@ -22,21 +22,15 @@ protected function setUp(): void { parent::setUp(); - $this->config = new TheConfig(self::MERCHANT_ID, 1, 'password', 'https://test.api.cz/', 'https://test.gate.cz/'); + $this->config = new TheConfig(); } /** - * method return TheClient witch use apiary mock server + * method return TheClient witch use mock server */ - protected function getApiaryClient(): TheClient + protected function getMockClient(): TheClient { - $config = new TheConfig( - '6cdf1b24', - 1212, - 'password', - 'https://private-aa6aa3-thepay.apiary-mock.com/', - 'https://private-ddc40-gatezalozeniplatby.apiary-mock.com/' - ); + $config = new TheConfig(); $httpFactory = new HttpFactory(); diff --git a/tests/CancelPreauthorizationPaymentTest.php b/tests/CancelPreauthorizationPaymentTest.php index e7f8d73..a48f62e 100644 --- a/tests/CancelPreauthorizationPaymentTest.php +++ b/tests/CancelPreauthorizationPaymentTest.php @@ -4,71 +4,29 @@ namespace ThePay\ApiClient\Tests; -use GuzzleHttp\Psr7\HttpFactory; -use GuzzleHttp\Psr7\Response; -use PHPUnit\Framework\MockObject\MockObject; -use Psr\Http\Client\ClientInterface; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; -use ThePay\ApiClient\Service\ApiService; -use ThePay\ApiClient\Service\SignatureService; use ThePay\ApiClient\TheClient; final class CancelPreauthorizationPaymentTest extends BaseTestCase { - /** @var MockObject&ClientInterface */ - private MockObject $httpClient; private TheClient $client; protected function setUp(): void { parent::setUp(); - $this->httpClient = $this->createMock(ClientInterface::class); - $httpFactory = new HttpFactory(); - $apiService = new ApiService( - $this->config, - $this->createMock(SignatureService::class), - $this->httpClient, - $httpFactory, - $httpFactory - ); - $this->client = new TheClient($this->config, $apiService); + $this->client = $this->getMockClient(); } public function testRequest(): void { - $this->httpClient - ->expects(self::once()) - ->method('sendRequest') - ->willReturnCallback(function (RequestInterface $request): ResponseInterface { - $expectedUrl = $this->config->getApiUrl() . 'projects/1/payments/abc/preauthorized?merchant_id=' . self::MERCHANT_ID; - - self::assertSame('DELETE', $request->getMethod()); - self::assertSame($expectedUrl, $request->getUri()->__toString()); - - return $this->getOkResponse(); - }) - ; - $this->client->cancelPreauthorizedPayment('abc'); + self::assertTrue(true); } public function testNotOkResponse(): void { $this->expectException(\Exception::class); - $this->httpClient->method('sendRequest')->willReturn($this->getNotOkResponse()); - - $this->client->cancelPreauthorizedPayment('abc'); - } - - private function getOkResponse(): ResponseInterface - { - return new Response(204); - } - - private function getNotOkResponse(): ResponseInterface - { - return new Response(401); + /** @phpstan-ignore-next-line */ + $this->client->cancelPreauthorizedPayment(''); } } diff --git a/tests/GeneratePaymentConfirmationTest.php b/tests/GeneratePaymentConfirmationTest.php index 4f58833..0ca186d 100644 --- a/tests/GeneratePaymentConfirmationTest.php +++ b/tests/GeneratePaymentConfirmationTest.php @@ -4,70 +4,24 @@ namespace ThePay\ApiClient\Tests; -use GuzzleHttp\Psr7\HttpFactory; -use GuzzleHttp\Psr7\Response; -use Psr\Http\Client\ClientInterface; -use Psr\Http\Message\RequestInterface; -use ThePay\ApiClient\Service\ApiService; -use ThePay\ApiClient\Service\SignatureService; -use ThePay\ApiClient\TheClient; - final class GeneratePaymentConfirmationTest extends BaseTestCase { public function testSuccess(): void { - $httpFactory = new HttpFactory(); - - $httpClient = $this->createMock(ClientInterface::class); - $httpClient->expects(self::once())->method('sendRequest') - ->willReturnCallback(function (RequestInterface $request): Response { - self::assertSame('GET', $request->getMethod()); - self::assertSame( - $this->config->getApiUrl() . 'projects/1/payments/testUID/generate_confirmation?language=cs&merchant_id=' . self::MERCHANT_ID, - $request->getUri()->__toString() - ); - - return new Response(200, [], 'test pdf content'); - }) - ; - - $theClient = new TheClient( - $this->config, - new ApiService( - $this->config, - $this->createMock(SignatureService::class), - $httpClient, - $httpFactory, - $httpFactory - ) - ); + $theClient = $this->getMockClient(); $pdfContent = $theClient->generatePaymentConfirmationPdf('testUID', 'cs'); - self::assertSame('test pdf content', $pdfContent); + self::assertTrue(is_string($pdfContent)); } public function testFailed(): void { - $httpFactory = new HttpFactory(); - - $httpClient = $this->createMock(ClientInterface::class); - $httpClient->expects(self::once())->method('sendRequest') - ->willReturn(new Response(400)); - - $theClient = new TheClient( - $this->config, - new ApiService( - $this->config, - $this->createMock(SignatureService::class), - $httpClient, - $httpFactory, - $httpFactory - ) - ); + $theClient = $this->getMockClient(); - $this->expectException(\RuntimeException::class); + $this->expectException(\InvalidArgumentException::class); - $theClient->generatePaymentConfirmationPdf('testUID', 'cs'); + /** @phpstan-ignore-next-line */ + $theClient->generatePaymentConfirmationPdf(''); } } diff --git a/tests/GetAccountsBalancesTest.php b/tests/GetAccountsBalancesTest.php index b614098..76d91ab 100644 --- a/tests/GetAccountsBalancesTest.php +++ b/tests/GetAccountsBalancesTest.php @@ -4,63 +4,21 @@ namespace ThePay\ApiClient\Tests; -use GuzzleHttp\Psr7\HttpFactory; -use GuzzleHttp\Psr7\Response; -use Psr\Http\Client\ClientInterface; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; use ThePay\ApiClient\Model\AccountBalance; -use ThePay\ApiClient\Service\ApiService; -use ThePay\ApiClient\Service\SignatureService; -use ThePay\ApiClient\TheClient; final class GetAccountsBalancesTest extends BaseTestCase { public function test(): void { - $httpClient = $this->createMock(ClientInterface::class); - $httpClient - ->expects(self::once()) - ->method('sendRequest') - ->willReturnCallback(function (RequestInterface $request): ResponseInterface { - $expectedUrl = $this->config->getApiUrl() . 'balances?account_iban=TP7811112150822790787055&project_id=1&balance_at=2023-03-14T15%3A08%3A44%2B00%3A00&merchant_id=' . self::MERCHANT_ID; - - self::assertSame('GET', $request->getMethod()); - self::assertSame($expectedUrl, $request->getUri()->__toString()); - - return new Response( - 200, - [], - '[ - { - "iban": "TP7811112150822790787055", - "name": "Test", - "balance": { - "CZK": "45899", - "EUR": "500" - } - } - ]' - ); - }); - - $httpFactory = new HttpFactory(); - $apiService = new ApiService( - $this->config, - $this->createMock(SignatureService::class), - $httpClient, - $httpFactory, - $httpFactory - ); - $client = new TheClient($this->config, $apiService); + $client = $this->getMockClient(); $balances = $client->getAccountsBalances('TP7811112150822790787055', 1, new \DateTime('2023-03-14 15:08:44+00:00')); self::assertEquals( [ new AccountBalance( 'TP7811112150822790787055', - 'Test', - ['CZK' => '45899', 'EUR' => '500'] + 'Account #1', + ['CZK' => '1256', 'EUR' => '231', 'USD' => '0'] ), ], $balances diff --git a/tests/InvalidatePaymentTest.php b/tests/InvalidatePaymentTest.php index d897f58..0ea7877 100644 --- a/tests/InvalidatePaymentTest.php +++ b/tests/InvalidatePaymentTest.php @@ -4,71 +4,29 @@ namespace ThePay\ApiClient\Tests; -use GuzzleHttp\Psr7\HttpFactory; -use GuzzleHttp\Psr7\Response; -use PHPUnit\Framework\MockObject\MockObject; -use Psr\Http\Client\ClientInterface; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; -use ThePay\ApiClient\Service\ApiService; -use ThePay\ApiClient\Service\SignatureService; use ThePay\ApiClient\TheClient; final class InvalidatePaymentTest extends BaseTestCase { - /** @var MockObject&ClientInterface */ - private MockObject $httpClient; private TheClient $client; protected function setUp(): void { parent::setUp(); - $this->httpClient = $this->createMock(ClientInterface::class); - $httpFactory = new HttpFactory(); - $apiService = new ApiService( - $this->config, - $this->createMock(SignatureService::class), - $this->httpClient, - $httpFactory, - $httpFactory - ); - $this->client = new TheClient($this->config, $apiService); + $this->client = $this->getMockClient(); } public function testRequest(): void { - $this->httpClient - ->expects(self::once()) - ->method('sendRequest') - ->willReturnCallback(function (RequestInterface $request): ResponseInterface { - $expectedUrl = $this->config->getApiUrl() . 'projects/1/payments/abc/invalidate?merchant_id=' . self::MERCHANT_ID; - - self::assertSame('PUT', $request->getMethod()); - self::assertSame($expectedUrl, $request->getUri()->__toString()); - - return $this->getOkResponse(); - }) - ; - $this->client->invalidatePayment('abc'); + + self::assertTrue(true); } public function testNotOkResponse(): void { $this->expectException(\Exception::class); - $this->httpClient->method('sendRequest')->willReturn($this->getNotOkResponse()); - - $this->client->invalidatePayment('abdc'); - } - - private function getOkResponse(): ResponseInterface - { - return new Response(200); - } - - private function getNotOkResponse(): ResponseInterface - { - return new Response(404); + $this->client->invalidatePayment(''); } } diff --git a/tests/Mocks/TheConfig.php b/tests/Mocks/TheConfig.php new file mode 100644 index 0000000..d9eeee4 --- /dev/null +++ b/tests/Mocks/TheConfig.php @@ -0,0 +1,30 @@ +getApiaryClient(); + $client = $this->getMockClient(); $methods = $client->getActivePaymentMethods(); $cardMethod = $methods->get('card'); diff --git a/tests/RealizePreauthorizationPaymentTest.php b/tests/RealizePreauthorizationPaymentTest.php index 8ce22b8..7bf9112 100644 --- a/tests/RealizePreauthorizationPaymentTest.php +++ b/tests/RealizePreauthorizationPaymentTest.php @@ -4,22 +4,12 @@ namespace ThePay\ApiClient\Tests; -use GuzzleHttp\Psr7\HttpFactory; -use GuzzleHttp\Psr7\Response; -use PHPUnit\Framework\MockObject\MockObject; -use Psr\Http\Client\ClientInterface; -use Psr\Http\Message\RequestInterface; -use Psr\Http\Message\ResponseInterface; use ThePay\ApiClient\Model\RealizePreauthorizedPaymentParams; use ThePay\ApiClient\Model\RealizePreauthorizedPaymentResult; -use ThePay\ApiClient\Service\ApiService; -use ThePay\ApiClient\Service\SignatureService; use ThePay\ApiClient\TheClient; final class RealizePreauthorizationPaymentTest extends BaseTestCase { - /** @var MockObject&ClientInterface */ - private MockObject $httpClient; private TheClient $client; /** @@ -28,36 +18,12 @@ final class RealizePreauthorizationPaymentTest extends BaseTestCase protected function setUp(): void { parent::setUp(); - - $this->httpClient = $this->createMock(ClientInterface::class); - $httpFactory = new HttpFactory(); - $apiService = new ApiService( - $this->config, - $this->createMock(SignatureService::class), - $this->httpClient, - $httpFactory, - $httpFactory - ); - $this->client = new TheClient($this->config, $apiService); + $this->client = $this->getMockClient(); } public function testRequest(): void { - $this->httpClient - ->expects(self::once()) - ->method('sendRequest') - ->willReturnCallback(function (RequestInterface $request): ResponseInterface { - $expectedUrl = $this->config->getApiUrl('v2') . 'projects/1/payments/abc/preauthorized?merchant_id=' . self::MERCHANT_ID; - - self::assertSame('POST', $request->getMethod()); - self::assertSame($expectedUrl, $request->getUri()->__toString()); - self::assertSame('{"amount":100}', $request->getBody()->getContents()); - - return $this->getOkResponse(); - }) - ; - - $response = $this->client->realizePreauthorizedPayment(new RealizePreauthorizedPaymentParams(100, 'abc')); + $response = $this->client->realizePreauthorizedPayment(new RealizePreauthorizedPaymentParams(9945, 'efd7d8e6-2fa3-3c46-b475-51762331bf56')); self::assertInstanceOf(RealizePreauthorizedPaymentResult::class, $response); self::assertSame(RealizePreauthorizedPaymentResult::STATE_PAID, $response->getState()); @@ -67,18 +33,6 @@ public function testNotOkResponse(): void { $this->expectException(\Exception::class); - $this->httpClient->method('sendRequest')->willReturn($this->getNotOkResponse()); - - $this->client->realizePreauthorizedPayment(new RealizePreauthorizedPaymentParams(100, 'abc')); - } - - private function getOkResponse(): ResponseInterface - { - return new Response(200, [], '{"state":"paid"}'); - } - - private function getNotOkResponse(): ResponseInterface - { - return new Response(401); + $this->client->realizePreauthorizedPayment(new RealizePreauthorizedPaymentParams(100, '')); } }