Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions lib/NFe/APIRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,34 @@ public function request( $method, $url, $data = array() ) {
$headers = $this->_defaultHeaders();

list( $response_body, $response_code ) = $this->requestWithCURL( $method, $url, $headers, $data );
$response = json_encode("{}");

if ( $response_code == 302 ) {
$response = $response_body;
}
else {
else if (!empty($response_body)) {
$response = json_decode($response_body);
if ( json_last_error() != JSON_ERROR_NONE ) {
throw new NFeInvalidResponseBody($response_body);
}
}

if ( json_last_error() != JSON_ERROR_NONE ) {
throw new NFeObjectNotFound($response_body);
if ( $response_code == 403 ) {
throw new NFeAuthorizationException($response_body);
}

if ( $response_code == 409 ) {
throw new NFeObjectAlreadyExists($response_body);
}

if ( $response_code == 404 ) {
throw new NFeObjectNotFound($response_body);
}

if ( $response_code == 400 ) {
throw new NFeException($response->message);
}

if ( isset($response->errors) ) {
if ( ( gettype($response->errors) != "string") && count( get_object_vars($response->errors) ) == 0 ) {
unset($response->errors);
Expand Down
25 changes: 16 additions & 9 deletions lib/NFe/APIResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,30 @@ public static function API() {

public static function endpointAPI( $object = null, $uri_path = '' ) {
$path = '';
$resource = self::objectBaseURI();

if ( is_string($object) || is_integer($object) ) {
$path = '/' . $object;
}
elseif (is_array($object) && isset($object['company_id'])) {
$uri_path = '/companies/' . $object['company_id'];
}
elseif (is_object($object) && isset($object->provider) && isset($object->provider->id) ) {
$uri_path = '/companies/' . $object->provider->id;
$uri_path = '/companies/' . $object['company_id'];

if (isset($object['id'])) {
$path = '/' . $object['id'];
}
}
elseif (is_object($object)) {
if (isset($object->provider) && isset($object->provider->id)) {
$uri_path = '/companies/' . $object->provider->id;
}

if (isset($object['id'])) {
$path = '/' . $object['id'];
if (!$object->is_new()) {
$path = '/' . $object->id;
}
}

return strtolower( NFe::getBaseURI() . $uri_path . '/' . self::objectBaseURI() . $path );

return strtolower( NFe::getBaseURI() . $uri_path . '/' . $resource . $path );
}

public static function url( $object = null ) {
Expand All @@ -74,7 +82,6 @@ protected function deleteAPI() {
// if ( $this['id'] == null ) { // $this['id']
// return false;
// }

try {
$response = self::API()->request( 'DELETE', static::url($this) );

Expand Down Expand Up @@ -102,7 +109,7 @@ protected static function searchAPI( $options = array() ) {

protected static function fetchAPI($key) {
try {
$response = self::API()->request( 'GET', static::url($key) );
$response = self::API()->request( 'GET', static::url($key) );
return self::createFromResponse($response);
}
catch ( NFeObjectNotFound $e ) {
Expand Down
16 changes: 8 additions & 8 deletions lib/NFe/Company.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ public static function create( $attributes = array() ) {
}

public static function fetch($key) {
return self::fetchAPI($key);
return self::fetchAPI($key);
}

public function save() {
public function save() {
return $this->saveAPI();
}

public function delete() {
return $this->deleteAPI();
public function delete() {
return $this->deleteAPI();
}

public function refresh() {
return $this->refreshAPI();
public function refresh() {
return $this->refreshAPI();
}

public static function search( $options = array() ) {
return self::searchAPI($options);
public static function search( $options = array() ) {
return self::searchAPI($options);
}
}
7 changes: 5 additions & 2 deletions lib/NFe/Error.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

class NFeAuthenticationException extends Exception {}
class NFeObjectNotFound extends Exception {}
class NFeAuthenticationException extends NFeException {}
class NFeAuthorizationException extends NFeException {}
class NFeObjectNotFound extends NFeException {}
class NFeException extends Exception {}
class NFeObjectAlreadyExists extends NFeException {}
class NFeInvalidResponseBody extends NFeException {}
2 changes: 1 addition & 1 deletion lib/NFe/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* NFe_Object manages the Object State
*
*
* Values that changed, values that need to be saved
*/
class NFe_Object implements arrayAccess {
Expand Down
18 changes: 15 additions & 3 deletions lib/NFe/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static function arrayToParams($array, $prefix = null) {
}

public static function arrayToParamsUrl($array, $prefix = null) {
if ( !is_array($array) ) {
if ( !is_array($array) ) {
return $array;
}

Expand Down Expand Up @@ -89,9 +89,21 @@ public static function createFromResponse( $object_type, $response ) {
return new NFe_SearchResult( $results, count($results) );
}
elseif ( is_object($response) ) {
return new $class_name( (array) $response );
$resources = $class_name::objectBaseURI();
if(isset($response->$resources)) {
$result = [];
if (is_array($response->$resources)) {
foreach($response->$resources as $resource) {
$result[] = new $class_name( (array) $resource );
}

return $result;
}

return new $class_name( (array) $response->$resources );
}
return new $class_name( (array) $response );
}

return null;
}
}
23 changes: 11 additions & 12 deletions test/NFe/CompanyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class NFe_CompanyTest extends NFe_TestCase {

public function testCreateAndDelete() {
$attributes = array(
'federalTaxNumber' => 48527553000123, // Generate CNPJ here: http://www.geradordecnpj.org/
'federalTaxNumber' => 97625117000100, // Generate CNPJ here: http://www.geradordecnpj.org/
'name' => 'TEST Company Name',
'tradeName' => 'Company Name',
'email' => 'nfe@mailinator.com',
Expand All @@ -28,38 +28,37 @@ public function testCreateAndDelete() {
$object = NFe_Company::create( $attributes );

$this->assertNotNull($object);
$this->assertNotNull( $object->companies->name );
$this->assertEqual( $object->companies->name, 'TEST Company Name' );

self::$id = $object->companies->id;
$this->assertNotNull( $object->name );
$this->assertEqual( $object->name, 'TEST Company Name' );
self::$id = $object->id;
}

public function testGet() {
$object = NFe_Company::fetch( self::$id );

$this->assertNotNull($object);
$this->assertNotNull($object->companies->name);
$this->assertEqual($object->companies->name, 'TEST Company Name');
$this->assertNotNull($object->name);
$this->assertEqual($object->name, 'TEST Company Name');
}

public function testUpdate() {
$object = NFe_Company::fetch( self::$id );

$object->companies->name = 'BB SA';
$object->name = 'BB SA';

// @todo Check it out why this is giving error
// $this->assertTrue( $object->save() );
// $this->assertTrue( $object->save() );

$this->assertNotNull($object);
$this->assertNotNull($object->companies->name);
$this->assertEqual($object->companies->name, 'BB SA');
$this->assertNotNull($object->name);
$this->assertEqual($object->name, 'BB SA');
}

public function testDelete() {
$object = NFe_Company::fetch( self::$id );

$this->assertNotNull($object);
$this->assertNotNull($object->companies->name);
$this->assertNotNull($object->name);
$this->assertTrue( $object->delete() );
}
}
1 change: 0 additions & 1 deletion test/NFe/LegalPersonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public function testFetchFail() {
$this->expectException('NFeObjectNotFound');

$result = NFe_LegalPerson::fetch( self::$company_id, self::$company_id );

$this->assertNull($result);
}

Expand Down
7 changes: 5 additions & 2 deletions test/NFe/ServiceInvoiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

class NFe_ServiceInvoiceTest extends NFe_TestCase {

protected $invoice;

public function testIssue() {
$this->invoice = NFe_ServiceInvoice::create(
// ID da empresa, você deve copiar exatamente como está no painel
Expand Down Expand Up @@ -58,7 +60,8 @@ public function testIssue() {
$this->assertEqual($this->invoice->cityServiceCode, '2690');
}

public function testFetchInvoice() {
public function testFetchInvoice() {

$fetched_invoice = NFe_ServiceInvoice::fetch(
"54244e0ee340420fdc94ad09",
$this->invoice->id
Expand All @@ -75,7 +78,7 @@ public function testDownloadPdfInvoice() {
"54244e0ee340420fdc94ad09",
$this->invoice->id
);

$this->assertTrue( strpos($url, "pdf") );
}

Expand Down
19 changes: 9 additions & 10 deletions test/NFe/WebhookTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,43 @@ public function testCreateAndDelete() {
$object = NFe_Webhook::create( $attributes );

$this->assertNotNull($object);
$this->assertNotNull($object->hooks->url);
$this->assertEqual($object->hooks->url, $attributes['url']);
$this->assertNotNull($object->url);
$this->assertEqual($object->url, $attributes['url']);

self::$id = $object->hooks->id;
self::$id = $object->id;
}

public function testGet() {
$object = NFe_Webhook::fetch( self::$id );

$this->assertNotNull( $object );
$this->assertNotNull( $object['hooks'][0]->url );
$this->assertEqual( $object['hooks'][0]->url, 'http://google.com/test' );
$this->assertNotNull( $object->url );
$this->assertEqual( $object->url, 'http://google.com/test' );
}

public function testUpdate() {
$object = NFe_Webhook::fetch( self::$id );

$new_url = 'http://google.com/test2';
$object->hooks->url = $new_url;
$object->url = $new_url;

$this->assertTrue($object->save());
$this->assertNotNull($object);
$this->assertNotNull($object->hooks->url);
$this->assertEqual($object->hooks->url, $new_url);
$this->assertNotNull($object->url);
$this->assertEqual($object->url, $new_url);
}

public function testDelete() {
$object = NFe_Webhook::fetch( self::$id );

$this->assertNotNull($object);
$this->assertNotNull( $object->hooks->url );
$this->assertNotNull( $object->url );
$this->assertTrue($object->delete());
}

public function testSearch() {
$hooks = NFe_Webhook::search();

$this->assertNotNull( $hooks );
$this->assertNotNull( $hooks->hooks );
}
}