From 65445485fee07eac30a7eef854680f0151cce1cb Mon Sep 17 00:00:00 2001 From: Sergey Mitroshin Date: Wed, 15 Mar 2017 14:12:28 -0500 Subject: [PATCH] Inspection API calls and functional tests --- lib/Endpoints/VehicleInspection.php | 57 +++++++ lib/Models/ExpenseReportRowContent.php | 4 +- lib/Models/Vehicle/Defect.php | 45 +++++- lib/Models/Vehicle/DefectAttachment.php | 40 ++++- lib/Models/Vehicle/InspectionExport.php | 18 +++ lib/Types/AttachmentType.php | 56 +++++++ lib/Types/ContentType.php | 16 +- lib/Types/VehicleDefectStatusType.php | 23 +++ lib/Types/VehicleDefectType.php | 53 ++++++ .../Endpoints/VehicleInspectionTest.php | 153 +++++++++++++++++- tests/Functional/config.sample.json | 3 +- 11 files changed, 459 insertions(+), 9 deletions(-) create mode 100644 lib/Models/Vehicle/InspectionExport.php create mode 100644 lib/Types/AttachmentType.php create mode 100644 lib/Types/VehicleDefectStatusType.php create mode 100644 lib/Types/VehicleDefectType.php diff --git a/lib/Endpoints/VehicleInspection.php b/lib/Endpoints/VehicleInspection.php index 128e23d..e3e3821 100644 --- a/lib/Endpoints/VehicleInspection.php +++ b/lib/Endpoints/VehicleInspection.php @@ -5,6 +5,7 @@ use Automile\Sdk\Exceptions\AutomileException; use Automile\Sdk\Config; use Automile\Sdk\Models\Vehicle\Inspection; +use Automile\Sdk\Models\Vehicle\InspectionExport; use Automile\Sdk\Models\Vehicle\InspectionRowset; /** @@ -26,6 +27,7 @@ public function getByInspectionId($id) } /** + * Get all vehicle inspections that the user has access to * @param int $vehicleInspectionId * @param int $vehicleId * @param \DateTime $fromDate @@ -61,4 +63,59 @@ public function getByInspectionIdVehicleAndDate($vehicleInspectionId = null, $ve throw new AutomileException($errorMessage ?: "Error code: {$response->getStatusCode()}"); } + /** + * Creates a new vehicle inspection + * @param Inspection $inspection + * @return Inspection + */ + public function createInspection(Inspection $inspection) + { + return $this->_create($this->_vehicleInspectionUri, $inspection); + } + + /** + * Updates the given vehicle inspection with new model + * @param Inspection $inspection + * @return Inspection + * @throws AutomileException + */ + public function editInspection(Inspection $inspection) + { + if (!$inspection->getVehicleInspectionId()) + { + throw new AutomileException('Vehicle Inspection ID is missing'); + } + + return $this->_edit($this->_vehicleInspectionUri, $inspection->getVehicleInspectionId(), $inspection); + } + + /** + * Export a vehicle inspection in pdf format via email + * @param int $vehicleInspectionId + * @param InspectionExport $model + * @return bool + * @throws AutomileException + */ + public function exportVehicleInspection($vehicleInspectionId, InspectionExport $model) + { + $request = Config::getNewRequest(); + $response = Config::getNewResponse(); + $client = Config::getNewHttpClient(); + + $this->_authorizeRequest($request); + + $request->setMethod(Config::METHOD_POST) + ->setUri($this->_vehicleInspectionUri . '/export/' . $vehicleInspectionId) + ->setBody($model->toJson()) + ->setContentType('application/json'); + + $isSuccessful = $client->send($request, $response); + + if ($isSuccessful) { + return true; + } + + $errorMessage = $response->getErrorMessage(); + throw new AutomileException($errorMessage ?: "Error code: {$response->getStatusCode()}"); + } } diff --git a/lib/Models/ExpenseReportRowContent.php b/lib/Models/ExpenseReportRowContent.php index 1d19edc..0ba7efe 100644 --- a/lib/Models/ExpenseReportRowContent.php +++ b/lib/Models/ExpenseReportRowContent.php @@ -13,8 +13,8 @@ * @method int getExpenseReportRowId() * @method int getContentType() * @method string getContentFileName() - * @method string getData() - * @method string getDataFile() + * @method string getData() base64-encoded raw data upon uploading + * @method string getDataFile() path to the attachment file upon uploading * * @method ExpenseReportRowContent setExpenseReportRowContentId(int $expenseReportRowContentId) * @method ExpenseReportRowContent setExpenseReportRowId(int $expenseReportRowId) diff --git a/lib/Models/Vehicle/Defect.php b/lib/Models/Vehicle/Defect.php index 5c8a62a..eca7d87 100644 --- a/lib/Models/Vehicle/Defect.php +++ b/lib/Models/Vehicle/Defect.php @@ -24,9 +24,6 @@ * @method Defect setCreatedByContactId(int $contactId) * @method Defect setNotes(string $notes) * @method Defect setDefectStatusType(int $statusType) - * @method Defect setVehicleDefectStatus(array|object $statuses) - * @method Defect setVehicleDefectAttachments(array|object $attachments) - * @method Defect setVehicleDefectComments(array|object $comments) */ class Defect extends ModelAbstract { @@ -44,6 +41,48 @@ class Defect extends ModelAbstract 'VehicleDefectComments' ]; + /** + * @param array|object $status + * @return Defect + */ + public function setVehicleDefectStatus($status) + { + if (!is_object($status) || !$status instanceof DefectStatusRowset) { + $status = new DefectStatusRowset($status); + } + + $this->_properties['VehicleDefectStatus'] = $status; + return $this; + } + + /** + * @param array|object $comments + * @return Defect + */ + public function setVehicleDefectComments($comments) + { + if (!is_object($comments) || !$comments instanceof DefectCommentRowset) { + $comments = new DefectCommentRowset($comments); + } + + $this->_properties['VehicleDefectComments'] = $comments; + return $this; + } + + /** + * @param array|object $attachments + * @return Defect + */ + public function setVehicleDefectAttachments($attachments) + { + if (!is_object($attachments) || !$attachments instanceof DefectAttachmentRowset) { + $attachments = new DefectAttachmentRowset($attachments); + } + + $this->_properties['VehicleDefectAttachments'] = $attachments; + return $this; + } + /** * @param string|\DateTime $dateTime a DateTime object or date in string representation * @return Defect diff --git a/lib/Models/Vehicle/DefectAttachment.php b/lib/Models/Vehicle/DefectAttachment.php index 1d9d040..f146af8 100644 --- a/lib/Models/Vehicle/DefectAttachment.php +++ b/lib/Models/Vehicle/DefectAttachment.php @@ -3,6 +3,8 @@ namespace Automile\Sdk\Models\Vehicle; use Automile\Sdk\Models\ModelAbstract; +use Automile\Sdk\Models\ModelException; +use Automile\Sdk\Types\AttachmentType; /** * Vehicle DefectAttachment Model @@ -12,12 +14,16 @@ * @method string getAttachmentType() * @method string getAttachmentLocation() * @method string getAttachmentDateUtc() + * @method string getData() base64-encoded raw data upon uploading + * @method string getDataFile() path to the attachment file upon uploading * * @method DefectAttachment setVehicleDefectAttachmentId(int $attachmentId) * @method DefectAttachment setVehicleDefectId(int $defectId) * @method DefectAttachment setAttachmentType(string $type) * @method DefectAttachment setAttachmentLocation(string $location) * @method DefectAttachment setAttachmentDateUtc(string $date) + * @method DefectAttachment setData(string $data) base64-encoded raw data upon uploading + * @method DefectAttachment setDataFile(string $dataFile) path to the attachment file upon uploading */ class DefectAttachment extends ModelAbstract { @@ -27,7 +33,39 @@ class DefectAttachment extends ModelAbstract 'VehicleDefectId', 'AttachmentType', 'AttachmentLocation', - 'AttachmentDateUtc' + 'AttachmentDateUtc', + 'Data', + 'DataFile' ]; + /** + * @return array + * @throws ModelException + */ + public function toArray() + { + $data = parent::toArray(); + + if (!empty($data['DataFile'])) { + if (!empty($data['Data'])) { + throw new ModelException('Content data already exists'); + } + + $content = @file_get_contents($data['DataFile']); + if (!$content) { + throw new ModelException("Data File is empty or inaccessible: '{$data['DataFile']}"); + } + + $data['Data'] = base64_encode($content); + + if (!isset($data['AttachmentType'])) { + $data['AttachmentType'] = AttachmentType::getByFilename($data['DataFile']); + } + + unset($data['DataFile']); + } + + return $data; + } + } diff --git a/lib/Models/Vehicle/InspectionExport.php b/lib/Models/Vehicle/InspectionExport.php new file mode 100644 index 0000000..36d8fc0 --- /dev/null +++ b/lib/Models/Vehicle/InspectionExport.php @@ -0,0 +1,18 @@ +getByInspectionId(self::_getSettings('vehicle_inspection.id')); @@ -58,4 +68,145 @@ public function testGetByInspectionIdVehicleAndDate() } } + /** + * @return Inspection $inspection + */ + public function testCreateInspection() + { + $data = [ + "VehicleId" => self::_getSettings('vehicle_inspection.vehicle_id'), + "InspectionDateUtc" => "2017-03-15", + "VehicleDefects" => [ + [ + "DefectType" => VehicleDefectType::OTHER, + "Notes" => "note1", + "VehicleDefectStatus" => [ + [ "DefectStatus" => VehicleDefectStatusType::NOT_RESOLVED ] + ], + "VehicleDefectAttachments" => [ + [ + "AttachmentType" => AttachmentType::IMAGE, + "DataFile" => "https://www.google.com/images/logo.png" + ] + ], + "VehicleDefectComments" => [ + [ "Comment" => "Comment 1" ], + [ "Comment" => "Comment 2" ] + ] + ] + ] + ]; + $inspectionCreate = new Inspection($data); + + $inspection = self::_getClient()->createInspection($inspectionCreate); + + $this->assertInstanceOf(\DateTime::class, $inspection->getInspectionDateUtc()); + $this->assertEquals($data['InspectionDateUtc'], $inspection->getInspectionDateUtc()->format('Y-m-d')); + + $this->assertInstanceOf(DefectRowset::class, $inspection->getVehicleDefects()); + $this->assertCount(1, $inspection->getVehicleDefects()); + $this->assertInstanceOf(Defect::class, $inspection->getVehicleDefects()[0]); + + $defect = $inspection->getVehicleDefects()[0]; + + $this->assertInstanceOf(DefectStatusRowset::class, $defect->getVehicleDefectStatus()); + $this->assertCount(1, $defect->getVehicleDefectStatus()); + $this->assertInstanceOf(DefectStatus::class, $defect->getVehicleDefectStatus()[0]); + + $this->assertInstanceOf(DefectAttachmentRowset::class, $defect->getVehicleDefectAttachments()); + $this->assertCount(1, $defect->getVehicleDefectAttachments()); + $this->assertInstanceOf(DefectAttachment::class, $defect->getVehicleDefectAttachments()[0]); + + $attachment = $defect->getVehicleDefectAttachments()[0]; + $originalFile = getimagesize($data['VehicleDefects'][0]['VehicleDefectAttachments'][0]['DataFile']); + $this->assertNotEmpty($originalFile); + $remoteFile = getimagesize('http://content.automile.com/images/' . $attachment->getAttachmentLocation()); + $this->assertNotEmpty($remoteFile); + $this->assertEquals($originalFile[3], $remoteFile[3]); + + $this->assertInstanceOf(DefectCommentRowset::class, $defect->getVehicleDefectComments()); + $this->assertCount(2, $defect->getVehicleDefectComments()); + $this->assertInstanceOf(DefectComment::class, $defect->getVehicleDefectComments()[0]); + + return $inspection; + } + + /** + * @depends testCreateInspection + * @param Inspection $inspectionCreated + */ + public function testEditInspection(Inspection $inspectionCreated) + { + $defectCreated = $inspectionCreated->getVehicleDefects()[0]; + $commentCreated = $defectCreated->getVehicleDefectComments()[0]; + $attachmentCreated = $defectCreated->getVehicleDefectAttachments()[0]; + + $data = [ + "VehicleInspectionId" => $inspectionCreated->getVehicleInspectionId(), + "VehicleDefects" => [ + [ + "VehicleDefectId" => $defectCreated->getVehicleDefectId(), + "DefectType" => VehicleDefectType::BATTERY, + "DefectStatusType" => VehicleDefectStatusType::RESOLVED, + "Notes" => "note1 - edited", + /*"VehicleDefectAttachments" => [ + [ + "VehicleDefectAttachmentId" => $attachmentCreated->getVehicleDefectAttachmentId(), + "AttachmentType" => AttachmentType::IMAGE, + "DataFile" => "http://help.bing.microsoft.com/Resources/content/styles/Images/logo_bing.png" + ] + ],*/ + "VehicleDefectComments" => [ + [ + "VehicleDefectCommentId" => $commentCreated->getVehicleDefectCommentId(), + "Comment" => "Comment 1 - edited" + ] + ] + ] + ] + ]; + $inspectionEdit = new Inspection($data); + + $inspectionEdited = self::_getClient()->editInspection($inspectionEdit); + $this->assertInstanceOf(Inspection::class, $inspectionEdited); + + $inspection = self::_getClient()->getByInspectionId($inspectionEdited->getVehicleInspectionId()); + + $this->assertInstanceOf(DefectRowset::class, $inspection->getVehicleDefects()); + $this->assertCount(1, $inspection->getVehicleDefects()); + $this->assertInstanceOf(Defect::class, $inspection->getVehicleDefects()[0]); + + $defect = $inspection->getVehicleDefects()[0]; + + $this->assertEquals($data['VehicleDefects'][0]['Notes'], $defect->getNotes()); + $this->assertEquals($data['VehicleDefects'][0]['DefectStatusType'], $defect->getDefectStatusType()); + $this->assertEquals($data['VehicleDefects'][0]['DefectType'], $defect->getDefectType()); + + $this->assertInstanceOf(DefectAttachmentRowset::class, $defect->getVehicleDefectAttachments()); + $this->assertCount(1, $defect->getVehicleDefectAttachments()); + $this->assertInstanceOf(DefectAttachment::class, $defect->getVehicleDefectAttachments()[0]); + + /*$attachment = $defect->getVehicleDefectAttachments()[0]; + $originalFile = getimagesize($data['VehicleDefects'][0]['VehicleDefectAttachments'][0]['DataFile']); + $this->assertNotEmpty($originalFile); + $remoteFile = getimagesize('http://content.automile.com/images/' . $attachment->getAttachmentLocation()); + $this->assertNotEmpty($remoteFile); + $this->assertEquals($originalFile[3], $remoteFile[3]);*/ + + $this->assertInstanceOf(DefectCommentRowset::class, $defect->getVehicleDefectComments()); + $this->assertCount(2, $defect->getVehicleDefectComments()); + $this->assertInstanceOf(DefectComment::class, $defect->getVehicleDefectComments()[0]); + $this->assertEquals($data['VehicleDefects'][0]['VehicleDefectComments'][0]['Comment'], $defect->getVehicleDefectComments()[0]->getComment()); + } + + public function testExportVehicleInspection() + { + $data = [ + 'ToEmail' => self::_getSettings('vehicle_inspection.export_email'), + 'ISO639LanguageCode' => 'en' + ]; + $result = self::_getClient()->exportVehicleInspection(self::_getSettings('vehicle_inspection.id'), new InspectionExport($data)); + $this->assertTrue($result); + } + } diff --git a/tests/Functional/config.sample.json b/tests/Functional/config.sample.json index 30e5fd3..e97755e 100644 --- a/tests/Functional/config.sample.json +++ b/tests/Functional/config.sample.json @@ -11,6 +11,7 @@ }, "vehicle_inspection": { "id": 0, - "vehicle_id": 0 + "vehicle_id": 0, + "export_email": "..." } } \ No newline at end of file