Skip to content

Commit be5923d

Browse files
committed
4268: Improved test setup
1 parent c09b61c commit be5923d

File tree

14 files changed

+252
-54
lines changed

14 files changed

+252
-54
lines changed

.github/workflows/pr.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ jobs:
2121
docker network create frontend
2222
task --yes site:update
2323
24-
- name: Load fixtures
24+
- name: Load test fixtures
2525
run: |
26-
task --yes fixtures:load
26+
task --yes fixtures:load:test
2727
2828
- name: Run API tests
2929
run: |

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ curl --silent --header "X-Api-Key: api_key_1" "http://$(docker compose port ngin
117117
118118
## Test
119119
120-
``` shell
121-
task fixtures:load --yes
122-
task api:test --yes
120+
``` shell name=run-tests
121+
task fixtures:load:test --yes
122+
task api:test
123123
```

Taskfile.yml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ tasks:
4545
ignore_error: true
4646
silent: true
4747

48-
api:test:
49-
desc: Run API tests
50-
prompt: This will reset your local data. Continue?
48+
fixtures:load:test:
49+
desc: Load local fixtures
50+
prompt: Really load all local fixtures?
5151
cmds:
52-
# Load local fixtures.
5352
- for:
5453
- events
5554
- organizations
@@ -61,11 +60,15 @@ tasks:
6160
task: compose
6261
vars:
6362
COMPOSE_ARGS: exec phpfpm bin/console app:fixtures:load {{.ITEM}} --url=file:///app/tests/resources/{{.ITEM}}.json
63+
# Loading some fixtures generates an 'Warning: Undefined array key "entityId"' error.
64+
ignore_error: true
65+
66+
api:test:
67+
desc: Run API tests
68+
cmds:
6469
- task: compose
6570
vars:
6671
COMPOSE_ARGS: exec phpfpm bin/phpunit
67-
# Loading some fixtures generates an 'Warning: Undefined array key "entityId"' error.
68-
ignore_error: true
6972

7073
composer:
7174
desc: "Run `composer` command. Example: task composer -- normalize"

src/Fixtures/FixtureLoader.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,11 @@ private function indexItems(string $indexName, array $items): void
103103
foreach ($items as $item) {
104104
$params = [
105105
'index' => $indexName,
106-
'id' => $item['entityId'],
107106
'body' => $item,
108107
];
108+
if (isset($item['entityId'])) {
109+
$params['id'] = $item['entityId'];
110+
}
109111
try {
110112
// No other places in this part of the frontend should index data, hence it's not in the index service.
111113
$response = $this->client->index($params);

tests/ApiPlatform/AbstractApiTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44

55
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
66
use ApiPlatform\Symfony\Bundle\Test\Client;
7+
use ApiPlatform\Symfony\Bundle\Test\Response;
78

89
abstract class AbstractApiTestCase extends ApiTestCase
910
{
11+
protected static string $requestPath;
12+
1013
protected static function createAuthenticatedClient(): Client
1114
{
1215
return static::createClient(defaultOptions: [
@@ -16,4 +19,13 @@ protected static function createAuthenticatedClient(): Client
1619
],
1720
]);
1821
}
22+
23+
protected function get(array $query, ?string $path = null, bool $authenticated = true): Response
24+
{
25+
$path ??= static::$requestPath;
26+
27+
$client = $authenticated ? self::createAuthenticatedClient() : self::createClient();
28+
29+
return $client->request('GET', $path.(str_contains($path, '?') ? '&' : '?').http_build_query($query));
30+
}
1931
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace ApiPlatform;
4+
5+
use App\Tests\ApiPlatform\AbstractApiTestCase;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
8+
/**
9+
* Test that filtering events work as expected.
10+
*/
11+
class EventsFilterTest extends AbstractApiTestCase
12+
{
13+
protected static string $requestPath = '/api/v2/events';
14+
15+
#[DataProvider('getEventsProvider')]
16+
public function testGetEvents(array $query, int $expectedCount): void
17+
{
18+
$response = $this->get($query);
19+
20+
$data = $response->toArray();
21+
$this->assertArrayHasKey('hydra:member', $data);
22+
$this->assertCount($expectedCount, $data['hydra:member']);
23+
}
24+
25+
public static function getEventsProvider(): iterable
26+
{
27+
yield [
28+
[],
29+
3,
30+
];
31+
32+
yield [
33+
['publicAccess' => 'true'],
34+
2,
35+
];
36+
37+
yield [
38+
['publicAccess' => 'false'],
39+
1,
40+
];
41+
}
42+
}

tests/ApiPlatform/EventsTest.php

Lines changed: 6 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,14 @@
22

33
namespace App\Tests\ApiPlatform;
44

5-
use Symfony\Component\HttpFoundation\Response;
5+
use App\Tests\ApiPlatform\Trait\GetEntitiesTestTrait;
66

7+
/**
8+
* Test that we can call the API.
9+
*/
710
class EventsTest extends AbstractApiTestCase
811
{
9-
public function testGetEvents(): void
10-
{
11-
static::createClient()
12-
->request('GET', '/api/v2/events');
12+
use GetEntitiesTestTrait;
1313

14-
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
15-
16-
$response = static::createAuthenticatedClient()
17-
->request('GET', '/api/v2/events');
18-
19-
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
20-
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
21-
$this->assertJson($response->getContent());
22-
23-
$data = $response->toArray();
24-
$this->assertArrayHasKey('hydra:member', $data);
25-
$this->assertCount(2, $data['hydra:member']);
26-
27-
$response = static::createAuthenticatedClient()
28-
->request('GET', '/api/v2/events?'.http_build_query(['publicAccess' => 'true']));
29-
30-
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
31-
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
32-
$this->assertJson($response->getContent());
33-
34-
$data = $response->toArray();
35-
$this->assertArrayHasKey('hydra:member', $data);
36-
$this->assertCount(1, $data['hydra:member']);
37-
38-
$response = static::createAuthenticatedClient()
39-
->request('GET', '/api/v2/events?'.http_build_query(['publicAccess' => 'false']));
40-
41-
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
42-
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
43-
$this->assertJson($response->getContent());
44-
45-
$data = $response->toArray();
46-
$this->assertArrayHasKey('hydra:member', $data);
47-
$this->assertCount(1, $data['hydra:member']);
48-
}
14+
protected static string $requestPath = '/api/v2/events';
4915
}

tests/ApiPlatform/TagsTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Tests\ApiPlatform;
4+
5+
use App\Tests\ApiPlatform\Trait\GetEntitiesTestTrait;
6+
7+
/**
8+
* Test that we can call the API.
9+
*/
10+
class TagsTest extends AbstractApiTestCase
11+
{
12+
use GetEntitiesTestTrait;
13+
14+
protected static string $requestPath = '/api/v2/tags';
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\Tests\ApiPlatform\Trait;
4+
5+
use App\Tests\ApiPlatform\AbstractApiTestCase;
6+
use Symfony\Component\HttpFoundation\Response;
7+
8+
trait GetEntitiesTestTrait
9+
{
10+
public function testGetEntities(): void
11+
{
12+
assert($this instanceof AbstractApiTestCase);
13+
$this->get([], authenticated: false);
14+
15+
$this->assertResponseStatusCodeSame(Response::HTTP_UNAUTHORIZED);
16+
17+
$response = $this->get([]);
18+
19+
$this->assertResponseStatusCodeSame(Response::HTTP_OK);
20+
$this->assertResponseHeaderSame('content-type', 'application/ld+json; charset=utf-8');
21+
$this->assertJson($response->getContent());
22+
}
23+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace App\Tests\ApiPlatform;
4+
5+
use App\Tests\ApiPlatform\Trait\GetEntitiesTestTrait;
6+
7+
/**
8+
* Test that we can call the API.
9+
*/
10+
class VocabulariesTest extends AbstractApiTestCase
11+
{
12+
use GetEntitiesTestTrait;
13+
14+
protected static string $requestPath = '/api/v2/vocabularies';
15+
}

0 commit comments

Comments
 (0)