Skip to content

DX-1855 added php tests #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/actions/validate/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/sh

composer -h
composer require
./vendor/bin/phpunit tests
12 changes: 12 additions & 0 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,15 @@ jobs:
uses: actions/checkout@v2
- name: Validate
uses: ./.github/actions/validate
env:
USERNAME: ${{ secrets.USERNAME }}
PASSWORD: ${{ secrets.PASSWORD }}
ACCOUNT_ID: ${{ secrets.ACCOUNT_ID }}
VOICE_APPLICATION_ID: ${{ secrets.VOICE_APPLICATION_ID }}
MESSAGING_APPLICATION_ID: ${{ secrets.MESSAGING_APPLICATION_ID }}
VOICE_CALLBACK_URL: ${{ secrets.VOICE_CALLBACK_URL }}
PHONE_NUMBER_OUTBOUND: ${{ secrets.PHONE_NUMBER_OUTBOUND }}
PHONE_NUMBER_INBOUND: ${{ secrets.PHONE_NUMBER_INBOUND }}
MFA_MESSAGING_APPLICATION_ID: ${{ secrets.MFA_MESSAGING_APPLICATION_ID }}
MFA_VOICE_APPLICATION_ID: ${{ secrets.MFA_VOICE_APPLICATION_ID }}
PHONE_NUMBER_MFA: ${{ secrets.PHONE_NUMBER_MFA }}
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,11 @@
"apimatic/jsonmapper": "~1.3.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "^2.7",
"phan/phan": "^1.2",
"phpunit/phpunit": "4.8.*"
"phpunit/phpunit": "^9"
},
"autoload": {
"psr-4": {
"BandwidthLib\\": "src/"
}
}
}
}
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
Expand Down
144 changes: 144 additions & 0 deletions tests/ApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

/**
* ApiTest.php
*
* A simple php integration test class for API requests and responses
*
* @copyright Bandwidth INC
*/

use PHPUnit\Framework\TestCase;

final class ApiTest extends TestCase
{
protected $bandwidthClient;

protected function setUp(): void {
$config = new BandwidthLib\Configuration(
array(
'messagingBasicAuthUserName' => getenv("USERNAME"),
'messagingBasicAuthPassword' => getenv("PASSWORD"),
'voiceBasicAuthUserName' => getenv("USERNAME"),
'voiceBasicAuthPassword' => getenv("PASSWORD"),
'twoFactorAuthBasicAuthUserName' => getenv("USERNAME"),
'twoFactorAuthBasicAuthPassword' => getenv("PASSWORD")
)
);
$this->bandwidthClient = new BandwidthLib\BandwidthClient($config);
}

public function testCreateMessage() {
$body = new BandwidthLib\Messaging\Models\MessageRequest();
$body->from = getenv("PHONE_NUMBER_INBOUND");
$body->to = [getenv("PHONE_NUMBER_OUTBOUND")];
$body->applicationId = getenv("MESSAGING_APPLICATION_ID");
$body->text = "PHP Monitoring";

$response = $this->bandwidthClient->getMessaging()->getClient()->createMessage(getenv("ACCOUNT_ID"), $body);

$this->assertTrue(strlen($response->getResult()->id) > 0); //validate that _some_ id was returned
}

public function testCreateMessageInvalidPhoneNumber() {
$body = new BandwidthLib\Messaging\Models\MessageRequest();
$body->from = getenv("PHONE_NUMBER_INBOUND");
$body->to = ["+1invalid"];
$body->applicationId = getenv("MESSAGING_APPLICATION_ID");
$body->text = "PHP Monitoring";

try {
$this->bandwidthClient->getMessaging()->getClient()->createMessage(getenv("ACCOUNT_ID"), $body);
//workaround to make sure that if the above error is not raised, the build will fail
$this->assertTrue(false);
} catch (BandwidthLib\Messaging\Exceptions\MessagingException $e) {
$this->assertTrue(strlen($e->description) > 0);
}
}

public function testUploadDownloadMedia() {
//constants

$mediaFileName = "php_monitoring";
$mediaFile = "12345"; //todo: confirm binary string?
//media upload
$this->bandwidthClient->getMessaging()->getClient()->uploadMedia(getenv("ACCOUNT_ID"), $mediaFileName, strlen($mediaFile), $mediaFile);

//media download
$downloadedMediaFile = $this->bandwidthClient->getMessaging()->getClient()->getMedia(getenv("ACCOUNT_ID"), $mediaFileName)->getResult();

//validate that response is the same
$this->assertEquals($downloadedMediaFile, $mediaFile);
}

public function testCreateCallAndGetCallState() {
$body = new BandwidthLib\Voice\Models\ApiCreateCallRequest();
$body->from = getenv("PHONE_NUMBER_INBOUND");
$body->to = getenv("PHONE_NUMBER_OUTBOUND");
$body->applicationId = getenv("VOICE_APPLICATION_ID");
$body->answerUrl = getenv("VOICE_CALLBACK_URL");
$response = $this->bandwidthClient->getVoice()->getClient()->createCall(getenv("ACCOUNT_ID"), $body);
$callId = $response->getResult()->callId;
$this->assertTrue(strlen($callId) > 0);

//get phone call information
$response = $this->bandwidthClient->getVoice()->getClient()->getCallState(getenv("ACCOUNT_ID"), $callId);
$this->assertTrue(strlen($response->getResult()->state) > 0);
}

public function testCreateCallInvalidPhoneNumber() {
$body = new BandwidthLib\Voice\Models\ApiCreateCallRequest();
$body->from = getenv("PHONE_NUMBER_INBOUND");
$body->to = "+1invalid";
$body->applicationId = getenv("VOICE_APPLICATION_ID");
$body->answerUrl = getenv("VOICE_CALLBACK_URL");

try {
$this->bandwidthClient->getVoice()->getClient()->createCall(getenv("ACCOUNT_ID"), $body);
//workaround to make sure that if the above error is not raised, the build will fail
$this->assertTrue(false);
} catch (BandwidthLib\Voice\Exceptions\ApiErrorResponseException $e) {
$this->assertTrue(strlen($e->description) > 0);
}
}

public function testMfaMessaging() {
$body = new BandwidthLib\TwoFactorAuth\Models\TwoFactorCodeRequestSchema();
$body->from = getenv("PHONE_NUMBER_MFA");
$body->to = getenv("PHONE_NUMBER_INBOUND");
$body->applicationId = getenv("MFA_MESSAGING_APPLICATION_ID");
$body->scope = "scope";
$body->digits = 6;
$body->message = "Your temporary {NAME} {SCOPE} code is {CODE}";

$response = $this->bandwidthClient->getTwoFactorAuth()->getMFA()->createMessagingTwoFactor(getenv("ACCOUNT_ID"), $body);
$this->assertTrue(strlen($response->getResult()->messageId) > 0); //validate that _some_ id was returned
}

public function testMfaVoice() {
$body = new BandwidthLib\TwoFactorAuth\Models\TwoFactorCodeRequestSchema();
$body->from = getenv("PHONE_NUMBER_MFA");
$body->to = getenv("PHONE_NUMBER_INBOUND");
$body->applicationId = getenv("MFA_VOICE_APPLICATION_ID");
$body->scope = "scope";
$body->digits = 6;
$body->message = "Your temporary {NAME} {SCOPE} code is {CODE}";

$response = $this->bandwidthClient->getTwoFactorAuth()->getMFA()->createVoiceTwoFactor(getenv("ACCOUNT_ID"), $body);
$this->assertTrue(strlen($response->getResult()->callId) > 0); //validate that _some_ id was returned
}

public function testMfaVerify() {
$body = new BandwidthLib\TwoFactorAuth\Models\TwoFactorVerifyRequestSchema();
$body->from = getenv("PHONE_NUMBER_MFA");
$body->to = getenv("PHONE_NUMBER_INBOUND");
$body->applicationId = getenv("MFA_VOICE_APPLICATION_ID");
$body->scope = "scope";
$body->code = "123456";
$body->digits = 6;
$body->expirationTimeInMinutes = 3;

$response = $this->bandwidthClient->getTwoFactorAuth()->getMFA()->createVerifyTwoFactor(getenv("ACCOUNT_ID"), $body);
$this->assertTrue(is_bool($response->getResult()->valid));
}
}
Loading