/** * Get a Chargify client instance * * @param string $mockResponseFile Filename containing mocked response * @param string $env (test|dev) * * @return Chargify */ public static function getInstance($mockResponseFile = null, $env = 'test') { $config = array(); switch ($env) { case 'test': $config = (require dirname(__DIR__) . '/configs/ClientConfig.Test.php'); break; case 'dev': $config = (require dirname(__DIR__) . '/configs/ClientConfig.Dev.php'); break; } $chargify = new Chargify($config); if (!empty($mockResponseFile)) { $mock = new MockHandler([Psr7\parse_response(MockResponse::read($mockResponseFile))]); $handler = HandlerStack::create($mock); // $logger = new Logger('Logger'); // $logger->pushHandler(new StreamHandler(dirname(__DIR__) . '/artifacts/logs/guzzle.log', Logger::DEBUG)); // // $middleware = new LoggerMiddleware($logger); // $template = MessageFormatter::DEBUG; // $middleware->setFormatter(new MessageFormatter($template)); // // $handler->push($middleware); $chargify->getHttpClient()->getConfig('handler')->setHandler($handler); } return $chargify; }
public function testAuthRequestFailure() { $mockFile = 'v2.authTest.error'; // create a client with invalid credentials $chargify = new ChargifyV2(['api_id' => 'fdsafdsaf', 'api_password' => 'fgsdgfdsg', 'api_secret' => 'fdsfdsaf']); $direct = $chargify->direct(); // set a fake redirect URL. Chargify will 500 on us if we don't have a redirect URL $direct->setRedirect('http://localhost'); $utilityAuthRequest = new AuthRequest($direct); // set a mock response on the client $mock = new Mock([MockResponse::read($mockFile)]); $utilityAuthRequest->getHttpClient()->getEmitter()->attach($mock); $success = $utilityAuthRequest->test(); $response = $utilityAuthRequest->getLastResponse(); // test should have failed $this->assertFalse($success); // status code should be 200 $this->assertEquals(200, $response->getStatusCode()); // chargify should not redirect us $locationHeader = trim($response->getHeader('Location')); $this->assertTrue(empty($locationHeader)); // body should contain 'Incorrect signature' $bodyIsInvalid = 0 === strcasecmp('Incorrect signature', trim((string) $response->getBody())); $this->assertTrue($bodyIsInvalid); }
public function testReadByChargifyId() { $chargify = new Chargify(array('hostname' => 'fgdfsgdfsgfds', 'api_key' => 'hgfdhdfghd', 'shared_key' => 'hgfdhgfdhg')); // set a mock response on the client $mock = new Mock([MockResponse::read('customer.readByChargifyId.success')]); $chargify->getHttpClient()->getEmitter()->attach($mock); $customer = $chargify->customer()->readByChargifyId(8003316); $this->assertFalse($customer->isError(), '$customer has an error'); }
public function testReadByChargifyId() { $chargify = ClientHelper::getInstance(); // set a mock response on the client $mock = new Mock([MockResponse::read('customer.readByChargifyId.success')]); $chargify->getHttpClient()->getEmitter()->attach($mock); $customer = $chargify->customer()->readByChargifyId(12345); $response = $customer->getService()->getLastResponse(); $this->assertFalse($customer->isError(), '$customer has an error'); $this->assertEquals(200, $response->getStatusCode(), 'Expected status code 200'); }
public function testFindNonExistentCodeIsError() { $chargify = ClientHelper::getInstance(); // set a mock response on the client $mock = new Mock([MockResponse::read('coupon.find.error')]); $chargify->getHttpClient()->getEmitter()->attach($mock); $coupon = $chargify->coupon()->setCode('THIS_CODE_DOESNT_EXIST')->find(1234); $response = $coupon->getService()->getLastResponse(); // $component object should indicate an error $this->assertTrue($coupon->isError(), '$coupon was not en error'); $this->assertEquals(404, $response->getStatusCode(), 'Expected status code 404'); }
public function testAuthFailure() { $mockFile = 'v2.authTest.error'; $chargify = ClientV2Helper::getInstance(); $direct = $chargify->direct(); // set mock on authtest http client before testing $authUtility = $direct->getAuthTestUtility(); $mock = new Mock([MockResponse::read($mockFile)]); $authUtility->getHttpClient()->getEmitter()->attach($mock); $success = $direct->checkAuth(); $this->assertFalse($success); }
/** * @todo assert http response code */ public function testNoShippingCreatesError() { $chargify = new Chargify(array('hostname' => 'sdfdsf', 'api_key' => 'fsdfdsf', 'shared_key' => 'fsdfdsf')); // set a mock response on the client $mock = new Mock([MockResponse::read('subscription.error.no_shipping')]); $chargify->getHttpClient()->getEmitter()->attach($mock); $subscription = $chargify->subscription()->setProductId(123)->setCustomerAttributes(array('first_name' => 'Darryl', 'last_name' => 'Strawberry', 'email' => '*****@*****.**', 'organization' => 'Mets'))->setPaymentProfileAttributes(array('first_name' => 'Darryl2', 'last_name' => 'Strawberry2', 'full_number' => '1', 'expiration_month' => '03', 'expiration_year' => '16', 'cvv' => '123', 'billing_address' => '600 N', 'billing_city' => 'Chicago', 'billing_state' => 'IL', 'billing_zip' => '60610', 'billing_country' => 'US'))->create(); // $subscription object should be in an error state $this->assertTrue($subscription->isError()); // get errors from $subscription $errors = $subscription->getErrors(); // check for $this->assertContains('Shipping Address: cannot be blank.', $errors); }
public function testCreateMeteredStairstepError() { $chargify = ClientHelper::getInstance(); // set a mock response on the client $mock = new Mock([MockResponse::read('adjustment.createMeteredStairstep.error.no_prices')]); $chargify->getHttpClient()->getEmitter()->attach($mock); $component = $chargify->component()->setName('Text Messages')->setUnitName('message')->setUnitPrice('0.0012')->setPricingScheme('stairstep')->createComponent(1234, 'metered_components'); $response = $component->getService()->getLastResponse(); // $component object should indicate an error $this->assertTrue($component->isError(), '$adjustment was not en error'); $this->assertEquals(422, $response->getStatusCode(), 'Expected status code 422'); // get errors from $adjustment $errors = $component->getErrors(); // check for error messages $this->assertContains('At least 1 price bracket must be defined', $errors); }
public function testNoAmountCreatesError() { $chargify = ClientHelper::getInstance(); // set a mock response on the client $mock = new Mock([MockResponse::read('adjustment.create.error.no_amount')]); $chargify->getHttpClient()->getEmitter()->attach($mock); $adjustment = $chargify->adjustment()->create(123); $response = $adjustment->getService()->getLastResponse(); // $adjustment object should indicate an error $this->assertTrue($adjustment->isError(), '$adjustment was not en error'); $this->assertEquals(422, $response->getStatusCode(), 'Expected status code 422'); // get errors from $adjustment $errors = $adjustment->getErrors(); // check for error messages $this->assertContains('Amount: is not a number.', $errors); }
/** * Get a Chargify client instance * * @param string $mockResponseFile Filename containing mocked response * @param string $env (test|dev) * * @return ChargifyV2 */ public static function getInstance($mockResponseFile = null, $env = 'test') { $config = array(); switch ($env) { case 'test': $config = (require dirname(__DIR__) . '/configs/ClientV2Config.Test.php'); break; case 'dev': $config = (require dirname(__DIR__) . '/configs/ClientV2Config.Dev.php'); break; } $chargify = new ChargifyV2($config); if (!empty($mockResponseFile)) { $mock = new MockHandler([Psr7\parse_response(MockResponse::read($mockResponseFile))]); $handler = HandlerStack::create($mock); $chargify->getHttpClient()->getConfig('handler')->setHandler($handler); } return $chargify; }
public function testNotFoundCreatesError() { $mockFile = 'v2.call.read.error.not_found'; $chargify = ClientV2Helper::getInstance(); // set a mock response on the client $mock = new Mock([MockResponse::read($mockFile)]); $chargify->getHttpClient()->getEmitter()->attach($mock); $call = $chargify->call()->readByChargifyId('1234'); $response = $call->getService()->getLastResponse(); // $adjustment object should indicate an error $this->assertTrue($call->isError(), '$call was not en error'); $this->assertEquals(404, $response->getStatusCode(), 'Expected status code 404'); // check for error messages // $errors = $call->getErrors(); // $this->assertContains([ // 'source' => 'client', // 'attribute' => null, // 'kind' => 'status_code', // 'message' => 'Bad status code: 404' // ], $errors); }