Skip to content
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

Dev 18195 add tests #15

Merged
merged 4 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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: 1 addition & 1 deletion src/Import/Application/Importer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

final readonly class Importer
{
private const NAME = 'Amazon S3 Parquet Importer';
public const NAME = 'Amazon S3 Parquet Importer';

public function __construct(
private Transporter $transporter,
Expand Down
103 changes: 103 additions & 0 deletions tests/ImporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Productsup\BinCdeAmazonS3Parquet\Import\Application\Converter\ParquetFileConverter;
use Productsup\BinCdeAmazonS3Parquet\Import\Application\Importer;
use Productsup\BinCdeAmazonS3Parquet\Import\Application\Processor\Processor;
use Productsup\BinCdeAmazonS3Parquet\Import\Application\Transporter\Transporter;
use Productsup\DK\Connector\Application\Feed\OutputFeedForImport;
use Productsup\DK\Connector\Application\Logger\ConnectorFinished;
use Productsup\DK\Connector\Application\Logger\ConnectorLogger;
use Productsup\DK\Connector\Application\Logger\ConnectorStarted;

class ImporterTest extends TestCase
{
private $transporter;
private $logger;
private $outputFeedForImport;
private $processor;
private $converter;
private $importer;

protected function setUp(): void
{
$this->transporter = $this->createMock(Transporter::class);
$this->logger = $this->createMock(ConnectorLogger::class);
$this->outputFeedForImport = $this->createMock(OutputFeedForImport::class);
$this->processor = $this->createMock(Processor::class);
$this->converter = $this->createMock(ParquetFileConverter::class);

$this->importer = new Importer(
$this->transporter,
$this->logger,
$this->outputFeedForImport,
$this->processor,
$this->converter
);
}

public function testImportProcessIsSuccessful(): void
{
$this->logger->expects($this->once())
->method('info')
->with($this->equalTo(ConnectorStarted::fromName(Importer::NAME)));

$this->transporter->expects($this->once())
->method('transport');

$this->converter->expects($this->once())
->method('convert');

$this->processor->expects($this->once())
->method('processFile')
->willReturn((function () {
yield ['row1'];
yield ['row2'];
})());

$this->outputFeedForImport->expects($this->exactly(2))
->method('appendToOutputFeed');

$this->outputFeedForImport->expects($this->once())
->method('end');

$this->logger->expects($this->once())
->method('success')
->with($this->equalTo(ConnectorFinished::fromName(Importer::NAME)));

$this->importer->import();
}

public function testImportProcessWithNoRows(): void
{
$this->logger->expects($this->once())
->method('info')
->with($this->equalTo(ConnectorStarted::fromName(Importer::NAME)));

$this->transporter->expects($this->once())
->method('transport');

$this->converter->expects($this->once())
->method('convert');

$this->processor->expects($this->once())
->method('processFile')
->willReturn((function () {
yield from [];
})());

$this->outputFeedForImport->expects($this->never())
->method('appendToOutputFeed');

$this->outputFeedForImport->expects($this->once())
->method('end');

$this->logger->expects($this->once())
->method('success')
->with($this->equalTo(ConnectorFinished::fromName(Importer::NAME)));

$this->importer->import();
}
}
29 changes: 29 additions & 0 deletions tests/ParquetFileConverterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

use PHPUnit\Framework\TestCase;
use Productsup\BinCdeAmazonS3Parquet\Import\Infrastructure\Converter\Exception\FailedQueryExecutionException;
use Productsup\BinCdeAmazonS3Parquet\Import\Infrastructure\Converter\ParquetFileConverter;

final class ParquetFileConverterTest extends TestCase
{
public function testSuccessfulConversion(): void
{
$tempFilename = (__DIR__.'/fixtures/test.parquet');
$converter = new ParquetFileConverter($tempFilename);
$converter->convert();
$this->assertEquals(file_get_contents(__DIR__.'/fixtures/expected.json'), file_get_contents('out.json'));
unlink((__DIR__.'/../out.json'));

Check warning on line 17 in tests/ParquetFileConverterTest.php

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

tests/ParquetFileConverterTest.php#L17

Using user input when deleting files with `unlink()` is potentially dangerous.
}

public function testFailedConversionDueToInvalidFile(): void
{
$tempFilename = '/invalid/path/to/file.parquet';
$converter = new ParquetFileConverter($tempFilename);

$this->expectException(FailedQueryExecutionException::class);

$converter->convert();
}
}
39 changes: 39 additions & 0 deletions tests/S3TransporterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

use League\Flysystem\FilesystemOperator;
use League\Flysystem\UnableToWriteFile;
use PHPUnit\Framework\TestCase;
use Productsup\BinCdeAmazonS3Parquet\Import\Infrastructure\Transporter\Exception\DownloadFailed;
use Productsup\BinCdeAmazonS3Parquet\Import\Infrastructure\Transporter\S3Transporter;
use Productsup\DK\Connector\Application\Logger\ConnectorLogger;

final class S3TransporterTest extends TestCase
{
public function testSuccessfulFileTransport(): void
{
$filesystemOperator = $this->createMock(FilesystemOperator::class);
$logger = $this->createMock(ConnectorLogger::class);
$transporter = new S3Transporter($filesystemOperator, $logger, 'filename', 'tempFilename');

$filesystemOperator->method('read')->willReturn('file content');

$this->expectNotToPerformAssertions();

$transporter->transport();
}

public function testFailedFileTransportDueToFilesystemException(): void
{
$filesystemOperator = $this->createMock(FilesystemOperator::class);
$logger = $this->createMock(ConnectorLogger::class);
$transporter = new S3Transporter($filesystemOperator, $logger, 'filename', 'tempFilename');

$filesystemOperator->method('read')->willThrowException(new UnableToWriteFile('error message'));

$this->expectException(DownloadFailed::class);

$transporter->transport();
}
}
Loading
Loading