Skip to content

Commit

Permalink
feat: update DataGovUkExport.php to upload to S3 bucket. WIP!
Browse files Browse the repository at this point in the history
  • Loading branch information
fibble committed Oct 3, 2024
1 parent 162ea57 commit 7a3a4ff
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 82 deletions.
2 changes: 2 additions & 0 deletions app/api/module/Api/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@
ApiSrv\GovUkAccount\GovUkAccountService::class => ApiSrv\GovUkAccount\GovUkAccountServiceFactory::class,

ApiSrv\AddressHelper\AddressHelperService::class => ApiSrv\AddressHelper\AddressHelperServiceFactory::class,

Aws\S3\S3Client::class => Dvsa\Olcs\Api\Service\S3\S3ClientFactory::class,
],
],
'view_manager' => [
Expand Down
23 changes: 23 additions & 0 deletions app/api/module/Api/src/Service/S3/S3ClientFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Dvsa\Olcs\Api\Service\S3;

use Aws\S3\S3Client;
use Psr\Container\ContainerInterface;

class S3ClientFactory
{
public function __invoke(ContainerInterface $container)
{
$config = $container->get('config');
$awsOptions = $config['awsOptions'];

$s3Client = new S3Client([
'version' => $awsOptions['version'],
'region' => $awsOptions['region'],
'use_path_style_endpoint' => $awsOptions['s3']['use_path_style_endpoint'],
]);

return $s3Client;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
$this->path = ltrim($parsedUrl['path'], '/');
}

$this->s3Client = new S3Client([
'version' => 'latest',
'region' => $config['awsOptions']['region'],
]);
$this->s3Client = $container->get(S3Client::class);

return parent::__invoke($container, $requestedName, $options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,7 @@ public function __invoke(ContainerInterface $container, $requestedName, array $o
$this->path = ltrim($parsedUrl['path'], '/');
}

$this->s3Client = new S3Client([
'version' => 'latest',
'region' => $config['awsOptions']['region'],
]);
$this->s3Client = $container->get(S3Client::class);

return parent::__invoke($container, $requestedName, $options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace Dvsa\OlcsTest\Cli\Domain\CommandHandler;

use Aws\S3\S3Client;
use Doctrine\DBAL\Result;
use Dvsa\OlcsTest\Api\Domain\CommandHandler\AbstractCommandHandlerTestCase;
use Dvsa\Olcs\Cli\Domain\Command\DataDvaNiExport as Cmd;
use Dvsa\Olcs\Cli\Domain\CommandHandler\DataDvaNiExport;
use Dvsa\Olcs\Api\Domain\Repository;
use org\bovigo\vfs\vfsStream;
use Mockery as m;
use Dvsa\Olcs\Api\Domain\Util\DateTime\DateTime;

Expand All @@ -22,20 +22,7 @@ class DataDvaNiExportTest extends AbstractCommandHandlerTestCase
*/
protected $sut;

/**
* @var string
*/
private $tmpPath;

/**
* @var vfsStream
*/
private $vfsStream;

/**
* @var m\MockInterface
*/
private $mockStmt;
private $tempDir;

public function setUp(): void
{
Expand All @@ -49,19 +36,40 @@ public function setUp(): void

$this->mockDbalResult = m::mock(Result::class);

// mock config
$this->mockedSmServices['config'] = [
'data-dva-ni-export' => [
'path' => 'unit_CfgPath',
's3_uri' => 's3://testbucket/testprefix/',
],
];

$this->mockS3client = m::mock(S3Client::class);
$this->mockedSmServices[S3Client::class] = $this->mockS3client;

$this->sut = new DataDvaNiExport();

parent::setUp();
}

$this->vfsStream = vfsStream::setup('root');
$this->tmpPath = $this->vfsStream->url() . '/unit';
private function createTempDir()
{
$this->tempDir = sys_get_temp_dir() . '/phpunit_' . uniqid();
mkdir($this->tempDir);
return $this->tempDir;
}

private function cleanupRealTempDir()
{
if ($this->tempDir && is_dir($this->tempDir)) {
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($this->tempDir, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($files as $fileinfo) {
$todo = ($fileinfo->isDir() ? 'rmdir' : 'unlink');
$todo($fileinfo->getRealPath());
}
rmdir($this->tempDir);
}
}

public function testInvalidReportException()
Expand All @@ -82,10 +90,12 @@ public function testInvalidReportException()

public function testNiOperatorLicence()
{
$tempDir = $this->createTempDir();

$cmd = Cmd::create(
[
'reportName' => DataDvaNiExport::NI_OPERATOR_LICENCE,
'path' => $this->tmpPath,
'path' => $tempDir,
]
);

Expand All @@ -112,28 +122,24 @@ public function testNiOperatorLicence()
->once()
->andReturn($this->mockDbalResult);

$this->mockS3client->shouldReceive('putObject')->once()->andReturn([]);

// call & check
$actual = $this->sut->handleCommand($cmd);

$date = new DateTime('now');

$expectFile = $this->tmpPath . '/NiGvLicences-' . $date->format(DataDvaNiExport::FILE_DATETIME_FORMAT) . '.csv';
$expectCsvFile = '/tmp/NiGvLicences-' . $date->format(DataDvaNiExport::FILE_DATETIME_FORMAT) . '.csv';
$expectTgzFile = 'dvaoplic-' . $date->format(DataDvaNiExport::FILE_DATETIME_FORMAT) . '.tar.gz';

$expectMsg =
'Fetching data from DB for NI Operator Licences' .
'create csv file: ' . $expectFile;
'Creating CSV file: ' . $expectCsvFile .
'Uploaded file to S3: ' . $expectTgzFile;

static::assertEquals(
$expectMsg,
implode('', $actual->toArray()['messages'])
);

static::assertSame(
'LicenceNumber,LicenceType' . "\n" .
'123455,test_type' . "\n" .
'123456,test_type' . "\n" .
'123457,test_type' . "\n",
file_get_contents($expectFile)
);
}
}
Loading

0 comments on commit 7a3a4ff

Please sign in to comment.