Skip to content

Commit

Permalink
Merge pull request #4 from Sysvale/feature/valida-version_xsd
Browse files Browse the repository at this point in the history
Adiciona checagem pela versão do XML
  • Loading branch information
jedsonmelo authored Mar 30, 2021
2 parents 3a3fea3 + d4f6d86 commit 4a1135e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "1.2.0",
"version": "1.3.0",
"name": "sysvale/validation-rules",
"description": "A Laravel library with useful custom rules",
"type": "library",
Expand Down
2 changes: 1 addition & 1 deletion resources/lang/pt_BR/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'max_in_the_zip_file' => 'O arquivo zip deve ter no máximo :quantity arquivo(s).',
'not_empty_zip' => 'O arquivo zip não pode está vazio.',
'zip_has_xml_file' => 'O arquivo zip deve conter um XML.',
'cnes_xml_identification' => 'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM e o campo de DESTINO.', // phpcs:ignore
'cnes_xml_identification' => 'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM, o campo de DESTINO e a se a versão do XML compatível é a :version_xsd', // phpcs:ignore
'cnes_xml_structure' => 'A estrutura do arquivo XML está inválida.',
'cnes_xml_date' => 'A competência do XML deve ser posterior a data :date.',
];
9 changes: 7 additions & 2 deletions src/Rules/CnesXMLIdentification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ class CnesXMLIdentification implements Rule
{
private $expected_ibge_code;

public function __construct($expected_ibge_code)
public function __construct($expected_ibge_code, $version_xsd = '2.1')
{
$this->expected_ibge_code = $expected_ibge_code;
$this->version_xsd = $version_xsd;
}

/**
Expand Down Expand Up @@ -41,7 +42,9 @@ public function passes($attribute, $value)
*/
public function message()
{
return __('SysvaleValidationRules::messages.cnes_xml_identification');
return __('SysvaleValidationRules::messages.cnes_xml_identification', [
'version_xsd' => $this->version_xsd,
]);
}


Expand All @@ -55,10 +58,12 @@ protected function hasValidIdentification(SimpleXMLElement $xml)
$origin = (string) $identification['ORIGEM'];
$target = (string) $identification['DESTINO'];
$ibge_code = (string) $identification['CO_IBGE_MUN'];
$version_xsd = (string) $identification['VERSION_XSD'];

if ($origin === 'PORTAL'
&& $target === 'ESUS_AB'
&& $ibge_code === $this->expected_ibge_code
&& $version_xsd === $this->version_xsd
) {
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions src/Rules/ZipHasValidCnesXML.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ class ZipHasValidCnesXML implements Rule
private $date;
protected $message;

public function __construct($expected_ibge_code, $date = null)
public function __construct($expected_ibge_code, $date = null, $version_xsd = null)
{
$this->expected_ibge_code = $expected_ibge_code;
$this->date = $date;
$this->version_xsd = $version_xsd;
}

/**
Expand Down Expand Up @@ -56,7 +57,7 @@ protected function hasValidStructure($attribute, $value)

protected function hasValidIdentification($attribute, $value)
{
$rule = new CnesXMLIdentification($this->expected_ibge_code);
$rule = new CnesXMLIdentification($this->expected_ibge_code, $this->version_xsd);

return $this->validateWithRule($rule, $attribute, $value);
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Support/CnesXMLContentsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ private function mockXmlContents($data = [], $contents = null)
if (is_null($contents)) {
$ibge_code = $data['ibge_code'] ?? '';
$date = $data['date'] ?? '';
$version_xsd = $data['version_xsd'] ?? 'VERSION_XSD="2.1"';

$contents = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<ImportarXMLCNES>
<IDENTIFICACAO DATA=\"$date\" ORIGEM=\"PORTAL\" DESTINO=\"ESUS_AB\" CO_IBGE_MUN=\"$ibge_code\">\r\n
<IDENTIFICACAO DATA=\"$date\" ORIGEM=\"PORTAL\" DESTINO=\"ESUS_AB\" CO_IBGE_MUN=\"$ibge_code\" $version_xsd>\r\n
<ESTABELECIMENTOS>\r\n
</ESTABELECIMENTOS>\r\n
<PROFISSIONAIS>\r\n
Expand Down
45 changes: 43 additions & 2 deletions tests/Unit/Rules/CnesXMLIdentificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,49 @@ class CnesXMLIdentificationTest extends TestCase
public function testPasses()
{
$ibge_code = '12345';

$this->mockXmlContents(['ibge_code' => $ibge_code]);

$rule = new CnesXMLIdentification($ibge_code, '2.1');

$this->assertTrue($rule->passes('file', UploadedFile::fake()->create('xml.zip')));
}

public function testNotPassesVersionXSD()
{
$ibge_code = '12345';

$this->mockXmlContents(['ibge_code' => $ibge_code, 'version_xsd' => '']);

$rule = new CnesXMLIdentification($ibge_code, '2.1');

$this->assertFalse($rule->passes('file', UploadedFile::fake()->create('xml.zip')));
}

public function testNotPassesVersionXSDDiff21()
{
$ibge_code = '12345';
$version_xsd = '3.0';

$this->mockXmlContents([
'ibge_code' => $ibge_code,
'version_xsd' => "VERSION_XSD=\"$version_xsd\""
]);

$rule = new CnesXMLIdentification($ibge_code, '2.1');

$this->assertFalse($rule->passes('file', UploadedFile::fake()->create('xml.zip')));
}

public function testSecondArgumentIsntPassed()
{
$ibge_code = '12345';

$this->mockXmlContents([
'ibge_code' => $ibge_code,
'version_xsd' => "VERSION_XSD=\"2.1\""
]);

$rule = new CnesXMLIdentification($ibge_code);

$this->assertTrue($rule->passes('file', UploadedFile::fake()->create('xml.zip')));
Expand All @@ -29,8 +70,8 @@ public function testPasses()
public function testIdentificationErrorMessage()
{
$this->assertSame(
'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM e o campo de DESTINO.',
$this->getErrorMessage(CnesXMLIdentification::class, ['0000'])
'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM, o campo de DESTINO e a se a versão do XML compatível é a 2.1',
$this->getErrorMessage(CnesXMLIdentification::class, ['0000', '2.1'])
);
}
}
12 changes: 8 additions & 4 deletions tests/Unit/Rules/ZipHasValidCnesXMLTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,19 +66,23 @@ public function testFileWithInvalidIdentification()

$this->assertFalse($passes);
$this->assertEquals(
'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM e o campo de DESTINO.', //phpcs:ignore
'XML com formato inválido. Por favor, verifique o código do IBGE, o campo de ORIGEM, o campo de DESTINO e a se a versão do XML compatível é a 2.1', //phpcs:ignore
$validator->errors()->first('file')
);
}

public function testFileWithInvalidDate()
{
$this->mockXmlContents(['ibge_code' => '', 'date' => '2020-10-10']);
$this->mockXmlContents([
'ibge_code' => '',
'date' =>'2020-10-10',
'version_xsd' => 'VERSION_XSD="2.1"'
]);

$validator = Validator::make([
'file' => UploadedFile::fake()->create('xml.zip'),
], [
'file' => [new ZipHasValidCnesXML('', '2020-10-10')],
'file' => [new ZipHasValidCnesXML('', '2020-10-10', $version_xsd = '2.1')],
]);

$passes = $validator->passes();
Expand All @@ -94,7 +98,7 @@ public function testValidFilePasses()
{
$this->mockXmlContents(['ibge_code' => '123456', 'date' => '2020-10-10']);

$rule = new ZipHasValidCnesXML('123456', '2020-10-09');
$rule = new ZipHasValidCnesXML('123456', '2020-10-09', '2.1');

$passes = $rule->passes('file', UploadedFile::fake()->create('xml.zip'));

Expand Down

0 comments on commit 4a1135e

Please sign in to comment.