Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* [PR-10](https://github.com/itk-dev/enter/pull/10)
Added test data setup
* [PR-19](https://github.com/itk-dev/enter/pull/19)
Declare source metadata in an `#[AsDataSource]` attribute on the source class
* [PR-12](https://github.com/itk-dev/enter/pull/12)
Expand Down
48 changes: 48 additions & 0 deletions docs/Testing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Testing

## Test sources

For local testing and development we use a test controller that's only enabled in the `dev` and `test` environments.

Furthermore, we use static test sources (fetching locally stored data) for testing and development. Test sources are
identified by the `#[TestDefinition]` attribute (rather than `#[Definition]` as real sources).

Test sources can be listed with the `test:source:list` command:

```shell
docker compose exec phpfpm php bin/console test:source:list
```

(the `app:source:list` command will list all source; including test sources.)

Example: Import and show data from the test source `test:mtm_spatialmaps-handicap-parking`:

```shell
docker compose exec phpfpm php bin/console app:source:import test:mtm_spatialmaps-handicap-parking
docker compose exec phpfpm curl 'http://scorpio:9090/ngsi-ld/v1/entities?type=https://smartdatamodels.org/dataModel.Parking/OnStreetParking'
```

See the result on <https://enter.local.itkdev.dk/test>.

### Refreshing test source data

The data for test sources are stored as plain files in the [../tests/resources/data](../tests/resources/data) folder.

The data files can be updated by running

```shell
docker compose exec phpfpm php bin/console test:source:fetch-content
```

As shown above, test sources can be imported just like real sources, but for convenience the `test:sources:import`
command can be used to import *all test sources*:

```shell
docker compose exec phpfpm php bin/console test:sources:import
```

To empty your local broker, e.g. before loading test data, run

```shell
docker compose exec phpfpm php bin/console app:broker:entity:delete --all
```
5 changes: 5 additions & 0 deletions src/Command/BrokerEntityDelete.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Attribute\Option;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\RuntimeException;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -30,6 +31,10 @@ public function __invoke(
if ($all) {
$data = $brokerClient->request(Request::METHOD_GET, '/ngsi-ld/v1/types')->toArray();
$entityTypes = $data['typeList'] ?? [];
} else {
if (0 === count($entityTypes)) {
throw new RuntimeException('Missing entity types');
}
}

$limit = 1000;
Expand Down
9 changes: 0 additions & 9 deletions src/Controller/DataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Yaml\Yaml;
use Symfony\Contracts\HttpClient\HttpClientInterface;

final class DataController extends AbstractController
Expand Down Expand Up @@ -54,12 +53,4 @@ public function index(Request $request, string $path, string $_format,
json: true,
);
}

#[Route('/test', name: 'data_test')]
public function test(): JsonResponse
{
$data = Yaml::parseFile(__DIR__.'/data.yaml');

return new JsonResponse($data);
}
}
2 changes: 1 addition & 1 deletion src/Controller/DefaultController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class DefaultController extends AbstractController
{
#[Route('/{path}', name: 'app_default', requirements: ['path' => Requirement::CATCH_ALL], methods: [Request::METHOD_GET])]
#[Route('/{path}', name: 'app_default', requirements: ['path' => Requirement::CATCH_ALL], methods: [Request::METHOD_GET], priority: -9999)]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a low priority!

public function index(?string $path = null): Response
{
return $this->render(null === $path ? 'default/index.html.twig' : sprintf('default/%s.html.twig', $path));
Expand Down
78 changes: 78 additions & 0 deletions src/Controller/TestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Routing\Requirement\Requirement;
use Symfony\Component\Yaml\Yaml;

#[When('dev')]
#[When('test')]
#[Route('/test', name: 'test_')]
final class TestController extends AbstractController
{
private const string FORMAT_JSON = 'json';
private const string FORMAT_GEOJSON = 'geojson';

private const string APPLICATION_GEOJSON = 'application/geo+json';
private const string APPLICATION_JSON = 'application/json';

#[Route('/{path}', name: 'default', requirements: ['path' => Requirement::CATCH_ALL], methods: [Request::METHOD_GET], priority: -99)]
public function index(?string $path = null): Response
{
return $this->render(null === $path ? 'test/index.html.twig' : sprintf('test/%s.html.twig', $path));
}

#[Route(
path: '/data/{path}.{_format}',
methods: [Request::METHOD_GET],
requirements: [
'path' => Requirement::CATCH_ALL,
'_format' => 'json|geojson',
],
defaults: ['_format' => self::FORMAT_JSON],
priority: -98,
)]
public function data(Request $request, string $path, string $_format): Response
{
// Remove "/test/"
$path = substr($request->getRequestUri(), 6);
$path = realpath(__DIR__.'/../../tests/resources/'.$path);
if (!file_exists($path)) {
throw new NotFoundHttpException($path);
}

$contentType = match ($_format) {
self::FORMAT_GEOJSON => self::APPLICATION_GEOJSON,
default => self::APPLICATION_JSON,
};

return new BinaryFileResponse($path, headers: [
'content-type' => $contentType,
]);
}

#[Route('/config', name: 'config', methods: [Request::METHOD_GET])]
public function config(
#[MapQueryParameter('type')]
string $type,
): JsonResponse {
$configName = match ($type) {
'https://smartdatamodels.org/dataModel.Parking/OnStreetParking' => 'Parking/OnStreetParking',
default => throw new BadRequestHttpException('Invalid type'),
};

$data = Yaml::parseFile(__DIR__.'/../../tests/resources/config/'.$configName.'.yaml');

return new JsonResponse($data);
}
}
2 changes: 1 addition & 1 deletion src/Source/Definition.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(
public static function of(string $class): self
{
$reflection = new \ReflectionClass($class);
$attribute = $reflection->getAttributes(Definition::class)[0]
$attribute = $reflection->getAttributes(Definition::class, flags: \ReflectionAttribute::IS_INSTANCEOF)[0]
?? throw new \LogicException(sprintf('Source %s declares no #[%s] attribute.', $class, Definition::class));

return $attribute->newInstance();
Expand Down
60 changes: 60 additions & 0 deletions src/Test/Command/SourceFetchContentCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace App\Test\Command;

use App\SourceManager;
use App\Test\Source\TestDefinition;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\When;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsCommand(
name: 'test:sources:fetch-content',
description: 'Fetch test source content',
)]
#[When('dev')]
class SourceFetchContentCommand
{
public function __invoke(
SymfonyStyle $io,
SourceManager $manager,
HttpClientInterface $httpClient,
Filesystem $filesystem,
OutputInterface $output,
Application $application,
): int {
foreach ($manager->getSources() as $source) {
$definition = $source->definition;
if (!$definition instanceof TestDefinition) {
continue;
}

try {
$io->section($source);
$url = $manager->getSource($definition->sourceId)->definition->accessUrl;
$filename = preg_replace('@^[a-z]+://[^/]+/test/@', '', $definition->accessUrl);
$filename = __DIR__.'/../../../tests/resources/'.$filename;

if ($filesystem->exists($filename)) {
$filesystem->remove($filename);
}

$io->writeln(sprintf('Fetching "%s"', $url));
$response = $httpClient->request(Request::METHOD_GET, $url);
$content = $response->getContent();
$filesystem->dumpFile($filename, $content);
$io->success(sprintf('Content written to file %s', realpath($filename)));
} catch (\Exception $e) {
$io->error($e->getMessage());
}
}

return Command::SUCCESS;
}
}
32 changes: 32 additions & 0 deletions src/Test/Command/SourceListCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Test\Command;

use App\Source\SourceInterface;
use App\SourceManager;
use App\Test\Source\TestDefinition;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\When;

#[AsCommand(
name: 'test:source:list',
)]
#[When('dev')]
class SourceListCommand
{
public function __invoke(
SymfonyStyle $io,
SourceManager $manager,
): int {
$sources = array_filter($manager->getSources(), static fn (SourceInterface $source) => $source->definition instanceof TestDefinition);

$io->writeln(sprintf('#sources: %d', \count($sources)));
foreach ($sources as $source) {
$io->writeln((string) $source);
}

return Command::SUCCESS;
}
}
50 changes: 50 additions & 0 deletions src/Test/Command/SourcesImportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Test\Command;

use App\SourceManager;
use App\Test\Source\TestDefinition;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Attribute\When;

#[AsCommand(
name: 'test:sources:import',
description: 'Import all test sources',
)]
#[When('dev')]
class SourcesImportCommand
{
public function __invoke(
SymfonyStyle $io,
SourceManager $manager,
OutputInterface $output,
Application $application,
): int {
foreach ($manager->getSources() as $source) {
$definition = $source->definition;
if (!$definition instanceof TestDefinition) {
continue;
}

try {
$io->section($source);
$input = new ArrayInput([
'command' => 'app:source:import',
'source' => $source->definition->id,
]);
$input->setInteractive(false);

$application->doRun($input, $output);
} catch (\Exception $e) {
$io->error($e->getMessage());
}
}

return Command::SUCCESS;
}
}
Loading