|
| 1 | +<?php |
| 2 | +namespace tests; |
| 3 | + |
| 4 | +use AvangateClient\Client; |
| 5 | +use GuzzleHttp\Exception\ClientException; |
| 6 | + |
| 7 | +class ConnectTest extends \PHPUnit_Framework_TestCase |
| 8 | +{ |
| 9 | + public function setUp() |
| 10 | + { |
| 11 | + if (MERCHANT_CODE === '*****') { |
| 12 | + $message = 'Set valid credentials (MERCHANT_CODE / MERCHANT_APIKEY) in phpunit.xml file.'; |
| 13 | + static::fail($message . ' Check https://secure.avangate.com/cpanel/account_settings.php for details.'); |
| 14 | + } |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * When invalid credentials are given then throw exception. |
| 19 | + */ |
| 20 | + public function testWhenInvalidCredentialsAreGivenThenThrowException() |
| 21 | + { |
| 22 | + $client = new Client([ |
| 23 | + 'code' => 'INVALID-MERCHANT-CODE', |
| 24 | + 'key' => 'INVALID-MERCHANT-APIKEY', |
| 25 | + 'base_uri' => 'https://api.avangate.com/3.0/' |
| 26 | + ]); |
| 27 | + |
| 28 | + try { |
| 29 | + $client->get('currencies/'); |
| 30 | + static::fail('When you see it, an expected exception was not thrown.'); |
| 31 | + |
| 32 | + } catch (ClientException $e) { |
| 33 | + static::assertEquals(401, $e->getResponse()->getStatusCode()); |
| 34 | + $sentDetails = json_decode($e->getResponse()->getBody()->getContents()); |
| 35 | + |
| 36 | + static::assertEquals('AUTHENTICATION_ERROR', $sentDetails->error_code); |
| 37 | + static::assertEquals('Authentication needed for this resource', $sentDetails->message); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * When valid credentials are set then return valid details. |
| 43 | + */ |
| 44 | + public function testWhenValidCredentialsAreSetThenReturnValidDetails() |
| 45 | + { |
| 46 | + $client = new Client([ |
| 47 | + 'code' => MERCHANT_CODE, |
| 48 | + 'key' => MERCHANT_APIKEY, |
| 49 | + 'base_uri' => 'https://api.avangate.com/3.0/' |
| 50 | + ]); |
| 51 | + |
| 52 | + $allowedCurrencies = json_decode($client->get('currencies/')->getBody()->getContents(), true); |
| 53 | + |
| 54 | + static::assertTrue(is_array($allowedCurrencies)); |
| 55 | + static::assertTrue(count($allowedCurrencies) > 0); |
| 56 | + |
| 57 | + foreach ($allowedCurrencies as $currency) { |
| 58 | + static::assertEquals(strtolower($currency), $currency); |
| 59 | + static::assertEquals(3, strlen($currency)); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments