From 8fa283b14d745857f7643bd8cb06f6799edc511f Mon Sep 17 00:00:00 2001 From: Marivaldo Junior Date: Sat, 20 Oct 2018 23:34:48 -0300 Subject: [PATCH 1/3] Fix cassete file path in CustomerTest --- tests/Iugu/CustomerTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Iugu/CustomerTest.php b/tests/Iugu/CustomerTest.php index cf92b9b..de15599 100644 --- a/tests/Iugu/CustomerTest.php +++ b/tests/Iugu/CustomerTest.php @@ -6,6 +6,11 @@ final class Iugu_CustomerTest extends TestCase { + public static function setupBeforeClass() + { + \VCR\VCR::configure()->setCassettePath('../fixtures') ; + } + protected function setUp() { Iugu::$endpoint = "http://api.desenvolvimento"; From bec8faee90d708074d1201773797b2aa1b892419 Mon Sep 17 00:00:00 2001 From: Marivaldo Junior Date: Sat, 20 Oct 2018 23:37:06 -0300 Subject: [PATCH 2/3] Added change_plan_simulation method in Subscription class --- lib/Iugu/Subscription.php | 30 ++++++++++ tests/Iugu/SubscriptionTest.php | 56 +++++++++++++++++++ .../iugu_subscription_change_plan_simulation | 31 ++++++++++ ..._subscription_should_create_a_subscription | 32 +++++++++++ 4 files changed, 149 insertions(+) create mode 100644 tests/Iugu/SubscriptionTest.php create mode 100644 tests/fixtures/iugu_subscription_change_plan_simulation create mode 100644 tests/fixtures/iugu_subscription_should_create_a_subscription diff --git a/lib/Iugu/Subscription.php b/lib/Iugu/Subscription.php index 94623eb..ce0cf6c 100644 --- a/lib/Iugu/Subscription.php +++ b/lib/Iugu/Subscription.php @@ -145,6 +145,36 @@ public function activate() return false; } + public function change_plan_simulation($identifier = null) + { + if ($this->is_new()) { + return false; + } + + if ($identifier == null) { + return false; + } + + try { + $response = self::API()->request( + 'GET', + static::url($this).'/change_plan_simulation/'.$identifier + ); + + if (isset($response->errors)) { + return false; + } + + $new_object = self::createFromResponse($response); + + return $new_object; + } catch (Exception $e) { + return false; + } + + return false; + } + public function change_plan($identifier = null) { if ($this->is_new()) { diff --git a/tests/Iugu/SubscriptionTest.php b/tests/Iugu/SubscriptionTest.php new file mode 100644 index 0000000..bde60f2 --- /dev/null +++ b/tests/Iugu/SubscriptionTest.php @@ -0,0 +1,56 @@ +setCassettePath('../fixtures') ; + } + + protected function setUp() + { + Iugu::$endpoint = "http://api.desenvolvimento"; + Iugu::setApiKey("development_api_token"); + } + + public function testShouldCreateASubscription() + { + \VCR\VCR::turnOn(); + \VCR\VCR::insertCassette("iugu_subscription_should_create_a_subscription"); + + $subscription = Iugu_Subscription::create([ + "plan_identifier" => "silver", + "customer_id" => "A11222C0E33445566E77A8899DD00B00" + ]); + + $this->assertEquals("silver", $subscription->plan_identifier); + $this->assertEquals("A11222C0E33445566E77A8899DD00B00", $subscription->customer_id); + + \VCR\VCR::eject(); + \VCR\VCR::turnOff(); + } + + public function testChangePlanSimulation() + { + \VCR\VCR::turnOn(); + \VCR\VCR::insertCassette("iugu_subscription_should_create_a_subscription"); + + $subscription = Iugu_Subscription::create([ + "plan_identifier" => "silver", + "customer_id" => "A11222C0E33445566E77A8899DD00B00" + ]); + + \VCR\VCR::insertCassette("iugu_subscription_change_plan_simulation"); + $simulation = $subscription->change_plan_simulation("gold"); + + $this->assertEquals("silver", $simulation->old_plan); + $this->assertEquals("gold", $simulation->new_plan); + + \VCR\VCR::eject(); + \VCR\VCR::turnOff(); + } +} \ No newline at end of file diff --git a/tests/fixtures/iugu_subscription_change_plan_simulation b/tests/fixtures/iugu_subscription_change_plan_simulation new file mode 100644 index 0000000..d79cbed --- /dev/null +++ b/tests/fixtures/iugu_subscription_change_plan_simulation @@ -0,0 +1,31 @@ + +- + request: + method: GET + url: 'http://api.desenvolvimento/v1/subscriptions/C00B1A22CE8A8877A55C444FDED99999/change_plan_simulation/gold' + headers: + Host: api.desenvolvimento + Authorization: 'Basic ZGV2ZWxvcG1lbnRfYXBpX3Rva2VuOg==' + Accept: application/json + Accept-Charset: utf-8 + User-Agent: 'Iugu PHPLibrary' + Accept-Language: 'pt-br;q=0.9,pt-BR' + response: + status: + http_version: '1.1' + code: '200' + message: OK + headers: + Server: nginx/1.13.5 + Date: 'Wed, 24 Jan 2018 19:47:34 GMT' + Content-Type: 'application/json; charset=utf-8' + Transfer-Encoding: chunked + Connection: keep-alive + x-HandledBy: 'api/v1/subscription#change_plan_simulation' + X-UA-Compatible: IE=Edge + ETag: '"fa7ae1adec07ffe45ea2191e16d645c8"' + Cache-Control: 'max-age=0, private, must-revalidate' + X-Request-Id: 639ff4e9bd2221957b52d2ca3bf2e2cb + X-Runtime: '0.067239' + Vary: Origin + body: '{"cost":1000,"discount":1500,"cycles":1,"expires_at":"2018-11-20","new_plan":"gold","old_plan":"silver"}' diff --git a/tests/fixtures/iugu_subscription_should_create_a_subscription b/tests/fixtures/iugu_subscription_should_create_a_subscription new file mode 100644 index 0000000..259890e --- /dev/null +++ b/tests/fixtures/iugu_subscription_should_create_a_subscription @@ -0,0 +1,32 @@ + +- + request: + method: POST + url: 'http://api.desenvolvimento/v1/subscriptions' + headers: + Host: api.desenvolvimento + Authorization: 'Basic ZGV2ZWxvcG1lbnRfYXBpX3Rva2VuOg==' + Accept: application/json + Accept-Charset: utf-8 + User-Agent: 'Iugu PHPLibrary' + Accept-Language: 'pt-br;q=0.9,pt-BR' + body: 'plan_identifier=silver&customer_id=A11222C0E33445566E77A8899DD00B00' + response: + status: + http_version: '1.1' + code: '200' + message: OK + headers: + Server: nginx/1.13.5 + Date: 'Wed, 24 Jan 2018 19:47:34 GMT' + Content-Type: 'application/json; charset=utf-8' + Transfer-Encoding: chunked + Connection: keep-alive + x-HandledBy: 'api/v1/subscription#create' + X-UA-Compatible: IE=Edge + ETag: '"fa7ae1adec07ffe45ea2191e16d645c8"' + Cache-Control: 'max-age=0, private, must-revalidate' + X-Request-Id: 639ff4e9bd2221957b52d2ca3bf2e2cb + X-Runtime: '0.067239' + Vary: Origin + body: '{"id":"C00B1A22CE8A8877A55C444FDED99999","suspended":false,"plan_identifier":"silver","price_cents":1500,"currency":"BRL","features":{},"expires_at":"2019-04-20","created_at":"2018-10-20T21:59:42-03:00","updated_at":"2018-10-20T21:59:45-03:00","customer_name":"Martin Fowler","customer_email":"martin@fowler.com","cycled_at":null,"credits_min":0,"credits_cycle":null,"payable_with":"all","ignore_due_email":null,"customer_id":"A11222C0E33445566E77A8899DD00B00","plan_name":"Silver","customer_ref":"Martin Fowler","plan_ref":"Silver","active":true,"in_trial":null,"credits":0,"credits_based":false,"recent_invoices":null,"subitems":[],"logs":[{"id":"9A8F732BE8054AB282AE915A00DB591B","description":"Fatura criada","notes":"Fatura criada com os items: 1x Ativa\u00e7\u00e3o de Assinatura: Silver = R$ 15,00;","created_at":"2018-10-20T21:59:44-03:00"},{"id":"9F21C27F154447CA97D96EB61EB76C64","description":"Assinatura Criada","notes":"Assinatura Criada ","created_at":"2018-10-20T21:59:42-03:00"}],"custom_variables":[]}' From 64aac066248823ebe16b4292cd40d434cc44ce88 Mon Sep 17 00:00:00 2001 From: Marivaldo Junior Date: Sat, 20 Oct 2018 23:46:56 -0300 Subject: [PATCH 3/3] Fix cassete path in CustomerTest and SubscriptionTest --- tests/Iugu/CustomerTest.php | 5 ----- tests/Iugu/SubscriptionTest.php | 5 ----- 2 files changed, 10 deletions(-) diff --git a/tests/Iugu/CustomerTest.php b/tests/Iugu/CustomerTest.php index de15599..cf92b9b 100644 --- a/tests/Iugu/CustomerTest.php +++ b/tests/Iugu/CustomerTest.php @@ -6,11 +6,6 @@ final class Iugu_CustomerTest extends TestCase { - public static function setupBeforeClass() - { - \VCR\VCR::configure()->setCassettePath('../fixtures') ; - } - protected function setUp() { Iugu::$endpoint = "http://api.desenvolvimento"; diff --git a/tests/Iugu/SubscriptionTest.php b/tests/Iugu/SubscriptionTest.php index bde60f2..0daa56a 100644 --- a/tests/Iugu/SubscriptionTest.php +++ b/tests/Iugu/SubscriptionTest.php @@ -6,11 +6,6 @@ final class Iugu_SubscriptionTest extends TestCase { - public static function setupBeforeClass() - { - \VCR\VCR::configure()->setCassettePath('../fixtures') ; - } - protected function setUp() { Iugu::$endpoint = "http://api.desenvolvimento";