diff --git a/lib/NFe/APIRequest.php b/lib/NFe/APIRequest.php index 215e63a..0eabe85 100755 --- a/lib/NFe/APIRequest.php +++ b/lib/NFe/APIRequest.php @@ -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); diff --git a/lib/NFe/APIResource.php b/lib/NFe/APIResource.php index ff94a72..5dec605 100755 --- a/lib/NFe/APIResource.php +++ b/lib/NFe/APIResource.php @@ -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 ) { @@ -74,7 +82,6 @@ protected function deleteAPI() { // if ( $this['id'] == null ) { // $this['id'] // return false; // } - try { $response = self::API()->request( 'DELETE', static::url($this) ); @@ -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 ) { diff --git a/lib/NFe/Company.php b/lib/NFe/Company.php index e3d253f..e8afffc 100644 --- a/lib/NFe/Company.php +++ b/lib/NFe/Company.php @@ -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); } } diff --git a/lib/NFe/Error.php b/lib/NFe/Error.php index bcbdf05..409ca9b 100644 --- a/lib/NFe/Error.php +++ b/lib/NFe/Error.php @@ -1,5 +1,8 @@ $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; } } diff --git a/test/NFe/CompanyTest.php b/test/NFe/CompanyTest.php index c3c5fec..334a89a 100644 --- a/test/NFe/CompanyTest.php +++ b/test/NFe/CompanyTest.php @@ -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', @@ -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() ); } } diff --git a/test/NFe/LegalPersonTest.php b/test/NFe/LegalPersonTest.php index d577bb9..4c8d65b 100755 --- a/test/NFe/LegalPersonTest.php +++ b/test/NFe/LegalPersonTest.php @@ -13,7 +13,6 @@ public function testFetchFail() { $this->expectException('NFeObjectNotFound'); $result = NFe_LegalPerson::fetch( self::$company_id, self::$company_id ); - $this->assertNull($result); } diff --git a/test/NFe/ServiceInvoiceTest.php b/test/NFe/ServiceInvoiceTest.php index eea108f..68b4eb9 100755 --- a/test/NFe/ServiceInvoiceTest.php +++ b/test/NFe/ServiceInvoiceTest.php @@ -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 @@ -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 @@ -75,7 +78,7 @@ public function testDownloadPdfInvoice() { "54244e0ee340420fdc94ad09", $this->invoice->id ); - + $this->assertTrue( strpos($url, "pdf") ); } diff --git a/test/NFe/WebhookTest.php b/test/NFe/WebhookTest.php index 5a95574..6254dad 100644 --- a/test/NFe/WebhookTest.php +++ b/test/NFe/WebhookTest.php @@ -16,37 +16,37 @@ 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()); } @@ -54,6 +54,5 @@ public function testSearch() { $hooks = NFe_Webhook::search(); $this->assertNotNull( $hooks ); - $this->assertNotNull( $hooks->hooks ); } }