Skip to content

Commit d38efb9

Browse files
committed
# This is a combination of 2 commits.
# This is the 1st commit message: 4268: Added API tests # The commit message #2 will be skipped: # 4268: Added API tests
1 parent ca9759c commit d38efb9

File tree

18 files changed

+539
-44
lines changed

18 files changed

+539
-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: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
COMPOSE_ARGS: pull
22+
- task: compose
23+
vars:
24+
COMPOSE_ARGS: up --detach --wait
25+
- task: composer
26+
vars:
27+
COMPOSER_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+
COMPOSE_ARGS: exec phpfpm bin/console app:fixtures:load {{.ITEM}}
44+
# Loading some fixtures generates an 'Warning: Undefined array key "entityId"' error.
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+
COMPOSE_ARGS: exec phpfpm bin/phpunit
55+
56+
composer:
57+
desc: "Run `composer` command. Example: task composer -- normalize"
58+
cmds:
59+
- task: compose
60+
vars:
61+
COMPOSE_ARGS: exec phpfpm composer {{.COMPOSER_ARGS}}
62+
63+
compose:
64+
desc: "Run `docker compose` command. Example: task compose -- ps"
65+
cmds:
66+
# Run docker compose with both arguments passed via var plus any cli args.
67+
- "{{.DOCKER_COMPOSE}} {{.COMPOSE_ARGS}} {{.CLI_ARGS}}"
68+
69+
coding-standards:apply:
70+
desc: "Apply coding standards"
71+
cmds:
72+
- task: coding-standards:markdown:apply
73+
- task: coding-standards:php:apply
74+
- task: coding-standards:twig:apply
75+
- task: coding-standards:yaml:apply
76+
silent: true
77+
78+
coding-standards:check:
79+
desc: "Apply coding standards"
80+
cmds:
81+
- task: coding-standards:markdown:check
82+
- task: coding-standards:php:check
83+
- task: coding-standards:twig:check
84+
- task: coding-standards:yaml:check
85+
silent: true
86+
87+
coding-standards:markdown:apply:
88+
desc: "Apply coding standards for Markdown"
89+
cmds:
90+
- task: compose
91+
vars:
92+
COMPOSE_ARGS: run --rm markdownlint markdownlint '**/*.md' --fix
93+
94+
coding-standards:markdown:check:
95+
desc: "Apply and check coding standards for Markdown"
96+
cmds:
97+
- task: coding-standards:markdown:apply
98+
- task: compose
99+
vars:
100+
COMPOSE_ARGS: run --rm markdownlint markdownlint '**/*.md'
101+
102+
coding-standards:php:apply:
103+
desc: "Apply coding standards for PHP"
104+
cmds:
105+
- task: compose
106+
vars:
107+
COMPOSE_ARGS: exec phpfpm vendor/bin/php-cs-fixer fix
108+
silent: true
109+
110+
coding-standards:php:check:
111+
desc: "Apply and check coding standards for PHP"
112+
cmds:
113+
- task: coding-standards:php:apply
114+
- task: compose
115+
vars:
116+
COMPOSE_ARGS: exec phpfpm vendor/bin/php-cs-fixer check
117+
silent: true
118+
119+
coding-standards:twig:apply:
120+
desc: "Apply coding standards for Twig"
121+
cmds:
122+
- task: compose
123+
vars:
124+
COMPOSE_ARGS: exec phpfpm vendor/bin/php-cs-fixer fix --dry-run --diff
125+
silent: true
126+
127+
coding-standards:twig:check:
128+
desc: "Apply and check coding standards for Twig"
129+
cmds:
130+
- task: coding-standards:twig:apply
131+
- task: compose
132+
vars:
133+
COMPOSE_ARGS: exec phpfpm vendor/bin/twig-cs-fixer lint
134+
silent: true
135+
136+
coding-standards:yaml:apply:
137+
desc: "Apply coding standards for YAML"
138+
cmds:
139+
- task: compose
140+
vars:
141+
COMPOSE_ARGS: run --rm prettier {{.YAML_FILES_GLOB}} --write
142+
silent: true
143+
144+
coding-standards:yaml:check:
145+
desc: "Apply and check coding standards for YAML"
146+
cmds:
147+
- task: coding-standards:yaml:apply
148+
- task: compose
149+
vars:
150+
COMPOSE_ARGS: run --rm prettier {{.YAML_FILES_GLOB}} --check
151+
silent: true
152+
153+
default:
154+
cmds:
155+
- task --list
156+
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)