Skip to content

Commit 1759fc5

Browse files
committed
Add support for getting the list of files provided by the print-api service
1 parent 9644d8e commit 1759fc5

File tree

5 files changed

+81
-1
lines changed

5 files changed

+81
-1
lines changed

.phpunit.result.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
C:37:"PHPUnit\Runner\DefaultTestResultCache":1964:{a:2:{s:7:"defects";a:6:{s:93:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWorksWithMinimalSetup";i:4;s:89:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillFillTheResult";i:4;s:79:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterCanFail";i:4;s:99:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillCallUrlWithoutException";i:4;s:117:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillNotBeExecutedIfCreateWasNotSuccessful";i:4;s:96:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillRespectTheFolder";i:4;}s:5:"times";a:12:{s:93:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWorksWithMinimalSetup";d:0.09;s:89:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillFillTheResult";d:0.005;s:79:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterCanFail";d:0.006;s:99:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillCallUrlWithoutException";d:0.006;s:117:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillNotBeExecutedIfCreateWasNotSuccessful";d:0.005;s:96:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillRespectTheFolder";d:0.007;s:82:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWorksWithMinimalSetup";d:0.084;s:78:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillFillTheResult";d:0.006;s:68:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterCanFail";d:0.006;s:88:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillCallUrlWithoutException";d:0.005;s:106:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillNotBeExecutedIfCreateWasNotSuccessful";d:0.004;s:85:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillRespectTheFolder";d:0.005;}}}
1+
C:37:"PHPUnit\Runner\DefaultTestResultCache":2731:{a:2:{s:7:"defects";a:13:{s:93:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWorksWithMinimalSetup";i:4;s:89:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillFillTheResult";i:4;s:79:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterCanFail";i:4;s:99:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillCallUrlWithoutException";i:4;s:117:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillNotBeExecutedIfCreateWasNotSuccessful";i:4;s:96:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillRespectTheFolder";i:4;s:79:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterListWillReturnData";i:5;s:82:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWorksWithMinimalSetup";i:4;s:78:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillFillTheResult";i:4;s:68:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterCanFail";i:4;s:88:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillCallUrlWithoutException";i:4;s:106:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillNotBeExecutedIfCreateWasNotSuccessful";i:4;s:85:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillRespectTheFolder";i:4;}s:5:"times";a:13:{s:93:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWorksWithMinimalSetup";d:0.09;s:89:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillFillTheResult";d:0.005;s:79:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterCanFail";d:0.006;s:99:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillCallUrlWithoutException";d:0.006;s:117:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillNotBeExecutedIfCreateWasNotSuccessful";d:0.005;s:96:"AMBERSIVE\PdfPrinter\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillRespectTheFolder";d:0.007;s:82:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWorksWithMinimalSetup";d:0.057;s:78:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillFillTheResult";d:0.003;s:68:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterCanFail";d:0.004;s:88:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterWillCallUrlWithoutException";d:0.003;s:106:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillNotBeExecutedIfCreateWasNotSuccessful";d:0.004;s:85:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterSaveWillRespectTheFolder";d:0.003;s:79:"AMBERSIVE\Tests\Unit\Classes\PdfPrinterTest::testIfPdfPrinterListWillReturnData";d:0.004;}}}

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased]
8+
9+
## [0.3.0] - 2020-07-30
10+
### Added
11+
- Added support for the [GET] /api/browse endpoint from the printer-api
12+
813
## [0.2.0] - 2020-07-10
914
### Changed
1015
- Restructure the project to be able to run unit tests automatically.

src/Classes/PdfPrinter.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
use AMBERSIVE\PdfPrinter\Interfaces\PdfPrinterInterface;
66
use AMBERSIVE\PdfPrinter\Classes\PdfPrinterOption;
7+
use AMBERSIVE\PdfPrinter\Classes\PdfPrinterFile;
78

89
use GuzzleHttp\Exception\GuzzleException;
910
use \GuzzleHttp\Client;
1011

1112
use \Illuminate\Http\Response;
13+
use Illuminate\Support\Collection;
1214

1315
use Validator;
1416
use Storage;
@@ -96,6 +98,33 @@ public function create(String $url, PdfPrinterOption $options = null, Callable $
9698
return $this;
9799
}
98100

101+
/**
102+
* Returns a collection of all files available on the printer api
103+
*
104+
* @return Collection
105+
*/
106+
public function listFiles(): Collection {
107+
108+
$headers = [];
109+
110+
if ($this->authType !== null && $this->authType !== "") {
111+
$headers['Authorization'] = $this->authToken;
112+
}
113+
114+
$response = $this->client->request("GET", $this->settings->url("api/browse"), [
115+
'headers' => $headers
116+
]);
117+
118+
$json = $response === null ? null : json_decode($response->getBody());
119+
120+
$result = collect($json)->map(function($item){
121+
return new PdfPrinterFile($item);
122+
});
123+
124+
return $result;
125+
126+
}
127+
99128
/**
100129
* Downlaod the pdf document if the the endpoint failed uploading it.
101130
*

src/Classes/PdfPrinterFile.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
4+
namespace AMBERSIVE\PdfPrinter\Classes;
5+
6+
use GuzzleHttp\Psr7\Response;
7+
8+
use Carbon\Carbon;
9+
10+
class PdfPrinterFile {
11+
12+
public String $file;
13+
public String $path;
14+
public Carbon $createdAt;
15+
public Carbon $updatedAt;
16+
17+
public function __construct(Object $file = null) {
18+
$this->file = optional($file)->filename !== null ? optional($file)->filename : '';
19+
$this->path = optional($file)->path !== null ? optional($file)->path : '';
20+
$this->createdAt = Carbon::parse(optional($file)->created_at);
21+
$this->updatedAt = Carbon::parse(optional($file)->updated_at);
22+
}
23+
24+
}

tests/Unit/PdfPrinterTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,26 @@ public function testIfPdfPrinterSaveWillRespectTheFolder():void {
238238

239239
}
240240

241+
/**
242+
* Test if the method will the listed files from the api endpoint
243+
*/
244+
public function testIfPdfPrinterListWillReturnData(): void {
245+
246+
$response = new PdfPrinterMockResponse(200,[
247+
[
248+
"file" => "XXX.pdf",
249+
"path" => "test/XXX.pd",
250+
"created_at" => "2020-06-27T22:27:09.876Z",
251+
"updated_at" => "2020-06-27T22:27:09.876Z"
252+
]
253+
]);
254+
255+
$pdfPrinter = $this->createApiMock([$response]);
256+
$result = $pdfPrinter->listFiles();
257+
258+
$this->assertNotNull($result);
259+
$this->assertEquals(1, $result->count());
260+
261+
}
262+
241263
}

0 commit comments

Comments
 (0)