Skip to content

Commit

Permalink
Merge pull request #8 from eclipxe13/master
Browse files Browse the repository at this point in the history
dev: increase code coverage
  • Loading branch information
blacktrue authored Aug 23, 2019
2 parents fc6eccb + bff6255 commit 5af2da8
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 6 deletions.
6 changes: 6 additions & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ In summary, [SemVer](https://semver.org/) can be viewed as ` Breaking . Feature
**Version `0.x.x` doesn't have to apply any of the SemVer rules**


## Version 0.2.2 2019-08-20

- Make sure that when constructing a `DateTime` it fails with an exception.
- Improve code coverage.


## Version 0.2.1 2019-08-20

- Make `PackageReader\MetadataContent` tolerant to non-strict CSV contents:
Expand Down
5 changes: 4 additions & 1 deletion src/PackageReader/AbstractPackageReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,16 @@ public function count(): int
}

/**
* @return Generator|string[]
* Generates the list of name => contents and yield only entries that pass name and content filters
*
* @return Generator|string[] pair of file name and contents
*/
public function fileContents()
{
for ($i = 0; $i < $this->zip->numFiles; $i++) {
$filename = strval($this->zip->getNameIndex($i));
if ('' === $filename) {
/** @codeCoverageIgnore */
continue; // cannot get the file name
}
if (! $this->filterEntryFilename($filename)) {
Expand Down
13 changes: 10 additions & 3 deletions src/Shared/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DateTimeImmutable;
use DateTimeZone;
use InvalidArgumentException;
use Throwable;

class DateTime
{
Expand All @@ -15,11 +16,17 @@ class DateTime

public function __construct($value = null)
{
$value = $value ?? 'now';
if (is_int($value)) {
$value = '@' . $value;
$value = sprintf('@%d', $value);
}
if (null === $value || is_string($value)) {
$value = new DateTimeImmutable($value ?? 'now');
if (is_string($value)) {
try {
$value = new DateTimeImmutable($value ?? 'now');
} catch (Throwable $exception) {
$message = sprintf('Unable to create a Datetime("%s")', strval($value));
throw new InvalidArgumentException($message, 0, $exception);
}
}
if (! $value instanceof DateTimeImmutable) {
throw new InvalidArgumentException('Unable to create a Datetime');
Expand Down
3 changes: 2 additions & 1 deletion tests/Unit/PackageReader/CfdiPackageReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@

use PhpCfdi\SatWsDescargaMasiva\PackageReader\CfdiPackageReader;
use PhpCfdi\SatWsDescargaMasiva\Tests\TestCase;
use RuntimeException;

class CfdiPackageReaderTest extends TestCase
{
public function testReaderZipWhenTheContentIsInvalid(): void
{
$zipContents = 'INVALID_ZIP_CONTENT';
$this->expectException(\RuntimeException::class);
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Could not open zip');
CfdiPackageReader::createFromContents($zipContents);
}
Expand Down
1 change: 1 addition & 0 deletions tests/Unit/Services/Download/DownloadTranslatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function testCreateDownloadResultFromSoapResponseWithPackage(): void
$result = $translator->createDownloadResultFromSoapResponse($responseBody);
$status = $result->getStatus();

$this->assertGreaterThan(0, $result->getPackageLenght());
$this->assertNotEmpty($result->getPackageContent());
$this->assertEquals($expectedStatusCode, $status->getCode());
$this->assertEquals($expectedMessage, $status->getMessage());
Expand Down
21 changes: 21 additions & 0 deletions tests/Unit/Shared/DateTimeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace PhpCfdi\SatWsDescargaMasiva\Tests\Unit\Shared;

use InvalidArgumentException;
use PhpCfdi\SatWsDescargaMasiva\Shared\DateTime;
use PhpCfdi\SatWsDescargaMasiva\Tests\TestCase;

Expand Down Expand Up @@ -32,4 +33,24 @@ public function testFormatSatUsesZuluTimeZone(): void
$this->assertSame('2019-01-14T04:23:24.000Z', $date->formatSat());
$this->assertSame('2019-01-13T22:23:24.000CST', $date->formatDefaultTimeZone());
}

public function testCreateDateTimeWithTimestamp(): void
{
$date = new DateTime(316569600);
$this->assertSame('1980-01-13T00:00:00.000Z', $date->formatSat());
}

public function testCreateDateTimeWithInvalidStringValue(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to create a Datetime("foo")');
new DateTime('foo');
}

public function testCreateDateTimeWithInvalidArgument(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Unable to create a Datetime');
new DateTime([]);
}
}
27 changes: 27 additions & 0 deletions tests/Unit/Shared/InteractsXmlTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace PhpCfdi\SatWsDescargaMasiva\Tests\Unit\Shared;

use DOMDocument;
use InvalidArgumentException;
use PhpCfdi\SatWsDescargaMasiva\Shared\InteractsXmlTrait;
use PhpCfdi\SatWsDescargaMasiva\Tests\TestCase;

class InteractsXmlTraitTest extends TestCase
Expand Down Expand Up @@ -43,6 +46,30 @@ public function testFindElementExpectingOne(): void
);
}

public function testReadXmlDocumentWithoutContentThrowsException(): void
{
$specimen = new InteractsXmlTraitSpecimen();
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot load an xml with empty content');
$specimen->readXmlDocument('');
}

public function testReadXmlElementWithoutDocumentRootElementThrowsException(): void
{
$specimen = new class() {
use InteractsXmlTrait;

public function readXmlDocument(string $source): DOMDocument
{
unset($source);
return new DOMDocument();
}
};
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot load an xml without document element');
$specimen->readXmlElement('');
}

public function testFindElementExpectingNone(): void
{
$specimen = new InteractsXmlTraitSpecimen();
Expand Down
70 changes: 70 additions & 0 deletions tests/Unit/Shared/TokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace PhpCfdi\SatWsDescargaMasiva\Tests\Unit\Shared;

use InvalidArgumentException;
use PhpCfdi\SatWsDescargaMasiva\Shared\DateTime;
use PhpCfdi\SatWsDescargaMasiva\Shared\Token;
use PhpCfdi\SatWsDescargaMasiva\Tests\TestCase;

class TokenTest extends TestCase
{
public function testCreateTokenWithInvalidDates(): void
{
$created = new DateTime();
$expires = $created->modify('- 1 second');
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot create a token with expiration lower than creation');
new Token($created, $expires, '');
}

public function testTokenNotExpired(): void
{
$created = new DateTime();
$expires = $created->modify('+ 5 seconds');
$token = new Token($created, $expires, '');
$this->assertFalse($token->isExpired());
}

public function testTokenExpired(): void
{
$created = new DateTime('- 10 seconds');
$expires = $created->modify('+ 5 seconds');
$token = new Token($created, $expires, '');
$this->assertTrue($token->isExpired());
}

public function testValueNotEmpty(): void
{
$created = new DateTime('- 10 seconds');
$expires = $created->modify('+ 5 seconds');
$token = new Token($created, $expires, '');
$this->assertTrue($token->isValueEmpty());
}

public function testValueIsNotEmpty(): void
{
$created = new DateTime('- 10 seconds');
$expires = $created->modify('+ 5 seconds');
$token = new Token($created, $expires, 'foo');
$this->assertFalse($token->isValueEmpty());
}

/**
* @param string $created
* @param string $expires
* @param string $value
* @param bool $expected
* @testWith ["- 10 seconds", "+ 10 seconds", "foo", true]
* ["- 10 seconds", "- 1 seconds", "foo", false]
* ["- 10 seconds", "+ 10 seconds", "", false]
* ["- 10 seconds", "- 1 seconds", "", false]
*/
public function testIsValid(string $created, string $expires, string $value, bool $expected): void
{
$token = new Token(new DateTime($created), new DateTime($expires), $value);
$this->assertSame($expected, $token->isValid());
}
}
2 changes: 1 addition & 1 deletion tests/_files/zip/cfdi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
</cfdi:Traslados>
</cfdi:Impuestos>
<cfdi:Complemento>
<tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/cfd/TimbreFiscalDigital/TimbreFiscalDigitalv11.xsd" Version="1.1" SelloCFD="SELLOCFD" NoCertificadoSAT="00001000000405332712" RfcProvCertif="FIN1203015JA" UUID="11111111-2222-3333-4444-000000000001" FechaTimbrado="2019-07-24T11:58:36" SelloSAT="SelloSAT"/>
</cfdi:Complemento>
<cfdi:Addenda/>
</cfdi:Comprobante>
Binary file modified tests/_files/zip/cfdi.zip
Binary file not shown.
Binary file modified tests/_files/zip/metadata.zip
Binary file not shown.

0 comments on commit 5af2da8

Please sign in to comment.