diff --git a/src/Common/ConfigNfse/Config.php b/src/Common/ConfigNfse/Config.php new file mode 100644 index 0000000..8a0c015 --- /dev/null +++ b/src/Common/ConfigNfse/Config.php @@ -0,0 +1,75 @@ +prefeitura = $prefeitura; + } + + public function getPrefeitura() + { + return $this->prefeitura; + } + + public function setRps(Rps $rps) + { + $this->rps = $rps; + } + + public function getRps() + { + return $this->rps; + } + + public function setProducao($producao) + { + if ( + !v::boolVal()->validate($producao) + ) { + throw new ValidationError( + 'Producao tem que ser um valor booleano' + ); + } + + $this->producao = $producao; + } + + public function getProducao() + { + return $this->producao; + } +} diff --git a/src/Common/ConfigNfse/Prefeitura.php b/src/Common/ConfigNfse/Prefeitura.php new file mode 100644 index 0000000..67ac848 --- /dev/null +++ b/src/Common/ConfigNfse/Prefeitura.php @@ -0,0 +1,74 @@ +login = $login; + } + + public function getLogin() + { + return $this->login; + } + + public function setSenha($senha) + { + $this->senha = $senha; + } + + public function getSenha() + { + return $this->senha; + } + + public function setReceitaBruta($receitaBruta) + { + $this->receitaBruta = $receitaBruta; + } + + public function getReceitaBruta() + { + return $this->receitaBruta; + } + + public function setLei($lei) + { + $this->lei = $lei; + } + + public function getLei() + { + return $this->lei; + } + + public function setDataInicio($dataInicio) + { + if( + !is_null($dataInicio) && + !v::date('Y-m-d')->validate($dataInicio) + ) { + throw new ValidationError( + 'Formato da data é inválido. Formato válido YYYY-MM-DD.' + ); + } + $this->dataInicio = $dataInicio; + } + + public function getDataInicio() + { + return $this->dataInicio; + } +} diff --git a/src/Common/ConfigNfse/Rps.php b/src/Common/ConfigNfse/Rps.php new file mode 100644 index 0000000..01b00a2 --- /dev/null +++ b/src/Common/ConfigNfse/Rps.php @@ -0,0 +1,72 @@ +validate($serie) + ) { + throw new ValidationError( + 'Serie tem que ser um valor string' + ); + } + + $this->serie = $serie; + } + + public function getSerie() + { + return $this->serie; + } + + public function setNumero($numero) + { + if( + !is_null($numero) && + !v::intType()->validate($numero) + ) { + throw new ValidationError( + 'Numero tem que ser um valor inteiro' + ); + } + + $this->numero = $numero; + } + + public function getNumero() + { + return $this->numero; + } + + public function setLote($lote) + { + if( + !is_null($lote) && + !v::intType()->validate($lote) + ) { + throw new ValidationError( + 'Lote tem que ser um valor inteiro' + ); + } + + $this->lote = $lote; + } + + public function getLote() + { + return $this->lote; + } +} diff --git a/src/Common/Nfse.php b/src/Common/Nfse.php index ce8f001..2b105de 100644 --- a/src/Common/Nfse.php +++ b/src/Common/Nfse.php @@ -2,14 +2,32 @@ namespace TecnoSpeed\Plugnotas\Common; -use TecnoSpeed\Plugnotas\Abstracts\BuilderAbstract; -use TecnoSpeed\Plugnotas\Error\ValidationError; +use FerFabricio\Hydrator\Hydrate; use Respect\Validation\Validator as v; +use TecnoSpeed\Plugnotas\Error\ValidationError; +use TecnoSpeed\Plugnotas\Error\InvalidTypeError; +use TecnoSpeed\Plugnotas\Common\ConfigNfse\Config; +use TecnoSpeed\Plugnotas\Abstracts\BuilderAbstract; + class Nfse extends BuilderAbstract { private $ativo; private $tipoContrato; + private $config; + + public static function fromArray($data) + { + if (!is_array($data)) { + throw new InvalidTypeError('Deve ser informado um array.'); + } + + if (array_key_exists('config', $data)) { + $data['config'] = Config::fromArray($data['config']); + } + + return Hydrate::toObject(self::class, $data); + } public function setAtivo($ativo) { @@ -22,13 +40,13 @@ public function setAtivo($ativo) ); } - $this->ativo = $ativo; + $this->ativo = $ativo; } public function getAtivo() { return $this->ativo; - } + } public function setTipoContrato($tipoContrato) { @@ -39,14 +57,24 @@ public function setTipoContrato($tipoContrato) ){ throw new ValidationError( 'Valor inválido para o TipoContrato. Valores aceitos: null, 0, 1' - ); + ); } - $this->tipoContrato = $tipoContrato; + $this->tipoContrato = $tipoContrato; } public function getTipoContrato() { return $this->tipoContrato; - } + } + + public function setConfig(Config $config) + { + $this->config = $config; + } + + public function getConfig() + { + return $this->config; + } } diff --git a/tests/Common/ConfigNfse/ConfigTest.php b/tests/Common/ConfigNfse/ConfigTest.php new file mode 100644 index 0000000..a5399e0 --- /dev/null +++ b/tests/Common/ConfigNfse/ConfigTest.php @@ -0,0 +1,69 @@ +expectException(ValidationError::class); + $this->expectExceptionMessage('Producao tem que ser um valor booleano'); + $config = new Config(); + $config->setProducao('teste'); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Config::fromArray + */ + public function testWithInvalidArray() + { + $this->expectException(InvalidTypeError::class); + $this->expectExceptionMessage('Deve ser informado um array.'); + Config::fromArray('invalid-array'); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Config::fromArray + */ + public function testWithValidArray() + { + $config = Config::fromArray([ + 'producao' => true, + 'rps' => [], + 'prefeitura' => [] + ]); + + $this->assertSame($config->getProducao(), true); + $this->assertEquals($config->getRps(), new Rps([])); + $this->assertEquals($config->getPrefeitura(), new Prefeitura([])); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Config::setRps + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Config::setPrefeitura + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Config::setProducao + */ + public function testWithValidData() + { + $config = new Config(); + $config->setRps(new Rps([])); + $config->setPrefeitura(new Prefeitura([])); + $config->setProducao(true); + + $this->assertSame($config->getProducao(), true); + $this->assertEquals($config->getRps(), new Rps([])); + $this->assertEquals($config->getPrefeitura(), new Prefeitura([])); + } +} diff --git a/tests/Common/ConfigNfse/PrefeituraTest.php b/tests/Common/ConfigNfse/PrefeituraTest.php new file mode 100644 index 0000000..19581b4 --- /dev/null +++ b/tests/Common/ConfigNfse/PrefeituraTest.php @@ -0,0 +1,65 @@ +expectException(ValidationError::class); + $this->expectExceptionMessage('Formato da data é inválido. Formato válido YYYY-MM-DD.'); + $rps = new Prefeitura(); + $rps->setDataInicio('30/04/2021'); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Prefeitura::fromArray + */ + public function testWithValidArray() + { + $rps = Prefeitura::fromArray([ + 'login' => 'login', + 'senha' => 'senha', + 'receitaBruta' => 100.0, + 'lei' => '113', + 'dataInicio' => '2021-04-30', + ]); + + $this->assertSame($rps->getLogin(), 'login'); + $this->assertEquals($rps->getSenha(), 'senha'); + $this->assertEquals($rps->getReceitaBruta(), 100.0); + $this->assertEquals($rps->getLei(), '113'); + $this->assertEquals($rps->getDataInicio(), '2021-04-30'); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Prefeitura::setLogin + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Prefeitura::setSenha + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Prefeitura::setReceitaBruta + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Prefeitura::setLei + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Prefeitura::setDataInicio + */ + public function testWithValidData() + { + $rps = new Prefeitura(); + $rps->setLogin('login'); + $rps->setSenha('senha'); + $rps->setReceitaBruta(100.0); + $rps->setLei('113'); + $rps->setDataInicio('2021-04-30'); + + $this->assertSame($rps->getLogin(), 'login'); + $this->assertEquals($rps->getSenha(), 'senha'); + $this->assertEquals($rps->getReceitaBruta(), 100.0); + $this->assertEquals($rps->getLei(), '113'); + $this->assertEquals($rps->getDataInicio(), '2021-04-30'); + } +} diff --git a/tests/Common/ConfigNfse/RpsTest.php b/tests/Common/ConfigNfse/RpsTest.php new file mode 100644 index 0000000..eeecd40 --- /dev/null +++ b/tests/Common/ConfigNfse/RpsTest.php @@ -0,0 +1,77 @@ +expectException(ValidationError::class); + $this->expectExceptionMessage('Serie tem que ser um valor string'); + $rps = new Rps(); + $rps->setSerie(1); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Rps::setNumero + */ + public function testInvalidNumero() + { + $this->expectException(ValidationError::class); + $this->expectExceptionMessage('Numero tem que ser um valor inteiro'); + $rps = new Rps(); + $rps->setNumero('numero'); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Rps::setLote + */ + public function testInvalidLote() + { + $this->expectException(ValidationError::class); + $this->expectExceptionMessage('Lote tem que ser um valor inteiro'); + $rps = new Rps(); + $rps->setLote('lote'); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Rps::fromArray + */ + public function testWithValidArray() + { + $rps = Rps::fromArray([ + 'lote' => 0, + 'serie' => '1', + 'numero' => 0 + ]); + + $this->assertSame($rps->getLote(), 0); + $this->assertEquals($rps->getSerie(), '1'); + $this->assertEquals($rps->getNumero(), 0); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Rps::setLote + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Rps::setSerie + * @covers TecnoSpeed\Plugnotas\Common\ConfigNfse\Rps::setNumero + */ + public function testWithValidData() + { + $rps = new Rps(); + $rps->setLote(0); + $rps->setSerie('1'); + $rps->setNumero(0); + + $this->assertSame($rps->getLote(), 0); + $this->assertSame($rps->getSerie(), '1'); + $this->assertSame($rps->getNumero(), 0); + } +} diff --git a/tests/Common/NfseTest.php b/tests/Common/NfseTest.php new file mode 100644 index 0000000..9073b74 --- /dev/null +++ b/tests/Common/NfseTest.php @@ -0,0 +1,61 @@ +expectException(ValidationError::class); + $this->expectExceptionMessage('Ativo é requerido para o cadastro de NFS-e.'); + $nfse = new Nfse(); + $nfse->setAtivo('teste'); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\Nfse::setAtivo + */ + public function testEmptyTipoAtivo() + { + $this->expectException(ValidationError::class); + $this->expectExceptionMessage('Ativo é requerido para o cadastro de NFS-e.'); + $nfse = new Nfse(); + $nfse->setAtivo(null); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\Nfse::setTipoContrato + */ + public function testInvalidTipoContrato() + { + $this->expectException(ValidationError::class); + $this->expectExceptionMessage('Valor inválido para o TipoContrato. Valores aceitos: null, 0, 1'); + $nfse = new Nfse(); + $nfse->setTipoContrato('teste'); + } + + /** + * @covers TecnoSpeed\Plugnotas\Common\Nfse::fromArray + */ + public function testWithValidArray() + { + $nfse = Nfse::fromArray([ + 'ativo' => true, + 'tipoContrato' => 0, + 'config' => [] + ]); + + $this->assertSame($nfse->getAtivo(), true); + $this->assertSame($nfse->getTipoContrato(), 0); + $this->assertEquals($nfse->getConfig(), new Config([])); + } +}