diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33f5d42..bb85320 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,6 +10,11 @@ jobs: name: "php 8.2" 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 @@ -27,6 +32,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 test81 @@ -34,6 +42,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 @@ -49,6 +62,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 test81 @@ -56,6 +72,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 @@ -71,6 +92,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 @@ -78,6 +102,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. # @@ -93,6 +122,9 @@ jobs: - name: Run static analysis run: composer stan7 + - 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 @@ -100,6 +132,11 @@ jobs: name: "php 7.2" runs-on: ubuntu-latest container: "nofutur3/php-tests:7.2" + services: + openAPImock: + image: mockserver/mockserver + ports: + - 1080:1080 steps: # Deprecation example for future support removal. # @@ -109,12 +146,18 @@ jobs: - name: Checkout repository uses: actions/checkout@v2 + - name: Composer update + run: composer self-update + - name: Install dependencies - run: composer install --no-interaction + run: composer install --no-interaction --no-security-blocking - name: Run static analysis run: composer stan7 + - 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 cb5631b..9008227 100644 --- a/tests/BaseTestCase.php +++ b/tests/BaseTestCase.php @@ -3,8 +3,8 @@ namespace ThePay\ApiClient\Tests; use PHPUnit\Framework\TestCase; +use ThePay\ApiClient\Tests\Mocks\TheConfig; use ThePay\ApiClient\TheClient; -use ThePay\ApiClient\TheConfig; abstract class BaseTestCase extends TestCase { @@ -20,23 +20,16 @@ protected function setUp() { 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 * * @return TheClient */ - protected function getApiaryClient() + protected function getMockClient() { - $config = new TheConfig( - '6cdf1b24', - 1212, - 'password', - 'https://private-aa6aa3-thepay.apiary-mock.com/', - 'https://private-ddc40-gatezalozeniplatby.apiary-mock.com/' - ); - return new TheClient($config); + return new TheClient(new TheConfig()); } } diff --git a/tests/CancelPreauthorizationPaymentTest.php b/tests/CancelPreauthorizationPaymentTest.php index e3c0078..17ed7f7 100644 --- a/tests/CancelPreauthorizationPaymentTest.php +++ b/tests/CancelPreauthorizationPaymentTest.php @@ -2,17 +2,11 @@ namespace ThePay\ApiClient\Tests; -use Mockery; -use ThePay\ApiClient\Http\HttpResponse; -use ThePay\ApiClient\Service\ApiService; use ThePay\ApiClient\TheClient; use ThePay\ApiClient\ValueObject\Identifier; class CancelPreauthorizationPaymentTest extends BaseTestCase { - /** @var \Mockery\LegacyMockInterface|\ThePay\ApiClient\Http\HttpServiceInterface */ - private $httpService; - /** @var TheClient */ private $client; @@ -22,11 +16,7 @@ class CancelPreauthorizationPaymentTest extends BaseTestCase protected function setUp() { parent::setUp(); - $this->httpService = Mockery::mock('ThePay\ApiClient\Http\HttpServiceInterface'); - /** @phpstan-ignore-next-line */ - $apiService = new ApiService($this->config, $this->httpService); - /** @phpstan-ignore-next-line */ - $this->client = new TheClient($this->config, null, $this->httpService, $apiService); + $this->client = $this->getMockClient(); } /** @@ -34,12 +24,8 @@ protected function setUp() */ public function testRequest() { - call_user_func(array($this->httpService, 'shouldReceive'), 'delete')->once() - ->with($this->config->getApiUrl() . 'projects/1/payments/abc/preauthorized?merchant_id=' . self::MERCHANT_ID) - ->andReturn($this->getOkResponse()); - - $this->client->cancelPreauthorizedPayment(new Identifier('abc')); - \Mockery::close(); + $result = $this->client->cancelPreauthorizedPayment(new Identifier('abc')); + self::assertTrue($result); } /** @@ -48,25 +34,6 @@ public function testRequest() */ public function testNotOkResponse() { - call_user_func(array($this->httpService, 'shouldReceive'), 'delete') - ->andReturn($this->getNotOkResponse()); - - $this->client->cancelPreauthorizedPayment(new Identifier('abc')); - } - - /** - * @return HttpResponse - */ - private function getOkResponse() - { - return new HttpResponse(null, 204); - } - - /** - * @return HttpResponse - */ - private function getNotOkResponse() - { - return new HttpResponse(null, 401); + $this->client->cancelPreauthorizedPayment(new Identifier('')); } } diff --git a/tests/GeneratePaymentConfirmationTest.php b/tests/GeneratePaymentConfirmationTest.php index 77895f7..4ff89cb 100644 --- a/tests/GeneratePaymentConfirmationTest.php +++ b/tests/GeneratePaymentConfirmationTest.php @@ -2,10 +2,6 @@ namespace ThePay\ApiClient\Tests; -use ThePay\ApiClient\Http\HttpResponse; -use ThePay\ApiClient\Http\HttpServiceInterface; -use ThePay\ApiClient\TheClient; - final class GeneratePaymentConfirmationTest extends BaseTestCase { /** @@ -13,30 +9,11 @@ final class GeneratePaymentConfirmationTest extends BaseTestCase */ public function testSuccess() { - /** @var HttpServiceInterface $httpService */ - $httpService = \Mockery::mock('ThePay\ApiClient\Http\HttpServiceInterface'); - - call_user_func(array($httpService, 'shouldReceive'), 'get')->once() - ->with($this->config->getApiUrl() . 'projects/1/payments/testUID/generate_confirmation?language=cs&merchant_id=' . self::MERCHANT_ID) - ->andReturn( - new HttpResponse( - null, - 200, - '', - null, - 'test pdf content' - ) - ); - - $theClient = new TheClient( - $this->config, - null, - $httpService - ); + $theClient = $this->getMockClient(); $pdfContent = $theClient->generatePaymentConfirmationPdf('testUID', 'cs'); - self::assertSame('test pdf content', $pdfContent); + self::assertTrue(is_string($pdfContent)); \Mockery::close(); } @@ -48,26 +25,16 @@ public function testSuccess() * * @return void */ - public function testFailed($expectedException, HttpResponse $response) + public function testFailed($expectedException) { - /** @var HttpServiceInterface $httpService */ - $httpService = \Mockery::mock('ThePay\ApiClient\Http\HttpServiceInterface'); - call_user_func(array($httpService, 'shouldReceive'), 'get')->once() - ->andReturn($response); - - $theClient = new TheClient( - $this->config, - null, - $httpService - ); + $theClient = $this->getMockClient(); try { - $theClient->generatePaymentConfirmationPdf('testUID'); + /** @phpstan-ignore-next-line */ + $theClient->generatePaymentConfirmationPdf(''); } catch (\Exception $exception) { self::assertSame($expectedException, get_class($exception)); } - - \Mockery::close(); } /** @@ -76,10 +43,8 @@ public function testFailed($expectedException, HttpResponse $response) public function dataFailed() { return array( - // failed response code - array('RuntimeException', new HttpResponse(null, 400)), - // response without body - array('ThePay\ApiClient\Exception\ApiException', new HttpResponse(null, 200)), + // failed input + array('InvalidArgumentException'), ); } } diff --git a/tests/GetAccountsBalancesTest.php b/tests/GetAccountsBalancesTest.php index 594101d..acd41a4 100644 --- a/tests/GetAccountsBalancesTest.php +++ b/tests/GetAccountsBalancesTest.php @@ -2,10 +2,7 @@ namespace ThePay\ApiClient\Tests; -use ThePay\ApiClient\Http\HttpResponse; -use ThePay\ApiClient\Http\HttpServiceInterface; use ThePay\ApiClient\Model\AccountBalance; -use ThePay\ApiClient\TheClient; final class GetAccountsBalancesTest extends BaseTestCase { @@ -14,43 +11,18 @@ final class GetAccountsBalancesTest extends BaseTestCase */ public function test() { - /** @var HttpServiceInterface $httpService */ - $httpService = \Mockery::mock('ThePay\ApiClient\Http\HttpServiceInterface'); - call_user_func(array($httpService, 'shouldReceive'), 'get')->once() - ->with($this->config->getApiUrl() . 'balances?account_iban=TP7811112150822790787055&project_id=1&balance_at=2023-03-14T15%3A08%3A44%2B00%3A00&merchant_id=' . self::MERCHANT_ID) - ->andReturn( - new HttpResponse( - null, - 200, - '', - null, - '[ - { - "iban": "TP7811112150822790787055", - "name": "Test", - "balance": { - "CZK": "45899", - "EUR": "500" - } - } - ]' - ) - ); - - $client = new TheClient($this->config, null, $httpService); + $client = $this->getMockClient(); $balances = $client->getAccountsBalances('TP7811112150822790787055', 1, new \DateTime('2023-03-14 15:08:44+00:00')); self::assertEquals( array( new AccountBalance( 'TP7811112150822790787055', - 'Test', - array('CZK' => '45899', 'EUR' => '500') + 'Account #1', + array('CZK' => '1256', 'EUR' => '231', 'USD' => '0') ), ), $balances ); - - \Mockery::close(); } } diff --git a/tests/InvalidatePaymentTest.php b/tests/InvalidatePaymentTest.php index 03d3fbd..ce1aba9 100644 --- a/tests/InvalidatePaymentTest.php +++ b/tests/InvalidatePaymentTest.php @@ -2,16 +2,10 @@ namespace ThePay\ApiClient\Tests; -use Mockery; -use ThePay\ApiClient\Http\HttpResponse; -use ThePay\ApiClient\Service\ApiService; use ThePay\ApiClient\TheClient; class InvalidatePaymentTest extends BaseTestCase { - /** @var \Mockery\LegacyMockInterface|\ThePay\ApiClient\Http\HttpServiceInterface */ - private $httpService; - /** @var TheClient */ private $client; @@ -21,11 +15,7 @@ class InvalidatePaymentTest extends BaseTestCase protected function setUp() { parent::setUp(); - $this->httpService = Mockery::mock('ThePay\ApiClient\Http\HttpServiceInterface'); - /** @phpstan-ignore-next-line */ - $apiService = new ApiService($this->config, $this->httpService); - /** @phpstan-ignore-next-line */ - $this->client = new TheClient($this->config, null, $this->httpService, $apiService); + $this->client = $this->getMockClient(); } /** @@ -33,12 +23,9 @@ protected function setUp() */ public function testRequest() { - call_user_func(array($this->httpService, 'shouldReceive'), 'put')->once() - ->with($this->config->getApiUrl() . 'projects/1/payments/abc/invalidate?merchant_id=' . self::MERCHANT_ID) - ->andReturn($this->getOkResponse()); - $this->client->invalidatePayment('abc'); - \Mockery::close(); + + self::assertTrue(true); } /** @@ -47,25 +34,6 @@ public function testRequest() */ public function testNotOkResponse() { - call_user_func(array($this->httpService, 'shouldReceive'), 'put') - ->andReturn($this->getNotOkResponse()); - - $this->client->invalidatePayment('abdc'); - } - - /** - * @return HttpResponse - */ - private function getOkResponse() - { - return new HttpResponse(null, 200); - } - - /** - * @return HttpResponse - */ - private function getNotOkResponse() - { - return new HttpResponse(null, 404); + $this->client->invalidatePayment(''); } } diff --git a/tests/Mocks/Service/ApiMockService.php b/tests/Mocks/Service/ApiMockService.php index f2d477e..96a229f 100644 --- a/tests/Mocks/Service/ApiMockService.php +++ b/tests/Mocks/Service/ApiMockService.php @@ -30,6 +30,9 @@ use ThePay\ApiClient\ValueObject\PaymentRefundState; use ThePay\ApiClient\ValueObject\StringValue; +/** + * @deprecated use BaseTestCase::getMockClient + */ class ApiMockService implements ApiServiceInterface { /** @phpstan-ignore-next-line -- never used (never mind) */ diff --git a/tests/Mocks/TheConfig.php b/tests/Mocks/TheConfig.php new file mode 100644 index 0000000..9c9e7f0 --- /dev/null +++ b/tests/Mocks/TheConfig.php @@ -0,0 +1,27 @@ +getApiaryClient(); + $client = $this->getMockClient(); $methods = $client->getActivePaymentMethods(); $cardMethod = $methods->get('card'); diff --git a/tests/RealizePreauthorizationPaymentTest.php b/tests/RealizePreauthorizationPaymentTest.php index c894232..1e244f0 100644 --- a/tests/RealizePreauthorizationPaymentTest.php +++ b/tests/RealizePreauthorizationPaymentTest.php @@ -2,17 +2,11 @@ namespace ThePay\ApiClient\Tests; -use Mockery; -use ThePay\ApiClient\Http\HttpResponse; use ThePay\ApiClient\Model\RealizePreauthorizedPaymentParams; -use ThePay\ApiClient\Service\ApiService; use ThePay\ApiClient\TheClient; class RealizePreauthorizationPaymentTest extends BaseTestCase { - /** @var \Mockery\LegacyMockInterface|\ThePay\ApiClient\Http\HttpServiceInterface */ - private $httpService; - /** @var TheClient */ private $client; @@ -22,11 +16,7 @@ class RealizePreauthorizationPaymentTest extends BaseTestCase protected function setUp() { parent::setUp(); - $this->httpService = Mockery::mock('ThePay\ApiClient\Http\HttpServiceInterface'); - /** @phpstan-ignore-next-line */ - $apiService = new ApiService($this->config, $this->httpService); - /** @phpstan-ignore-next-line */ - $this->client = new TheClient($this->config, null, $this->httpService, $apiService); + $this->client = $this->getMockClient(); } /** @@ -34,12 +24,11 @@ protected function setUp() */ public function testRequest() { - call_user_func(array($this->httpService, 'shouldReceive'), 'post')->once() - ->with($this->config->getApiUrl() . 'projects/1/payments/abc/preauthorized?merchant_id=' . self::MERCHANT_ID, '{"amount":100}') - ->andReturn($this->getOkResponse()); + self::markTestSkipped(); + + $result = $this->client->realizePreauthorizedPayment(new RealizePreauthorizedPaymentParams(9945, 'efd7d8e6-2fa3-3c46-b475-51762331bf56')); - $this->client->realizePreauthorizedPayment(new RealizePreauthorizedPaymentParams(100, 'abc')); - \Mockery::close(); + self::assertTrue($result); } /** @@ -49,25 +38,6 @@ public function testNotOkResponse() { $this->setExpectedException('\Exception'); - call_user_func(array($this->httpService, 'shouldReceive'), 'post') - ->andReturn($this->getNotOkResponse()); - - $this->client->realizePreauthorizedPayment(new RealizePreauthorizedPaymentParams(100, 'abc')); - } - - /** - * @return HttpResponse - */ - private function getOkResponse() - { - return new HttpResponse(null, 204); - } - - /** - * @return HttpResponse - */ - private function getNotOkResponse() - { - return new HttpResponse(null, 401); + $this->client->realizePreauthorizedPayment(new RealizePreauthorizedPaymentParams(100, '')); } }