From 867dbcd519af3506854353c36769396c0be3430f Mon Sep 17 00:00:00 2001 From: Lucas Date: Wed, 24 Jan 2018 17:20:05 -0200 Subject: [PATCH 01/11] Change lib to use phpunit --- README.md | 5 +- composer.json | 22 +- test/Iugu.php | 28 - test/Iugu/ChargeTest.php | 51 -- test/Iugu/CustomerTest.php | 80 -- test/Iugu/InvoiceTest.php | 98 --- test/Iugu/PaymentMethodTest.php | 91 --- test/Iugu/PaymentTokenTest.php | 38 - test/Iugu/PlanTest.php | 58 -- test/Iugu/SubscriptionTest.php | 73 -- test/Iugu/TestCase.php | 14 - test/console.php | 1353 ------------------------------- tests/Iugu/CustomerTest.php | 14 + 13 files changed, 30 insertions(+), 1895 deletions(-) delete mode 100644 test/Iugu.php delete mode 100644 test/Iugu/ChargeTest.php delete mode 100644 test/Iugu/CustomerTest.php delete mode 100644 test/Iugu/InvoiceTest.php delete mode 100644 test/Iugu/PaymentMethodTest.php delete mode 100644 test/Iugu/PaymentTokenTest.php delete mode 100644 test/Iugu/PlanTest.php delete mode 100644 test/Iugu/SubscriptionTest.php delete mode 100644 test/Iugu/TestCase.php delete mode 100644 test/console.php create mode 100644 tests/Iugu/CustomerTest.php diff --git a/README.md b/README.md index 7cab625..7bea8b8 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ Acesse [iugu.com/documentacao](http://iugu.com/documentacao) para referência ## Testes -Instale as dependências. Iugu-PHP utiliza SimpleTest. +Instale as dependências: ``` composer update @@ -62,5 +62,6 @@ composer update Execute a comitiva de testes: ``` -php ./test/Iugu.php +composer tests ``` + diff --git a/composer.json b/composer.json index 6c5111c..25fded9 100644 --- a/composer.json +++ b/composer.json @@ -3,9 +3,9 @@ "description": "iugu PHP Library", "homepage": "https://iugu.com", "keywords": [ - "iugu", - "pagamentos", - "api" + "iugu", + "pagamentos", + "api" ], "license": "MIT", "authors": [ @@ -15,15 +15,19 @@ } ], "require": { - "php": ">=5.4", - "ext-curl": "*", - "ext-json": "*", - "ext-mbstring": "*" + "php": ">=5.4", + "ext-curl": "*", + "ext-json": "*", + "ext-mbstring": "*" }, "require-dev": { - "simpletest/simpletest": "1.1.5" + "phpunit/phpunit": "^6" }, "autoload": { - "classmap": ["lib/Iugu/"] + "classmap": ["lib/Iugu/"] + }, + "scripts": { + "tests": "./vendor/bin/phpunit --colors tests" } } + diff --git a/test/Iugu.php b/test/Iugu.php deleted file mode 100644 index 6ceb0a3..0000000 --- a/test/Iugu.php +++ /dev/null @@ -1,28 +0,0 @@ -.'; - exit(1); -} - -include_once dirname(__FILE__).'/../lib/Iugu.php'; - -include_once dirname(__FILE__).'/Iugu/TestCase.php'; -include_once dirname(__FILE__).'/Iugu/CustomerTest.php'; -include_once dirname(__FILE__).'/Iugu/PaymentMethodTest.php'; -include_once dirname(__FILE__).'/Iugu/PaymentTokenTest.php'; -include_once dirname(__FILE__).'/Iugu/ChargeTest.php'; -include_once dirname(__FILE__).'/Iugu/SubscriptionTest.php'; -include_once dirname(__FILE__).'/Iugu/PlanTest.php'; diff --git a/test/Iugu/ChargeTest.php b/test/Iugu/ChargeTest.php deleted file mode 100644 index b77725e..0000000 --- a/test/Iugu/ChargeTest.php +++ /dev/null @@ -1,51 +0,0 @@ - 'credit_card', - 'data' => [ - 'number' => '4111111111111111', - 'verification_value' => '123', - 'first_name' => 'Joao', - 'last_name' => 'Silva', - 'month' => '12', - 'year' => date('Y'), - ], - ] - ); - - $this->assertNotNull($payment_token); - $this->assertTrue(count($payment_token['errors']) == 0); - - $charge = Iugu_Charge::create( - [ - 'token' => $payment_token, - 'email' => 'patricknegri@gmail.com', - 'items' => [ - [ - 'description' => 'Item Teste', - 'quantity' => '1', - 'price_cents' => '1000', - ], - ], - ] - ); - - $this->assertNotNull($charge); - $this->assertTrue(count($charge['errors']) == 0); - $this->assertTrue($charge->invoice()->items_total_cents == 1000); - - $this->assertNotNull($charge->invoice()); - } - - public function testCreateEmptyCharge() - { - $charge = Iugu_Charge::create(); - $this->assertNotNull($charge); - $this->assertTrue(count($charge['errors']) > 0); - } -} diff --git a/test/Iugu/CustomerTest.php b/test/Iugu/CustomerTest.php deleted file mode 100644 index 9bb5379..0000000 --- a/test/Iugu/CustomerTest.php +++ /dev/null @@ -1,80 +0,0 @@ -assertNotNull($object); - $this->assertNotNull($object['id']); - - $this->assertTrue($object->delete()); - } - - public function testCreateWrongCustomer() - { - $object = self::createTestCustomer(['email' => 'patricknegri']); - - $this->assertNull($object['id']); - $this->assertTrue(count($object['errors']) > 0); - - $object->email = 'patricknegri@gmail.com'; - $object->save(); - - $this->assertNotNull($object['id']); - $this->assertTrue($object->delete()); - } - - public function testCreateCustomerFromSave() - { - $object = new Iugu_Customer(); - $object['email'] = 'patricknegri@gmail.com'; - $object['name'] = 'Patrick Ribeiro Negri'; - - $this->assertTrue($object->save()); - $this->assertTrue($object->delete()); - } - - public function testModifyCustomer() - { - $object = self::createTestCustomer(); - $object->name = 'Patrick Ribeiro Negri'; - $this->assertTrue($object->save()); - $this->assertTrue($object->delete()); - } - - public function testRefreshCustomer() - { - $object = self::createTestCustomer(); - $customer_id = $object->id; - $customer = new Iugu_Customer(); - $customer->id = $customer_id; - $this->assertTrue($customer->refresh()); - $this->assertEqual($object->name, $customer->name); - $this->assertTrue($object->delete()); - } - - public function testFetch() - { - $object = self::createTestCustomer(); - $customer = Iugu_Customer::fetch($object->id); - $this->assertNotNull($customer); - $this->assertNotNull($customer['id']); - - $this->expectException('IuguObjectNotFound'); - Iugu_Customer::fetch('D245B36FCD4B42DDB44208D868FF2C10'); - - $this->assertTrue($object->delete()); - } - - public function testSearch() - { - $object = self::createTestCustomer(); - - $searchResults = Iugu_Customer::search([]); - - $this->assertTrue($searchResults->total() > 0); - $this->assertTrue($object->delete()); - } -} diff --git a/test/Iugu/InvoiceTest.php b/test/Iugu/InvoiceTest.php deleted file mode 100644 index 8261228..0000000 --- a/test/Iugu/InvoiceTest.php +++ /dev/null @@ -1,98 +0,0 @@ -customer = self::createTestCustomer(); - $this->invoice = Iugu_Invoice::create( - [ - 'email' => 'patrick@iugu.com', - 'due_date' => '30/12/'.date('Y'), - 'customer_id' => $this->customer, - 'items' => [ - [ - 'description' => 'Item Teste', - 'quantity' => '1', - 'price_cents' => '1000', - ], - ], - ] - ); - - $payment_token = Iugu_PaymentToken::create( - [ - 'method' => 'credit_card', - 'data' => [ - 'number' => '4111111111111111', - 'verification_value' => '123', - 'first_name' => 'Joao', - 'last_name' => 'Silva', - 'month' => '12', - 'year' => date('Y'), - ], - ] - ); - - $charge = Iugu_Charge::create( - [ - 'token' => $payment_token, - 'invoice_id' => $this->invoice->id - ] - ); - - } - - public function testCreateInvoice() - { - $this->assertNotNull($this->invoice); - $this->assertTrue(count($this->invoice['errors']) == 0); - } - - public function testCaptureInvoice() - { - $this->invoice->capture(); - $this->assertTrue($this->invoice->status == "paid"); - $this->assertNotNull($this->invoice); - $this->assertTrue(count($this->invoice['errors']) == 0); - } - - public function testCreateEmptyInvoice() - { - $invoice = Iugu_Invoice::create(); - $this->assertNotNull($invoice); - $this->assertTrue(count($invoice['errors']) > 0); - } - - public function testRefreshInvoice() - { - $this->assertTrue($this->invoice->refresh()); - } - - public function testFetchInvoice() - { - $this->expectException('IuguException'); - $new_invoice = Iugu_Invoice::fetch('NO VALID INVOICE'); - - $fetched_invoice = Iugu_Invoice::fetch($this->invoice->id); - $this->assertNotNull($fetched_invoice); - $this->assertNotNull($fetched_invoice['id']); - } - - public function testSearchPaymentMethod() - { - $searchResults = Iugu_Invoice::search([]); - $this->assertTrue($searchResults->total() > 0); - } - - public function testCustomerWithValidInvoices() - { - $this->assertTrue($this->customer->invoices()->search()->total() > 0); - } - - public function tearDown() - { - $this->assertTrue($this->invoice->delete()); - $this->assertTrue($this->customer->delete()); - } -} diff --git a/test/Iugu/PaymentMethodTest.php b/test/Iugu/PaymentMethodTest.php deleted file mode 100644 index 5a53054..0000000 --- a/test/Iugu/PaymentMethodTest.php +++ /dev/null @@ -1,91 +0,0 @@ -customer = self::createTestCustomer(); - $this->pm = $this->customer->payment_methods(); - $this->cc = $this->pm->create( - [ - 'description' => 'Cartão Teste', - 'item_type' => 'credit_card', - 'data' => [ - 'number' => '4111111111111111', - 'verification_value' => '123', - 'first_name' => 'Patrick', - 'last_name' => 'Negri', - 'month' => '12', - 'year' => date('Y'), - ], - ] - ); - $this->customer->refresh(); // Refresh to get default payment - } - - public function tearDown() - { - $this->assertTrue($this->cc->delete()); - $this->assertTrue($this->customer->delete()); - } - - public function testCreatePaymentMethods() - { - $this->assertNotNull($this->cc); - $this->assertTrue(count($this->cc['errors']) == 0); - } - - public function testCreateEmptyPaymentMethods() - { - $payment_method = Iugu_PaymentToken::create(); - $this->assertNotNull($payment_method); - $this->assertTrue(count($payment_method['errors']) > 0); - } - - public function testRefreshPaymentMethod() - { - $this->assertTrue($this->cc->refresh()); - } - - public function testFetchPaymentMethod() - { - $this->expectException('IuguException'); - $new_cc = Iugu_PaymentMethod::fetch($this->cc->id); - - $fetched_cc = $pm->fetch($new_cc['id']); - $this->assertNotNull($fetched_cc); - $this->assertNotNull($fetched_cc['id']); - } - - public function testSearchPaymentMethod() - { - $searchResults = $this->pm->search([]); - $this->assertTrue($searchResults->total() > 0); - } - - public function testCustomerWithDefaultPayment() - { - $this->assertTrue($this->customer->default_payment_method()); - } - - public function testCreateWrongPaymentMethod() - { - $cc = $this->pm->create( - [ - 'description' => 'Cartão Teste', - 'item_type' => 'credit_card', - 'data' => [ - 'number' => '4111111111111115', - 'verification_value' => '123', - 'first_name' => 'Patrick', - 'last_name' => 'Negri', - 'month' => '12', - 'year' => '10', - ], - ] - ); - - $this->assertNotNull($cc); - $this->assertTrue(count($cc['errors']) > 0); - } -} diff --git a/test/Iugu/PaymentTokenTest.php b/test/Iugu/PaymentTokenTest.php deleted file mode 100644 index 1eddce4..0000000 --- a/test/Iugu/PaymentTokenTest.php +++ /dev/null @@ -1,38 +0,0 @@ -payment_token = Iugu_PaymentToken::create( - [ - 'method' => 'credit_card', - 'data' => [ - 'number' => '4111111111111111', - 'verification_value' => '123', - 'first_name' => 'Joao', - 'last_name' => 'Silva', - 'month' => '12', - 'year' => date('Y'), - ], - ] - ); - } - - public function tearDown() - { - } - - public function testCreatePaymentToken() - { - $this->assertNotNull($this->payment_token); - $this->assertTrue(count($this->payment_token['errors']) == 0); - } - - public function testCreateEmptyPaymentToken() - { - $payment_token = Iugu_PaymentToken::create(); - $this->assertNotNull($payment_token); - $this->assertTrue(count($payment_token['errors']) > 0); - } -} diff --git a/test/Iugu/PlanTest.php b/test/Iugu/PlanTest.php deleted file mode 100644 index d52d2c3..0000000 --- a/test/Iugu/PlanTest.php +++ /dev/null @@ -1,58 +0,0 @@ -results()); - //die(); - $this->plan = Iugu_Plan::create( - [ - 'name' => 'name', - 'identifier' => 'identifier', - 'interval' => 1, - 'interval_type' => 'months', //months or weeks - 'prices.value_cents' => 1, - ] - ); - } - - public function testCreatePlan() - { - $this->assertNotNull($this->plan); - $this->assertTrue(count($this->plan['errors']) == 0); - } - - public function testCreateEmptyPlan() - { - $plan = Iugu_Plan::create(); - $this->assertNotNull($plan); - $this->assertTrue(count($plan['errors']) > 0); - } - - public function testRefreshPlan() - { - $this->assertTrue($this->plan->refresh()); - } - - public function testFetchPlan() - { - $this->expectException('IuguObjectNotFound'); - $new_plan = Iugu_Plan::fetch('NOT_VALID'); - - $fetched_plan = Iugu_Plan::fetch($this->plan->id); - $this->assertNotNull($fetched_plan); - $this->assertNotNull($fetched_plan['id']); - } - - public function testSearchPlan() - { - $searchResults = Iugu_Plan::search([]); - $this->assertTrue($searchResults->total() > 0); - } - - public function tearDown() - { - $this->assertTrue($this->plan->delete()); - } -} diff --git a/test/Iugu/SubscriptionTest.php b/test/Iugu/SubscriptionTest.php deleted file mode 100644 index 56d5c68..0000000 --- a/test/Iugu/SubscriptionTest.php +++ /dev/null @@ -1,73 +0,0 @@ -customer = self::createTestCustomer(); - $this->subscription = Iugu_Subscription::create( - [ - 'plan_identifier' => 'basic_plan', - 'customer_id' => $this->customer, - ] - ); - } - - public function testCreateSubscription() - { - $this->assertNotNull($this->subscription); - $this->assertTrue(count($this->subscription['errors']) == 0); - } - - public function testCreateEmptySubscription() - { - $subscription = Iugu_Subscription::create(); - $this->assertNotNull($subscription); - $this->assertTrue(count($subscription['errors']) > 0); - } - - public function testRefreshSubscription() - { - $this->assertTrue($this->subscription->refresh()); - } - - public function testFetchSubscription() - { - $this->expectException('IuguObjectNotFound'); - $new_subscription = Iugu_Subscription::fetch('NOT_VALID'); - - $fetched_subscription = Iugu_Subscription::fetch($this->subscription->id); - $this->assertNotNull($fetched_subscription); - $this->assertNotNull($fetched_subscription['id']); - } - - public function testSearchSubscription() - { - $searchResults = Iugu_Subscription::search([]); - $this->assertTrue($searchResults->total() > 0); - } - - public function testSubscriptionHasCustomer() - { - $this->assertTrue($this->subscription->customer()); - } - - public function testSubscriptionChangePlan() - { - $this->assertFalse($this->subscription->change_plan('basic_plan')); - $this->assertTrue($this->subscription->change_plan('advanced_plan')); - } - - public function testSuspendAndActivateSubscription() - { - $this->assertTrue($this->subscription->suspend()); - $this->assertFalse($this->subscription->suspend()); - $this->assertFalse($this->subscription->activate()); - } - - public function tearDown() - { - $this->assertTrue($this->subscription->delete()); - $this->assertTrue($this->customer->delete()); - } -} diff --git a/test/Iugu/TestCase.php b/test/Iugu/TestCase.php deleted file mode 100644 index 32a55ed..0000000 --- a/test/Iugu/TestCase.php +++ /dev/null @@ -1,14 +0,0 @@ - 'patricknegri@gmail.com', - 'name' => 'Patrick Negri', - ]; - - return Iugu_Customer::create(array_merge($attributes, $_attributes)); - } -} diff --git a/test/console.php b/test/console.php deleted file mode 100644 index 7513624..0000000 --- a/test/console.php +++ /dev/null @@ -1,1353 +0,0 @@ - ... ) - * - * @var array - * - * @see registerCommand - */ - protected $commands = []; - - /** - * register your own command for the shell. - * - * @param string $regex a regex to match against the input line - * @param string $obj a Object - * @param string $method a method in the object to call of the regex matches - * @param string $cmd the command string for the help - * @param string $help the full help description for this command - */ - public function registerCommand($regex, $obj, $method, $cmd, $help) - { - $this->commands[] = [ - 'regex' => $regex, - 'obj' => $obj, - 'method' => $method, - 'command' => $cmd, - 'description' => $help, - ]; - } - - /** - * return a copy of the commands array. - * - * @return all commands - */ - public function getCommands() - { - return $this->commands; - } - - public static function getInstance() - { - if (is_null(self::$instance)) { - $class = __CLASS__; - self::$instance = new $class(); - } - - return self::$instance; - } -} - -//Shell/Extensions.php - -interface PHP_Shell_Extension -{ - public function register(); -} - -/** - * storage class for Shell Extensions. - */ -class PHP_Shell_Extensions -{ - /** - * @var PHP_Shell_Extensions - */ - protected static $instance; - - /** - * storage for the extension. - * - * @var array - */ - protected $exts = []; - - /** - * the extension object gives access to the register objects - * through the a simple $exts->name->... - * - * @param string registered name of the extension - * - * @return PHP_Shell_Extension object handle - */ - public function __get($key) - { - if (!isset($this->exts[$key])) { - throw new Exception("Extension $s is not known."); - } - - return $this->exts[$key]; - } - - /** - * register set of extensions. - * - * @param array set of (name, class-name) pairs - */ - public function registerExtensions($exts) - { - foreach ($exts as $k => $v) { - $this->registerExtension($k, $v); - } - } - - /** - * register a single extension. - * - * @param string name of the registered extension - * @param PHP_Shell_Extension the extension object - */ - public function registerExtension($k, PHP_Shell_Extension $obj) - { - $obj->register(); - - $this->exts[$k] = $obj; - } - - /** - * @return object a singleton of the class - */ - public static function getInstance() - { - if (is_null(self::$instance)) { - $class = __CLASS__; - self::$instance = new $class(); - } - - return self::$instance; - } -} - -//Shell/Options.php -class PHP_Shell_Options implements PHP_Shell_Extension -{ - /* - * instance of the current class - * - * @var PHP_Shell_Options - */ - protected static $instance; - - /** - * known options and their setors. - * - * @var array - * - * @see registerOption - */ - protected $options = []; - - /** - * known options and their setors. - * - * @var array - * - * @see registerOptionAlias - */ - protected $option_aliases = []; - - public function register() - { - $cmd = PHP_Shell_Commands::getInstance(); - $cmd->registerCommand('#^:set #', $this, 'cmdSet', ':set ', 'set a shell variable'); - } - - /** - * register a option. - * - * @param string name of the option - * @param object a object handle - * @param string method-name of the setor in the object - * @param string (unused) - */ - public function registerOption($option, $obj, $setor, $getor = null) - { - if (!method_exists($obj, $setor)) { - throw new Exception(sprintf("setor %s doesn't exist on class %s", $setor, get_class($obj))); - } - - $this->options[trim($option)] = ['obj' => $obj, 'setor' => $setor]; - } - - /** - * set a shell-var. - * - * :set al to enable autoload - * :set bg=dark to enable highlighting with a dark backgroud - */ - public function cmdSet($l) - { - if (!preg_match('#:set\s+([a-z]+)\s*(?:=\s*([a-z0-9]+)\s*)?$#i', $l, $a)) { - echo ':set failed: either :set