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;
|
|
|
|
|
2018-02-27 09:41:07 +01:00
|
|
|
PHPUnit_Framework_Assert::assertGreaterThan(0, count((array) $userAccounts));
|
2015-11-27 13:55:28 +01:00
|
|
|
|
|
|
|
$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
|
|
|
|
2016-02-03 15:03:56 +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-02-03 15:03:56 +01:00
|
|
|
|
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;
|
2016-05-04 14:16:49 +02:00
|
|
|
$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');
|
|
|
|
|
2017-05-30 12:56:51 +02:00
|
|
|
$data = new stdClass;
|
2017-05-30 13:50:25 +02:00
|
|
|
$data->client_id = $clientId;
|
2017-05-30 12:56:51 +02:00
|
|
|
$data->amount = 1;
|
|
|
|
$this->createEntity('credit', $data);
|
|
|
|
$this->listEntities('credits');
|
|
|
|
|
2016-11-06 13:00:37 +01:00
|
|
|
$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
|
|
|
|
2018-02-27 10:38:54 +01:00
|
|
|
PHPUnit_Framework_Assert::assertGreaterThan(0, count((array) $response->data));
|
2015-11-27 13:55:28 +01:00
|
|
|
|
|
|
|
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
|
|
|
}
|