diff --git a/src/Factory.php b/src/Factory.php index 3b10e8a2..b3d9ca00 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -28,7 +28,9 @@ * @method \SevenShores\Hubspot\Resources\Forms forms() * @method \SevenShores\Hubspot\Resources\HubDB hubDB() * @method \SevenShores\Hubspot\Resources\Keywords keywords() + * @method \SevenShores\Hubspot\Resources\LineItems lineItems() * @method \SevenShores\Hubspot\Resources\Pages pages() + * @method \SevenShores\Hubspot\Resources\Products products() * @method \SevenShores\Hubspot\Resources\SocialMedia socialMedia() * @method \SevenShores\Hubspot\Resources\Tickets tickets() * @method \SevenShores\Hubspot\Resources\Timeline timeline() diff --git a/src/Resources/BlogAuthors.php b/src/Resources/BlogAuthors.php index 11918ec6..f7fbb26c 100644 --- a/src/Resources/BlogAuthors.php +++ b/src/Resources/BlogAuthors.php @@ -8,7 +8,7 @@ class BlogAuthors extends Resource * Get all blog authors. * * @param array $params optional parameters - * + * * @see https://developers.hubspot.com/docs/methods/blog/v3/list-blog-authors * * @return \SevenShores\Hubspot\Http\Response @@ -32,7 +32,7 @@ public function all(array $params = []) * @param array $params optional parameters * * @see https://developers.hubspot.com/docs/methods/blog/v3/search-blog-authors - * + * * @return \SevenShores\Hubspot\Http\Response */ public function search($q = '', array $params = []) @@ -42,18 +42,18 @@ public function search($q = '', array $params = []) $params['q'] = $q; return $this->client->request( - 'get', - $endpoint, - [], - build_query_string($params) - ); + 'get', + $endpoint, + [], + build_query_string($params) + ); } /** * Get a specific blog author. * * @param int $id unique identifier for a blog author - * + * * @see https://developers.hubspot.com/docs/methods/blog/v3/get-blog-author-by-id * * @return \SevenShores\Hubspot\Http\Response @@ -63,18 +63,18 @@ public function getById($id, array $params = []) $endpoint = "https://api.hubapi.com/blogs/v3/blog-authors/{$id}"; return $this->client->request( - 'get', - $endpoint, - [], - build_query_string($params) - ); + 'get', + $endpoint, + [], + build_query_string($params) + ); } - + /** * Create a new blog author. * * @param array $options optional Parameters - * + * * @see https://developers.hubspot.com/docs/methods/blog/v3/create-blog-author * * @return \SevenShores\Hubspot\Http\Response @@ -84,11 +84,11 @@ public function create(array $options = [], array $params = []) $endpoint = 'https://api.hubapi.com/blogs/v3/blog-authors'; return $this->client->request( - 'post', - $endpoint, - ['json' => $options], - build_query_string($params) - ); + 'post', + $endpoint, + ['json' => $options], + build_query_string($params) + ); } /** @@ -96,7 +96,7 @@ public function create(array $options = [], array $params = []) * * @param int $id unique identifier for a blog author * @param array $params fields to update - * + * * @see https://developers.hubspot.com/docs/methods/blog/v3/update-blog-author * * @return \SevenShores\Hubspot\Http\Response @@ -114,7 +114,7 @@ public function update($id, array $params = []) * @param int $id unique identifier for the blog author to delete * * @see https://developers.hubspot.com/docs/methods/blog/v3/delete-blog-author - * + * * @return \SevenShores\Hubspot\Http\Response */ public function delete($id) diff --git a/src/Resources/Contacts.php b/src/Resources/Contacts.php index ec646343..98a99fc1 100644 --- a/src/Resources/Contacts.php +++ b/src/Resources/Contacts.php @@ -391,8 +391,6 @@ public function search(string $query, array $params = []) } /** - * @deprecated - * * @return \SevenShores\Hubspot\Http\Response */ public function statistics() diff --git a/src/Resources/LineItems.php b/src/Resources/LineItems.php new file mode 100644 index 00000000..331348ff --- /dev/null +++ b/src/Resources/LineItems.php @@ -0,0 +1,195 @@ +client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); + } + + /** + * Get a line item by ID. + * + * @param int $id + * + * @see https://developers.hubspot.com/docs/methods/line-items/get_line_item_by_id + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function getById($id, array $params = []) + { + $endpoint = "https://api.hubapi.com/crm-objects/v1/objects/line_items/{$id}"; + + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); + } + + /** + * Get a group of line items by ID. + * + * @see https://developers.hubspot.com/docs/methods/line-items/batch-get-line-items + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function getBatchByIds(array $ids, array $params = []) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/line_items/batch-read'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => ['ids' => $ids]], + build_query_string($params) + ); + } + + /** + * Create a line item. + * + * @see https://developers.hubspot.com/docs/methods/line-items/create-line-item + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function create(array $properties) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/line_items'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => $properties] + ); + } + + /** + * Create a group of line items. + * + * @see https://developers.hubspot.com/docs/methods/line-items/batch-create-line-items + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function createBatch(array $lineItems) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/line_items/batch-create'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => $lineItems] + ); + } + + /** + * Update a line item. + * + * @param int $id + * + * @see https://developers.hubspot.com/docs/methods/line-items/update-line-item + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function update($id, array $properties) + { + $endpoint = "https://api.hubapi.com/crm-objects/v1/objects/line_items/{$id}"; + + return $this->client->request( + 'put', + $endpoint, + ['json' => $properties] + ); + } + + /** + * Update a group of line items. + * + * @see https://developers.hubspot.com/docs/methods/line-items/batch-update-line-items + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function updateBatch(array $lineItems) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/line_items/batch-update'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => $lineItems] + ); + } + + /** + * Delete a line item. + * + * @param int $id + * + * @see https://developers.hubspot.com/docs/methods/line-items/delete-line-item + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function delete($id) + { + $endpoint = "https://api.hubapi.com/crm-objects/v1/objects/line_items/{$id}"; + + return $this->client->request('delete', $endpoint); + } + + /** + * Delete a group of line items. + * + * @see https://developers.hubspot.com/docs/methods/line-items/batch-delete-line-items + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function deleteBatch(array $ids) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/objects/line_items/batch-delete'; + + return $this->client->request( + 'post', + $endpoint, + ['json' => ['ids' => $ids]] + ); + } + + /** + * Get a log of changes for line items. + * + * @see https://developers.hubspot.com/docs/methods/line-items/get-line-item-changes + * + * @return \SevenShores\Hubspot\Http\Response + */ + public function getLineItemChanges(array $params = []) + { + $endpoint = 'https://api.hubapi.com/crm-objects/v1/change-log/line_items'; + + return $this->client->request( + 'get', + $endpoint, + [], + build_query_string($params) + ); + } +} diff --git a/tests/Integration/Abstraction/ProductData.php b/tests/Integration/Abstraction/ProductData.php new file mode 100644 index 00000000..5a9ae9e3 --- /dev/null +++ b/tests/Integration/Abstraction/ProductData.php @@ -0,0 +1,16 @@ + 'name', 'value' => $name], + ['name' => 'description', 'value' => 'A description of this product.'], + ['name' => 'price', 'value' => 27.50], + ['name' => 'recurringbillingfrequency', 'value' => 'quarterly'], + ]; + } +} diff --git a/tests/Integration/Resources/BlogAuthorsTest.php b/tests/Integration/Resources/BlogAuthorsTest.php index fad14ee1..e6e1014e 100644 --- a/tests/Integration/Resources/BlogAuthorsTest.php +++ b/tests/Integration/Resources/BlogAuthorsTest.php @@ -92,11 +92,12 @@ public function delete() $response = $this->resource->delete($this->entity->id); $this->assertEquals(204, $response->getStatusCode()); - + $this->entity = null; } - protected function createEntity() { + protected function createEntity() + { return $this->resource->create([ 'fullName' => 'John Smith '.uniqid(), 'email' => 'john.smith'.uniqid().'@example.com', @@ -104,7 +105,8 @@ protected function createEntity() { ]); } - protected function deleteEntity() { + protected function deleteEntity() + { $this->resource->delete($this->entity->id); } } diff --git a/tests/Integration/Resources/LineItemsTest.php b/tests/Integration/Resources/LineItemsTest.php new file mode 100644 index 00000000..c34067fd --- /dev/null +++ b/tests/Integration/Resources/LineItemsTest.php @@ -0,0 +1,234 @@ +product = $this->createProduct(); + + parent::setUp(); + } + + public function tearDown() + { + parent::tearDown(); + + if (!empty($this->product)) { + $this->deleteProduct(); + } + } + + /** @test */ + public function all() + { + $response = $this->resource->all(); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertGreaterThanOrEqual(1, count($response->objects)); + } + + /** @test */ + public function getById() + { + $response = $this->resource->getById($this->entity->objectId); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** @test */ + public function getBatchByIds() + { + $lineItem = $this->createEntity(); + + $ids = [ + $this->entity->objectId, + $lineItem->objectId, + ]; + + $response = $this->resource->getBatchByIds($ids); + + $this->assertEquals(200, $response->getStatusCode()); + $this->assertCount(2, $response->toArray()); + + $this->resource->delete($lineItem->objectId); + } + + /** @test */ + public function create() + { + $this->assertEquals(200, $this->entity->getStatusCode()); + } + + /** @test */ + public function createBatch() + { + $response = $this->resource->createBatch([ + $this->getData(), + $this->getData(), + ]); + + $this->assertEquals(200, $response->getStatusCode()); + + sleep(1); + + $this->resource->deleteBatch(array_map(function ($lineItem) { + return $lineItem->objectId; + }, array_values($response->getData()))); + } + + /** @test */ + public function update() + { + $response = $this->resource->update($this->entity->objectId, [ + ['name' => 'name', 'value' => 'An updated custom name for the product for this line item. Discounting 5% on bulk purchase.'], + ]); + + $this->assertEquals(200, $response->getStatusCode()); + } + + /** @test */ + public function updateBatch() + { + $lineItem = $this->createEntity(); + + $response = $this->resource->updateBatch([ + [ + 'objectId' => $this->entity->objectId, + 'properties' => [ + [ + 'name' => 'price', + 'value' => 55.00, + ], + [ + 'name' => 'description', + 'value' => 'This is an updated description for this item, it\'s getting a price change.', + ], + ], + ], + [ + 'objectId' => $lineItem->objectId, + 'properties' => [ + [ + 'name' => 'name', + 'value' => 'Updated name, new quantity', + ], + [ + 'name' => 'quantity', + 'value' => 20, + ], + ], + ], + ]); + + $this->assertEquals(200, $response->getStatusCode()); + + $this->resource->delete($lineItem->objectId); + } + + /** @test */ + public function delete() + { + $response = $this->deleteEntity(); + + $this->assertEquals(204, $response->getStatusCode()); + + $this->entity = null; + } + + /** @test */ + public function deleteBatch() + { + $response = $this->resource->createBatch([ + $this->getData(), + $this->getData(), + ]); + + sleep(1); + + $deleteResponse = $this->resource->deleteBatch(array_map(function ($lineItem) { + return $lineItem->objectId; + }, array_values($response->getData()))); + + $this->assertEquals(204, $deleteResponse->getStatusCode()); + } + + /** @test */ + public function getLineItemChanges() + { + $response = $this->resource->getLineItemChanges(); + + $this->assertEquals(200, $response->getStatusCode()); + } + + protected function createEntity() + { + return $this->resource->create($this->getData()); + } + + protected function deleteEntity() + { + return $this->resource->delete($this->entity->objectId); + } + + protected function getData(): array + { + return [ + ['name' => 'hs_product_id', 'value' => $this->product->objectId], + ['name' => 'quantity', 'value' => 50], + ['name' => 'price', 'value' => 9.50], + ['name' => 'name', 'value' => 'A custom name for the product for this line item. Discounting 5% on bulk purchase.'], + ]; + } + + protected function getProducts() + { + if (empty($this->resourceProducts)) { + $this->resourceProducts = new Products(new Client(['key' => getenv('HUBSPOT_TEST_API_KEY')])); + } + + return $this->resourceProducts; + } + + protected function createProduct() + { + return $this->getProducts()->create($this->getProductData()); + } + + protected function deleteProduct() + { + return $this->getProducts()->delete($this->product->objectId); + } +} diff --git a/tests/Integration/Resources/ProductsTest.php b/tests/Integration/Resources/ProductsTest.php index dcb0f52d..608cdd7e 100644 --- a/tests/Integration/Resources/ProductsTest.php +++ b/tests/Integration/Resources/ProductsTest.php @@ -11,6 +11,8 @@ */ class ProductsTest extends EntityTestCase { + use \SevenShores\Hubspot\Tests\Integration\Abstraction\ProductData; + /** * @var SevenShores\Hubspot\Resources\Products */ @@ -172,14 +174,4 @@ protected function deleteEntity() { return $this->resource->delete($this->entity->objectId); } - - protected function getData(string $name = 'A new product'): array - { - return [ - ['name' => 'name', 'value' => $name], - ['name' => 'description', 'value' => 'A description of this product.'], - ['name' => 'price', 'value' => 27.50], - ['name' => 'recurringbillingfrequency', 'value' => 'quarterly'], - ]; - } }