Skip to content

Commit

Permalink
Merge pull request #12 from eclipxe13/known-sat-ns
Browse files Browse the repository at this point in the history
Espacios de nombres conocidos (v1.1.5)
  • Loading branch information
eclipxe13 committed Jan 21, 2022
2 parents 1c91adb + 91ee851 commit f4c3f03
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/.php-cs-fixer.dist.php export-ignore
/.scrutinizer.yml export-ignore
/phpcs.xml.dist export-ignore
/phpstan.neon.dist export-ignore
/phpunit.xml.dist export-ignore
/sonar-project.properties export-ignore
22 changes: 0 additions & 22 deletions .scrutinizer.yml

This file was deleted.

3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"symfony/polyfill-php80": "^1.22"
},
"require-dev": {
"phpunit/phpunit": "^9.3"
"phpunit/phpunit": "^9.3",
"ext-json": "*"
},
"autoload": {
"psr-4": {
Expand Down
20 changes: 19 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,27 @@ Utilizamos [Versionado Semántico 2.0.0](SEMVER.md).

Los cambios no liberados se integran a la rama principal, pero no requieren de la liberación de una nueva versión.

## Versión 1.1.5

### Espacios de nombres conocidos

Se actualiza la lista de espacios de nombres conocidos para:

- CFDI 4.0.
- CFDI de retenciones e información de pagos 2.0.
- Complemento de pagos 2.0.
- Complemento de carta porte 1.0.
- Complemento de carta porte 2.0.

Además, se agrega una prueba que usa <https://github.com/phpcfdi/sat-ns-registry> para verificar que la lista
se mantiene actualizada.

### Integración continua

Se agrega PHP 8.1 a la matriz de pruebas.
- Se agrega PHP 8.1 a la matriz de pruebas.
- Se configura [SonarCloud](https://sonarcloud.io/project/overview?id=phpcfdi_cfdi-cleaner).
- Se remueve Scrutinizer CI. Gracias por todo.
- Se actualizan los *badges* del proyecto.

## Versión 1.1.4

Expand Down
23 changes: 22 additions & 1 deletion src/XmlDocumentCleaners/SetKnownSchemaLocations.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class SetKnownSchemaLocations implements XmlDocumentCleanerInterface
* @var array<string, string>
*/
private const KNOWN_NAMESPACES = [
'http://www.sat.gob.mx/cfd/4#4.0'
=> 'http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd',
'http://www.sat.gob.mx/cfd/3#3.3'
=> 'http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv33.xsd',
'http://www.sat.gob.mx/cfd/3#3.2'
Expand All @@ -33,6 +35,8 @@ class SetKnownSchemaLocations implements XmlDocumentCleanerInterface
=> 'http://www.sat.gob.mx/sitio_internet/cfd/2/cfdv22.xsd',
'http://www.sat.gob.mx/cfd/2#2.0'
=> 'http://www.sat.gob.mx/sitio_internet/cfd/2/cfdv2.xsd',
'http://www.sat.gob.mx/esquemas/retencionpago/2#2.0'
=> 'http://www.sat.gob.mx/esquemas/retencionpago/2/retencionpagov2.xsd',
'http://www.sat.gob.mx/esquemas/retencionpago/1#1.0'
=> 'http://www.sat.gob.mx/esquemas/retencionpago/1/retencionpagov1.xsd',
'http://www.sat.gob.mx/TimbreFiscalDigital#1.0'
Expand Down Expand Up @@ -104,6 +108,8 @@ class SetKnownSchemaLocations implements XmlDocumentCleanerInterface
=> 'http://www.sat.gob.mx/sitio_internet/cfd/ComercioExterior/ComercioExterior10.xsd',
'http://www.sat.gob.mx/Pagos#1.0'
=> 'http://www.sat.gob.mx/sitio_internet/cfd/Pagos/Pagos10.xsd',
'http://www.sat.gob.mx/Pagos20#2.0'
=> 'http://www.sat.gob.mx/sitio_internet/cfd/Pagos/Pagos20.xsd',
'http://www.sat.gob.mx/GastosHidrocarburos10#1.0'
=> 'http://www.sat.gob.mx/sitio_internet/cfd/GastosHidrocarburos10/GastosHidrocarburos10.xsd',
'http://www.sat.gob.mx/iedu#1.0'
Expand All @@ -116,6 +122,10 @@ class SetKnownSchemaLocations implements XmlDocumentCleanerInterface
=> 'http://www.sat.gob.mx/sitio_internet/cfd/terceros/terceros11.xsd',
'http://www.sat.gob.mx/acreditamiento#1.0'
=> 'http://www.sat.gob.mx/sitio_internet/cfd/acreditamiento/AcreditamientoIEPS10.xsd',
'http://www.sat.gob.mx/CartaPorte#1.0'
=> 'http://www.sat.gob.mx/sitio_internet/cfd/CartaPorte/CartaPorte.xsd',
'http://www.sat.gob.mx/CartaPorte20#2.0'
=> 'http://www.sat.gob.mx/sitio_internet/cfd/CartaPorte/CartaPorte20.xsd',
'http://www.sat.gob.mx/esquemas/retencionpago/1/arrendamientoenfideicomiso#1.0'
=> 'http://www.sat.gob.mx/esquemas/retencionpago/1/arrendamientoenfideicomiso/arrendamientoenfideicomiso.xsd',
'http://www.sat.gob.mx/esquemas/retencionpago/1/dividendos#1.0'
Expand Down Expand Up @@ -188,12 +198,23 @@ private function obtainAttributeValueFromFirstNodeOfNamespace(
return '';
}

// @phpstan-ignore-next-line PHPStan is fine, it just report an impossible scenario considering the query
// @phpstan-ignore-next-line PHPStan is fine, it just reports an impossible scenario considering the query
return $nodes->item(0)->attributes->getNamedItem($attributeName)->nodeValue;
}

private function obtainLocationForNamespaceVersion(string $namespace, string $version, string $default): string
{
return self::KNOWN_NAMESPACES[$namespace . '#' . $version] ?? $default;
}

/**
* Pairs of key-value of namespace and version to XSD locations
* Key: namespace#version
* Value: location
* @return array<string, string>
*/
public static function getKnownNamespaces(): array
{
return self::KNOWN_NAMESPACES;
}
}
49 changes: 47 additions & 2 deletions tests/Features/CleanerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

final class CleanerTest extends TestCase
{
public function testStaticCleanStringDocument(): void
public function testStaticCleanStringDocument33(): void
{
$xmlDirty = /** @lang text */ <<<XML
DIRTY
Expand All @@ -33,7 +33,7 @@ public function testStaticCleanStringDocument(): void
$this->assertXmlStringEqualsXmlString($expected, $xmlClean);
}

public function testCleanXmlDocument(): void
public function testCleanXmlDocument33(): void
{
$document = $this->createDocument(<<<XML
<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
Expand All @@ -56,6 +56,51 @@ public function testCleanXmlDocument(): void
$this->assertEquals($expected, $document);
}

public function testStaticCleanStringDocument40(): void
{
$xmlDirty = /** @lang text */ <<<XML
DIRTY
<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cfdi="http://www.sat.gob.mx/cfd/4"
xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 cfdi40.xsd"
Version="4.0"/>
XML;

$xmlClean = Cleaner::staticClean($xmlDirty);

$expected = /** @lang text */ <<<XML
<?xml version="1.0"?>
<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cfdi="http://www.sat.gob.mx/cfd/4"
xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd"
Version="4.0"/>
XML;
$this->assertXmlStringEqualsXmlString($expected, $xmlClean);
}

public function testCleanXmlDocument40(): void
{
$document = $this->createDocument(<<<XML
<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cfdi="http://www.sat.gob.mx/cfd/4"
xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 cfdi44.xsd"
Version="4.0"/>
XML
);

$cleaner = new Cleaner();
$cleaner->cleanDocument($document);

$expected = $this->createDocument(<<<XML
<cfdi:Comprobante xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cfdi="http://www.sat.gob.mx/cfd/4"
xsi:schemaLocation="http://www.sat.gob.mx/cfd/4 http://www.sat.gob.mx/sitio_internet/cfd/4/cfdv40.xsd"
Version="4.0"/>
XML
);
$this->assertEquals($expected, $document);
}

/**
* @param string $filename
* @testWith ["cfdi32-real.xml"]
Expand Down
30 changes: 30 additions & 0 deletions tests/Features/XmlDocumentCleaners/SetKnownSchemaLocationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,34 @@ public function testSetKnownSchemaLocationsWithUnknownNamespace(): void
);
$this->assertEquals($expected, $document);
}

/** @noinspection PhpUnhandledExceptionInspection */
public function testKnowAllLocationsFromSatNsRegistry(): void
{
// obtain the list of known locations from phpcfdi/sat-ns-registry
$satNsRegistryUrl = 'https://raw.githubusercontent.com/phpcfdi/sat-ns-registry/master/complementos_v1.json';
/** @var array<array{namespace: ?string, version: ?string, xsd: ?string}> $registry */
$registry = json_decode(file_get_contents($satNsRegistryUrl) ?: '[]', true, 512, JSON_THROW_ON_ERROR);

// re-create the known list of namespace#version => xsd-location
$expected = [];
foreach ($registry as $entry) {
$namespace = $entry['namespace'] ?? '';
$version = $entry['version'] ?? '';
$xsd = $entry['xsd'] ?? '';
if ($namespace && $xsd) {
$expected[$namespace . '#' . $version] = $xsd;
}
}
asort($expected);

$knownLocations = SetKnownSchemaLocations::getKnownNamespaces();
asort($knownLocations);

$this->assertSame(
$expected,
$knownLocations,
'The list of known namespace#version => xsd-location is different from phpcfdi/sat-ns-registry',
);
}
}

0 comments on commit f4c3f03

Please sign in to comment.