-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Sysvale/hotfix/version-xsd
Corrige erro de validação quando não é passsado versão na regra ZipHasValidCnesXML
- Loading branch information
Showing
10 changed files
with
191 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace Sysvale\ValidationRules\Rules; | ||
|
||
abstract class CnesXMLRule | ||
{ | ||
protected function getIdentificationAttributes($xml) | ||
{ | ||
$identification = $xml->{'IDENTIFICACAO'}; | ||
|
||
if (empty($identification)) { | ||
return []; | ||
} | ||
|
||
return array_values((array) $identification->attributes())[0]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<?php | ||
|
||
namespace Sysvale\ValidationRules\Rules; | ||
|
||
use SimpleXMLElement; | ||
use Illuminate\Contracts\Validation\Rule; | ||
use Sysvale\ValidationRules\Rules\CnesXMLRule; | ||
use Sysvale\ValidationRules\Support\ZipWithXMLHandler; | ||
|
||
class CnesXMLVersionXSD extends CnesXMLRule implements Rule | ||
{ | ||
private $version; | ||
|
||
public function __construct($version) | ||
{ | ||
$this->version = $version; | ||
} | ||
|
||
/** | ||
* Determine if the validation rule passes. | ||
* | ||
* @param string $attribute | ||
* @param mixed $value | ||
* @return bool | ||
*/ | ||
public function passes($attribute, $value) | ||
{ | ||
$zip_handler = resolve(ZipWithXMLHandler::class) | ||
->buildZip($value->path()); | ||
|
||
$passes = $this->hasValidVersion($zip_handler->getSimpleXMLElement()); | ||
|
||
$zip_handler->closeZip(); | ||
|
||
return $passes; | ||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @return string | ||
*/ | ||
public function message() | ||
{ | ||
return __('SysvaleValidationRules::messages.cnes_xml_version_xsd', [ | ||
'version_xsd' => $this->version, | ||
]); | ||
} | ||
|
||
protected function hasValidVersion(SimpleXMLElement $xml) | ||
{ | ||
$identification = $this->getIdentificationAttributes($xml); | ||
$current_version_xsd = $identification['VERSION_XSD'] ?? null; | ||
|
||
return $current_version_xsd === $this->version; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace Sysvale\ValidationRules\Tests\Unit\Rules; | ||
|
||
use Illuminate\Http\UploadedFile; | ||
use Sysvale\ValidationRules\Tests\TestCase; | ||
use Sysvale\ValidationRules\Rules\CnesXMLVersionXSD; | ||
use Sysvale\ValidationRules\Rules\CnesXMLIdentification; | ||
use Sysvale\ValidationRules\Tests\Support\CnesXMLContentsHandler; | ||
use Sysvale\ValidationRules\Tests\Support\RuleErrorMessageHandler; | ||
|
||
class CnesXMLVersionXSDTest extends TestCase | ||
{ | ||
use RuleErrorMessageHandler; | ||
use CnesXMLContentsHandler; | ||
|
||
public function testPasses() | ||
{ | ||
$version = '2.1'; | ||
|
||
$this->mockXmlContents(['version_xsd' => "VERSION_XSD=\"$version\""]); | ||
|
||
$rule = new CnesXMLVersionXSD('2.1'); | ||
|
||
$this->assertTrue($rule->passes('file', UploadedFile::fake()->create('xml.zip'))); | ||
} | ||
|
||
public function testNotPasses() | ||
{ | ||
$version = '2'; | ||
|
||
$this->mockXmlContents(['version_xsd' => "VERSION_XSD=\"$version\""]); | ||
|
||
$rule = new CnesXMLVersionXSD('2.1'); | ||
|
||
$this->assertFalse($rule->passes('file', UploadedFile::fake()->create('xml.zip'))); | ||
} | ||
|
||
public function testIdentificationErrorMessage() | ||
{ | ||
$this->assertSame( | ||
'A versão do XML deve ser compatível com a 2.1', | ||
$this->getErrorMessage(CnesXMLVersionXSD::class, ['2.1']) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters