diff --git a/api/BusinessLogic/Attachments/AttachmentRetriever.php b/api/BusinessLogic/Attachments/AttachmentRetriever.php index bf905018..00961d7f 100644 --- a/api/BusinessLogic/Attachments/AttachmentRetriever.php +++ b/api/BusinessLogic/Attachments/AttachmentRetriever.php @@ -19,6 +19,10 @@ class AttachmentRetriever { } function getAttachmentContentsForTicket($id, $heskSettings) { + $attachment = $this->attachmentGateway->getAttachmentById($id, $heskSettings); + $contents = base64_encode($this->fileReader->readFromFile( + $attachment->savedName, $heskSettings['attach_dir'])); + return $contents; } } \ No newline at end of file diff --git a/api/DataAccess/Files/FileReader.php b/api/DataAccess/Files/FileReader.php index e30f810c..296c966d 100644 --- a/api/DataAccess/Files/FileReader.php +++ b/api/DataAccess/Files/FileReader.php @@ -7,11 +7,10 @@ class FileReader { /** * @param $name string - The file name (including extension) * @param $folder - The folder name (relative to the ROOT of the helpdesk) - * @param $contents string - The contents of the file to write - * @return int The file size, in bytes + * @returns string - The contents of the file to write * @throws \Exception When the file fails to save */ - function readFromFile($name, $folder, $contents) { + function readFromFile($name, $folder) { // __DIR__ === '/{ROOT}/api/DataAccess/Files $location = __DIR__ . "/../../../{$folder}/{$name}"; $fileContents = file_get_contents($location); diff --git a/api/Tests/BusinessLogic/Attachments/AttachmentRetrieverTest.php b/api/Tests/BusinessLogic/Attachments/AttachmentRetrieverTest.php index 3c74198a..4790262c 100644 --- a/api/Tests/BusinessLogic/Attachments/AttachmentRetrieverTest.php +++ b/api/Tests/BusinessLogic/Attachments/AttachmentRetrieverTest.php @@ -18,14 +18,34 @@ class AttachmentRetrieverTest extends TestCase { /* @var $attachmentRetriever AttachmentRetriever */ private $attachmentRetriever; + /* @var $heskSettings array */ + private $heskSettings; + protected function setUp() { $this->attachmentGateway = $this->createMock(AttachmentGateway::class); $this->fileReader = $this->createMock(FileReader::class); + $this->heskSettings = array('attach_dir' => 'attachments'); $this->attachmentRetriever = new AttachmentRetriever($this->attachmentGateway, $this->fileReader); } - function testItGetsTheAttachmentFromTheFilesystem() { - + function testItGetsTheMetadataFromTheGateway() { + //-- Arrange + $attachmentMeta = new Attachment(); + $attachmentMeta->savedName = '5'; + $attachmentContents = 'string'; + $expectedContents = base64_encode($attachmentContents); + $this->attachmentGateway->method('getAttachmentById') + ->with(4, $this->heskSettings) + ->willReturn($attachmentMeta); + $this->fileReader->method('readFromFile') + ->with('5', $this->heskSettings['attach_dir']) + ->willReturn($attachmentContents); + + //-- Act + $actualContents = $this->attachmentRetriever->getAttachmentContentsForTicket(4, $this->heskSettings); + + //-- Assert + self::assertThat($actualContents, self::equalTo($expectedContents)); } }