Skip to content

Commit c949f4e

Browse files
committed
4268: Added API tests
1 parent ca9759c commit c949f4e

File tree

18 files changed

+538
-44
lines changed

18 files changed

+538
-44
lines changed

.editorconfig

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# EditorConfig is awesome: https://editorconfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*]
7+
end_of_line = lf
8+
insert_final_newline = true
9+
charset = utf-8
10+
indent_style = space
11+
indent_size = 4
12+
13+
[*.{yml,yaml}]
14+
indent_size = 2

.env.test

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ APP_SECRET='$ecretf0rt3st'
44
SYMFONY_DEPRECATIONS_HELPER=999999
55
PANTHER_APP_ENV=panther
66
PANTHER_ERROR_SCREENSHOT_DIR=./var/error-screenshots
7+
8+
APP_API_KEYS='[
9+
{"username": "test_user", "apikey": "test_api_key"}
10+
]'

.github/workflows/build_release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
on:
22
push:
33
tags:
4-
- '*.*.*'
4+
- "*.*.*"
55

66
name: Create Github Release
77

.github/workflows/pr.yaml

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,59 @@
1-
on: pull_request
1+
on:
2+
pull_request:
3+
24
name: Review
5+
6+
env:
7+
COMPOSE_USER: root
8+
39
jobs:
4-
test-suite:
5-
name: Test suite (${{ matrix.php }})
10+
api-test:
11+
name: API test
612
runs-on: ubuntu-latest
7-
strategy:
8-
fail-fast: false
9-
matrix:
10-
php: [ '8.3' ]
1113
steps:
1214
- uses: actions/checkout@v4
1315

14-
- name: Setup PHP, with composer and extensions
15-
uses: shivammathur/setup-php@v2
16-
with:
17-
php-version: ${{ matrix.php}}
18-
extensions: http, ctype, iconv
19-
coverage: xdebug
16+
# https://taskfile.dev/installation/#github-actions
17+
- uses: arduino/setup-task@v2
2018

21-
- name: Get composer cache directory
22-
id: composer-cache
23-
run: echo "::set-output name=dir::$(composer config cache-files-dir)"
19+
- name: Start docker compose setup and install site
20+
run: |
21+
docker network create frontend
22+
task --yes site:update
2423
25-
- name: Cache dependencies
26-
uses: actions/cache@v4
27-
with:
28-
path: ${{ steps.composer-cache.outputs.dir }}
29-
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }}-${{ matrix.dependency-version }}-
30-
restore-keys: ${{ runner.os }}-composer-${{ matrix.dependency-version }}-
24+
- name: Load fixtures
25+
run: |
26+
task --yes fixtures:load
3127
32-
- name: Install Dependencies
33-
run: composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
28+
- name: Run API tests
29+
run: |
30+
task --yes api:test
3431
35-
- name: Test suite
36-
run: ./vendor/bin/phpunit --coverage-clover=coverage/unit.xml
32+
coding-standards-yaml:
33+
name: Coding standards YAML
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v4
3737

38-
- name: Upload coverage to Codecov test
39-
uses: codecov/codecov-action@v2
40-
with:
41-
files: ./coverage/unit.xml
42-
flags: unittests, ${{ matrix.php }}
38+
# https://taskfile.dev/installation/#github-actions
39+
- uses: arduino/setup-task@v2
40+
41+
- run: |
42+
docker network create frontend
43+
44+
- name: Load fixtures
45+
run: |
46+
task --yes coding-standards:yaml:check
47+
48+
- name: Check that nothing has changed
49+
run: git diff --exit-code
4350

4451
psalm:
4552
runs-on: ubuntu-latest
4653
strategy:
4754
fail-fast: false
4855
matrix:
49-
php: ['8.3']
56+
php: ["8.3"]
5057
name: Psalm static analysis (${{ matrix.php}})
5158
steps:
5259
- uses: actions/checkout@v4
@@ -82,7 +89,7 @@ jobs:
8289
strategy:
8390
fail-fast: false
8491
matrix:
85-
php: [ '8.3' ]
92+
php: ["8.3"]
8693
steps:
8794
- name: Checkout
8895
uses: actions/checkout@v4

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
###> symfony/framework-bundle ###
32
/.env.local
43
/.env.local.php
@@ -27,3 +26,5 @@
2726
###> vincentlanglet/twig-cs-fixer ###
2827
/.twig-cs-fixer.cache
2928
###< vincentlanglet/twig-cs-fixer ###
29+
30+
.phpunit.cache

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ See [keep a changelog] for information about writing changes to this log.
99
## [Unreleased]
1010

1111
- [PR-18](https://github.com/itk-dev/event-database-api/pull/18)
12-
Updated docker compose setup
12+
- Updated docker compose setup
13+
- Added simple API tests and resolved some deprecations
1314

1415
## [1.1.1] - 2025-03-28
1516

README.md

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ in this repository.
3333
## Installation
3434

3535
```shell
36-
docker compose up -d
36+
docker compose up --detach
3737
docker compose exec phpfpm composer install
3838
```
3939

@@ -43,19 +43,56 @@ The project comes with doctrine fixtures to help development on local machines.
4343
doctrine fixture load command:
4444

4545
```shell
46-
docker compose exec phpfpm bin/console app:fixtures:load
46+
docker compose exec phpfpm bin/console app:fixtures:load <index>
4747
```
4848

49+
`<index>` must be one of `events`, `organizations`, `occurrences`, `daily_occurrences`, `tags`, `vocabularies` or
50+
`locations` (cf. [`src/Model/IndexName.php`](src/Model/IndexName.php)).
51+
4952
The fixtures are related to the backend where the fixtures are generated by using the `app:index:dump` command. The load
5053
above command downloads the fixtures from
5154
[GitHub](https://github.com/itk-dev/event-database-imports/tree/develop/src/DataFixtures/indexes) and loads them into
5255
ElasticSearch.
5356

54-
### Production
57+
## Accessing the API
58+
59+
To access the API, a valid API key must be presented in the `X-Api-Key` header, e.g.
60+
61+
``` shell
62+
curl --header "X-Api-Key: api_key_1" "http://$(docker compose port nginx 8080)/api/v2/events"
63+
```
64+
65+
Valid API keys are defined via the `APP_API_KEYS` environment variable:
66+
67+
``` shell
68+
# .env.local
69+
APP_API_KEYS='[
70+
{"username": "user_1", "apikey": "api_key_1"},
71+
{"username": "user_2", "apikey": "api_key_2"}
72+
]'
73+
```
74+
75+
## Production
5576

5677
When installing composer and Symfony based application in production, you should not install development packages,
5778
hence use this command:
5879

5980
```shell
6081
docker compose exec phpfpm composer install --no-dev --optimize-autoloader
6182
```
83+
84+
## API request examples
85+
86+
Get events with(out) public access:
87+
88+
``` shell
89+
curl --silent --header "X-Api-Key: api_key_1" "http://$(docker compose port nginx 8080)/api/v2/events?publicAccess=true" | docker run --rm --interactive ghcr.io/jqlang/jq:latest '.["hydra:member"]|length'
90+
curl --silent --header "X-Api-Key: api_key_1" "http://$(docker compose port nginx 8080)/api/v2/events?publicAccess=false" | docker run --rm --interactive ghcr.io/jqlang/jq:latest '.["hydra:member"]|length'
91+
```
92+
93+
## Test
94+
95+
``` shell
96+
task fixtures:load --yes
97+
task api:test --yes
98+
```

Taskfile.yml

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
# https://taskfile.dev
2+
3+
version: "3"
4+
5+
# https://taskfile.dev/usage/#env-files
6+
dotenv: [".env.local", ".env"]
7+
8+
vars:
9+
# https://taskfile.dev/reference/templating/
10+
DOCKER_COMPOSE: '{{.TASK_DOCKER_COMPOSE | default "docker compose"}}'
11+
12+
# We're not yet ready to normalize config files …
13+
YAML_FILES_GLOB: "Taskfile.yml .github/workflows/**.{yml,yaml}"
14+
15+
tasks:
16+
site:update:
17+
desc: Update/install site
18+
cmds:
19+
- task: compose
20+
vars:
21+
CLI_ARGS: pull
22+
- task: compose
23+
vars:
24+
CLI_ARGS: up --detach
25+
- task: composer
26+
vars:
27+
CLI_ARGS: install
28+
29+
fixtures:load:
30+
desc: Load all fixtures
31+
prompt: Really load all fixtures?
32+
cmds:
33+
- for:
34+
- events
35+
- organizations
36+
- occurrences
37+
- daily_occurrences
38+
- tags
39+
- vocabularies
40+
- locations
41+
task: compose
42+
vars:
43+
CLI_ARGS: exec phpfpm bin/console app:fixtures:load {{.ITEM}}
44+
# We can only load each fixture type once (!)
45+
ignore_error: true
46+
silent: true
47+
48+
api:test:
49+
desc: Run API tests
50+
prompt: Make sure to load fixtures before running tests (`task fixtures:load`)
51+
cmds:
52+
- task: compose
53+
vars:
54+
CLI_ARGS: exec phpfpm bin/phpunit {{.CLI_ARGS}}
55+
56+
composer:
57+
desc: "Run `composer` command. Example: task composer -- normalize"
58+
cmds:
59+
- task: compose
60+
vars:
61+
CLI_ARGS: exec phpfpm composer {{.CLI_ARGS}}
62+
63+
compose:
64+
desc: "Run `docker compose` command. Example: task compose -- ps"
65+
cmds:
66+
- "{{.DOCKER_COMPOSE}} {{.CLI_ARGS}}"
67+
68+
coding-standards:apply:
69+
desc: "Apply coding standards"
70+
cmds:
71+
- task: coding-standards:markdown:apply
72+
- task: coding-standards:php:apply
73+
- task: coding-standards:twig:apply
74+
- task: coding-standards:yaml:apply
75+
silent: true
76+
77+
coding-standards:check:
78+
desc: "Apply coding standards"
79+
cmds:
80+
- task: coding-standards:markdown:check
81+
- task: coding-standards:php:check
82+
- task: coding-standards:twig:check
83+
- task: coding-standards:yaml:check
84+
silent: true
85+
86+
coding-standards:markdown:apply:
87+
desc: "Apply coding standards for Markdown"
88+
cmds:
89+
- task: compose
90+
vars:
91+
CLI_ARGS: run --rm markdownlint markdownlint '**/*.md' --fix
92+
93+
coding-standards:markdown:check:
94+
desc: "Apply and check coding standards for Markdown"
95+
cmds:
96+
- task: coding-standards:markdown:apply
97+
- task: compose
98+
vars:
99+
CLI_ARGS: run --rm markdownlint markdownlint '**/*.md'
100+
101+
coding-standards:php:apply:
102+
desc: "Apply coding standards for PHP"
103+
cmds:
104+
- task: compose
105+
vars:
106+
CLI_ARGS: exec phpfpm vendor/bin/php-cs-fixer fix
107+
silent: true
108+
109+
coding-standards:php:check:
110+
desc: "Apply and check coding standards for PHP"
111+
cmds:
112+
- task: coding-standards:php:apply
113+
- task: compose
114+
vars:
115+
CLI_ARGS: exec phpfpm vendor/bin/php-cs-fixer check
116+
silent: true
117+
118+
coding-standards:twig:apply:
119+
desc: "Apply coding standards for Twig"
120+
cmds:
121+
- task: compose
122+
vars:
123+
CLI_ARGS: exec phpfpm vendor/bin/php-cs-fixer fix --dry-run --diff
124+
silent: true
125+
126+
coding-standards:twig:check:
127+
desc: "Apply and check coding standards for Twig"
128+
cmds:
129+
- task: coding-standards:twig:apply
130+
- task: compose
131+
vars:
132+
CLI_ARGS: exec phpfpm vendor/bin/twig-cs-fixer lint
133+
silent: true
134+
135+
coding-standards:yaml:apply:
136+
desc: "Apply coding standards for YAML"
137+
cmds:
138+
- task: compose
139+
vars:
140+
CLI_ARGS: run --rm prettier {{.YAML_FILES_GLOB}} --write
141+
silent: true
142+
143+
coding-standards:yaml:check:
144+
desc: "Apply and check coding standards for YAML"
145+
cmds:
146+
- task: coding-standards:yaml:apply
147+
- task: compose
148+
vars:
149+
CLI_ARGS: run --rm prettier {{.YAML_FILES_GLOB}} --check
150+
silent: true
151+
152+
default:
153+
cmds:
154+
- task --list
155+
silent: true

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"friendsofphp/php-cs-fixer": "^3.40",
3434
"phpunit/phpunit": "^10.5",
3535
"psalm/plugin-symfony": "^5.1",
36+
"symfony/browser-kit": "~6.4.0",
3637
"symfony/maker-bundle": "^1.52",
3738
"symfony/phpunit-bridge": "^7.0",
3839
"symfony/stopwatch": "~6.4.0",

0 commit comments

Comments
 (0)