1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 15:13:29 +01:00
invoiceninja/tests/acceptance/APICest.php

140 lines
4.0 KiB
PHP
Raw Normal View History

2015-11-27 13:55:28 +01:00
<?php
use Codeception\Util\Fixtures;
use Faker\Factory;
use Codeception\Util\Debug;
2015-11-30 08:21:46 +01:00
class APICest
2015-11-27 13:55:28 +01:00
{
private $faker;
private $token;
public function _before(AcceptanceTester $I)
{
$this->faker = Factory::create();
Debug::debug('Create/get token');
$data = new stdClass;
$data->email = Fixtures::get('username');
$data->password = Fixtures::get('password');
$data->api_secret = Fixtures::get('api_secret');
$data->token_name = 'iOS Token';
$response = $this->sendRequest('login', $data);
$userAccounts = $response->data;
PHPUnit_Framework_Assert::assertGreaterThan(0, count($userAccounts));
$userAccount = $userAccounts[0];
$this->token = $userAccount->token;
2015-11-28 18:19:37 +01:00
Debug::debug("Token: {$this->token}");
2015-11-27 13:55:28 +01:00
}
public function testAPI(AcceptanceTester $I)
{
$I->wantTo('test the API');
$data = new stdClass;
$data->contact = new stdClass;
$data->contact->email = $this->faker->safeEmail;
$clientId = $this->createEntity('client', $data);
2016-11-06 13:00:37 +01:00
$this->listEntities('clients');
2015-11-27 13:55:28 +01:00
$data = new stdClass;
$data->client_id = $clientId;
$data->description = $this->faker->realText(100);
$this->createEntity('task', $data);
2016-11-06 13:00:37 +01:00
$this->listEntities('tasks');
2015-11-27 13:55:28 +01:00
$lineItem = new stdClass;
$lineItem->qty = $this->faker->numberBetween(1, 10);
$lineItem->cost = $this->faker->numberBetween(1, 10);
$data = new stdClass;
$data->client_id = $clientId;
$data->invoice_items = [
$lineItem
];
$invoiceId = $this->createEntity('invoice', $data);
2016-11-06 13:00:37 +01:00
$this->listEntities('invoices');
2015-11-27 13:55:28 +01:00
$data = new stdClass;
$data->invoice_id = $invoiceId;
$data->amount = 1;
$this->createEntity('payment', $data);
2016-11-06 13:00:37 +01:00
$this->listEntities('payments');
2015-11-27 13:55:28 +01:00
$data = new stdClass;
$data->name = $this->faker->word;
$data->rate = $this->faker->numberBetween(1, 10);
$this->createEntity('tax_rate', $data);
2016-11-06 13:00:37 +01:00
$this->listEntities('tax_rates');
2016-05-02 21:51:22 +02:00
$data = new stdClass;
$data->product_key = $this->faker->word;
$data->notes = $this->faker->realText(100);
$this->createEntity('product', $data);
2016-11-06 13:00:37 +01:00
$this->listEntities('products');
2016-05-02 21:51:22 +02:00
$data = new stdClass;
$data->name = $this->faker->word;
$data->vendor_contacts = [];
2016-05-02 21:51:22 +02:00
$this->createEntity('vendor', $data);
2016-11-06 13:00:37 +01:00
$this->listEntities('vendors');
$this->listEntities('accounts');
$this->listEntities('dashboard');
2016-05-02 21:51:22 +02:00
2015-11-27 13:55:28 +01:00
}
private function createEntity($entityType, $data)
{
Debug::debug("Create {$entityType}");
$response = $this->sendRequest("{$entityType}s", $data);
$entityId = $response->data->id;
2016-11-06 13:00:37 +01:00
2015-11-27 13:55:28 +01:00
PHPUnit_Framework_Assert::assertGreaterThan(0, $entityId);
return $entityId;
}
private function listEntities($entityType)
{
2016-11-06 13:00:37 +01:00
Debug::debug("List {$entityType}");
$response = $this->sendRequest("{$entityType}", null, 'GET');
2015-11-27 13:55:28 +01:00
PHPUnit_Framework_Assert::assertGreaterThan(0, count($response->data));
return $response;
}
private function sendRequest($url, $data, $type = 'POST')
{
$url = Fixtures::get('url') . '/api/v1/' . $url;
$data = json_encode($data);
$curl = curl_init();
$opts = [
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $type,
CURLOPT_POST => $type === 'POST' ? 1 : 0,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Content-Length: ' . strlen($data),
'X-Ninja-Token: '. $this->token,
],
];
curl_setopt_array($curl, $opts);
$response = curl_exec($curl);
curl_close($curl);
2016-02-17 21:59:06 +01:00
//Debug::debug('Response: ' . $response);
2016-02-17 21:08:57 +01:00
2015-11-27 13:55:28 +01:00
return json_decode($response);
}
2016-11-06 13:00:37 +01:00
}