From 7eb064a9ecd157fd4838cbea665ee8aeb827433e Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Fri, 30 Dec 2016 13:15:24 -0500 Subject: [PATCH] 57 - Don't Mock What You Don't Own --- .../Alternate/StripePaymentGateway.php | 24 ++ app/Billing/StripePaymentGateway.php | 24 ++ composer.json | 3 +- composer.lock | 225 +++++++++++++++++- .../Billing/MockStripePaymentGatewayTest.php | 21 ++ .../unit/Billing/StripePaymentGatewayTest.php | 3 - 6 files changed, 294 insertions(+), 6 deletions(-) create mode 100644 app/Billing/Alternate/StripePaymentGateway.php create mode 100644 tests/unit/Billing/MockStripePaymentGatewayTest.php diff --git a/app/Billing/Alternate/StripePaymentGateway.php b/app/Billing/Alternate/StripePaymentGateway.php new file mode 100644 index 0000000..abd7f0e --- /dev/null +++ b/app/Billing/Alternate/StripePaymentGateway.php @@ -0,0 +1,24 @@ +stripeClient = $stripeClient; + } + + public function charge($amount, $token) + { + $this->stripeClient->createCharge([ + 'amount' => $amount, + 'source' => $token, + 'currency' => 'usd', + ]); + } +} diff --git a/app/Billing/StripePaymentGateway.php b/app/Billing/StripePaymentGateway.php index 6f5a988..f16ad5e 100644 --- a/app/Billing/StripePaymentGateway.php +++ b/app/Billing/StripePaymentGateway.php @@ -22,3 +22,27 @@ class StripePaymentGateway implements PaymentGateway ], ['api_key' => $this->apiKey]); } } + +// class StripePaymentGateway implements PaymentGateway +// { +// private $apiKey; + +// public function __construct($apiKey) +// { +// $this->apiKey = $apiKey; +// } + +// public function charge($amount, $token) +// { +// (new \GuzzleHttp\Client)->post('https://api.stripe.com/v1/charges', [ +// 'headers' => [ +// 'Authorization' => "Bearer {$this->apiKey}", +// ], +// 'form_params' => [ +// 'amount' => $amount, +// 'token' => $token, +// 'currency' => 'usd', +// ] +// ]); +// } +// } diff --git a/composer.json b/composer.json index f9275cc..3bec5de 100644 --- a/composer.json +++ b/composer.json @@ -8,7 +8,8 @@ "php": ">=5.6.4", "laravel/framework": "5.3.*", "zondicons/blade-bridge": "^0.1.0", - "stripe/stripe-php": "^4.3" + "stripe/stripe-php": "^4.3", + "guzzlehttp/guzzle": "^6.2" }, "require-dev": { "fzaninotto/faker": "~1.4", diff --git a/composer.lock b/composer.lock index c9f68df..5be11d5 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "8cdff7401ffaa77e712fc773720a0a25", - "content-hash": "4283a0ebb9e0602a14c02b45b50cb2d7", + "hash": "483f8274bdccb5d6398c5c9b7607e8e2", + "content-hash": "cf64a65c2814f1dab2944cbf2f70101d", "packages": [ { "name": "classpreloader/classpreloader", @@ -161,6 +161,177 @@ ], "time": "2015-11-06 14:35:42" }, + { + "name": "guzzlehttp/guzzle", + "version": "6.2.2", + "source": { + "type": "git", + "url": "https://github.com/guzzle/guzzle.git", + "reference": "ebf29dee597f02f09f4d5bbecc68230ea9b08f60" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/guzzle/zipball/ebf29dee597f02f09f4d5bbecc68230ea9b08f60", + "reference": "ebf29dee597f02f09f4d5bbecc68230ea9b08f60", + "shasum": "" + }, + "require": { + "guzzlehttp/promises": "^1.0", + "guzzlehttp/psr7": "^1.3.1", + "php": ">=5.5" + }, + "require-dev": { + "ext-curl": "*", + "phpunit/phpunit": "^4.0", + "psr/log": "^1.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "6.2-dev" + } + }, + "autoload": { + "files": [ + "src/functions_include.php" + ], + "psr-4": { + "GuzzleHttp\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle is a PHP HTTP client library", + "homepage": "http://guzzlephp.org/", + "keywords": [ + "client", + "curl", + "framework", + "http", + "http client", + "rest", + "web service" + ], + "time": "2016-10-08 15:01:37" + }, + { + "name": "guzzlehttp/promises", + "version": "v1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/promises.git", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/promises/zipball/a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "reference": "a59da6cf61d80060647ff4d3eb2c03a2bc694646", + "shasum": "" + }, + "require": { + "php": ">=5.5.0" + }, + "require-dev": { + "phpunit/phpunit": "^4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Promise\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "Guzzle promises library", + "keywords": [ + "promise" + ], + "time": "2016-12-20 10:07:11" + }, + { + "name": "guzzlehttp/psr7", + "version": "1.3.1", + "source": { + "type": "git", + "url": "https://github.com/guzzle/psr7.git", + "reference": "5c6447c9df362e8f8093bda8f5d8873fe5c7f65b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/guzzle/psr7/zipball/5c6447c9df362e8f8093bda8f5d8873fe5c7f65b", + "reference": "5c6447c9df362e8f8093bda8f5d8873fe5c7f65b", + "shasum": "" + }, + "require": { + "php": ">=5.4.0", + "psr/http-message": "~1.0" + }, + "provide": { + "psr/http-message-implementation": "1.0" + }, + "require-dev": { + "phpunit/phpunit": "~4.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.4-dev" + } + }, + "autoload": { + "psr-4": { + "GuzzleHttp\\Psr7\\": "src/" + }, + "files": [ + "src/functions_include.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Michael Dowling", + "email": "mtdowling@gmail.com", + "homepage": "https://github.com/mtdowling" + } + ], + "description": "PSR-7 message implementation", + "keywords": [ + "http", + "message", + "stream", + "uri" + ], + "time": "2016-06-24 23:00:38" + }, { "name": "jakub-onderka/php-console-color", "version": "0.1", @@ -828,6 +999,56 @@ ], "time": "2016-10-17 15:23:22" }, + { + "name": "psr/http-message", + "version": "1.0.1", + "source": { + "type": "git", + "url": "https://github.com/php-fig/http-message.git", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/http-message/zipball/f6561bf28d520154e4b0ec72be95418abe6d9363", + "reference": "f6561bf28d520154e4b0ec72be95418abe6d9363", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Psr\\Http\\Message\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for HTTP messages", + "homepage": "https://github.com/php-fig/http-message", + "keywords": [ + "http", + "http-message", + "psr", + "psr-7", + "request", + "response" + ], + "time": "2016-08-06 14:39:51" + }, { "name": "psr/log", "version": "1.0.2", diff --git a/tests/unit/Billing/MockStripePaymentGatewayTest.php b/tests/unit/Billing/MockStripePaymentGatewayTest.php new file mode 100644 index 0000000..1982791 --- /dev/null +++ b/tests/unit/Billing/MockStripePaymentGatewayTest.php @@ -0,0 +1,21 @@ +charge(2500, 'valid-token'); + + $stripeClient->shouldHaveReceived('createCharge')->with([ + 'amount' => 2500, + 'source' => 'valid-token', + 'currency' => 'usd', + ])->once(); + } +} diff --git a/tests/unit/Billing/StripePaymentGatewayTest.php b/tests/unit/Billing/StripePaymentGatewayTest.php index 66fe196..f4d875e 100644 --- a/tests/unit/Billing/StripePaymentGatewayTest.php +++ b/tests/unit/Billing/StripePaymentGatewayTest.php @@ -1,9 +1,6 @@