Skip to content

Commit 3feb777

Browse files
committed
Added more unit tests.
1 parent 84852f3 commit 3feb777

21 files changed

+2476
-14
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ jobs:
151151

152152
- name: Run tests
153153
working-directory: build
154-
run: php -d pcov.directory=.. vendor/bin/phpunit || [ "${CI_TEST_IGNORE_FAILURE:-0}" -eq 1 ] || [ "${CI_TEST_ACCEPT_CANARY_FAILURE:-0}" -eq 1 ]
154+
run: php -d pcov.directory=.. vendor/bin/phpunit --coverage-text --colors=never || [ "${CI_TEST_IGNORE_FAILURE:-0}" -eq 1 ] || [ "${CI_TEST_ACCEPT_CANARY_FAILURE:-0}" -eq 1 ]
155155
env:
156156
BROWSERTEST_OUTPUT_DIRECTORY: /tmp
157157
CI_TEST_IGNORE_FAILURE: ${{ vars.CI_TEST_IGNORE_FAILURE }}
@@ -160,11 +160,12 @@ jobs:
160160

161161
- name: Check code coverage
162162
working-directory: build
163-
run: vendor/bin/coverage-check ${CI_CHECK_COVERAGE_COMMAND:-} ../.logs/coverage/phpunit/clover.xml "${CI_CHECK_COVERAGE_THRESHOLD:-50}" || [ "${CI_CHECK_COVERAGE_IGNORE_FAILURE:-0}" -eq 1 ]
163+
run: vendor/bin/coverage-check ${CI_CHECK_COVERAGE_EXTRA:-} ${CI_CHECK_COVERAGE_COMMAND:-} ../.logs/coverage/phpunit/clover.xml "${CI_CHECK_COVERAGE_THRESHOLD:-50}" || [ "${CI_CHECK_COVERAGE_IGNORE_FAILURE:-0}" -eq 1 ]
164164
if: always()
165165
env:
166166
# Drupal 10 uses older version of coverage-check which requires an extra param.
167167
CI_CHECK_COVERAGE_COMMAND: ${{ contains(matrix.name, 'd10') && 'coverage:check' || '' }}
168+
CI_CHECK_COVERAGE_EXTRA: ${{ contains(matrix.name, 'd10') && '--no-ansi' || '' }}
168169
# TODO: Increase the threshold to 80-90% when we have enough tests.
169170
CI_CHECK_COVERAGE_THRESHOLD: ${{ vars.CI_CHECK_COVERAGE_THRESHOLD || 50 }}
170171
# Temporarily accept code coverage check failure.

src/OpenFisca/Client.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@ public function post(string $endpoint, array|RequestPayload $payload) : ?Respons
139139
}
140140
}
141141

142+
/**
143+
* {@inheritDoc}
144+
*/
145+
public function getBaseUri(): string {
146+
return $this->baseApiUri;
147+
}
148+
142149
/**
143150
* {@inheritDoc}
144151
*/

src/OpenFisca/ClientInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ interface ClientInterface {
2626
public const string ENDPOINT_VARIABLE = 'variable';
2727
public const string ENDPOINT_VARIABLES = 'variables';
2828

29+
/**
30+
* Get the base URI of the API Client.
31+
*
32+
* @return string
33+
* The base URI.
34+
*/
35+
public function getBaseUri(): string;
36+
2937
/**
3038
* Get data from an OpenFisca endpoint.
3139
*

src/OpenFisca/Helper.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,23 +58,23 @@ public static function parseOpenFiscaFieldMapping(string $field_mapping, ?string
5858
$keys = explode('.', trim($field_mapping));
5959
$group_entity = NULL;
6060
$entity = NULL;
61+
62+
array_walk($keys, static fn ($key) : string => !empty($key) ? trim((string) $key) : '');
63+
$path = $keys;
64+
6165
// The last element is the variable, e.g 'salary'.
6266
$variable = array_pop($keys);
63-
$path = [$variable];
67+
$parents = $keys;
68+
6469
// The second element is the entity, e.g 'PersonA'.
6570
if (isset($keys[1])) {
6671
$entity = trim($keys[1]);
67-
array_unshift($path, $entity);
6872
}
6973
// The first element is the group entity, e.g 'persons'.
7074
if (isset($keys[0])) {
7175
$group_entity = trim($keys[0]);
72-
array_unshift($path, $group_entity);
7376
}
7477

75-
$parents = $path;
76-
array_pop($parents);
77-
7878
return $variable ?: '';
7979
}
8080
// @codeCoverageIgnoreStart
@@ -142,7 +142,7 @@ public static function formatPeriod(string $period_format = 'DAY', string $perio
142142
'DAY' => $date->format('Y-m-d'),
143143
// Date format yyyy-Wxx eg. 2022-W44.
144144
'WEEK' => $date->format('Y-\WW'),
145-
// Date format yyyy-Wxx-x eg. 2022-W44-2 (2 = Tuesday the weekday number).
145+
// Date format yyyy-Wxx-x eg. 2022-W44-3 (3 = Wednesday the weekday no.).
146146
'WEEKDAY' => $date->format('Y-\WW-N'),
147147
// Date format yyyy-mm eg. 2022-11.
148148
'MONTH' => $date->format('Y-m'),

src/OpenFisca/Payload.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ public function getData(): array {
6565
*/
6666
public static function fromJson(string $json) : static {
6767
$payload = new static();
68-
$payload->payload = !empty($json) ? Json::decode($json) : [];
68+
$data = !empty($json) ? Json::decode($json) : [];
69+
$payload->payload = is_array($data) ? $data : [];
6970
return $payload;
7071
}
7172

@@ -75,7 +76,7 @@ public static function fromJson(string $json) : static {
7576
* @param array|string $key_path
7677
* The key path in the format:
7778
* - array: ['persons', 'PersonA', 'age'].
78-
* - string: 'persons.PersonsA.age'.
79+
* - string: 'persons.PersonA.age'.
7980
*
8081
* @return bool
8182
* TRUE if the key path exists.
@@ -95,7 +96,7 @@ public function keyPathExists(array|string $key_path): bool {
9596
* @param array|string $key_path
9697
* The key path in the format:
9798
* - array: ['persons', 'PersonA', 'age'].
98-
* - string: 'persons.PersonsA.age'.
99+
* - string: 'persons.PersonA.age'.
99100
*
100101
* @return mixed
101102
* The value.
@@ -143,7 +144,7 @@ public function setValue(array|string $key_path, mixed $value) : static {
143144
* @return array|null
144145
* The path of the first matching key, or NULL if not found.
145146
*/
146-
public function findKeyRecursive(array $data, string $key, array $parents = []): ?array {
147+
protected function findKeyRecursive(array $data, string $key, array $parents = []): ?array {
147148
foreach ($data as $data_key => $data_value) {
148149
$parents[] = $data_key;
149150
if ($data_key === $key) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"welcome": "This is the root of an OpenFisca Web API. To learn how to use it, check the general documentation (https://openfisca.org/doc/) and the OpenAPI specification of this instance (https://training-rac.salsadev.au/spec)."
3+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"person": {
3+
"description": "An individual. The minimal legal entity on which Centrelink provides a range of social security payments and services.",
4+
"documentation": "",
5+
"plural": "persons"
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"description": "Minimum age at which a person is eligible for AU Study.",
3+
"id": "austudy_age_threshold",
4+
"metadata": {},
5+
"source": "https://github.com/openfisca/country-template/blob/0.0.1/openfisca_rules/parameters/austudy_age_threshold.yaml",
6+
"values": {
7+
"2024-05-01": 25
8+
}
9+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"austudy_age_threshold": {
3+
"description": "Minimum age at which a person is eligible for AU Study.",
4+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/austudy_age_threshold"
5+
},
6+
"ftb.part_a.base_rate": {
7+
"description": "Family Tax Benefit Part A child base rate.",
8+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/ftb/part_a/base_rate"
9+
},
10+
"ftb.part_a.income_high_threshold": {
11+
"description": "Family Tax Benefit Part A high income threshold.",
12+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/ftb/part_a/income_high_threshold"
13+
},
14+
"ftb.part_a.income_low_threshold": {
15+
"description": "Family Tax Benefit Part A low income threshold.",
16+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/ftb/part_a/income_low_threshold"
17+
},
18+
"ftb.part_a.income_max_threshold": {
19+
"description": "Family Tax Benefit Part A max income threshold.",
20+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/ftb/part_a/income_max_threshold"
21+
},
22+
"ftb.part_a.max_rate_junior_child": {
23+
"description": "Family Tax Benefit Part A junior child maximum rate.",
24+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/ftb/part_a/max_rate_junior_child"
25+
},
26+
"ftb.part_a.max_rate_middle_child": {
27+
"description": "Family Tax Benefit Part A middle child maximum rate.",
28+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/ftb/part_a/max_rate_middle_child"
29+
},
30+
"ftb.part_a.max_rate_senior_child": {
31+
"description": "Family Tax Benefit Part A senior child maximum rate.",
32+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/ftb/part_a/max_rate_senior_child"
33+
},
34+
"ftb.part_a.rate_reduction_higher": {
35+
"description": "Family Tax Benefit Part A rate reduction for income over maximum thresholds.",
36+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/ftb/part_a/rate_reduction_higher"
37+
},
38+
"ftb.part_a.rate_reduction_lower": {
39+
"description": "Family Tax Benefit Part A rate reduction for income between minimum and maximum thresholds.",
40+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/ftb/part_a/rate_reduction_lower"
41+
},
42+
"jobseeker_age_threshold": {
43+
"description": "Minimum age at which a person is eligible for Jobseeker.",
44+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/jobseeker_age_threshold"
45+
},
46+
"pension_age_threshold": {
47+
"description": "Minimum age at which a person is eligible for Pension.",
48+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/pension_age_threshold"
49+
},
50+
"youth_allowance_age_threshold": {
51+
"description": "Maximum age at which a person is eligible for Youth Allowance.",
52+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/youth_allowance_age_threshold"
53+
},
54+
"youth_allowance_for_jobseekers_age_threshold": {
55+
"description": "Maximum age at which a person is eligible for Youth Allowance for Jobseekers.",
56+
"href": "https://fisca-py.main.rac-sandpit-c04-sa.lagoon.salsa.hosting/parameter/youth_allowance_for_jobseekers_age_threshold"
57+
}
58+
}

0 commit comments

Comments
 (0)