public function get(Request $request) { $filePath = dirname(__FILE__) . '/../../../tests/files/test4.csv'; $error = null; try { $response = CSVFileService::uploadCSV($filePath); } catch (GuzzleException $e) { if ($e->getResponse() != null && $e->getResponse()->getBody() != null) { $error = json_decode($e->getResponse()->getBody()->getContents()); } else { $error = new \StdClass(); $error->code = $e->getCode(); $error->message = $e->getMessage(); $trace = null; if ($e->getTrace()) { $trace = array_map(function ($curr) { $node = new \StdClass(); if (isset($curr['file'])) { $node->file = $curr['file']; } if (isset($curr['line'])) { $node->line = $curr['line']; } if (isset($curr['function'])) { $node->function = $curr['function']; } if (isset($curr['class'])) { $node->class = $curr['class']; } return $node; }, $e->getTrace()); } $error->trace = $trace; } $response = null; } $updatedNumber = 0; $createdNumber = 0; if ($response) { foreach ($response as $person) { $updated = strtotime($person->updated_at->date); $created = strtotime($person->created_at->date); if ($updated != $created) { $person->uploadStatus = 'updated'; $updatedNumber++; } else { $person->uploadStatus = 'created'; $createdNumber++; } } } return view('home', array('results' => $response, 'updated' => $updatedNumber, 'created' => $createdNumber, 'error' => $error)); }
/** * Tests the upload process of a csv file as an input. **/ public function testUploadCSV() { $bodyData = CSVFileService::readFileBody(CSVFileService::getFilePointer($this->getThirdTestFilePath()), true); $response = CSVFileService::uploadCSV($this->getThirdTestFilePath(), function ($responseData, $startingAt, $endingAt) use($bodyData) { $this->assertNotNull($responseData); $this->assertInternalType('array', $responseData); $this->assertEquals(6, count($responseData[0]->personValues)); }); }