Skip to content

Commit 46c8923

Browse files
committed
fined tuned tests and added env config
1 parent a8d5141 commit 46c8923

File tree

4 files changed

+81
-64
lines changed

4 files changed

+81
-64
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,11 @@ create a formstack.php at the root dir of the package with the following content
6363

6464
?>
6565
```
66+
67+
- Finally, you can put `access_token` in the environment for use. In which case, the default `base_url` will be used.
6668

67-
It is from this file that the `ConfigHelper` class will read the config token from by default.
69+
The `ConfigHelper` class will read the configuration first from the env, then try the Laravel config helper,
70+
and finally try the config file in root dir of package.
6871

6972

7073
FormStack Object Instantiation

src/config/ConfigHelper.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,14 @@ public static function config() {
2020
"base_url" => "https://www.formstack.com/api/v2/"
2121
];
2222
}
23+
else if(function_exists("config")) {
2324

24-
//if not set in the env, try looking for a config file
25-
if(!file_exists(realpath("./formstack.php")) ) {
25+
return [
26+
"access_token" => config("formstack.access_token"),
27+
"base_url" => config("formstack.base_url")
28+
];
29+
}
30+
else if(!file_exists(realpath("./formstack.php")) ) {
2631
throw new \Exception("ERROR: config file [formstack.php] not found in package root's dir.
2732
\nFile is expected here in this dir:
2833
". realpath ( "."));

src/formstack/FSClient.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,19 @@ class FSClient
2121
protected $client;
2222
protected $xmlResponseType;
2323

24+
/**
25+
* FSClient constructor.
26+
* @param null $token
27+
* @param null $baseUrl
28+
* @param bool $xmlResponseType
29+
*/
2430
protected function __construct($token = null, $baseUrl = null, $xmlResponseType = false) {
2531

2632
if(is_null($token)) {
27-
$token = function_exists("config") ? config("formstack.access_token") : ConfigHelper::config()["access_token"];
33+
$token = ConfigHelper::config()["access_token"];
2834
}
2935
if(is_null($baseUrl)) {
30-
$baseUrl = function_exists("config") ? config("formstack.base_url") : ConfigHelper::config()["base_url"];
36+
$baseUrl = ConfigHelper::config()["base_url"];
3137
}
3238

3339
$this->token = $token;

tests/FSFormUnitTest.php

Lines changed: 62 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,100 +13,103 @@
1313
use FormStack\FSField;
1414
use FormStack\FSForm;
1515
use FormStack\FSSubmission;
16+
use GuzzleHttp\Exception\RequestException;
1617
use PHPUnit\Framework\TestCase;
1718

1819
class FSFormUnitTest extends TestCase {
1920

2021
public function testForm_whenFormMethods_givenRequiredParams_thenPrintAPIResponse() {
2122

22-
$fsForm = new FSForm();
23+
try {
24+
$fsForm = new FSForm();
2325

24-
print("There are " . count($fsForm->all()) . " forms in record\n");
26+
print("There are " . count($fsForm->all()) . " forms in record\n");
2527

26-
print("\nTrying to create a form with the api\n");
27-
print_r( $newForm = $fsForm->create(["name" => "Created by API"]));
28+
print("\nTrying to create a form with the api\n");
29+
print_r($newForm = $fsForm->create(["name" => "Created by API"]));
2830

29-
print("\nThere are ". count($allForms = $fsForm->all()) . " forms in record after api creation\n");
31+
print("\nThere are " . count($allForms = $fsForm->all()) . " forms in record after api creation\n");
3032

31-
$formId = $newForm["id"];
33+
$formId = $newForm["id"];
3234

33-
/*
34-
* testing FSField
35-
* */
35+
/*
36+
* testing FSField
37+
* */
3638

37-
$fsField = new FSField();
39+
$fsField = new FSField();
3840

39-
print("\nGetting all fields for the created form\n");
40-
print_r($allFields = $fsField->all($formId));
41+
print("\nGetting all fields for the created form\n");
42+
print_r($allFields = $fsField->all($formId));
4143

42-
print_r("\nAdding a new field to the created form");
43-
$param = ["field_type" => "text", "label" => "Dev API Created Field"];
44-
print_r($field = $fsField->newField($formId, $param));
44+
print_r("\nAdding a new field to the created form");
45+
$param = ["field_type" => "text", "label" => "Dev API Created Field"];
46+
print_r($field = $fsField->newField($formId, $param));
4547

46-
$fieldId = $field["id"];
48+
$fieldId = $field["id"];
4749

48-
print("\nDetails of created field \n");
49-
print_r($fieldDetail = $fsField->get($fieldId));
50+
print("\nDetails of created field \n");
51+
print_r($fieldDetail = $fsField->get($fieldId));
5052

51-
print_r("\nUpdating created field ". $fieldDetail["label"]);
53+
print_r("\nUpdating created field " . $fieldDetail["label"]);
5254

53-
$param = ["field_type" => "text", "label" => "Dev API Updated"];
54-
print_r($fsField->update($fieldId, $param));
55+
$param = ["field_type" => "text", "label" => "Dev API Updated"];
56+
print_r($fsField->update($fieldId, $param));
5557

56-
print("\nDetails of updated field");
57-
print_r( $fsField->get($fieldId));
58+
print("\nDetails of updated field");
59+
print_r($fsField->get($fieldId));
5860

5961

60-
print("\nGetting details for form ". $newForm["name"]."\n");
61-
print_r($detail = $fsForm->get($formId));
62+
print("\nGetting details for form " . $newForm["name"] . "\n");
63+
print_r($detail = $fsForm->get($formId));
6264

63-
print_r("\nUpdating the details - name \n");
64-
print_r($fsForm->update($formId, ["name" => $detail["name"] . " updated by API"]));
65+
print_r("\nUpdating the details - name \n");
66+
print_r($fsForm->update($formId, ["name" => $detail["name"] . " updated by API"]));
6567

66-
print("\nGetting details for form after update\n");
67-
print_r($fsForm->get($formId));
68+
print("\nGetting details for form after update\n");
69+
print_r($fsForm->get($formId));
6870

69-
/*
70-
* Testing FSSubmission
71-
* */
71+
/*
72+
* Testing FSSubmission
73+
* */
7274

73-
$fsSubmission = new FSSubmission();
75+
$fsSubmission = new FSSubmission();
7476

75-
print("\nMaking a Submission to the Created Form\n");
76-
$data = [
77-
"field_".$fieldId => "Demo Organization",
78-
];
79-
print_r($fsSubmission->newSubmission($formId, $data));
77+
print("\nMaking a Submission to the Created Form\n");
78+
$data = [
79+
"field_" . $fieldId => "Demo Organization",
80+
];
81+
print_r($fsSubmission->newSubmission($formId, $data));
8082

81-
print("\nCounting all Submissions to the Form\n");
82-
$allSubmissions = $fsSubmission->all($formId);
83-
print("\nThere are " . count($allSubmissions) . " submissions");
83+
print("\nCounting all Submissions to the Form\n");
84+
$allSubmissions = $fsSubmission->all($formId);
85+
print("\nThere are " . count($allSubmissions) . " submissions");
8486

85-
print("\nDetails of a Single Submission\n");
86-
print_r($fsSubmission->get($allSubmissions["submissions"][0]["id"]));
87+
print("\nDetails of a Single Submission\n");
88+
print_r($fsSubmission->get($allSubmissions["submissions"][0]["id"]));
8789

88-
print("\nUpdating a Submission\n");
89-
$data = [
90-
"field_".$fieldId => "Demo Organization Updated",
91-
];
92-
print_r($fsSubmission->update($allSubmissions["submissions"][0]["id"], $data));
90+
print("\nUpdating a Submission\n");
91+
$data = [
92+
"field_" . $fieldId => "Demo Organization Updated",
93+
];
94+
print_r($fsSubmission->update($allSubmissions["submissions"][0]["id"], $data));
9395

9496

97+
/*
98+
* Deleting Operations
99+
* */
95100

101+
print("\nDeleting API Submitted Submission\n");
102+
print_r($fsSubmission->delete($allSubmissions["submissions"][0]["id"]));
96103

104+
print("\nDeleting API Created field\n");
105+
print_r($fsField->delete($fieldId));
97106

98-
/*
99-
* Deleting Operations
100-
* */
107+
print("\nDeleting API created form\n");
108+
print_r($fsForm->delete($formId));
101109

102-
print("\nDeleting API Submitted Submission\n");
103-
print_r($fsSubmission->delete($allSubmissions["submissions"][0]["id"]));
104-
105-
print("\nDeleting API Created field\n");
106-
print_r($fsField->delete($fieldId));
107-
108-
print("\nDeleting API created form\n");
109-
print_r($fsForm->delete($formId));
110+
}catch (RequestException $e) {
111+
print_r($e->getResponse()->getBody());
112+
}
110113

111114
}
112115

0 commit comments

Comments
 (0)