-
Notifications
You must be signed in to change notification settings - Fork 0
Added test data setup #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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!