public function testCardsListFromCustomerObject() { $GLOBALS['CURLOPT_URL_DATA'] = null; $this->_requestMock->expects($this->once())->method('exec')->will($this->returnValue('{"data":[{"id": "card1"}, {"id": "card2"}]}')); $this->_requestMock->expects($this->any())->method('getinfo')->will($this->returnCallback(function ($option) { switch ($option) { case CURLINFO_HTTP_CODE: return 200; } return null; })); $this->_requestMock->expects($this->any())->method('setopt')->will($this->returnCallback(function ($option, $value = null) { switch ($option) { case CURLOPT_URL: $GLOBALS['CURLOPT_URL_DATA'] = $value; return true; } return true; })); $cards = Payplug\Card::listCards(Customer::fromAttributes(array('id' => 'a_customer_id'))); $this->assertContains('a_customer_id', $GLOBALS['CURLOPT_URL_DATA']); $this->assertEquals(2, count($cards)); $this->assertTrue('card1' === $cards[0]->id || 'card2' === $cards[1]->id); $this->assertTrue(('card1' === $cards[1]->id || 'card2' === $cards[1]->id) && $cards[0]->id !== $cards[1]->id); unset($GLOBALS['CURLOPT_URL_DATA']); }
public function testAddCardToCustomer() { $GLOBALS['CURLOPT_URL_DATA'] = null; $this->_requestMock->expects($this->once())->method('exec')->will($this->returnValue('{"id": "card1"}')); $this->_requestMock->expects($this->any())->method('getinfo')->will($this->returnCallback(function ($option) { switch ($option) { case CURLINFO_HTTP_CODE: return 200; } return null; })); $this->_requestMock->expects($this->any())->method('setopt')->will($this->returnCallback(function ($option, $value = null) { switch ($option) { case CURLOPT_URL: $GLOBALS['CURLOPT_URL_DATA'] = $value; return true; } return true; })); $customer = Customer::fromAttributes(array('id' => 'a_customer_id')); $card = $customer->addCard(array('some' => 'creation_data')); $this->assertContains('a_customer_id', $GLOBALS['CURLOPT_URL_DATA']); $this->assertEquals('card1', $card->id); unset($GLOBALS['CURLOPT_URL_DATA']); }
/** * List customers. * * @param Payplug\Payplug $payplug the client configuration * * @param int $perPage the number of results per page * @param int $page the page number * @return Customer[] the array of payments * * @throws Payplug\Exception\InvalidPaymentException * @throws Payplug\Exception\UnexpectedAPIResponseException */ public static function listCustomers($perPage = null, $page = null, Payplug\Payplug $payplug = null) { if ($payplug === null) { $payplug = Payplug\Payplug::getDefaultConfiguration(); } $httpClient = new Payplug\Core\HttpClient($payplug); $pagination = array('per_page' => $perPage, 'page' => $page); $response = $httpClient->get(Payplug\Core\APIRoutes::getRoute(Payplug\Core\APIRoutes::CUSTOMER_RESOURCE, null, array(), $pagination)); if (!array_key_exists('data', $response['httpResponse']) || !is_array($response['httpResponse']['data'])) { throw new Payplug\Exception\UnexpectedAPIResponseException("Expected 'data' key in API response.", $response['httpResponse']); } $customers = array(); foreach ($response['httpResponse']['data'] as &$customer) { $customers[] = Customer::fromAttributes($customer); } return $customers; }