diff --git a/.gitignore b/.gitignore index adec12e..ac48d21 100644 --- a/.gitignore +++ b/.gitignore @@ -4,5 +4,6 @@ vendor/ composer.phar +phpunit.phar .idea/ .DS_Store diff --git a/Apruve.php b/Apruve.php index 2d27d5f..3c28589 100644 --- a/Apruve.php +++ b/Apruve.php @@ -1,8 +1,7 @@ =5.3.0" - }, - "autoload": { - "psr-0" : { - "Apruve" : "src" - } + "name": "apruve/apruve-php", + "description": "Apruve PHP Library to support apruve.js.", + "require-dev": { + "phpunit/phpunit": "4.1.0" + }, + "license": "MIT", + "authors": [ + { + "name": "Apruve", + "email": "support@apruve.com" } + ], + "require": { + "php": ">=5.3.0" + }, + "autoload": { + "psr-0": { + "Apruve": "src" + } + } } diff --git a/src/Apruve/ApruveObject.php b/src/Apruve/ApruveObject.php index d3ca46d..9b98758 100644 --- a/src/Apruve/ApruveObject.php +++ b/src/Apruve/ApruveObject.php @@ -4,59 +4,47 @@ abstract class ApruveObject { - protected $client; - - public function __construct($values=[], $client=null) - { - if ($client == null) - { - $this->client = new Client(); - } - else - { - $this->client = $client; - } - foreach ($values as $name => $value) - { - $this->$name = $value; - } - } - - public function toHashString() - { - $ret = ''; - $called_class = get_called_class(); - foreach ($called_class::$hash_order as $key) - { - $ret .= $this->$key; - } - return $ret; - } - - public function toJsonArray() - { - $jsonArr = []; - $called_class = get_called_class(); - foreach ($called_class::$json_fields as $key) - { - if (gettype($this->$key) == "array") - { - $jsonArr[$key] = []; - foreach($this->$key as $item) - { - array_push($jsonArr[$key], $item->toJsonArray()); - } - } - else - { - $jsonArr[$key] = $this->$key; - } - } - return $jsonArr; - } - - public function toJson() - { - return json_encode($this->toJsonArray()); - } + protected $client; + + public function __construct( $values = [], $client = null ) { + if ( $client == null ) { + $this->client = new Client(); + } else { + $this->client = $client; + } + foreach ( $values as $name => $value ) { + $this->$name = $value; + } + } + + public function toHashString() { + $ret = ''; + $called_class = get_called_class(); + foreach ( $called_class::$hash_order as $key ) { + $ret .= $this->$key; + } + + return $ret; + } + + public function toJson() { + return json_encode( $this->toJsonArray() ); + } + + public function toJsonArray() { + $jsonArr = []; + $called_class = get_called_class(); + foreach ( $called_class::$json_fields as $key ) { + if ( gettype( $this->$key ) == "array" ) { + $jsonArr[ $key ] = []; + foreach ( $this->$key as $item ) { + array_push( $jsonArr[ $key ], $item->toJsonArray() ); + } + } else { + $jsonArr[ $key ] = $this->$key; + } + } + + return $jsonArr; + } } diff --git a/src/Apruve/Client.php b/src/Apruve/Client.php index 6aae7e9..4f1e8d1 100644 --- a/src/Apruve/Client.php +++ b/src/Apruve/Client.php @@ -4,134 +4,121 @@ require_once 'CurlRequest.php'; -class ClientStorage -{ - public static $apiKey; - public static $env; - - public static function setEnvironment(Environment $env) - { - static::$env = $env; - } - - public static function setApiKey($apiKey) - { - static::$apiKey = $apiKey; - } +class ClientStorage { + public static $apiKey; + public static $env; + + public static function setEnvironment( Environment $env ) { + static::$env = $env; + } + + public static function setApiKey( $apiKey ) { + static::$apiKey = $apiKey; + } } -class Client -{ - private $apiKey; - private $env; - - public function __construct($apiKey='', Environment $env=null) - { - if(empty($apiKey) or empty($env)) - { - if (empty(ClientStorage::$apiKey) or empty(ClientStorage::$env)) - { - throw new \InvalidArgumentException('Client must be initialized with init() first!'); - } - else - { - $apiKey = ClientStorage::$apiKey; - $env = ClientStorage::$env; - } - } - $this->apiKey = $apiKey; - $this->env = $env; - } - - static function init($ApiKey, Environment $env) - { - if ($env->getBaseUrl() != Environment::PROD and - $env->getBaseUrl() != Environment::TEST and - $env->getBaseUrl() != Environment::DEV) - { - throw new \InvalidArgumentException - ('$env must be Apruve\Environment::PROD or Apruve\Environment::TEST to be valid.'); - - } - ClientStorage::setEnvironment($env); - ClientStorage::setApiKey($ApiKey); - return new Client(); - } - - function getApiKey() - { - return $this->apiKey; - } - - function setApiKey($ApiKey) - { - $this->ApiKey = $ApiKey; - } - - function getEnvironment() - { - return $this->env; - } - - protected function initCurl($url) - { - return new CurlRequest($url); - } - - protected function restRequest($path) - { - $client = $this->initCurl($this->env->getApiUrl().$path); - $client->setOption(CURLOPT_HTTPHEADER, [ - 'Content-Type: application/json', - "Apruve-Api-Key: $this->apiKey", - ]); - return $client; - } - - public function post($path, $payload) - { - $client = $this->restRequest($path); - $client->setOption(CURLOPT_POST, true); - $client->setOption(CURLOPT_POSTFIELDS, $payload); - $client->setOption(CURLOPT_RETURNTRANSFER, true); - $response = $client->execute(); - $ret = [$client->getInfo(CURLINFO_HTTP_CODE), json_decode($response, true), $client->error()]; - $client->close(); - return $ret; - } - - public function get($path) - { - $client = $this->restRequest($path); - $client->setOption(CURLOPT_RETURNTRANSFER, true); - $response = $client->execute(); - $ret = [$client->getInfo(CURLINFO_HTTP_CODE), json_decode($response, true), $client->error()]; - $client->close(); - return $ret; - } - - public function delete($path) - { - $client = $this->restRequest($path); - $client->setOption(CURLOPT_CUSTOMREQUEST, 'DELETE'); - $client->setOption(CURLOPT_RETURNTRANSFER, true); - $response = $client->execute(); - $ret = [$client->getInfo(CURLINFO_HTTP_CODE), json_decode($response, true), $client->error()]; - $client->close(); - return $ret; - } - - public function put($path, $payload) - { - $client = $this->restRequest($path); - $client->setOption(CURLOPT_CUSTOMREQUEST, 'PUT'); - $client->setOption(CURLOPT_POSTFIELDS, $payload); - $client->setOption(CURLOPT_RETURNTRANSFER, true); - $response = $client->execute(); - $ret = [$client->getInfo(CURLINFO_HTTP_CODE), json_decode($response, true), $client->error()]; - $client->close(); - return $ret; - } +class Client { + private $apiKey; + private $env; + + public function __construct( $apiKey = '', Environment $env = null ) { + if ( empty( $apiKey ) or empty( $env ) ) { + if ( empty( ClientStorage::$apiKey ) or empty( ClientStorage::$env ) ) { + throw new \InvalidArgumentException( 'Client must be initialized with init() first!' ); + } else { + $apiKey = ClientStorage::$apiKey; + $env = ClientStorage::$env; + } + } + $this->apiKey = $apiKey; + $this->env = $env; + } + + static function init( $ApiKey, Environment $env ) { + if ( $env->getBaseUrl() != Environment::PROD and + $env->getBaseUrl() != Environment::TEST and + $env->getBaseUrl() != Environment::DEV + ) { + throw new \InvalidArgumentException + ( '$env must be Apruve\Environment::PROD or Apruve\Environment::TEST to be valid.' ); + + } + ClientStorage::setEnvironment( $env ); + ClientStorage::setApiKey( $ApiKey ); + + return new Client(); + } + + function getApiKey() { + return $this->apiKey; + } + + function setApiKey( $ApiKey ) { + $this->ApiKey = $ApiKey; + } + + function getEnvironment() { + return $this->env; + } + + public function post( $path, $payload ) { + $client = $this->restRequest( $path ); + $client->setOption( CURLOPT_POST, true ); + $client->setOption( CURLOPT_POSTFIELDS, $payload ); + $client->setOption( CURLOPT_RETURNTRANSFER, true ); + $response = $client->execute(); + $ret = [ $client->getInfo( CURLINFO_HTTP_CODE ), json_decode( $response, true ), $client->error() ]; + $client->close(); + + return $ret; + } + + protected function restRequest( $path ) { + $client = $this->initCurl( $this->env->getApiUrl() . $path ); + $client->setOption( CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + "Apruve-Api-Key: $this->apiKey", + ] ); + + return $client; + } + + protected function initCurl( $url ) { + return new CurlRequest( $url ); + } + + public function get( $path ) { + $client = $this->restRequest( $path ); + $client->setOption( CURLOPT_RETURNTRANSFER, true ); + $response = $client->execute(); + $ret = [ $client->getInfo( CURLINFO_HTTP_CODE ), json_decode( $response, true ), $client->error() ]; + $client->close(); + + return $ret; + } + + public function delete( $path ) { + $client = $this->restRequest( $path ); + $client->setOption( CURLOPT_CUSTOMREQUEST, 'DELETE' ); + $client->setOption( CURLOPT_RETURNTRANSFER, true ); + $response = $client->execute(); + $ret = [ $client->getInfo( CURLINFO_HTTP_CODE ), json_decode( $response, true ), $client->error() ]; + $client->close(); + + return $ret; + } + + public function put( $path, $payload ) { + $client = $this->restRequest( $path ); + $client->setOption( CURLOPT_CUSTOMREQUEST, 'PUT' ); + $client->setOption( CURLOPT_POSTFIELDS, $payload ); + $client->setOption( CURLOPT_RETURNTRANSFER, true ); + $response = $client->execute(); + $ret = [ $client->getInfo( CURLINFO_HTTP_CODE ), json_decode( $response, true ), $client->error() ]; + $client->close(); + + return $ret; + } } diff --git a/src/Apruve/CurlRequest.php b/src/Apruve/CurlRequest.php index 567f3ba..53562ab 100644 --- a/src/Apruve/CurlRequest.php +++ b/src/Apruve/CurlRequest.php @@ -2,45 +2,40 @@ namespace Apruve; -interface HttpRequest -{ - public function setOption($name, $value); - public function execute(); - public function getInfo($name); - public function close(); +interface HttpRequest { + public function setOption( $name, $value ); + + public function execute(); + + public function getInfo( $name ); + + public function close(); } -class CurlRequest implements HttpRequest -{ - private $ch = null; - - public function __construct($url) - { - $this->ch = curl_init($url); - } - - public function setOption($name, $value) - { - curl_setopt($this->ch, $name, $value); - } - - public function execute() - { - return curl_exec($this->ch); - } - - public function getInfo($name) - { - return curl_getinfo($this->ch, $name); - } - - public function error() - { - return curl_error($this->ch); - } - - public function close() - { - curl_close($this->ch); - } +class CurlRequest implements HttpRequest { + private $ch = null; + + public function __construct( $url ) { + $this->ch = curl_init( $url ); + } + + public function setOption( $name, $value ) { + curl_setopt( $this->ch, $name, $value ); + } + + public function execute() { + return curl_exec( $this->ch ); + } + + public function getInfo( $name ) { + return curl_getinfo( $this->ch, $name ); + } + + public function error() { + return curl_error( $this->ch ); + } + + public function close() { + curl_close( $this->ch ); + } } diff --git a/src/Apruve/Environment.php b/src/Apruve/Environment.php index 43258d9..6d0d646 100644 --- a/src/Apruve/Environment.php +++ b/src/Apruve/Environment.php @@ -2,48 +2,40 @@ namespace Apruve; -class Environment -{ - const PROD = 'https://app.apruve.com'; - const TEST = 'https://test.apruve.com'; - const DEV = 'http://localhost:3000'; - - private $baseUrl; - - private function __construct($environment) - { - $this->baseUrl = $environment; - } - - public static function PROD() - { - return new self(self::PROD); - } - - public static function TEST() - { - return new self(self::TEST); - } - - public static function DEV() - { - return new self(self::DEV); - } - - public function getJsUrl() - { - return $this->baseUrl.'/js/apruve.js'; - } - - public function getApiUrl() - { - return $this->baseUrl.'/api/v3'; - } - - public function getBaseUrl() - { - return $this->baseUrl; - } +class Environment { + const PROD = 'https://app.apruve.com'; + const TEST = 'https://test.apruve.com'; + const DEV = 'http://localhost:3000'; + + private $baseUrl; + + private function __construct( $environment ) { + $this->baseUrl = $environment; + } + + public static function PROD() { + return new self( self::PROD ); + } + + public static function TEST() { + return new self( self::TEST ); + } + + public static function DEV() { + return new self( self::DEV ); + } + + public function getJsUrl() { + return $this->baseUrl . '/js/apruve.js'; + } + + public function getApiUrl() { + return $this->baseUrl . '/api/v4'; + } + + public function getBaseUrl() { + return $this->baseUrl; + } } diff --git a/src/Apruve/Invoice.php b/src/Apruve/Invoice.php new file mode 100644 index 0000000..ce51d1f --- /dev/null +++ b/src/Apruve/Invoice.php @@ -0,0 +1,66 @@ + $invoice_item ) { + if ( is_array( $invoice_item ) ) { + $invoice['invoice_items'][ $key ] = new InvoiceItem( $invoice_item ); + } + } + } + + parent::__construct( $invoice, $client ); + } + + public static function get( $order_id, $invoice_id, $client = null ) { + if ( $client == null ) { + $client = new Client(); + } + $response = $client->get( sprintf( self::$INVOICES_PATH, $order_id ) . $invoice_id ); + + if ( $response[0] == 200 ) { + return new self( $response[1], $client ); + } else { + return $response[2]; + } + } + + public function save() { + $response = $this->client->post( + sprintf( + self::$INVOICES_PATH, $this->order_id ), $this->toJson() ); + if ( $response[0] == 201 ) { + return new self( $response[1], $this->client ); + } else { + return $response[2]; + } + } + +} diff --git a/src/Apruve/InvoiceItem.php b/src/Apruve/InvoiceItem.php new file mode 100644 index 0000000..4efbef3 --- /dev/null +++ b/src/Apruve/InvoiceItem.php @@ -0,0 +1,31 @@ +currency = "USD"; + $this->reason = "OTHER"; + parent::__construct( $values, $client ); + } + + public static function get( $invoice_id, $invoice_return_id, $client = null ) { + if ( $client == null ) { + $client = new Client(); + } + $response = $client->get( sprintf( self::$REFUND_PATH, $invoice_id ) . $invoice_return_id ); + + if ( $response[0] == 200 ) { + return new self( $response[1], $client ); + } else { + return $response[2]; + } + } + + public function save() { + $response = $this->client->post( + sprintf( + self::$REFUND_PATH, $this->invoice_id ), $this->toJson() ); + if ( $response[0] == 201 ) { + return new self( $response[1], $this->client ); + } else { + return $response[2]; + } + } + +} diff --git a/src/Apruve/LineItem.php b/src/Apruve/LineItem.php deleted file mode 100644 index 675d98b..0000000 --- a/src/Apruve/LineItem.php +++ /dev/null @@ -1,51 +0,0 @@ - $order_item ) { + if ( is_array( $order_item ) ) { + $order['order_items'][ $key ] = new OrderItem( $order_item ); + } + } + } + parent::__construct( $order, $client ); + } + + public static function get( $order_id, $client = null ) { + if ( $client == null ) { + $client = new Client(); + } + $response = $client->get( self::$ORDERS_PATH . $order_id ); + + if ( $response[0] == 200 ) { + $object = new self( $response[1], $client ); + + return $object; + } else { + return $response[2]; + } + } + + public static function getInvoices( $apruveOrderId ) { + $client = new Client(); + $response = $client->get( sprintf( __( self::$INVOICES_PATH ), $apruveOrderId ) ); + + return $response[1]; + } + + public static function cancel( $apruveOrderId ) { + $client = new Client(); + echo sprintf( __( self::$CANCEL_PATH, $apruveOrderId ) ); + $response = $client->post( sprintf( __( self::$CANCEL_PATH ), $apruveOrderId ), '' ); + + return $response[0] == 200; + } + + public function setShippingCents( $shipping_cents ) { + $this->shipping_cents = $shipping_cents; + } + + public function setTaxCents( $tax_cents ) { + $this->tax_cents = $tax_cents; + } + + public function setAmountCents( $amount_cents ) { + $this->amount_cents = $amount_cents; + } + + public function setMerchantOrderId( $merchant_order_id ) { + $this->merchant_order_id = $merchant_order_id; + } + + public function setExpireAt( $expire_at ) { + $this->expire_at = $expire_at; + } + + public function toSecureHash() { + $apiKey = $this->client->getApiKey(); + + return hash( "sha256", $apiKey . $this->toSecureString() ); + } + + public function toSecureString() { + $hashString = $this->toHashString(); + foreach ( $this->order_items as $order_item ) { + $hashString .= $order_item->toHashString(); + } + + return $hashString; + } + + public function addOrderItem( $order_item ) { + if ( get_class( $order_item ) == 'Apruve\OrderItem' ) { + return array_push( $this->order_items, $order_item ); + } else { + return false; + } + } + + public function update() { + $response = $this->client->put( sprintf( self::$UPDATE_PATH, $this->id ), + $this->toJson() ); + + return $response[0] == 200; + } + +} diff --git a/src/Apruve/OrderItem.php b/src/Apruve/OrderItem.php new file mode 100644 index 0000000..8203668 --- /dev/null +++ b/src/Apruve/OrderItem.php @@ -0,0 +1,48 @@ + $payment_item) - { - if (is_array($payment_item)) - { - $payment['payment_items'][$key] = new PaymentItem($payment_item); - } - } - } - - parent::__construct($payment, $client); - } - - public static function get($payment_request_id, $payment_id, $client=null) - { - if ($client == null) - { - $client = new Client(); - } - $response = $client->get(sprintf(self::$PAYMENTS_PATH, $payment_request_id).$payment_id); - - if ($response[0] == 200) - { - return new self($response[1], $client); - } - else - { - return $response[2]; - } - } - - public function save() - { - $response = $this->client->post( - sprintf( - self::$PAYMENTS_PATH, $this->payment_request_id), $this->toJson()); - if ($response[0] == 201) - { - return new self($response[1], $this->client); - } - else - { - return $response[2]; - } - } - -} diff --git a/src/Apruve/PaymentItem.php b/src/Apruve/PaymentItem.php deleted file mode 100644 index 8c3f9d8..0000000 --- a/src/Apruve/PaymentItem.php +++ /dev/null @@ -1,32 +0,0 @@ - $line_item) - { - if (is_array($line_item)) - { - $payment_request['line_items'][$key] = new LineItem($line_item); - } - } - } - - parent::__construct($payment_request, $client); - } - - public function setShippingCents($shipping_cents) - { - $this->shipping_cents = $shipping_cents; - } - - public function setTaxCents($tax_cents) - { - $this->tax_cents = $tax_cents; - } - - public function setAmountCents($amount_cents) - { - $this->amount_cents = $amount_cents; - } - - public function setMerchantOrderId($merchant_order_id) - { - $this->merchant_order_id = $merchant_order_id; - } - - public function setExpireAt($expire_at) - { - $this->expire_at = $expire_at; - } - - public function toSecureString() - { - $hashString = $this->toHashString(); - foreach($this->line_items as $line_item) - { - $hashString .= $line_item->toHashString(); - } - return $hashString; - } - - - public function toSecureHash() - { - $apiKey = $this->client->getApiKey(); - return hash("sha256", $apiKey.$this->toSecureString()); - } - - public function addLineItem($line_item) - { - if (get_class($line_item) == 'Apruve\LineItem') - { - return array_push($this->line_items, $line_item); - } - else - { - return false; - } - } - - public static function get($payment_request_id, $client=null) - { - if ($client == null) - { - $client = new Client(); - } - $response = $client->get(self::$PAYMENT_REQUESTS_PATH.$payment_request_id); - - if ($response[0] == 200) - { - $object = new self($response[1], $client); - return $object; - } - else - { - return $response[2]; - } - } - - public function update() - { - $response = $this->client->put(self::$PAYMENT_REQUESTS_PATH.$this->id, - $this->toJson()); - return $response[0] == 200; - } - - - -} diff --git a/src/Apruve/SubscriptionAdjustment.php b/src/Apruve/SubscriptionAdjustment.php index 25ec9e4..c5172d8 100644 --- a/src/Apruve/SubscriptionAdjustment.php +++ b/src/Apruve/SubscriptionAdjustment.php @@ -4,84 +4,68 @@ require_once 'ApruveObject.php'; -class SubscriptionAdjustment extends ApruveObject -{ +class SubscriptionAdjustment extends ApruveObject { - protected static $SUBSCRIPTION_ADJUSTMENT_PATH = '/subscriptions/%s/adjustments/'; + protected static $SUBSCRIPTION_ADJUSTMENT_PATH = '/subscriptions/%s/adjustments/'; + protected static $json_fields = [ + 'title', + 'amount_cents', + 'quantity', + 'price_ea_cents', + 'merchant_notes', + 'description', + 'variant_info', + 'sku', + 'vendor', + 'view_product_url', + ]; + var $id; + var $subscription_id; + var $status; + var $title; + var $amount_cents; + var $quantity; + var $price_ea_cents; + var $merchant_notes; + var $description; + var $variant_info; + var $sku; + var $vendor; + var $view_product_url; - var $id; - var $subscription_id; - var $status; - var $title; - var $amount_cents; - var $quantity; - var $price_ea_cents; - var $merchant_notes; - var $description; - var $variant_info; - var $sku; - var $vendor; - var $view_product_url; + public static function get( $subscription_id, $subscription_adjustment_id, $client = null ) { + if ( $client == null ) { + $client = new Client(); + } + $response = $client->get( sprintf( self::$SUBSCRIPTION_ADJUSTMENT_PATH, $subscription_id ) . $subscription_adjustment_id ); + if ( $response[0] == 200 ) { + return new self( $response[1], $client ); + } else { + return $response[2]; + } + } - protected static $json_fields = [ - 'title', - 'amount_cents', - 'quantity', - 'price_ea_cents', - 'merchant_notes', - 'description', - 'variant_info', - 'sku', - 'vendor', - 'view_product_url', - ]; + public static function delete( $subscription_id, $subscription_adjustment_id, $client = null ) { + if ( $client == null ) { + $client = new Client(); + } + $response = $client->delete( sprintf( self::$SUBSCRIPTION_ADJUSTMENT_PATH, $subscription_id ) . $subscription_adjustment_id ); + if ( $response[0] == 200 ) { + return new self( $response[1], $client ); + } else { + return $response[2]; + } + } - public static function get($subscription_id, $subscription_adjustment_id, $client=null) - { - if ($client == null) - { - $client = new Client(); - } - $response = $client->get(sprintf(self::$SUBSCRIPTION_ADJUSTMENT_PATH, $subscription_id).$subscription_adjustment_id); - if ($response[0] == 200) - { - return new self($response[1], $client); - } - else - { - return $response[2]; - } - } - - public function save() - { - $response = $this->client->post( - sprintf( - self::$SUBSCRIPTION_ADJUSTMENT_PATH, $this->subscription_id), $this->toJson()); - if ($response[0] == 201 || $response[0] == 200) // TODO: Return this to checking for 201 only if/when things on the apruve end are changed to return 201 - { - return new self($response[1], $this->client); - } - else - { - return $response[2]; - } - } - - public static function delete($subscription_id, $subscription_adjustment_id, $client=null) - { - if ($client == null) - { - $client = new Client(); - } - $response = $client->delete(sprintf(self::$SUBSCRIPTION_ADJUSTMENT_PATH, $subscription_id).$subscription_adjustment_id); - if ($response[0] == 200) - { - return new self($response[1], $client); - } - else - { - return $response[2]; - } - } + public function save() { + $response = $this->client->post( + sprintf( + self::$SUBSCRIPTION_ADJUSTMENT_PATH, $this->subscription_id ), $this->toJson() ); + if ( $response[0] == 201 || $response[0] == 200 ) // TODO: Return this to checking for 201 only if/when things on the apruve end are changed to return 201 + { + return new self( $response[1], $this->client ); + } else { + return $response[2]; + } + } } diff --git a/tests/Apruve/ApruveObjectTest.php b/tests/Apruve/ApruveObjectTest.php index ea7a3e9..0c0b37a 100644 --- a/tests/Apruve/ApruveObjectTest.php +++ b/tests/Apruve/ApruveObjectTest.php @@ -1,73 +1,64 @@ client; - } + public function getClient() { + return $this->client; + } } -class ApruveObjectTest extends PHPUnit_Framework_TestCase -{ +class ApruveObjectTest extends PHPUnit_Framework_TestCase { - protected function setUp() { - $this->client = Apruve\Client::init('a key', Apruve\Environment::DEV()); - $this->object = new TestClass([ - 'third' => 3, - 'first' => 1, - 'second' => 'second', - ]); - } + public function testPropertiesAreDefined() { + $vars = get_class_vars( get_class( $this->object ) ); - public function testPropertiesAreDefined() - { - $vars = get_class_vars(get_class($this->object)); + $this->assertEquals( array_keys( $vars ), array( + 'first', + 'third', + 'second', + ) ); + $this->assertEquals( 3, count( $vars ) ); + } - $this->assertEquals(array_keys($vars),array( - 'first', - 'third', - 'second', - )); - $this->assertEquals(3, count($vars)); - } + public function testToHashString() { + $this->assertEquals( + '1second3', $this->object->toHashString() ); + } - public function testToHashString() - { - $this->assertEquals( - '1second3', $this->object->toHashString()); - } + public function testToJson() { + $this->assertJsonStringEqualsJsonString( + '{"first": 1, "second": "second" }', + $this->object->toJson() + ); + } - public function testToJson() - { - $this->assertJsonStringEqualsJsonString( - '{"first": 1, "second": "second" }', - $this->object->toJson() - ); - } + public function testClientInjection() { + $this->client = Apruve\Client::init( 'a key', Apruve\Environment::DEV() ); + $this->assertEquals( $this->client->getApiKey(), $this->object->getClient()->getApiKey() ); + } - public function testClientInjection() - { - $this->client = Apruve\Client::init('a key', Apruve\Environment::DEV()); - $this->assertEquals($this->client->getApiKey(), $this->object->getClient()->getApiKey()); - } + protected function setUp() { + $this->client = Apruve\Client::init( 'a key', Apruve\Environment::DEV() ); + $this->object = new TestClass( [ + 'third' => 3, + 'first' => 1, + 'second' => 'second', + ] ); + } } diff --git a/tests/Apruve/ClientTest.php b/tests/Apruve/ClientTest.php index 483f9fc..d388cfe 100644 --- a/tests/Apruve/ClientTest.php +++ b/tests/Apruve/ClientTest.php @@ -1,148 +1,137 @@ assertEquals($env, $client->getEnvironment()); - } - - public function getMockClient($httpStub) - { - $mock = $this->getMockBuilder('Apruve\Client') - ->setMethods(['initCurl']) - ->setConstructorArgs([static::$AN_API_KEY, Apruve\Environment::DEV()]) - ->getMock(); - $mock->expects($this->atLeastOnce()) - ->method('initCurl') - ->will($this->returnValue($httpStub)); - return $mock; - } - - public function testPost() - { - $httpStub = $this->getMockBuilder('Apruve\CurlRequest') - ->setMethods(['execute', 'setOption']) - ->setConstructorArgs(['http://localhost:3000/api/v3/blah']) - ->getMock(); - $httpStub->expects($this->atLeastOnce()) - ->method('setOption') - ->withConsecutive( - [$this->equalTo(CURLOPT_HTTPHEADER), $this->anything()], - [$this->equalTo(CURLOPT_POST), $this->equalTo(true)], - [$this->equalTo(CURLOPT_POSTFIELDS), $this->anything()], - [$this->equalTo(CURLOPT_RETURNTRANSFER), $this->equalTo(true)] - ); - - $client = $this->getMockClient($httpStub); - - $response = $client->post('/blah', '{"payload": "blah"}'); - - $this->assertEquals(3, count($response)); - } - - public function testPut() - { - $httpStub = $this->getMockBuilder('Apruve\CurlRequest') - ->setMethods(['execute', 'setOption']) - ->setConstructorArgs(['http://localhost:3000/api/v3/blah']) - ->getMock(); - $httpStub->expects($this->atLeastOnce()) - ->method('setOption') - ->withConsecutive( - [$this->equalTo(CURLOPT_HTTPHEADER), $this->anything()], - [$this->equalTo(CURLOPT_CUSTOMREQUEST), $this->equalTo('PUT')], - [$this->equalTo(CURLOPT_POSTFIELDS), $this->anything()], - [$this->equalTo(CURLOPT_RETURNTRANSFER), $this->equalTo(true)] - ); - - $client = $this->getMockClient($httpStub); - - $response = $client->put('/blah', '{"payload": "blah"}'); - - $this->assertEquals(3, count($response)); - } - - public function testGet() - { - $httpStub = $this->getMockBuilder('Apruve\CurlRequest') - ->setMethods(['execute', 'setOption']) - ->setConstructorArgs(['http://localhost:3000/api/v3/blah']) - ->getMock(); - $httpStub->expects($this->atLeastOnce()) - ->method('setOption') - ->withConsecutive( - [$this->equalTo(CURLOPT_HTTPHEADER), $this->anything()], - [$this->equalTo(CURLOPT_RETURNTRANSFER), $this->equalTo(true)] - ); - - $client = $this->getMockClient($httpStub); - - $response = $client->get('/blah'); - - $this->assertEquals(3, count($response)); - } - - public function testDelete() - { - $httpStub = $this->getMockBuilder('Apruve\CurlRequest') - ->setMethods(['execute', 'setOption']) - ->setConstructorArgs(['http://localhost:3000/api/v3/blah']) - ->getMock(); - $httpStub->expects($this->atLeastOnce()) - ->method('setOption') - ->withConsecutive( - [$this->equalTo(CURLOPT_HTTPHEADER), $this->anything()], - [$this->equalTo(CURLOPT_CUSTOMREQUEST), $this->equalTo('DELETE')], - [$this->equalTo(CURLOPT_RETURNTRANSFER), $this->equalTo(true)] - ); - - $client = $this->getMockClient($httpStub); - - $response = $client->delete('/blah'); - - $this->assertEquals(3, count($response)); - } - - public function initClient() - { - return Apruve\Client::init(self::$AN_API_KEY, Apruve\Environment::DEV()); - } - - public function testCreateAClient() - { - $client = $this->initClient(); - $this->assertEquals(self::$AN_API_KEY, $client->getApiKey()); - } - - public function testGetUninstantiatedClient() - { - $this->setExpectedException('InvalidArgumentException'); - $client = new Apruve\Client(); - } - - public function testBaseUrlSet() - { - $client = $this->initClient(); - $this->assertEquals(Apruve\Environment::DEV, $client->getEnvironment()->getBaseUrl()); - } + public static $AN_API_KEY = 'AnApiKey'; + + public function testGetEnvironment() { + $env = Apruve\Environment::DEV(); + $client = Apruve\Client::init( 'a key', $env ); + $this->assertEquals( $env, $client->getEnvironment() ); + } + + public function testPost() { + $httpStub = $this->getMockBuilder( 'Apruve\CurlRequest' ) + ->setMethods( [ 'execute', 'setOption' ] ) + ->setConstructorArgs( [ 'http://localhost:3000/api/v3/blah' ] ) + ->getMock(); + $httpStub->expects( $this->atLeastOnce() ) + ->method( 'setOption' ) + ->withConsecutive( + [ $this->equalTo( CURLOPT_HTTPHEADER ), $this->anything() ], + [ $this->equalTo( CURLOPT_POST ), $this->equalTo( true ) ], + [ $this->equalTo( CURLOPT_POSTFIELDS ), $this->anything() ], + [ $this->equalTo( CURLOPT_RETURNTRANSFER ), $this->equalTo( true ) ] + ); + + $client = $this->getMockClient( $httpStub ); + + $response = $client->post( '/blah', '{"payload": "blah"}' ); + + $this->assertEquals( 3, count( $response ) ); + } + + public function getMockClient( $httpStub ) { + $mock = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'initCurl' ] ) + ->setConstructorArgs( [ static::$AN_API_KEY, Apruve\Environment::DEV() ] ) + ->getMock(); + $mock->expects( $this->atLeastOnce() ) + ->method( 'initCurl' ) + ->will( $this->returnValue( $httpStub ) ); + + return $mock; + } + + public function testPut() { + $httpStub = $this->getMockBuilder( 'Apruve\CurlRequest' ) + ->setMethods( [ 'execute', 'setOption' ] ) + ->setConstructorArgs( [ 'http://localhost:3000/api/v4/blah' ] ) + ->getMock(); + $httpStub->expects( $this->atLeastOnce() ) + ->method( 'setOption' ) + ->withConsecutive( + [ $this->equalTo( CURLOPT_HTTPHEADER ), $this->anything() ], + [ $this->equalTo( CURLOPT_CUSTOMREQUEST ), $this->equalTo( 'PUT' ) ], + [ $this->equalTo( CURLOPT_POSTFIELDS ), $this->anything() ], + [ $this->equalTo( CURLOPT_RETURNTRANSFER ), $this->equalTo( true ) ] + ); + + $client = $this->getMockClient( $httpStub ); + + $response = $client->put( '/blah', '{"payload": "blah"}' ); + + $this->assertEquals( 3, count( $response ) ); + } + + public function testGet() { + $httpStub = $this->getMockBuilder( 'Apruve\CurlRequest' ) + ->setMethods( [ 'execute', 'setOption' ] ) + ->setConstructorArgs( [ 'http://localhost:3000/api/v4/blah' ] ) + ->getMock(); + $httpStub->expects( $this->atLeastOnce() ) + ->method( 'setOption' ) + ->withConsecutive( + [ $this->equalTo( CURLOPT_HTTPHEADER ), $this->anything() ], + [ $this->equalTo( CURLOPT_RETURNTRANSFER ), $this->equalTo( true ) ] + ); + + $client = $this->getMockClient( $httpStub ); + + $response = $client->get( '/blah' ); + + $this->assertEquals( 3, count( $response ) ); + } + + public function testDelete() { + $httpStub = $this->getMockBuilder( 'Apruve\CurlRequest' ) + ->setMethods( [ 'execute', 'setOption' ] ) + ->setConstructorArgs( [ 'http://localhost:3000/api/v4/blah' ] ) + ->getMock(); + $httpStub->expects( $this->atLeastOnce() ) + ->method( 'setOption' ) + ->withConsecutive( + [ $this->equalTo( CURLOPT_HTTPHEADER ), $this->anything() ], + [ $this->equalTo( CURLOPT_CUSTOMREQUEST ), $this->equalTo( 'DELETE' ) ], + [ $this->equalTo( CURLOPT_RETURNTRANSFER ), $this->equalTo( true ) ] + ); + + $client = $this->getMockClient( $httpStub ); + + $response = $client->delete( '/blah' ); + + $this->assertEquals( 3, count( $response ) ); + } + + public function testCreateAClient() { + $client = $this->initClient(); + $this->assertEquals( self::$AN_API_KEY, $client->getApiKey() ); + } + + public function initClient() { + return Apruve\Client::init( self::$AN_API_KEY, Apruve\Environment::DEV() ); + } + + public function testGetUninstantiatedClient() { + $this->setExpectedException( 'InvalidArgumentException' ); + $client = new Apruve\Client(); + } + + public function testBaseUrlSet() { + $client = $this->initClient(); + $this->assertEquals( Apruve\Environment::DEV, $client->getEnvironment()->getBaseUrl() ); + } + + protected function setUp() { + Apruve\ClientStorage::$apiKey = null; + Apruve\ClientStorage::$env = null; + } + + protected function tearDown() { + + } } diff --git a/tests/Apruve/EnvironmentTest.php b/tests/Apruve/EnvironmentTest.php index d6bb657..c2d40a9 100644 --- a/tests/Apruve/EnvironmentTest.php +++ b/tests/Apruve/EnvironmentTest.php @@ -1,48 +1,41 @@ assertEquals('https://app.apruve.com', Apruve\Environment::PROD); - $this->assertEquals('https://test.apruve.com', Apruve\Environment::TEST); - $this->assertEquals('http://localhost:3000', Apruve\Environment::DEV); - - } - - public function testGetJsUrl() - { - $env = Apruve\Environment::TEST(); - $this->assertEquals( - 'https://test.apruve.com/js/apruve.js', - $env->getJsUrl() - ); - } - - public function testGetApiUrl() - { - $env = Apruve\Environment::TEST(); - $this->assertEquals('https://test.apruve.com/api/v3', $env->getApiUrl()); - } - - public function testGetBaseUrl() - { - $env = Apruve\Environment::TEST(); - $this->assertEquals('https://test.apruve.com', $env->getBaseUrl()); - } - - public function testPRODGetBaseUrl() - { - $env = Apruve\Environment::PROD(); - $this->assertEquals('https://app.apruve.com', $env->getBaseUrl()); - } - - +require_once dirname( __FILE__ ) . '/../../src/Apruve/Environment.php'; + +class ApruveEnvironmentTest extends PHPUnit_Framework_TestCase { + + public function testURLs() { + $this->assertEquals( 'https://app.apruve.com', Apruve\Environment::PROD ); + $this->assertEquals( 'https://test.apruve.com', Apruve\Environment::TEST ); + $this->assertEquals( 'http://localhost:3000', Apruve\Environment::DEV ); + + } + + public function testGetJsUrl() { + $env = Apruve\Environment::TEST(); + $this->assertEquals( + 'https://test.apruve.com/js/apruve.js', + $env->getJsUrl() + ); + } + + public function testGetApiUrl() { + $env = Apruve\Environment::TEST(); + $this->assertEquals( 'https://test.apruve.com/api/v4', $env->getApiUrl() ); + } + + public function testGetBaseUrl() { + $env = Apruve\Environment::TEST(); + $this->assertEquals( 'https://test.apruve.com', $env->getBaseUrl() ); + } + + public function testPRODGetBaseUrl() { + $env = Apruve\Environment::PROD(); + $this->assertEquals( 'https://app.apruve.com', $env->getBaseUrl() ); + } + + protected function setUp() { + } + + } diff --git a/tests/Apruve/InvoiceItemTest.php b/tests/Apruve/InvoiceItemTest.php new file mode 100644 index 0000000..de3f344 --- /dev/null +++ b/tests/Apruve/InvoiceItemTest.php @@ -0,0 +1,66 @@ +item ) ); + + $this->assertEquals( array_keys( $item_vars ), array( + 'title', + 'amount_cents', + 'quantity', + 'price_ea_cents', + 'merchant_notes', + 'description', + 'variant_info', + 'sku', + 'vendor', + 'view_product_url', + ) ); + $this->assertEquals( 10, count( $item_vars ) ); + } + + public function testToJsonString() { + $this->assertJsonStringEqualsJsonString( + '{ + "title": "A title", + "amount_cents": 3400, + "quantity": 1, + "price_ea_cents": 3400, + "merchant_notes": "some notes.", + "description": "a description.", + "variant_info": "some variation.", + "sku": "a sku", + "vendor": "ACME", + "view_product_url": "A Url." + }', $this->item->toJson() + ); + } + + protected function setUp() { + $this->item = new InvoiceItem( [ + 'title' => 'A title', + 'amount_cents' => 3400, + 'quantity' => 1, + 'price_ea_cents' => 3400, + 'merchant_notes' => 'some notes.', + 'description' => 'a description.', + 'variant_info' => 'some variation.', + 'sku' => 'a sku', + 'vendor' => 'ACME', + 'view_product_url' => 'A Url.', + ], $this->getMockBuilder( 'Apruve\Client' ) + ->setConstructorArgs( [ 'a key', Apruve\Environment::DEV() ] ) + ->getMock() + ); + } + + protected function tearDown() { + + } +} + diff --git a/tests/Apruve/InvoiceReturnTest.php b/tests/Apruve/InvoiceReturnTest.php new file mode 100644 index 0000000..0291dc5 --- /dev/null +++ b/tests/Apruve/InvoiceReturnTest.php @@ -0,0 +1,107 @@ +invoiceReturn ) ); + + $this->assertEquals( array_keys( $vars ), [ + 'invoice_id', + 'amount_cents', + 'currency', + 'reason', + ] ); + $this->assertEquals( 4, count( $vars ) ); + + } + + public function testToJson() { + $this->assertJsonStringEqualsJsonString( + '{ + "invoice_id": "asdf1234", + "amount_cents": 6000, + "currency": "USD", + "reason": "OTHER" + }', + $this->invoiceReturn->toJson() + ); + } + + public function testGet() { + $client = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'get' ] ) + ->getMock(); + $client->expects( $this->Once() ) + ->method( 'get' ) + ->with( $this->equalTo( '/invoices/asdf1234/invoice_returns/8764ase' ) ) + ->will( $this->returnValue( [ + 200, + [ + 'id' => '8764asew', + 'invoice_id' => 'asdf1234', + 'amount_cents' => 6000, + 'currency' => 'USD', + ], + '' + ] ) + ); + + $r = InvoiceReturn::get( 'asdf1234', '8764ase', $client ); + + $this->assertEquals( '8764asew', $r->id ); + $this->assertEquals( 'Apruve\InvoiceReturn', get_class( $r ) ); + } + + public function testSave() { + $client = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'post' ] ) + ->getMock(); + $client->expects( $this->Once() ) + ->method( 'post' ) + ->with( $this->equalTo( '/invoices/asdf1234/invoice_returns/' ), + $this->anything() ) + ->will( $this->returnValue( [ + 201, + [ + 'id' => '8764asew', + 'invoice_id' => 'asdf1234', + 'amount_cents' => 6000, + 'currency' => 'USD', + 'reason' => 'OTHER' + ], + '' + ] ) + ); + + $invoiceReturn = new InvoiceReturn( [ + "amount_cents" => 4300, + "merchant_notes" => 'some notes', + "invoice_id" => 'asdf1234' + ], $client ); + $ir = $invoiceReturn->save(); + + $this->assertEquals( 'asdf1234', $ir->invoice_id ); + $this->assertEquals( 'Apruve\InvoiceReturn', get_class( $ir ) ); + + } + + protected function setUp() { + Apruve\Client::init( 'a key', Apruve\Environment::DEV() ); + $this->invoiceReturn = new InvoiceReturn( [ + "amount_cents" => 6000, + "invoice_id" => 'asdf1234', + "currency" => "USD", + "reason" => "OTHER", + ] ); + + } + + +} + diff --git a/tests/Apruve/InvoiceTest.php b/tests/Apruve/InvoiceTest.php new file mode 100644 index 0000000..fd64d31 --- /dev/null +++ b/tests/Apruve/InvoiceTest.php @@ -0,0 +1,134 @@ +invoice ) ); + + $this->assertEquals( array_keys( $vars ), [ + 'id', + 'order_id', + 'status', + 'amount_cents', + 'currency', + 'merchant_notes', + 'api_url', + 'view_url', + 'created_at', + 'updated_at', + 'invoice_items', + ] ); + $this->assertEquals( 11, count( $vars ) ); + + } + + public function testToJson() { + $this->assertJsonStringEqualsJsonString( + '{ + "amount_cents": 6000, + "currency": "USD", + "merchant_notes": null, + "invoice_items": [ + { + "title": "test", + "amount_cents": 100, + "quantity" : 1, + "price_ea_cents": 100, + "merchant_notes": null, + "description": null, + "variant_info": null, + "sku": null, + "vendor": null, + "view_product_url": null + } + ] + }', + $this->invoice->toJson() + ); + } + + public function testGet() { + $client = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'get' ] ) + ->getMock(); + $client->expects( $this->Once() ) + ->method( 'get' ) + ->with( $this->equalTo( '/orders/asdf1234/invoices/1234asdf' ) ) + ->will( $this->returnValue( [ + 200, + [ + 'id' => 'asdf1234', + 'order_id' => 'asdf1234', + 'amount_cents' => 6000, + 'currency' => 'USD', + ], + '' + ] ) + ); + + $i = Invoice::get( 'asdf1234', '1234asdf', $client ); + + $this->assertEquals( 'asdf1234', $i->id ); + $this->assertEquals( 'Apruve\Invoice', get_class( $i ) ); + } + + public function testSave() { + $client = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'post' ] ) + ->getMock(); + $client->expects( $this->Once() ) + ->method( 'post' ) + ->with( $this->equalTo( '/orders/asdf1234/invoices/' ), + $this->anything() ) + ->will( $this->returnValue( [ + 201, + [ + 'id' => 'asdf1234', + 'order_id' => 'asdf1234', + 'amount_cents' => 6000, + 'currency' => 'USD', + ], + '' + ] ) + ); + + $invoice = new Invoice( [ + "amount_cents" => 4300, + "merchant_notes" => 'some notes', + "order_id" => 'asdf1234' + ], $client ); + $i = $invoice->save(); + + $this->assertEquals( 'asdf1234', $i->id ); + $this->assertEquals( 'Apruve\Invoice', get_class( $i ) ); + + } + + protected function setUp() { + Apruve\Client::init( 'a key', Apruve\Environment::DEV() ); + $this->invoice = new Invoice( [ + "amount_cents" => 6000, + "currency" => "USD", + "merchant_notes" => null, + "invoice_items" => [ + [ + 'title' => 'test', + 'amount_cents' => 100, + 'quantity' => 1, + 'price_ea_cents' => 100 + ] + ] + ] ); + + } + + +} + diff --git a/tests/Apruve/LineItemTest.php b/tests/Apruve/LineItemTest.php deleted file mode 100644 index 162d6f5..0000000 --- a/tests/Apruve/LineItemTest.php +++ /dev/null @@ -1,85 +0,0 @@ -item = new LineItem([ - 'title' => 'A title', - 'sku' => 'sku', - 'plan_code' => 'plan', - 'amount_cents' => 3400, - 'price_ea_cents' => 3400, - 'quantity' => 1, - 'merchant_notes' => 'some notes.', - 'description' => 'a description.', - 'variant_info' => 'some variation.', - 'vendor' => 'ACME', - 'view_product_url' => 'A Url.', - 'payment_request_id' => '1234', - ], $this->getMockBuilder('Apruve\Client') - ->setConstructorArgs(['a key', Apruve\Environment::DEV()]) - ->getMock() - ); - } - - protected function tearDown() { - - } - - public function testPropertiesAreDefined() - { - $item_vars = get_class_vars(get_class($this->item)); - - $this->assertEquals(array_keys($item_vars),array( - 'id', - 'payment_request_id', - 'title', - 'plan_code', - 'amount_cents', - 'quantity', - 'price_ea_cents', - 'merchant_notes', - 'description', - 'variant_info', - 'sku', - 'vendor', - 'view_product_url', - )); - $this->assertEquals(13, count($item_vars)); - } - - public function testToHashString() - { - $this->assertEquals( - 'A titleplan340034001some notes.a description.some variation.skuACMEA Url.', - $this->item->toHashString() - ); - } - - public function testToJsonString() - { - $this->assertJsonStringEqualsJsonString( - '{ - "payment_request_id":"1234", - "title": "A title", - "plan_code": "plan", - "amount_cents": 3400, - "price_ea_cents": 3400, - "quantity": 1, - "merchant_notes": "some notes.", - "description": "a description.", - "variant_info": "some variation.", - "sku": "sku", - "vendor": "ACME", - "view_product_url": "A Url." - }', $this->item->toJson() - ); - } - - -} - diff --git a/tests/Apruve/OrderItemTest.php b/tests/Apruve/OrderItemTest.php new file mode 100644 index 0000000..279d528 --- /dev/null +++ b/tests/Apruve/OrderItemTest.php @@ -0,0 +1,81 @@ +item ) ); + + $this->assertEquals( array_keys( $item_vars ), array( + 'id', + 'order_id', + 'title', + 'plan_code', + 'amount_cents', + 'quantity', + 'price_ea_cents', + 'merchant_notes', + 'description', + 'variant_info', + 'sku', + 'vendor', + 'view_product_url', + ) ); + $this->assertEquals( 13, count( $item_vars ) ); + } + + public function testToHashString() { + $this->assertEquals( + 'A titleplan340034001some notes.a description.some variation.skuACMEA Url.', + $this->item->toHashString() + ); + } + + public function testToJsonString() { + $this->assertJsonStringEqualsJsonString( + '{ + "title": "A title", + "plan_code": "plan", + "amount_cents": 3400, + "price_ea_cents": 3400, + "quantity": 1, + "merchant_notes": "some notes.", + "description": "a description.", + "variant_info": "some variation.", + "sku": "sku", + "vendor": "ACME", + "view_product_url": "A Url." + }', $this->item->toJson() + ); + } + + protected function setUp() { + $this->item = new OrderItem( [ + 'title' => 'A title', + 'sku' => 'sku', + 'plan_code' => 'plan', + 'amount_cents' => 3400, + 'price_ea_cents' => 3400, + 'quantity' => 1, + 'merchant_notes' => 'some notes.', + 'description' => 'a description.', + 'variant_info' => 'some variation.', + 'vendor' => 'ACME', + 'view_product_url' => 'A Url.', + 'order_id' => '1234', + ], $this->getMockBuilder( 'Apruve\Client' ) + ->setConstructorArgs( [ 'a key', Apruve\Environment::DEV() ] ) + ->getMock() + ); + } + + protected function tearDown() { + + } + + +} + diff --git a/tests/Apruve/OrderTest.php b/tests/Apruve/OrderTest.php new file mode 100644 index 0000000..2c820e2 --- /dev/null +++ b/tests/Apruve/OrderTest.php @@ -0,0 +1,195 @@ +po ) ); + + $this->assertEquals( array_keys( $vars ), [ + 'id', + 'merchant_id', + 'username', + 'status', + 'merchant_order_id', + 'amount_cents', + 'tax_cents', + 'shipping_cents', + 'currency', + 'expire_at', + 'order_items', + 'api_url', + 'view_url', + 'created_at', + 'updated_at', + ] ); + $this->assertEquals( 15, count( $vars ) ); + + } + + public function testToJson() { + $this->assertJsonStringEqualsJsonString( + '{ + "id": "id", + "merchant_id": "asdf1234", + "merchant_order_id": "order1234", + "amount_cents": 6000, + "tax_cents": 500, + "shipping_cents": 1000, + "currency": "USD", + "expire_at": "2014-07-15T10:12:27-05:00", + "order_items": [ + { + "title": "a title", + "amount_cents": 4500, + "plan_code": null, + "price_ea_cents": null, + "quantity": null, + "merchant_notes": null, + "description": null, + "variant_info": null, + "sku": null, + "vendor": null, + "view_product_url": null + } + ] + }', + $this->po->toJson() + ); + } + + public function testToHashString() { + $this->assertEquals( + 'asdf1234order12346000USD50010002014-07-15T10:12:27-05:00', + $this->po->toHashString() + ); + } + + public function testToSecureString() { + $this->assertEquals( + 'asdf1234order12346000USD50010002014-07-15T10:12:27-05:00a title4500', + $this->po->toSecureString() + ); + } + + public function testToSecureHash() { + Apruve\Client::init( 'a key', Apruve\Environment::DEV() ); + $this->assertEquals( + hash( 'sha256', 'a keyasdf1234order12346000USD50010002014-07-15T10:12:27-05:00a title4500' ), + $this->po->toSecureHash() + ); + } + + public function testAddOrderItem() { + $item_count = count( $this->po->order_items ); + $this->po->addOrderItem( + new OrderItem( [ + 'title' => 'A new title', + 'amount_cents' => 4521, + ] ) + ); + $this->assertEquals( $item_count + 1, count( $this->po->order_items ) ); + + } + + public function testAddOrderItemOnlyAllowsOrderItems() { + $this->assertEquals( false, $this->po->addOrderItem( new Order ) ); + } + + public function testGet() { + $client = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'get' ] ) + ->getMock(); + $client->expects( $this->Once() ) + ->method( 'get' ) + ->with( $this->equalTo( '/orders/asdf1234' ) ) + ->will( $this->returnValue( [ + 200, + [ + 'id' => 'asdf1234', + 'merchant_id' => 'asdf1234', + 'merchant_order_id' => 'order1234', + 'amount_cents' => 6000, + 'tax_cents' => 500, + 'shipping_cents' => 1000, + 'currency' => 'USD', + ], + '' + ] ) + ); + + $po = Order::get( 'asdf1234', $client ); + + $this->assertEquals( 'asdf1234', $po->id ); + $this->assertEquals( 'Apruve\Order', get_class( $po ) ); + } + + public function testUpdate() { + $client = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'put' ] ) + ->getMock(); + $client->expects( $this->Once() ) + ->method( 'put' ) + ->with( $this->equalTo( '/orders/asdf1234' ) ) + ->will( $this->returnValue( [ + 200, + [ + 'id' => 'asdf1234', + 'merchant_id' => 'asdf1234', + 'merchant_order_id' => 'order1234', + 'amount_cents' => 6000, + 'tax_cents' => 500, + 'shipping_cents' => 1000, + 'currency' => 'USD', + ], + '' + ] ) + ); + + $po = new Order( [ + 'id' => 'asdf1234', + 'merchant_id' => 'asdf1234', + 'merchant_order_id' => 'order1234', + 'amount_cents' => 6000, + 'currency' => 'USD' + ], $client ); + $po->setShippingCents( 500 ); + $po->setTaxCents( 439 ); + $po->setAmountCents( 6199 ); + + $po = $po->update(); + + $this->assertEquals( true, $po ); + } + + protected function setUp() { + Apruve\Client::init( 'a key', Apruve\Environment::DEV() ); + $this->po = new Order( [ + 'id' => 'id', + 'merchant_id' => 'asdf1234', + 'merchant_order_id' => 'order1234', + 'amount_cents' => 6000, + 'tax_cents' => 500, + 'shipping_cents' => 1000, + 'currency' => 'USD', + 'expire_at' => '2014-07-15T10:12:27-05:00', + 'order_items' => [ + [ + 'title' => 'a title', + 'amount_cents' => 4500, + 'order_id' => 'id', + ] + ], + ] ); + } + + +} + diff --git a/tests/Apruve/PaymentItemTest.php b/tests/Apruve/PaymentItemTest.php deleted file mode 100644 index 57f0420..0000000 --- a/tests/Apruve/PaymentItemTest.php +++ /dev/null @@ -1,68 +0,0 @@ -item = new PaymentItem([ - 'title' => 'A title', - 'amount_cents' => 3400, - 'quantity' => 1, - 'price_ea_cents' => 3400, - 'merchant_notes' => 'some notes.', - 'description' => 'a description.', - 'variant_info' => 'some variation.', - 'sku' => 'a sku', - 'vendor' => 'ACME', - 'view_product_url' => 'A Url.', - ], $this->getMockBuilder('Apruve\Client') - ->setConstructorArgs(['a key', Apruve\Environment::DEV()]) - ->getMock() - ); - } - - protected function tearDown() { - - } - - public function testPropertiesAreDefined() - { - $item_vars = get_class_vars(get_class($this->item)); - - $this->assertEquals(array_keys($item_vars),array( - 'title', - 'amount_cents', - 'quantity', - 'price_ea_cents', - 'merchant_notes', - 'description', - 'variant_info', - 'sku', - 'vendor', - 'view_product_url', - )); - $this->assertEquals(10, count($item_vars)); - } - - public function testToJsonString() - { - $this->assertJsonStringEqualsJsonString( - '{ - "title": "A title", - "amount_cents": 3400, - "quantity": 1, - "price_ea_cents": 3400, - "merchant_notes": "some notes.", - "description": "a description.", - "variant_info": "some variation.", - "sku": "a sku", - "vendor": "ACME", - "view_product_url": "A Url." - }', $this->item->toJson() - ); - } -} - diff --git a/tests/Apruve/PaymentRequestTest.php b/tests/Apruve/PaymentRequestTest.php deleted file mode 100644 index 2e4851b..0000000 --- a/tests/Apruve/PaymentRequestTest.php +++ /dev/null @@ -1,204 +0,0 @@ -pr = new PaymentRequest([ - 'id' => 'id', - 'merchant_id' => 'asdf1234', - 'merchant_order_id' => 'order1234', - 'amount_cents' => 6000, - 'tax_cents' => 500, - 'shipping_cents' => 1000, - 'currency' => 'USD', - 'expire_at' => '2014-07-15T10:12:27-05:00', - 'line_items' => [ - [ - 'title' => 'a title', - 'amount_cents' => 4500, - 'payment_request_id' => 'id', - ] - ], - ]); - } - - public function testPropertiesAreDefined() - { - $vars = get_class_vars(get_class($this->pr)); - - $this->assertEquals(array_keys($vars),[ - 'id', - 'merchant_id', - 'username', - 'status', - 'merchant_order_id', - 'amount_cents', - 'tax_cents', - 'shipping_cents', - 'currency', - 'expire_at', - 'line_items', - 'api_url', - 'view_url', - 'created_at', - 'updated_at', - ]); - $this->assertEquals(15, count($vars)); - - } - - public function testToJson() - { - $this->assertJsonStringEqualsJsonString( - '{ - "id": "id", - "merchant_id": "asdf1234", - "merchant_order_id": "order1234", - "amount_cents": 6000, - "tax_cents": 500, - "shipping_cents": 1000, - "currency": "USD", - "expire_at": "2014-07-15T10:12:27-05:00", - "line_items": [ - { - "payment_request_id": "id", - "title": "a title", - "amount_cents": 4500, - "plan_code": null, - "price_ea_cents": null, - "quantity": null, - "merchant_notes": null, - "description": null, - "variant_info": null, - "sku": null, - "vendor": null, - "view_product_url": null - } - ] - }', - $this->pr->toJson() - ); - } - - public function testToHashString() - { - $this->assertEquals( - 'asdf1234order12346000USD50010002014-07-15T10:12:27-05:00', - $this->pr->toHashString() - ); - } - - public function testToSecureString() - { - $this->assertEquals( - 'asdf1234order12346000USD50010002014-07-15T10:12:27-05:00a title4500', - $this->pr->toSecureString() - ); - } - - public function testToSecureHash() - { - Apruve\Client::init('a key', Apruve\Environment::DEV()); - $this->assertEquals( - hash('sha256', 'a keyasdf1234order12346000USD50010002014-07-15T10:12:27-05:00a title4500'), - $this->pr->toSecureHash() - ); - } - - public function testAddLineItem() - { - $item_count = count($this->pr->line_items); - $this->pr->addLineItem( - new LineItem([ - 'title' => 'A new title', - 'amount_cents' => 4521, - ]) - ); - $this->assertEquals($item_count + 1, count($this->pr->line_items)); - - } - - public function testAddLineItemOnlyAllowsLineItems() - { - $this->assertEquals(false,$this->pr->addLineItem(new PaymentRequest)); - } - - - public function testGet() - { - $client = $this->getMockBuilder('Apruve\Client') - ->setMethods(['get']) - ->getMock(); - $client->expects($this->Once()) - ->method('get') - ->with($this->equalTo('/payment_requests/asdf1234')) - ->will($this->returnValue([ - 200, - [ - 'id' => 'asdf1234', - 'merchant_id' => 'asdf1234', - 'merchant_order_id' => 'order1234', - 'amount_cents' => 6000, - 'tax_cents' => 500, - 'shipping_cents' => 1000, - 'currency' => 'USD', - ], - '']) - ); - - $pr = PaymentRequest::get('asdf1234', $client); - - $this->assertEquals('asdf1234', $pr->id); - $this->assertEquals('Apruve\PaymentRequest', get_class($pr)); - } - - public function testUpdate() - { - $client = $this->getMockBuilder('Apruve\Client') - ->setMethods(['put']) - ->getMock(); - $client->expects($this->Once()) - ->method('put') - ->with($this->equalTo('/payment_requests/asdf1234')) - ->will($this->returnValue([ - 200, - [ - 'id' => 'asdf1234', - 'merchant_id' => 'asdf1234', - 'merchant_order_id' => 'order1234', - 'amount_cents' => 6000, - 'tax_cents' => 500, - 'shipping_cents' => 1000, - 'currency' => 'USD', - ], - '']) - ); - - $pr = new PaymentRequest([ - 'id' => 'asdf1234', - 'merchant_id' => 'asdf1234', - 'merchant_order_id' => 'order1234', - 'amount_cents' => 6000, - 'currency' => 'USD'], $client); - $pr->setShippingCents(500); - $pr->setTaxCents(439); - $pr->setAmountCents(6199); - - $pr = $pr->update(); - - $this->assertEquals(true, $pr); - } - - -} - diff --git a/tests/Apruve/PaymentTest.php b/tests/Apruve/PaymentTest.php deleted file mode 100644 index 4ceb479..0000000 --- a/tests/Apruve/PaymentTest.php +++ /dev/null @@ -1,135 +0,0 @@ -payment = new Payment([ - "amount_cents" => 6000, - "currency" => "USD", - "merchant_notes" => null, - "payment_items" => [ - [ - 'title' => 'test', - 'amount_cents' => 100, - 'quantity' => 1, - 'price_ea_cents' => 100 - ] - ] - ]); - - } - - public function testPropertiesAreDefined() - { - $vars = get_class_vars(get_class($this->payment)); - - $this->assertEquals(array_keys($vars),[ - 'id', - 'payment_request_id', - 'status', - 'amount_cents', - 'currency', - 'merchant_notes', - 'api_url', - 'view_url', - 'created_at', - 'updated_at', - 'payment_items', - ]); - $this->assertEquals(11, count($vars)); - - } - - public function testToJson() - { - $this->assertJsonStringEqualsJsonString( - '{ - "amount_cents": 6000, - "currency": "USD", - "merchant_notes": null, - "payment_items": [ - { - "title": "test", - "amount_cents": 100, - "quantity" : 1, - "price_ea_cents": 100, - "merchant_notes": null, - "description": null, - "variant_info": null, - "sku": null, - "vendor": null, - "view_product_url": null - } - ] - }', - $this->payment->toJson() - ); - } - - public function testGet() - { - $client = $this->getMockBuilder('Apruve\Client') - ->setMethods(['get']) - ->getMock(); - $client->expects($this->Once()) - ->method('get') - ->with($this->equalTo('/payment_requests/asdf1234/payments/1234asdf')) - ->will($this->returnValue([ - 200, - [ - 'id' => 'asdf1234', - 'payment_request_id' => 'asdf1234', - 'amount_cents' => 6000, - 'currency' => 'USD', - ], - '']) - ); - - $pr = Payment::get('asdf1234', '1234asdf', $client); - - $this->assertEquals('asdf1234', $pr->id); - $this->assertEquals('Apruve\Payment', get_class($pr)); - } - - public function testSave() - { - $client = $this->getMockBuilder('Apruve\Client') - ->setMethods(['post']) - ->getMock(); - $client->expects($this->Once()) - ->method('post') - ->with($this->equalTo('/payment_requests/asdf1234/payments/'), - $this->anything()) - ->will($this->returnValue([ - 201, - [ - 'id' => 'asdf1234', - 'payment_request_id' => 'asdf1234', - 'amount_cents' => 6000, - 'currency' => 'USD', - ], - '']) - ); - - $payment = new Payment([ - "amount_cents" => 4300, - "merchant_notes" => 'some notes', - "payment_request_id" => 'asdf1234'], $client); - $pr = $payment->save(); - - $this->assertEquals('asdf1234', $pr->id); - $this->assertEquals('Apruve\Payment', get_class($pr)); - - } - - -} - diff --git a/tests/Apruve/SubscriptionAdjustmentTest.php b/tests/Apruve/SubscriptionAdjustmentTest.php index 82d2412..5f4d799 100644 --- a/tests/Apruve/SubscriptionAdjustmentTest.php +++ b/tests/Apruve/SubscriptionAdjustmentTest.php @@ -1,52 +1,37 @@ subscription_adjustment = new SubscriptionAdjustment([ - 'title' => 'test', - 'amount_cents' => 100, - 'quantity' => 1, - 'price_ea_cents' => 100 - ]); - - } - - public function testPropertiesAreDefined() - { - $vars = get_class_vars(get_class($this->subscription_adjustment)); - - $this->assertEquals(array_keys($vars),[ - 'id', - 'subscription_id', - 'status', - 'title', - 'amount_cents', - 'quantity', - 'price_ea_cents', - 'merchant_notes', - 'description', - 'variant_info', - 'sku', - 'vendor', - 'view_product_url' - ]); - $this->assertEquals(13, count($vars)); - - } - - public function testToJson() - { - $this->assertJsonStringEqualsJsonString( - '{ +class SubscriptionAdjustmentTest extends PHPUnit_Framework_TestCase { + + public function testPropertiesAreDefined() { + $vars = get_class_vars( get_class( $this->subscription_adjustment ) ); + + $this->assertEquals( array_keys( $vars ), [ + 'id', + 'subscription_id', + 'status', + 'title', + 'amount_cents', + 'quantity', + 'price_ea_cents', + 'merchant_notes', + 'description', + 'variant_info', + 'sku', + 'vendor', + 'view_product_url' + ] ); + $this->assertEquals( 13, count( $vars ) ); + + } + + public function testToJson() { + $this->assertJsonStringEqualsJsonString( + '{ "title": "test", "amount_cents": 100, "quantity": 1, @@ -58,92 +43,104 @@ public function testToJson() "vendor": null, "view_product_url": null }', - $this->subscription_adjustment->toJson() - ); - } - - public function testGet() - { - $client = $this->getMockBuilder('Apruve\Client') - ->setMethods(['get']) - ->getMock(); - $client->expects($this->Once()) - ->method('get') - ->with($this->equalTo('/subscriptions/asdf1234/adjustments/1234asdf')) - ->will($this->returnValue([ - 200, - [ - 'id' => 'asdf1234', - 'payment_request_id' => 'asdf1234', - 'amount_cents' => 6000, - 'currency' => 'USD', - ], - '']) - ); - - $pr = SubscriptionAdjustment::get('asdf1234', '1234asdf', $client); - - $this->assertEquals('asdf1234', $pr->id); - $this->assertEquals('Apruve\SubscriptionAdjustment', get_class($pr)); - } - - public function testSave() - { - $client = $this->getMockBuilder('Apruve\Client') - ->setMethods(['post']) - ->getMock(); - $client->expects($this->Once()) - ->method('post') - ->with($this->equalTo('/subscriptions/asdf1234/adjustments/'), - $this->anything()) - ->will($this->returnValue([ - 201, - [ - 'id' => 'asdf1234', - 'subscription_id' => 'asdf1234', - 'amount_cents' => 100, - ], - '']) - ); - - $subscription_adjustment = new SubscriptionAdjustment([ - "subscription_id" => 'asdf1234', - "title" => 'test', - "amount_cents" => 100, - "quantity" => 1, - "price_ea_cents" => 100], $client); - $sr = $subscription_adjustment->save(); - - $this->assertEquals('asdf1234', $sr->id); - $this->assertEquals('Apruve\SubscriptionAdjustment', get_class($sr)); - } - - public function testDelete() - { - $client = $this->getMockBuilder('Apruve\Client') - ->setMethods(['delete']) - ->getMock(); - $client->expects($this->Once()) - ->method('delete') - ->with($this->equalTo('/subscriptions/asdf1234/adjustments/asdf1234')) - ->will($this->returnValue([ - 200, - [ - 'id' => 'asdf1234', - 'subscription_id' => 'asdf1234', - 'api_url' => 'www.foo.com' - ], - '']) - ); - - $sr = SubscriptionAdjustment::delete('asdf1234', 'asdf1234', $client); - $this->assertEquals('asdf1234', $sr->id); - $this->assertEquals('asdf1234', $sr->subscription_id); - $this->assertEquals('www.foo.com', $sr->api_url); - - $this->assertEquals('Apruve\SubscriptionAdjustment', get_class($sr)); - } - + $this->subscription_adjustment->toJson() + ); + } + + public function testGet() { + $client = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'get' ] ) + ->getMock(); + $client->expects( $this->Once() ) + ->method( 'get' ) + ->with( $this->equalTo( '/subscriptions/asdf1234/adjustments/1234asdf' ) ) + ->will( $this->returnValue( [ + 200, + [ + 'id' => 'asdf1234', + 'order_id' => 'asdf1234', + 'amount_cents' => 6000, + 'currency' => 'USD', + ], + '' + ] ) + ); + + $pr = SubscriptionAdjustment::get( 'asdf1234', '1234asdf', $client ); + + $this->assertEquals( 'asdf1234', $pr->id ); + $this->assertEquals( 'Apruve\SubscriptionAdjustment', get_class( $pr ) ); + } + + public function testSave() { + $client = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'post' ] ) + ->getMock(); + $client->expects( $this->Once() ) + ->method( 'post' ) + ->with( $this->equalTo( '/subscriptions/asdf1234/adjustments/' ), + $this->anything() ) + ->will( $this->returnValue( [ + 201, + [ + 'id' => 'asdf1234', + 'subscription_id' => 'asdf1234', + 'amount_cents' => 100, + ], + '' + ] ) + ); + + $subscription_adjustment = new SubscriptionAdjustment( [ + "subscription_id" => 'asdf1234', + "title" => 'test', + "amount_cents" => 100, + "quantity" => 1, + "price_ea_cents" => 100 + ], $client ); + $sr = $subscription_adjustment->save(); + + $this->assertEquals( 'asdf1234', $sr->id ); + $this->assertEquals( 'Apruve\SubscriptionAdjustment', get_class( $sr ) ); + } + + public function testDelete() { + $client = $this->getMockBuilder( 'Apruve\Client' ) + ->setMethods( [ 'delete' ] ) + ->getMock(); + $client->expects( $this->Once() ) + ->method( 'delete' ) + ->with( $this->equalTo( '/subscriptions/asdf1234/adjustments/asdf1234' ) ) + ->will( $this->returnValue( [ + 200, + [ + 'id' => 'asdf1234', + 'subscription_id' => 'asdf1234', + 'api_url' => 'www.foo.com' + ], + '' + ] ) + ); + + $sr = SubscriptionAdjustment::delete( 'asdf1234', 'asdf1234', $client ); + $this->assertEquals( 'asdf1234', $sr->id ); + $this->assertEquals( 'asdf1234', $sr->subscription_id ); + $this->assertEquals( 'www.foo.com', $sr->api_url ); + + $this->assertEquals( 'Apruve\SubscriptionAdjustment', get_class( $sr ) ); + } + + protected function setUp() { + Apruve\Client::init( 'a key', Apruve\Environment::DEV() ); + $this->subscription_adjustment = new SubscriptionAdjustment( [ + 'title' => 'test', + 'amount_cents' => 100, + 'quantity' => 1, + 'price_ea_cents' => 100 + ] ); + + } + }