Skip to content

Commit

Permalink
Improve testing and code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
eclipxe13 committed Sep 28, 2022
1 parent 6beaa28 commit 91fce41
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/PackageReader/Internal/FilteredPackageReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,29 @@ public static function createFromFile(string $filename): self
return new self($filename, $archive);
}

/** @inheritDoc */
/**
* @inheritDoc
* @codeCoverageIgnore Unable to produce code coverage for error handling
*/
public static function createFromContents(string $content): self
{
// create temp file
try {
$tmpfile = tempnam(sys_get_temp_dir(), '');
} catch (Throwable $exception) {
/** @codeCoverageIgnore */
throw CreateTemporaryZipFileException::create('Cannot create a temporary file', $exception);
}
if (false === $tmpfile) {
/** @codeCoverageIgnore */
throw CreateTemporaryZipFileException::create('Cannot not create a temporary file');
}

// write contents
try {
$write = file_put_contents($tmpfile, $content);
} catch (Throwable $exception) {
/** @codeCoverageIgnore */
throw CreateTemporaryZipFileException::create('Cannot store contents on temporary file', $exception);
}
if (false === $write) {
/** @codeCoverageIgnore */
throw CreateTemporaryZipFileException::create('Cannot store contents on temporary file');
}

Expand Down
77 changes: 77 additions & 0 deletions tests/Unit/PackageReader/Internal/ThirdPartiesExtractorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

declare(strict_types=1);

namespace PhpCfdi\SatWsDescargaMasiva\Tests\Unit\PackageReader\Internal;

use ArrayIterator;
use LogicException;
use PhpCfdi\SatWsDescargaMasiva\PackageReader\Internal\CsvReader;
use PhpCfdi\SatWsDescargaMasiva\PackageReader\Internal\FileFilters\NullFileFilter;
use PhpCfdi\SatWsDescargaMasiva\PackageReader\Internal\FilteredPackageReader;
use PhpCfdi\SatWsDescargaMasiva\PackageReader\Internal\ThirdPartiesExtractor;
use PhpCfdi\SatWsDescargaMasiva\PackageReader\PackageReaderInterface;
use PhpCfdi\SatWsDescargaMasiva\Tests\TestCase;

final class ThirdPartiesExtractorTest extends TestCase
{
public function testExtractor(): void
{
$source = [
['Uuid', 'RfcACuentaTerceros', 'NombreACuentaTerceros'],
['00000000-aaaa-bbbb-1111-000000000001', 'AAAA010101AAA', 'Registro de ejemplo 1'],
['00000000-aaaa-bbbb-1111-000000000002', 'AAAA010102AAA', 'Registro de ejemplo 2'],
['00000000-aaaa-bbbb-1111-000000000003', 'AAAA010103AAA', 'Registro de ejemplo 3'],
];
$expected = [
'00000000-AAAA-BBBB-1111-000000000001' => [
'RfcACuentaTerceros' => 'AAAA010101AAA',
'NombreACuentaTerceros' => 'Registro de ejemplo 1',
],
'00000000-AAAA-BBBB-1111-000000000002' => [
'RfcACuentaTerceros' => 'AAAA010102AAA',
'NombreACuentaTerceros' => 'Registro de ejemplo 2',
],
'00000000-AAAA-BBBB-1111-000000000003' => [
'RfcACuentaTerceros' => 'AAAA010103AAA',
'NombreACuentaTerceros' => 'Registro de ejemplo 3',
],
];
$extractor = new ThirdPartiesExtractor(new CsvReader(new ArrayIterator($source)));
$this->assertSame($expected, iterator_to_array($extractor->eachRecord()));
}

public function testEmptyUuidIsIgnored(): void
{
$source = [
['Uuid', 'RfcACuentaTerceros', 'NombreACuentaTerceros'],
['', 'AAAA010101AAA', 'Registro de ejemplo 1'],
];
$expected = [];
$extractor = new ThirdPartiesExtractor(new CsvReader(new ArrayIterator($source)));
$this->assertSame($expected, iterator_to_array($extractor->eachRecord()));
}

public function testCreateFromPackageReaderNotFilteredPackageReader(): void
{
$packageReader = $this->createMock(PackageReaderInterface::class);
$this->expectException(LogicException::class);
$this->expectExceptionMessage('PackageReader parameter must be a FilteredPackageReader');
ThirdPartiesExtractor::createFromPackageReader($packageReader);
}

public function testCreateFromPackageReaderRestoreFilter(): void
{
$packageReader = FilteredPackageReader::createFromFile($this->filePath('zip/metadata.zip'));
$filter = new NullFileFilter();
$packageReader->setFilter($filter);

ThirdPartiesExtractor::createFromPackageReader($packageReader);

$this->assertSame(
$filter,
$packageReader->getFilter(),
'FilteredPackageReader filter must not change after call createFromPackageReader'
);
}
}
16 changes: 16 additions & 0 deletions tests/Unit/RequestBuilder/FielRequestBuilder/FielTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace PhpCfdi\SatWsDescargaMasiva\Tests\Unit\RequestBuilder\FielRequestBuilder;

use Exception;
use PhpCfdi\Credentials\Certificate;
use PhpCfdi\Credentials\Credential;
use PhpCfdi\Credentials\Internal\SatTypeEnum;
use PhpCfdi\Credentials\PrivateKey;
use PhpCfdi\SatWsDescargaMasiva\RequestBuilder\FielRequestBuilder\Fiel;
use PhpCfdi\SatWsDescargaMasiva\Tests\TestCase;

Expand Down Expand Up @@ -51,4 +55,16 @@ public function testIsNotValidUsingCsd(): void
);
$this->assertFalse($fiel->isValid());
}

public function testIsNotValidExpiredCertificate(): void
{
$certificate = $this->createMock(Certificate::class);
$certificate->method('satType')->willReturn(SatTypeEnum::fiel());
$certificate->method('validOn')->willReturn(false);
$privateKey = $this->createMock(PrivateKey::class);
$privateKey->method('belongsTo')->willReturn(true);
$credential = new Credential($certificate, $privateKey);
$fiel = new Fiel($credential);
$this->assertFalse($fiel->isValid());
}
}

0 comments on commit 91fce41

Please sign in to comment.