diff --git a/composer.json b/composer.json index f66b6bf3..44b01f92 100644 --- a/composer.json +++ b/composer.json @@ -37,13 +37,14 @@ } }, "require-dev": { + "brianium/paratest": "^1.1", "friendsofphp/php-cs-fixer": "^2.4", "jakub-onderka/php-parallel-lint": "^0.9.2", "phpro/grumphp": "^0.11.6", "phpstan/phpstan": "^0.8", - "phpunit/phpunit": "^5.7", - "sensiolabs/security-checker": "^4.1", - "sebastian/phpcpd": "^3.0" + "phpunit/phpunit": "^6.0.13", + "sebastian/phpcpd": "^3.0", + "sensiolabs/security-checker": "^4.1" }, "bin": [ "bin/bunq-install" diff --git a/phpunit.xml b/phpunit.xml index 50025404..b3401326 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -3,6 +3,11 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.5/phpunit.xsd" cacheTokens="false"> + + + ./tests/ + + ./tests diff --git a/test/BunqSdkTestBase.php b/test/BunqSdkTestBase.php deleted file mode 100644 index be92bada..00000000 --- a/test/BunqSdkTestBase.php +++ /dev/null @@ -1,73 +0,0 @@ -save(static::FILENAME_CONTEXT_CONFIG); - - static::$apiContext = $apiContext; - } - - /** - * @return ApiContext - */ - protected static function createApiContext() - { - return ApiContext::create(BunqEnumApiEnvironmentType::SANDBOX(), TestConfig::getApiKey(), - static::DEVICE_DESCRIPTION, [TestConfig::getIpAddress()]); - } - - /** - * @return ApiContext - */ - protected static function getApiContext() - { - return static::$apiContext; - } -} diff --git a/test/JsonParserTest.php b/test/JsonParserTest.php deleted file mode 100644 index ff824ad0..00000000 --- a/test/JsonParserTest.php +++ /dev/null @@ -1,124 +0,0 @@ - - * @since 20170630 Initial creation. - */ -class JsonParserTest extends TestCase -{ - /** - * Test constants. - */ - const JSON_ID = '{"Response": [{"Id": {"id": 37}}]}'; - const EXPECTED_ID = 37; - const JSON_UUID = '{"Response": [{"Uuid": {"uuid": "5a442b1d-3d43-4285-b532-dbb21055824a"}}]}'; - const EXPECTED_UUID = '5a442b1d-3d43-4285-b532-dbb21055824a'; - const EXPECTED_TOKEN = 'a4f9d888eea84f52722b9baf2f17c289d549edf6e0eccdbf868eb922be306fb6'; - const EXPECTED_PUBLIC_KEY = 'My server public key'; - const EXPECTED_NAME_BUNQ = 'bunq'; - const EXPECTED_EMAIL_BRAVO = 'bravo@bunq.com'; - const EXPECTED_INSTALLATION_ID = 26; - - /** - * Resource location constants. - */ - const RESOURCE_INSTALLATION_JSON = '/resource/installation.json'; - const RESOURCE_USER_COMPANY_JSON = '/resource/user_company.json'; - - /** - * Function constants. - */ - const FUNCTION_CLASS_FROM_JSON = 'classFromJson'; - const FUNCTION_FROM_JSON = 'fromJson'; - const FUNCTION_PROCESS_FOR_ID = 'processForId'; - const FUNCTION_PROCESS_FOR_UUID = 'processForUuid'; - - /** - * Call a private static method on a class. - * - * @param string $class - * @param string $method - * @param array $args - * - * @return mixed - */ - private function callPrivateStaticMethod($class, $method, array $args) - { - $reflectionClass = new \ReflectionClass($class); - $createFromJsonMethod = $reflectionClass->getMethod($method); - $createFromJsonMethod->setAccessible(true); - - return $createFromJsonMethod->invokeArgs(null, $args); - } - - /** - * Test creation of Id. - */ - public function testCreateIdFromJson() - { - $id = $this->callPrivateStaticMethod(BunqModel::class, self::FUNCTION_PROCESS_FOR_ID, [self::JSON_ID]); - - static::assertEquals(self::EXPECTED_ID, $id); - } - - /** - * Test creation of Uuid. - */ - public function testCreateUuidFromJson() - { - $uuid = $this->callPrivateStaticMethod(BunqModel::class, self::FUNCTION_PROCESS_FOR_UUID, [self::JSON_UUID]); - - static::assertEquals(self::EXPECTED_UUID, $uuid); - } - - /** - * Test creation of UserCompany. - */ - public function testCreateFromJson() - { - $userCompanyJson = FileUtil::getFileContents(__DIR__ . self::RESOURCE_USER_COMPANY_JSON); - /** @var UserCompany $userCompany */ - $userCompany = $this->callPrivateStaticMethod( - UserCompany::class, - self::FUNCTION_FROM_JSON, - [$userCompanyJson] - ); - - static::assertInstanceOf(UserCompany::class, $userCompany); - static::assertEquals(self::EXPECTED_NAME_BUNQ, $userCompany->getName()); - static::assertEquals(self::EXPECTED_EMAIL_BRAVO, $userCompany->getAlias()[0]->getValue()); - static::assertInstanceOf(Amount::class, $userCompany->getDailyLimitWithoutConfirmationLogin()); - } - - /** - * Test creating a predefined class from json. - */ - public function testCreateClassFormJson() - { - /** @var Installation $installation */ - $installation = $this->callPrivateStaticMethod( - BunqModel::class, - self::FUNCTION_CLASS_FROM_JSON, - [ - Installation::class, - FileUtil::getFileContents(__DIR__ . self::RESOURCE_INSTALLATION_JSON), - ] - ); - - static::assertInstanceOf(Installation::class, $installation); - static::assertEquals(self::EXPECTED_INSTALLATION_ID, $installation->getId()->getId()); - static::assertEquals(self::EXPECTED_TOKEN, $installation->getToken()->getToken()); - static::assertEquals( - self::EXPECTED_PUBLIC_KEY, - $installation->getServerPublicKey()->getServerPublicKey()->getKey() - ); - } -} diff --git a/test/Model/Generated/AttachmentPublicTest.php b/test/Model/Generated/AttachmentPublicTest.php deleted file mode 100644 index 8e3cbd6c..00000000 --- a/test/Model/Generated/AttachmentPublicTest.php +++ /dev/null @@ -1,74 +0,0 @@ -getFileContentsOfAttachment(); - $customHeadersMap = [ - ApiClient::HEADER_CONTENT_TYPE => static::$contentType, - ApiClient::HEADER_ATTACHMENT_DESCRIPTION => static::$attachmentDescription, - ]; - - $beforeUuid = AttachmentPublic::create(static::getApiContext(), $beforeBytes, $customHeadersMap); - $bytesAfter = AttachmentPublicContent::listing(static::getApiContext(), $beforeUuid); - - static::assertEquals($beforeBytes, $bytesAfter); - } - - /** - * @return string - */ - private function getFileContentsOfAttachment() - { - $path = __DIR__ . self::PATH_ATTACHMENT . static::$attachmentPathIn; - - return FileUtil::getFileContents($path); - } -} diff --git a/test/Model/Generated/AvatarTest.php b/test/Model/Generated/AvatarTest.php deleted file mode 100644 index 9e9704c0..00000000 --- a/test/Model/Generated/AvatarTest.php +++ /dev/null @@ -1,85 +0,0 @@ -getFileContentsOfAttachment(); - $customHeadersMap = [ - ApiClient::HEADER_ATTACHMENT_DESCRIPTION => static::$attachmentDescription, - ApiClient::HEADER_CONTENT_TYPE => static::$contentType, - ]; - - $attachmentUuidBefore = AttachmentPublic::create(static::getApiContext(), $fileContentsBefore, $customHeadersMap); - $avatarMap = [ - Avatar::FIELD_ATTACHMENT_PUBLIC_UUID => $attachmentUuidBefore, - ]; - $avatarUuid = Avatar::create(static::getApiContext(), $avatarMap); - - $attachmentUuidAfter = Avatar::get(static::getApiContext(), $avatarUuid); - $imageInfoArray = $attachmentUuidAfter->getImage(); - $attachmentPublicUuid = $imageInfoArray[self::INDEX_FIRST]->getAttachmentPublicUuid(); - $fileContentsAfter = AttachmentPublicContent::listing(static::getApiContext(), $attachmentPublicUuid); - - static::assertEquals($fileContentsBefore, $fileContentsAfter); - } - - /** - * @return string - */ - private function getFileContentsOfAttachment() - { - $path = __DIR__ . self::PATH_ATTACHMENT . static::$attachmentPathIn; - - return FileUtil::getFileContents($path); - } -} diff --git a/test/Model/Generated/CardDebitTest.php b/test/Model/Generated/CardDebitTest.php deleted file mode 100644 index e29a039e..00000000 --- a/test/Model/Generated/CardDebitTest.php +++ /dev/null @@ -1,84 +0,0 @@ -getPossibleCardNameArray(); - static::$alias = - User::listing(static::getApiContext())[self::INDEX_FIRST]->getUserCompany()->getAlias( - )[self::INDEX_FIRST]; - } - - /** - * Test if the user can order a new card with an unique second line. - */ - public function testOrderingDebitCard() - { - $apiContext = static::getApiContext(); - $alias = new Pointer(static::$alias->getType(), static::$alias->getValue()); - - $cardDebitMap = [ - CardDebit::FIELD_ALIAS => $alias, - CardDebit::FIELD_SECOND_LINE => uniqid(self::CARD_SECOND_LINE_PREFIX), - CardDebit::FIELD_PIN_CODE => self::CARD_PIN_CODE, - CardDebit::FIELD_NAME_ON_CARD => static::$nameOnCard[self::INDEX_FIRST], - ]; - $cardDebit = CardDebit::create($apiContext, $cardDebitMap, static::$userId); - $card = Card::get($apiContext, static::$userId, $cardDebit->getId()); - - static::assertEquals($cardDebit->getNameOnCard(), $card->getNameOnCard()); - static::assertEquals($cardDebit->getCreated(), $card->getCreated()); - static::assertEquals($cardDebit->getSecondLine(), $card->getSecondLine()); - } -} diff --git a/test/Model/Generated/DraftShareInviteBankQrCodeContentTest.php b/test/Model/Generated/DraftShareInviteBankQrCodeContentTest.php deleted file mode 100644 index dc536c6b..00000000 --- a/test/Model/Generated/DraftShareInviteBankQrCodeContentTest.php +++ /dev/null @@ -1,94 +0,0 @@ -createConnect($apiContext); - $qrContent = $this->getQrContent($apiContext, $draftShareId); - file_put_contents(self::PATH_QR_OUTPUT, $qrContent); - } - - /** - * @param ApiContext $apiContext - * - * @return int - */ - private function createConnect($apiContext) - { - $readOnly = new ShareDetailReadOnly(true, true, true); - $shareDetail = new ShareDetail(); - $shareDetail->setReadOnly($readOnly); - $draftMap = [ - DraftShareInviteBank::FIELD_EXPIRATION => static::$expirationDate, - DraftShareInviteBank::FIELD_DRAFT_SHARE_SETTINGS => new DraftShareInviteBankEntry($shareDetail), - ]; - - return DraftShareInviteBank::create($apiContext, $draftMap, static::$userId); - } - - /** - * @param ApiContext $apiContext - * @param int $draftShareId - * - * @return string - */ - private function getQrContent($apiContext, $draftShareId) - { - return DraftShareInviteBankQrCodeContent::listing($apiContext, static::$userId, $draftShareId); - } -} diff --git a/test/Model/Generated/MonetaryAccountBankTest.php b/test/Model/Generated/MonetaryAccountBankTest.php deleted file mode 100644 index 3c1b5fed..00000000 --- a/test/Model/Generated/MonetaryAccountBankTest.php +++ /dev/null @@ -1,79 +0,0 @@ - self::STATUS, - MonetaryAccountBank::FIELD_SUB_STATUS => self::SUB_STATUS, - MonetaryAccountBank::FIELD_REASON => self::REASON, - MonetaryAccountBank::FIELD_REASON_DESCRIPTION => self::REASON_DESCRIPTION, - ]; - - MonetaryAccountBank::update($apiContext, $requestMap, static::$userId, static::$monetaryAccountBankToCloseId); - } - } - - /** - * Test making a new monetary account with an unique description. - * - * This test has no assertion as of its testing to see if the code runs without errors. - */ - public function testCreateNewMonetaryAccount() - { - $apiContext = static::getApiContext(); - $requestMap = [ - MonetaryAccountBank::FIELD_CURRENCY => self::CURRENCY, - MonetaryAccountBank::FIELD_DESCRIPTION => uniqid(self::PREFIX_MONETARY_ACCOUNT_DESCRIPTION), - ]; - - static::$monetaryAccountBankToCloseId = MonetaryAccountBank::create($apiContext, $requestMap, static::$userId); - } -} diff --git a/test/Model/Generated/PaymentTest.php b/test/Model/Generated/PaymentTest.php deleted file mode 100644 index 74248540..00000000 --- a/test/Model/Generated/PaymentTest.php +++ /dev/null @@ -1,149 +0,0 @@ - new Pointer( - static::$counterTypeOtherUser, - static::$counterAliasOtherUser - ), - Payment::FIELD_AMOUNT => new Amount(self::PAYMENT_AMOUNT_IN_EUR, self::PAYMENT_CURRENCY), - Payment::FIELD_DESCRIPTION => self::PAYMENT_DESCRIPTION, - ]; - - Payment::create($apiContext, $requestMap, static::$userId, static::$monetaryAccountId); - } - - /** - * Test sending money to other monetaryAccount. - * - * This test has no assertion as of its testing to see if the code runs without errors. - */ - public function testSendMoneyToOtherMonetaryAccount() - { - $apiContext = static::getApiContext(); - $requestMap = [ - Payment::FIELD_AMOUNT => new Amount(self::PAYMENT_AMOUNT_IN_EUR, self::PAYMENT_CURRENCY), - Payment::FIELD_COUNTERPARTY_ALIAS => new Pointer( - static::$counterTypeSameUserOtherAccount, - static::$counterAliasSameUserOtherAccount - ), - Payment::FIELD_DESCRIPTION => self::PAYMENT_DESCRIPTION, - ]; - - static::$paymentId = Payment::create($apiContext, $requestMap, static::$userId, static::$monetaryAccountId); - } - - /** - * Test sending a payment chat to a payment. - * - * This test has no assertion as of its testing to see if the code runs without errors. - * - * @depends testSendMoneyToOtherMonetaryAccount - */ - public function testSendMessageToPayment() - { - $apiContext = static::getApiContext(); - $chatId = PaymentChat::create( - $apiContext, - [], - static::$userId, - static::$monetaryAccountId, - static::$paymentId - ); - $messageMap = [ - ChatMessageText::FIELD_TEXT => self::PAYMENT_CHAT_TEXT_MESSAGE, - ]; - - ChatMessageText::create($apiContext, $messageMap, static::$userId, $chatId); - } -} diff --git a/test/Model/Generated/RequestInquiryTest.php b/test/Model/Generated/RequestInquiryTest.php deleted file mode 100644 index a7854f40..00000000 --- a/test/Model/Generated/RequestInquiryTest.php +++ /dev/null @@ -1,125 +0,0 @@ -sendRequest($apiContext); - $requestResponseId = RequestResponse::listing($apiContext, static::$userId, static::$monetaryAccountId2)[ - self::INDEX_FIRST]->getId(); - $this->acceptRequest($apiContext, $requestResponseId); - } - - /** - * @param ApiContext $apiContext - */ - private function sendRequest($apiContext) - { - $requestMap = [ - RequestInquiry::FIELD_AMOUNT_INQUIRED => new Amount(self::REQUEST_AMOUNT_IN_EUR, - self::REQUEST_CURRENCY), - RequestInquiry::FIELD_COUNTERPARTY_ALIAS => new Pointer(static::$counterType, static::$counterAlias), - RequestInquiry::FIELD_DESCRIPTION => self::REQUEST_DESCRIPTION, - RequestInquiry::FIELD_ALLOW_BUNQME => false, - ]; - - RequestInquiry::create($apiContext, $requestMap, static::$userId, static::$monetaryAccountId); - } - - /** - * @param ApiContext $apiContext - * @param int $requestResponseId - */ - private function acceptRequest($apiContext, $requestResponseId) - { - $responseMap = [ - RequestResponse::FIELD_STATUS => self::REQUEST_STATUS_ACCEPTED, - ]; - - RequestResponse::update( - $apiContext, - $responseMap, - static::$userId, - static::$monetaryAccountId2, - $requestResponseId - ); - } -} - diff --git a/test/Model/Generated/SessionTest.php b/test/Model/Generated/SessionTest.php deleted file mode 100644 index 0a326261..00000000 --- a/test/Model/Generated/SessionTest.php +++ /dev/null @@ -1,35 +0,0 @@ - self::TAB_DESCRIPTION, - TabUsageSingle::FIELD_STATUS => self::TAB_STATUS_BEFORE, - TabUsageSingle::FIELD_AMOUNT_TOTAL => new Amount(self::AMOUNT_IN_EUR, self::TAB_CURRENCY), - ]; - static::$tabUuid = TabUsageSingle::create( - $apiContext, - $tabUsageSingleCreateMap, - static::$userId, - static::$monetaryAccountId, - static::$cashRegisterId - ); - } - - /** - * Tests adding a new tab item to an existing tab. - * - * @depends testCreateTab - */ - public function testAddItemToTab() - { - $apiContext = static::getApiContext(); - - $tabItemShopMap = [ - TabItemShop::FIELD_DESCRIPTION => self::ITEM_DESCRIPTION, - TabItemShop::FIELD_AMOUNT => new Amount(self::AMOUNT_IN_EUR, self::TAB_CURRENCY), - ]; - TabItemShop::create( - $apiContext, - $tabItemShopMap, - static::$userId, - static::$monetaryAccountId, - static::$cashRegisterId, - static::$tabUuid - ); - } - - /** - * Tests updating a tab after adding an item to it. - * - * @depends testAddItemToTab - */ - public function testUpdateTab() - { - $apiContext = static::getApiContext(); - - $tabUsageSingleUpdateMap = [ - TabUsageSingle::FIELD_STATUS => self::TAB_STATUS_AFTER, - ]; - TabUsageSingle::update( - $apiContext, - $tabUsageSingleUpdateMap, - static::$userId, - static::$monetaryAccountId, - static::$cashRegisterId, - static::$tabUuid - ); - } -} diff --git a/test/TestConfig.php b/test/TestConfig.php deleted file mode 100644 index 7a43d2cb..00000000 --- a/test/TestConfig.php +++ /dev/null @@ -1,145 +0,0 @@ -", - "USER_ID": "xxxx", - "MONETARY_ACCOUNT_ID": "xxxx", - "MONETARY_ACCOUNT_ID2": "xxxx", - "ipAddress": "", - "AttachmentPublicTest":{ - "CONTENT_TYPE": "image/png", - "DESCRIPTION": "TEST PNG PHP", - "PATH_IN": "/bunq_App_Icon_Square@4x.png" - }, - "TabUsageSingleTest": { - "CASH_REGISTER_ID": "xxx" - }, - "CounterPartySelf": { - "Alias": "", - "Type": "EMAIL" - }, - "CounterPartyOther": { - "Alias": "", - "Type": "EMAIL" - } -} diff --git a/test/resource/installation.json b/test/resource/installation.json deleted file mode 100644 index b7073856..00000000 --- a/test/resource/installation.json +++ /dev/null @@ -1,22 +0,0 @@ -{ - "Response": [ - { - "Id": { - "id": 26 - } - }, - { - "Token": { - "id": 134, - "created": "2015-06-13 23:19:16.215235", - "updated": "2015-06-30 09:12:31.981573", - "token": "a4f9d888eea84f52722b9baf2f17c289d549edf6e0eccdbf868eb922be306fb6" - } - }, - { - "ServerPublicKey": { - "server_public_key": "My server public key" - } - } - ] -} diff --git a/test/resource/user_company.json b/test/resource/user_company.json deleted file mode 100644 index 6e67d3af..00000000 --- a/test/resource/user_company.json +++ /dev/null @@ -1,96 +0,0 @@ -{ - "Response": [ - { - "UserCompany": { - "id": 42, - "created": "2015-06-13 23:19:16.215235", - "updated": "2015-06-30 09:12:31.981573", - "public_uuid": "252e-fb1e-04b74214-b9e9467c3-c6d2fbf", - "name": "bunq", - "display_name": "bunq", - "public_nick_name": "bunq", - "alias": [ - { - "type": "EMAIL", - "value": "bravo@bunq.com", - "name": "" - } - ], - "chamber_of_commerce_number": "NL040492904", - "type_of_business_entity": "One man business", - "sector_of_industry": "Education", - "counter_bank_iban": "NL12BUNQ1234567890", - "avatar": { - "uuid": "5a442bed-3d43-4a85-b532-dbb251052f4a", - "anchor_uuid": "f0de919f-8c36-46ee-acb7-ea9c35c1b231", - "image": [ - { - "attachment_public_uuid": "d93e07e3-d420-45e5-8684-fc0c09a63686", - "content_type": "image/jpeg", - "height": 380, - "width": 520 - } - ] - }, - "address_main": { - "street": "Example Boulevard", - "house_number": "123a", - "po_box": "09392", - "postal_code": "1234AA", - "city": "Amsterdam", - "country": "NL" - }, - "address_postal": { - "street": "Example Boulevard", - "house_number": "123a", - "po_box": "09392", - "postal_code": "1234AA", - "city": "Amsterdam", - "country": "NL" - }, - "version_terms_of_service": "1.2", - "director_alias": { - "uuid": "252e-fb1e-04b74214-b9e9467c3-c6d2fbf", - "avatar": { - "uuid": "5a442bed-3d43-4a85-b532-dbb251052f4a", - "anchor_uuid": "f0de919f-8c36-46ee-acb7-ea9c35c1b231", - "image": [ - { - "attachment_public_uuid": "d93e07e3-d420-45e5-8684-fc0c09a63686", - "content_type": "image/jpeg", - "height": 380, - "width": 520 - } - ] - }, - "public_nick_name": "Mary", - "display_name": "Mary", - "country": "NL" - }, - "language": "en_US", - "region": "en_US", - "ubo": [ - { - "name": "A. Person", - "date_of_birth": "1990-03-27", - "nationality": "NL" - } - ], - "status": "ACTIVE", - "sub_status": "APPROVAL", - "session_timeout": 1, - "daily_limit_without_confirmation_login": { - "value": "12.50", - "currency": "EUR" - }, - "notification_filters": [ - { - "notification_delivery_method": "URL", - "notification_target": "https://my.company.com/callback-url", - "category": "PAYMENT" - } - ] - } - } - ] -} diff --git a/tests/BunqSdkTestBase.php b/tests/BunqSdkTestBase.php index 76278cc0..de0758c8 100644 --- a/tests/BunqSdkTestBase.php +++ b/tests/BunqSdkTestBase.php @@ -3,10 +3,16 @@ use bunq\Context\ApiContext; use bunq\Context\BunqContext; -use bunq\Exception\ApiException; use bunq\Exception\BunqException; -use bunq\Model\Generated\Endpoint\User; +use bunq\Http\ApiClient; +use bunq\Model\Generated\Endpoint\AttachmentPublic; +use bunq\Model\Generated\Endpoint\Avatar; +use bunq\Model\Generated\Endpoint\CashRegister; +use bunq\Model\Generated\Endpoint\MonetaryAccountBank; +use bunq\Model\Generated\Object\Pointer; use bunq\Util\BunqEnumApiEnvironmentType; +use bunq\Util\FileUtil; +use bunq\Util\InstallationUtil; use PHPUnit\Framework\TestCase; /** @@ -14,50 +20,229 @@ */ class BunqSdkTestBase extends TestCase { + /** + * Error constants. + */ + const ERROR_COULD_NOT_DETERMINE_IBAN_POINTER = 'Could not determine IBAN pointer'; + const ERROR_COULD_NOT_DETERMINE_USER_ALIAS = 'Could not determine user alias.'; + const WARMING_TEST_SKIPPED_DUE_TO_INSUFFICIENT_BALANCE = 'Not enough money on primary account.'; + + /* + * CashRegister constants. + */ + const CASH_REGISTER_NAME = 'PHP test cash register'; + const CASH_REGISTER_STATUS = 'PENDING_APPROVAL'; + + /** + * MonetaryAccount constants. + */ + const MOMENTARY_ACCOUNT_CURRENCY = 'EUR'; + const MONETARY_ACCOUNT_DESCRIPTION = 'test account php'; + const MONETARY_ACCOUNT_BALANCE_THRESHOLD = 0.00; + + /** + * Pointer constants. + */ + const POINTER_TYPE_IBAN = 'IBAN'; + const POINTER_TYPE_EMAIL = 'EMAIL'; + const EMAIL_BRAVO = 'bravo@bunq.com'; + /** * Full name of context config file to use for testing. */ - const FILENAME_CONTEXT_CONFIG = __DIR__ . '/../bunq-test.conf'; + const FILE_PATH_CONTEXT_CONFIG = __DIR__ . '/../bunq-test.conf'; + const FILE_PATH_AVATAR = '/resource/bunq_App_Icon_Square@4x.png'; + + /** + * Attachment constants. + */ + const ATTACHMENT_CONTENT_TYPE = 'image/png'; + const ATTACHMENT_DESCRIPTION = 'TEST PNG PHP'; + const ATTACHMENT_PATH_IN = '/bunq_App_Icon_Square@4x.png'; + + /** + * The index of the first item in an array. + */ + const INDEX_FIRST = 0; + + /** + * @var MonetaryAccountBank + */ + protected $secondMonetaryAccountBank; /** - * Device description for PHP unit tests + * @var CashRegister */ - const DEVICE_DESCRIPTION = 'PHP unit tests'; + protected $cashRegister; /** */ public static function setUpBeforeClass() { - static::ensureApiContextValid(); + static::createApiContext(); + BunqContext::loadApiContext( + ApiContext::restore(self::FILE_PATH_CONTEXT_CONFIG) + ); + } + + /** + */ + protected static function createApiContext() + { + InstallationUtil::automaticInstall( + BunqEnumApiEnvironmentType::SANDBOX(), + self::FILE_PATH_CONTEXT_CONFIG + ); + } + + /** + */ + protected function setUp() + { + $this->setSecondMonetaryAccountBank(); + } + + /** + */ + private function setCashRegister() + { + $attachmentUuid = AttachmentPublic::create( + FileUtil::getFileContents(__DIR__ . self::FILE_PATH_AVATAR), + [ + ApiClient::HEADER_CONTENT_TYPE => $this->getAttachmentContentType(), + ApiClient::HEADER_ATTACHMENT_DESCRIPTION => $this->getAttachmentDescription(), + ] + ); + $avatarUuid = Avatar::create($attachmentUuid->getValue()); + $cashRegisterId = CashRegister::create( + self::CASH_REGISTER_NAME, + self::CASH_REGISTER_STATUS, + $avatarUuid->getValue() + ); + + $this->cashRegister = CashRegister::get($cashRegisterId->getValue()); + } + + /** + * @return string + */ + protected function getAttachmentContentType(): string + { + return self::ATTACHMENT_CONTENT_TYPE; } /** - * Ensures the API context is still valid. + * @return string */ - protected static function ensureApiContextValid() + protected function getAttachmentDescription(): string { - try { - $apiContext = ApiContext::restore(static::FILENAME_CONTEXT_CONFIG); - $apiContext->ensureSessionActive(); - } catch (BunqException $exception) { - $apiContext = self::createApiContext(); + return self::ATTACHMENT_DESCRIPTION; + } + + /** + */ + private function setSecondMonetaryAccountBank() + { + $createdId = MonetaryAccountBank::create( + self::MOMENTARY_ACCOUNT_CURRENCY, + self::MONETARY_ACCOUNT_DESCRIPTION + ); + + $this->secondMonetaryAccountBank = MonetaryAccountBank::get($createdId->getValue())->getValue(); + } + + /** + * @return Pointer + * + * @throws BunqException + */ + protected function getSecondMonetaryAccountAlias(): Pointer + { + $allAlias = $this->secondMonetaryAccountBank->getAlias(); + + foreach ($allAlias as $alias) { + if ($alias->getType() === self::POINTER_TYPE_IBAN) { + return $alias; + } } - $apiContext->save(static::FILENAME_CONTEXT_CONFIG); + throw new BunqException(self::ERROR_COULD_NOT_DETERMINE_IBAN_POINTER); + } + + /** + * @return string + */ + protected function getAttachmentFilePath(): string + { + return self::ATTACHMENT_PATH_IN; + } - BunqContext::loadApiContext($apiContext); + /** + * @return Pointer + * + * @throws BunqException + */ + protected function getUserAlias(): Pointer + { + if (BunqContext::getUserContext()->isOnlyUserPersonSet()) { + return BunqContext::getUserContext()->getUserPerson()->getAlias()[self::INDEX_FIRST]; + } elseif (BunqContext::getUserContext()->isOnlyUserCompanySet()) { + return BunqContext::getUserContext()->getUserCompany()->getAlias()[self::INDEX_FIRST]; + } else { + throw new BunqException(self::ERROR_COULD_NOT_DETERMINE_USER_ALIAS); + } } /** - * @return ApiContext + * @return Pointer */ - protected static function createApiContext(): ApiContext + protected function getPointerUserBravo(): Pointer { - return ApiContext::create( - BunqEnumApiEnvironmentType::SANDBOX(), - Config::getApiKey(), - static::DEVICE_DESCRIPTION, - Config::getPermittedIps() + return new Pointer( + self::POINTER_TYPE_EMAIL, + self::EMAIL_BRAVO ); } + + /** + * @return int + */ + protected function getSecondMonetaryAccountId(): int + { + return $this->secondMonetaryAccountBank->getId(); + } + + /** + * @return int + */ + protected function getCashRegisterId(): int + { + if (is_null($this->cashRegister)) { + $this->setCashRegister(); + } + + return $this->cashRegister->getId(); + } + + /** + * @return bool + */ + protected function isMonetaryAccountBalanceSufficient(): bool + { + $balance = floatval(BunqContext::getUserContext()->getPrimaryMonetaryAccount()->getBalance()->getValue()); + + return $balance > self::MONETARY_ACCOUNT_BALANCE_THRESHOLD; + } + + /** + * @return bool + */ + protected function skipTestIfNeededDueToInsufficientBalance(): bool + { + if (!$this->isMonetaryAccountBalanceSufficient()) { + static::markTestSkipped(self::WARMING_TEST_SKIPPED_DUE_TO_INSUFFICIENT_BALANCE); + } + + return true; + } } diff --git a/tests/Http/PaginationScenarioTest.php b/tests/Http/PaginationScenarioTest.php index 01c7f4cb..ea3d7c33 100644 --- a/tests/Http/PaginationScenarioTest.php +++ b/tests/Http/PaginationScenarioTest.php @@ -5,9 +5,7 @@ use bunq\Http\Pagination; use bunq\Model\Generated\Endpoint\Payment; use bunq\Model\Generated\Object\Amount; -use bunq\Model\Generated\Object\Pointer; use bunq\test\BunqSdkTestBase; -use bunq\test\Config; /** * Tests: @@ -29,24 +27,11 @@ class PaginationScenarioTest extends BunqSdkTestBase const PAYMENT_CURRENCY = 'EUR'; const PAYMENT_DESCRIPTION = 'PHP test Payment'; - /** - * @var Pointer - */ - private static $counterPartyAliasOther; - - /** - */ - public static function setUpBeforeClass() - { - parent::setUpBeforeClass(); - static::$counterPartyAliasOther = Config::getCounterPartyAliasOther(); - } - /** */ public function testApiScenarioPaymentListingWithPagination() { - static::ensureEnoughPayments(); + $this->ensureEnoughPayments(); $paymentsExpected = static::getPaymentsRequired(); $paginationCountOnly = new Pagination(); $paginationCountOnly->setCount(self::PAYMENT_LISTING_PAGE_SIZE); @@ -59,15 +44,17 @@ public function testApiScenarioPaymentListingWithPagination() $paymentsActual = array_merge($responsePreviousNext->getValue(), $responsePrevious->getValue()); - static::assertEquals($paymentsExpected, $paymentsActual); + $this->assertEquals($paymentsExpected, $paymentsActual); } /** */ - private static function ensureEnoughPayments() + private function ensureEnoughPayments() { + $this->skipTestIfNeededDueToInsufficientBalance(); + for ($i = self::NUMBER_ZERO; $i < self::getPaymentsMissingCount(); ++$i) { - static::createPayment(); + $this->createPayment(); } } @@ -102,11 +89,11 @@ private static function listPayments(array $urlParams): BunqResponse /** */ - public static function createPayment() + public function createPayment() { Payment::create( new Amount(self::PAYMENT_AMOUNT_EUR, self::PAYMENT_CURRENCY), - static::$counterPartyAliasOther, + $this->getSecondMonetaryAccountAlias(), self::PAYMENT_DESCRIPTION ); } diff --git a/tests/Model/Generated/Endpoint/AttachmentPublicTest.php b/tests/Model/Generated/Endpoint/AttachmentPublicTest.php index 8f42ea36..5ce19b0d 100644 --- a/tests/Model/Generated/Endpoint/AttachmentPublicTest.php +++ b/tests/Model/Generated/Endpoint/AttachmentPublicTest.php @@ -20,31 +20,6 @@ class AttachmentPublicTest extends BunqSdkTestBase */ const PATH_ATTACHMENT = '/../../../Resource/'; - /** - * @var string - */ - private static $contentType; - - /** - * @var string - */ - private static $attachmentDescription; - - /** - * @var string - */ - private static $attachmentPathIn; - - /** - */ - public static function setUpBeforeClass() - { - parent::setUpBeforeClass(); - static::$contentType = Config::getAttachmentContentType(); - static::$attachmentDescription = Config::getAttachmentDescription(); - static::$attachmentPathIn = Config::getAttachmentPathIn(); - } - /** * Check if the file send is indeed the same file we receive. */ @@ -52,8 +27,8 @@ public function testCompareBeforeAndAfterBytes() { $beforeBytes = $this->getFileContentsOfAttachment(); $customHeadersMap = [ - ApiClient::HEADER_CONTENT_TYPE => static::$contentType, - ApiClient::HEADER_ATTACHMENT_DESCRIPTION => static::$attachmentDescription, + ApiClient::HEADER_CONTENT_TYPE => $this->getAttachmentContentType(), + ApiClient::HEADER_ATTACHMENT_DESCRIPTION => $this->getAttachmentDescription(), ]; $beforeUuid = AttachmentPublic::create($beforeBytes, $customHeadersMap)->getValue(); @@ -67,7 +42,7 @@ public function testCompareBeforeAndAfterBytes() */ private function getFileContentsOfAttachment(): string { - $path = __DIR__ . self::PATH_ATTACHMENT . static::$attachmentPathIn; + $path = __DIR__ . self::PATH_ATTACHMENT . $this->getAttachmentFilePath(); return FileUtil::getFileContents($path); } diff --git a/tests/Model/Generated/Endpoint/AvatarTest.php b/tests/Model/Generated/Endpoint/AvatarTest.php index f5e6311b..c35cd3b2 100644 --- a/tests/Model/Generated/Endpoint/AvatarTest.php +++ b/tests/Model/Generated/Endpoint/AvatarTest.php @@ -26,31 +26,6 @@ class AvatarTest extends BunqSdkTestBase */ const PATH_ATTACHMENT = '/../../../Resource/'; - /** - * @var string - */ - private static $contentType; - - /** - * @var string - */ - private static $attachmentDescription; - - /** - * @var string - */ - private static $attachmentPathIn; - - /** - */ - public static function setUpBeforeClass() - { - parent::setUpBeforeClass(); - static::$attachmentDescription = Config::getAttachmentDescription(); - static::$attachmentPathIn = Config::getAttachmentPathIn(); - static::$contentType = Config::getAttachmentContentType(); - } - /** * Tests the creation of a new avatar. */ @@ -58,8 +33,8 @@ public function testCreateAvatar() { $fileContentsBefore = $this->getFileContentsOfAttachment(); $customHeadersMap = [ - ApiClient::HEADER_ATTACHMENT_DESCRIPTION => static::$attachmentDescription, - ApiClient::HEADER_CONTENT_TYPE => static::$contentType, + ApiClient::HEADER_ATTACHMENT_DESCRIPTION => $this->getAttachmentDescription(), + ApiClient::HEADER_CONTENT_TYPE => $this->getAttachmentContentType(), ]; $attachmentUuidBefore = AttachmentPublic::create( @@ -82,7 +57,7 @@ public function testCreateAvatar() */ private function getFileContentsOfAttachment(): string { - $path = __DIR__ . self::PATH_ATTACHMENT . static::$attachmentPathIn; + $path = __DIR__ . self::PATH_ATTACHMENT . $this->getAttachmentFilePath(); return FileUtil::getFileContents($path); } diff --git a/tests/Model/Generated/Endpoint/CardDebitTest.php b/tests/Model/Generated/Endpoint/CardDebitTest.php index 64e871f4..eda5e02c 100644 --- a/tests/Model/Generated/Endpoint/CardDebitTest.php +++ b/tests/Model/Generated/Endpoint/CardDebitTest.php @@ -1,14 +1,12 @@ getValue(); static::$nameOnCard = $cardNamesAllowed[self::INDEX_FIRST]->getPossibleCardNameArray(); - $usersAccessible = User::listing()->getValue(); - static::$alias = $usersAccessible[self::INDEX_FIRST]->getUserCompany()->getAlias()[self::INDEX_FIRST]; - static::$monetaryAccountId = Config::getMonetaryAccountId(); } /** @@ -72,26 +52,24 @@ public static function setUpBeforeClass() */ public function testOrderingDebitCard() { - $alias = new Pointer(static::$alias->getType(), static::$alias->getValue()); - $cardAssigned = - new CardPinAssignment( - self::CARD_ASSIGNMENT_TYPE_PRIMARY, - self::CARD_PIN_CODE, - static::$monetaryAccountId - ); - $cardDebit = CardDebit::create( uniqid(self::CARD_SECOND_LINE_PREFIX), static::$nameOnCard[self::INDEX_FIRST], - $alias, + $this->getUserAlias(), null, - [new CardPinAssignment(self::CARD_PIN_CODE_ASSIGNMENT, self::CARD_PIN_CODE, Config::getMonetaryAccountId())] + [ + new CardPinAssignment( + self::CARD_PIN_CODE_ASSIGNMENT, + self::CARD_PIN_CODE, + BunqContext::getUserContext()->getPrimaryMonetaryAccount()->getId() + ), + ] )->getValue(); $card = Card::get($cardDebit->getId())->getValue(); - static::assertEquals($cardDebit->getNameOnCard(), $card->getNameOnCard()); - static::assertEquals($cardDebit->getCreated(), $card->getCreated()); - static::assertEquals($cardDebit->getSecondLine(), $card->getSecondLine()); + $this->assertEquals($cardDebit->getNameOnCard(), $card->getNameOnCard()); + $this->assertEquals($cardDebit->getCreated(), $card->getCreated()); + $this->assertEquals($cardDebit->getSecondLine(), $card->getSecondLine()); } } diff --git a/tests/Model/Generated/Endpoint/DraftShareInviteBankQrCodeContentTest.php b/tests/Model/Generated/Endpoint/DraftShareInviteBankQrCodeContentTest.php index 44086553..5ad29f06 100644 --- a/tests/Model/Generated/Endpoint/DraftShareInviteBankQrCodeContentTest.php +++ b/tests/Model/Generated/Endpoint/DraftShareInviteBankQrCodeContentTest.php @@ -56,6 +56,8 @@ public function testCreateDraftShareAndGetGr() $draftShareId = $this->createConnect(); $qrContent = $this->getQrContent($draftShareId); file_put_contents(self::PATH_QR_OUTPUT, $qrContent); + + $this->assertTrue(file_exists(self::PATH_QR_OUTPUT)); } /** diff --git a/tests/Model/Generated/Endpoint/MonetaryAccountBankTest.php b/tests/Model/Generated/Endpoint/MonetaryAccountBankTest.php index 045105bd..131f7edc 100644 --- a/tests/Model/Generated/Endpoint/MonetaryAccountBankTest.php +++ b/tests/Model/Generated/Endpoint/MonetaryAccountBankTest.php @@ -30,13 +30,6 @@ class MonetaryAccountBankTest extends BunqSdkTestBase */ private static $monetaryAccountBankToCloseId; - /** - */ - public static function setUpBeforeClass() - { - parent::setUpBeforeClass(); - } - /** * Deletes the new created monetary account. */ @@ -67,5 +60,7 @@ public function testCreateNewMonetaryAccount() self::CURRENCY, uniqid(self::PREFIX_MONETARY_ACCOUNT_DESCRIPTION) )->getValue(); + + $this->assertTrue(is_integer(static::$monetaryAccountBankToCloseId)); } } diff --git a/tests/Model/Generated/Endpoint/PaymentTest.php b/tests/Model/Generated/Endpoint/PaymentTest.php index b30bc1c9..2d7a6255 100644 --- a/tests/Model/Generated/Endpoint/PaymentTest.php +++ b/tests/Model/Generated/Endpoint/PaymentTest.php @@ -8,6 +8,7 @@ use bunq\Model\Generated\Object\Pointer; use bunq\test\BunqSdkTestBase; use bunq\test\Config; +use phpDocumentor\Reflection\Types\This; /** * Tests: @@ -40,32 +41,7 @@ class PaymentTest extends BunqSdkTestBase /** * @var int */ - private static $monetaryAccountId; - - /** - * @var Pointer - */ - private static $counterPartyAliasOther; - - /** - * @var Pointer - */ - private static $counterPartyAliasSelf; - - /** - * @var int - */ - private static $paymentId; - - /** - */ - public static function setUpBeforeClass() - { - parent::setUpBeforeClass(); - static::$monetaryAccountId = Config::getMonetaryAccountId(); - static::$counterPartyAliasOther = Config::getCounterPartyAliasOther(); - static::$counterPartyAliasSelf = Config::getCounterPartyAliasSelf(); - } + private $paymentId; /** * Test sending money to other sandbox user. @@ -74,9 +50,11 @@ public static function setUpBeforeClass() */ public function testSendMoneyToOtherUser() { + $this->skipTestIfNeededDueToInsufficientBalance(); + Payment::create( new Amount(self::PAYMENT_AMOUNT_IN_EUR, self::PAYMENT_CURRENCY), - static::$counterPartyAliasOther, + $this->getPointerUserBravo(), self::PAYMENT_DESCRIPTION ); } @@ -88,9 +66,11 @@ public function testSendMoneyToOtherUser() */ public function testSendMoneyToOtherMonetaryAccount() { - static::$paymentId = Payment::create( + $this->skipTestIfNeededDueToInsufficientBalance(); + + $this->paymentId = Payment::create( new Amount(self::PAYMENT_AMOUNT_IN_EUR, self::PAYMENT_CURRENCY), - static::$counterPartyAliasSelf, + $this->getSecondMonetaryAccountAlias(), self::PAYMENT_DESCRIPTION )->getValue(); } @@ -105,7 +85,7 @@ public function testSendMoneyToOtherMonetaryAccount() public function testSendMessageToPayment() { $chatId = PaymentChat::create( - static::$paymentId + $this->paymentId )->getValue(); ChatMessageText::create($chatId, self::PAYMENT_CHAT_TEXT_MESSAGE); diff --git a/tests/Model/Generated/Endpoint/RequestInquiryTest.php b/tests/Model/Generated/Endpoint/RequestInquiryTest.php index 2fd65139..dda3438e 100644 --- a/tests/Model/Generated/Endpoint/RequestInquiryTest.php +++ b/tests/Model/Generated/Endpoint/RequestInquiryTest.php @@ -34,26 +34,6 @@ class RequestInquiryTest extends BunqSdkTestBase */ const INDEX_FIRST = 0; - /** - * @var int - */ - private static $monetaryAccountId2; - - /** - * @var string - */ - private static $counterPartyAliasSelf; - - - /** - */ - public static function setUpBeforeClass() - { - parent::setUpBeforeClass(); - static::$monetaryAccountId2 = Config::getMonetaryAccountId2(); - static::$counterPartyAliasSelf = Config::getCounterPartyAliasSelf(); - } - /** * Send a request from monetary account 1 to monetary account 2 and accept it. * @@ -61,8 +41,10 @@ public static function setUpBeforeClass() */ public function testSendingAndAcceptingRequest() { + $this->skipTestIfNeededDueToInsufficientBalance(); + $this->sendRequest(); - $responses = RequestResponse::listing(static::$monetaryAccountId2)->getValue(); + $responses = RequestResponse::listing($this->getSecondMonetaryAccountId())->getValue(); $requestResponseId = $responses[self::INDEX_FIRST]->getId(); $this->acceptRequest($requestResponseId); } @@ -73,7 +55,7 @@ private function sendRequest() { RequestInquiry::create( new Amount(self::REQUEST_AMOUNT_IN_EUR, self::REQUEST_CURRENCY), - static::$counterPartyAliasSelf, + $this->getSecondMonetaryAccountAlias(), self::REQUEST_DESCRIPTION, false ); @@ -86,7 +68,7 @@ private function acceptRequest(int $requestResponseId) { RequestResponse::update( $requestResponseId, - static::$monetaryAccountId2, + $this->getSecondMonetaryAccountId(), null, self::REQUEST_STATUS_ACCEPTED ); diff --git a/tests/Model/Generated/Endpoint/SessionTest.php b/tests/Model/Generated/Endpoint/SessionTest.php index cf2ad090..f3c1f04a 100644 --- a/tests/Model/Generated/Endpoint/SessionTest.php +++ b/tests/Model/Generated/Endpoint/SessionTest.php @@ -19,7 +19,7 @@ class SessionTest extends BunqSdkTestBase /** * Full name of context config file to use for testing. */ - const FILENAME_CONTEXT_CONFIG = __DIR__ . '/../bunq-test.conf'; + const FILE_PATH_CONTEXT_CONFIG = __DIR__ . '/../bunq-test.conf'; /** * The amount of secconds to sleep to prevent the api from returning a 429. @@ -33,7 +33,7 @@ class SessionTest extends BunqSdkTestBase */ public function testDeleteSession() { - Session::delete(self::SESSION_ID_DUMMY); + $this->assertNull(Session::delete(self::SESSION_ID_DUMMY)->getValue()); } /** @@ -43,6 +43,6 @@ public static function tearDownAfterClass() { sleep(self::SECONDS_TO_SLEEP); BunqContext::getApiContext()->resetSession(); - BunqContext::getApiContext()->save(self::FILENAME_CONTEXT_CONFIG); + BunqContext::getApiContext()->save(self::FILE_PATH_CONTEXT_CONFIG); } } diff --git a/tests/Model/Generated/Endpoint/TabUsageSingleTest.php b/tests/Model/Generated/Endpoint/TabUsageSingleTest.php index 46dbf09d..67acd20f 100644 --- a/tests/Model/Generated/Endpoint/TabUsageSingleTest.php +++ b/tests/Model/Generated/Endpoint/TabUsageSingleTest.php @@ -1,11 +1,11 @@ isOnlyUserPersonSet()) { + static::markTestSkipped(self::ERROR_ONLY_USER_COMPANY_CAN_CREATE_TABS); + } + static::$tabUuid = TabUsageSingle::create( - static::$cashRegisterId, + $this->getCashRegisterId(), self::TAB_DESCRIPTION, self::TAB_STATUS_BEFORE, new Amount(self::AMOUNT_IN_EUR, self::TAB_CURRENCY) @@ -84,7 +69,7 @@ public function testCreateTab() public function testAddItemToTab() { TabItemShop::create( - static::$cashRegisterId, + $this->getCashRegisterId(), static::$tabUuid, self::ITEM_DESCRIPTION, null, @@ -104,7 +89,7 @@ public function testAddItemToTab() public function testUpdateTab() { TabUsageSingle::update( - static::$cashRegisterId, + $this->getCashRegisterId(), static::$tabUuid, null, self::TAB_STATUS_AFTER